Example Six - Color Gradient

examples/qwiic_led_stick_ex6_color_gradient.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex6_color_gradient.py
 4#
 5# This example will display a linear gradient from one color to another on the LED
 6# Stick.
 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 6
40
41from __future__ import print_function
42import qwiic_led_stick
43import time
44import sys
45
46def color_gradient(LED_stick, r1, b1, g1, r2, g2, b2, LED_length):
47    # Subtract 1 from LED_length because there is one less transition color
48    # than length of LEDs
49    LED_length = LED_length - 1
50    # Calculate the slope of the line between r/g/b1 and r/g/b2
51    r_slope = (r2 - r1) / LED_length
52    g_slope = (g2 - g1) / LED_length
53    b_slope = (b2 - b1) / LED_length
54    # Set the color for each pixel on your LED Stick
55    for i in range(0, LED_length):
56        # Evaluate the ith point on the line between r/g/b1 and r/g/b2
57        r_value = r1 + r_slope * i
58        g_value = g1 + g_slope * i
59        b_value = b1 + b_slope * i
60        # Set the pixel to the calculated color
61        LED_stick.set_single_LED_color(i + 1, r_value, g_value, b_value)
62
63def run_example():
64
65    print("\nSparkFun Qwiic LED Stick Example 6")
66    my_stick = qwiic_led_stick.QwiicLEDStick()
67
68    if my_stick.begin() == False:
69        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
70            file=sys.stderr)
71        return
72    print("\nLED Stick ready!")
73
74    # Set the colors for the gradient
75    # These are for the first color
76    r1 =238
77    g1 = 49
78    b1 = 36
79    # These are for the last color    
80    r2 = 66
81    g2 = 235
82    b2 = 23
83
84    color_gradient(my_stick, r1, g1, b1, r2, g2, b2, 10)
85
86if __name__ == '__main__':
87    try:
88        run_example()
89    except (KeyboardInterrupt, SystemExit) as exErr:
90        print("\nEnding Example 6")
91        sys.exit(0)