Example Seven - Cycle Rainbow

examples/qwiic_led_stick_ex7_cycle_rainbow.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex7_cycle_rainbow.py
 4#
 5# This example ake the LED Stick smoothly change through the colors of the rainbow.
 6# --------------------------------------------------------------------------------
 7#
 8# Written by Priyanka Makin @ SparkFun Electronics, June 2021
 9# 
10# This python library supports the SpakrFun Electronics qwiic sensor/
11# board ecosystem on a Raspberry Pi (and compatible) board computers.
12#
13# More information on qwiic is at https://www.sparkfun.com/qwiic
14#
15# Do you like this library? Help support SparkFun by buying a board!
16#
17#==================================================================================
18# Copyright (c) 2019 SparkFun Electronics
19#
20# Permission is hereby granted, free of charge, to any person obtaining a copy 
21# of this software and associated documentation files (the "Software"), to deal 
22# in the Software without restriction, including without limitation the rights 
23# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
24# copies of the Software, and to permit persons to whom the Software is 
25# furnished to do so, subject to the following conditions:
26#
27# The above copyright notice and this permission notice shall be included in all 
28# copies or substantial portions of the Software.
29#
30# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
31# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
32# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
33# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
34# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
35# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
36# SOFTWARE.
37#==================================================================================
38# Example 7
39
40from __future__ import print_function
41import qwiic_led_stick
42import time
43import sys
44
45def cycle_rainbow(LED_stick, delay):
46    # Red to yellow
47    for g in range(0, 255):
48        LED_stick.set_all_LED_color(255, g, 0)
49        time.sleep(delay)
50    
51    # Yellow to green
52    for r in range(255, 0, -1):
53        LED_stick.set_all_LED_color(r, 255, 0)
54        time.sleep(delay)
55    
56    # Green to cyan
57    for b in range(0, 255):
58        LED_stick.set_all_LED_color(0, 255, b)
59        time.sleep(delay)
60    
61    # Cyan to blue
62    for g in range(255, 0, -1):
63        LED_stick.set_all_LED_color(0, g, 255)
64        time.sleep(delay)
65    
66    # Blue to magenta
67    for r in range(0, 255):
68        LED_stick.set_all_LED_color(r, 0, 255)
69        time.sleep(delay)
70    
71    # Magenta to red
72    for b in range(255, 0, -1):
73        LED_stick.set_all_LED_color(255, 0, b)
74        time.sleep(delay)
75        
76def run_example():
77
78    print("\nSparkFun Qwiic LED Stick Example 7")
79    my_stick = qwiic_led_stick.QwiicLEDStick()
80
81    if my_stick.begin() == False:
82        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
83            file=sys.stderr)
84        return
85    print("\nLED Stick ready!")
86
87    while True:
88        cycle_rainbow(my_stick, 0.01)
89
90if __name__ == '__main__':
91    try:
92        run_example()
93    except (KeyboardInterrupt, SystemExit) as exErr:
94        print("\nEnding Example 7")
95        sys.exit(0)