1# !/usr/bin/env python
2# ---------------------------------------------------------------------------------
3# qwiic_led_stick_ex8_walking_rainbow.py
4#
5# This example makes a moving rainbow on the LED Stick.
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 8
39
40from __future__ import print_function
41import qwiic_led_stick
42import math
43import time
44import sys
45
46def walking_rainbow(LED_stick, rainbow_length, LED_length, delay):
47 red_array = [None] * LED_length
48 blue_array = [None] * LED_length
49 green_array = [None] * LED_length
50
51 for j in range(0, rainbow_length):
52
53 for i in range(0, LED_length):
54 # There are n colors generated for the rainbow
55 # The value of n determins which color is generated at each pixel
56 n = i + 1 - j
57
58 # Loop n so that it is always between 1 and rainbow_length
59 if n <= 0:
60 n = n + rainbow_length
61
62 # The nth color is between red and yellow
63 if n <= math.floor(rainbow_length / 6):
64 red_array[i] = 255
65 green_array[i] = int(math.floor(6 * 255 / rainbow_length * n))
66 blue_array[i] = 0
67
68 # The nth color is between yellow and green
69 elif n <= math.floor(rainbow_length / 3):
70 red_array[i] = int(math.floor(510 - 6 * 255 / rainbow_length * n))
71 green_array[i] = 255
72 blue_array[i] = 0
73
74 # The nth color is between green and cyan
75 elif n <= math.floor(rainbow_length / 2):
76 red_array[i] = 0
77 green_array[i] = 255
78 blue_array[i] = int(math.floor(6 * 255 / rainbow_length * n - 510))
79
80 # The nth color is between blue and magenta
81 elif n <= math.floor(5 * rainbow_length / 6):
82 red_array[i] = int(math.floor(6 * 255 / rainbow_length * n - 1020))
83 green_array[i] = 0
84 blue_array[i] = 255
85
86 # The nth color is between magenta and red
87 else:
88 red_array[i] = 255
89 green_array[i] = 0
90 blue_array[i] = int(math.floor(1530 - (6 *255 / rainbow_length * n)))
91
92 # Set all the LEDs to the color values accordig to the arrays
93 LED_stick.set_all_LED_unique_color(red_array, green_array, blue_array, LED_length)
94 time.sleep(delay)
95
96def run_example():
97
98 print("\nSparkFun Qwiic LED Stick Example 1")
99 my_stick = qwiic_led_stick.QwiicLEDStick()
100
101 if my_stick.begin() == False:
102 print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
103 file=sys.stderr)
104 return
105 print("\nLED Stick ready!")
106
107 while True:
108 walking_rainbow(my_stick, 20, 10, 0.3)
109
110if __name__ == '__main__':
111 try:
112 run_example()
113 except (KeyboardInterrupt, SystemExit) as exErr:
114 print("\nEnding Example 8")
115 sys.exit(0)