Example Four - Set Brightness

examples/qwiic_led_stick_ex4_set_brightness.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex3_set_brightness.py
 4#
 5# This example changes brightness of the LED Stick in different ways, then stops 
 6# through each available brightness setting.
 7# --------------------------------------------------------------------------------
 8#
 9# Written by Priyanka Makin @ SparkFun Electronics, June 2021
10# 
11# This python library supports the SpakrFun Electronics qwiic sensor/
12# board ecosystem on a Raspberry Pi (and compatible) board computers.
13#
14# More information on qwiic is at https://www.sparkfun.com/qwiic
15#
16# Do you like this library? Help support SparkFun by buying a board!
17#
18#==================================================================================
19# Copyright (c) 2019 SparkFun Electronics
20#
21# Permission is hereby granted, free of charge, to any person obtaining a copy 
22# of this software and associated documentation files (the "Software"), to deal 
23# in the Software without restriction, including without limitation the rights 
24# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
25# copies of the Software, and to permit persons to whom the Software is 
26# furnished to do so, subject to the following conditions:
27#
28# The above copyright notice and this permission notice shall be included in all 
29# copies or substantial portions of the Software.
30#
31# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
32# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
33# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
34# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
35# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
36# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
37# SOFTWARE.
38#==================================================================================
39# Example 4
40
41from __future__ import print_function
42import qwiic_led_stick
43import time
44import sys
45
46def run_example():
47
48    print("\nSparkFun Qwiic LED Stick Example 3")
49    my_stick = qwiic_led_stick.QwiicLEDStick()
50
51    if my_stick.begin() == False:
52        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
53            file=sys.stderr)
54        return
55    print("\nLED Stick ready!")
56
57    # Initialize LEDs as a rainbow followed by 1 white pixel
58    red_list = [255, 255, 170, 0, 0, 0, 0, 170, 255, 255]
59    green_list = [0, 170, 255, 255, 255, 170, 0, 0, 0, 255]
60    blue_list = [0, 0, 0, 0, 170, 255, 255, 255, 170, 255]
61
62    # Turn on the LED Stick according to the 3 arrays
63    my_stick.set_all_LED_unique_color(red_list, green_list, blue_list, 10)
64
65    while True:
66
67        # This will step through each available brightness setting
68        # Brightness values can be from 0 - 31
69        for i in range(0, 32):
70            my_stick.set_all_LED_brightness(i)
71
72            print("\nBrightness level: " + str(i))
73            time.sleep(1)
74
75if __name__ == '__main__':
76    try:
77        run_example()
78    except (KeyboardInterrupt, SystemExit) as exErr:
79        print("\nEnding Example 4")
80        sys.exit(0)