Qwiic_LED_Stick_Py

follow on Twitter

SparkFun Qwiic LED Stick

Python module for the SparkFun Qwiic LED Stick - APA102C

This python package is a port of the existing SparkFun Qwiic LED Stick Arduino Library

This package can be used in conjunction with the overall SparkFun qwiic Python Package

New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.

Contents

Supported Platforms

The Qwiic LED Stick Python package currently supports the following platforms:

Dependencies

This driver package depends on the qwiic I2C driver: Qwiic_I2C_Py

Documentation

The SparkFun Qwiic LED Stick module documentation is hosted at ReadTheDocs

Installation

PyPi Installation

This repository is hosted on PyPi as the sparkfun-qwiic-led-stick package. On systems that support PyPi installation via pip, this library is installed using the following commands

For all users (note: the user must have sudo privileges):

sudo pip install sparkfun-qwiic-led-stick

For the current user:

pip install sparkfun-qwiic-led-stick

To install, make sure the setuptools package is installed on the system.

Direct installation at the command line:

python setup.py install

To build a package for use with pip:

python setup.py sdist

A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.

cd dist
pip install sparkfun-qwiic-led-stick-<version>.tar.gz

Example Use

See the examples directory for more detailed use examples.

from __future__ import print_function
import qwiic_led_stick
import time
import sys

def run_example():

    print("\nSparkFun Qwiic LED Stick Example 1")
    my_stick = qwiic_led_stick.QwiicLEDStick()

    if my_stick.begin() == False:
        print("\nThe Qwiic LED Stick isn't connected to the sytsem. Please check your connection", \
            file=sys.stderr)
        return
    print("\nLED Stick ready!")

    my_stick.set_all_LED_brightness(15)

    while True:

        # Turn on all the LEDs to white
        my_stick.set_all_LED_color(50, 50, 50)
        time.sleep(1)
        # Turn off all LEDs
        my_stick.LED_off()
        time.sleep(1)

if __name__ == '__main__':
    try:
        run_example()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 1")
        sys.exit(0)

SparkFun - Start Something

Table of Contents

API Reference

qwiic_led_stick

Python module for the SparkFun Qwiic LED Stick - APA102C.

This package is a port of the existing [SparkFun Qwiic LED Stick Arduino Library](https://github.com/sparkfun/SparkFun_Qwiic_LED_Stick_Arduino_Library).

This package can be used in conjunction with the overall [SparkFun Qwiic Python Package](https://github.com/sparkfun/Qwiic_Py).

New to qwiic? Take a look at the entire [SparkFun Qwiic Ecoststem](https://www.sparkfun.com/qwiic).

class qwiic_led_stick.QwiicLEDStick(address=None, i2c_driver=None)[source]
Parameters
  • address – The I2C address to use for the device. If not provided, the default address is used.

  • i2c_driver – An existing i2c driver object. If not provided a a driver is created.

Returns

The GPIO device object.

Return type

Object

LED_off()[source]

Turn all LEDs off by setting color to 0

Returns

true if the command was sent successfully, false otherwise.

Return type

bool

begin()[source]

Initialize the operation of the Qwiic LED Stick. Run is_connected()

Returns

Returns true if an LED Stick is connected to the system False otherwise.

Return type

bool

change_address(new_address)[source]

Change the I2C address from one address to another.

Parameters

new_address – the new address to be set to. Must be valid.

Returns

Nothing

Return type

Void

change_length(new_length)[source]

Change the length of the LED string

Parameters

new_length – the new length of the LED string

Returns

true if the command was sent successfully, false otherwise.

Return type

bool

is_connected()[source]

Determine if a Qwiic SGP40 device is connected to the system.

Returns

True if the device is connected, false otherwise.

Return type

bool

set_all_LED_brightness(brightness)[source]

Change the brightness of all LEDs while keeping their current color. To turn all LEDs off but remember their previous color, set brightness to 0

Parameters

brightness – value of LED brightness between 0 and 31.

Returns

true if the command was sent successfully, false otherwise.

Return type

bool

set_all_LED_color(red, green, blue)[source]

Set the color of all LEDs in the string. Each will be shining the same color. The color value must be between 0-255.

Parameters
  • red – the red value to set all LEDs to. Between 0 and 255.

  • green – the green value to set all LEDs to. Between 0 and 255.

  • blue – the blue value to set all the LEDs to. Between 0 and 255.

Returns

Returns true if command is written successfully, false otherwise

Return type

bool

set_all_LED_unique_color(red_list, green_list, blue_list, length)[source]

Change the color of all LEDs at once to individual values.

Parameters
  • red_list – a list of red values for the LEDs. Index 0 of red_list corresponds to the red value of LED 0.

  • blue_list – a list of blue values for the LEDs.

  • green_list – a list of green values for the LEDs.

  • length – the length of the LED string.

Returns

True if commands are written successfully, false otherwise

Return type

bool

set_single_LED_brightness(number, brightness)[source]

Change the brightness of a specific LED while keeping their current color. To turn LEDs off but remember their previous color, set brightness to 0.

Parameters
  • number – number of LED to change brightness. LEDs indexed starting at 1.

  • brightness – value of LED brightness between 0 and 31.

Returns

true if the command was sent successfully, false otherwise.

Return type

bool

set_single_LED_color(number, red, green, blue)[source]

Change the color of a specific LED.

Parameters
  • number – the number of LED. Indexing starts at 1.

  • red – the red value between 0 and 255

  • green – the green value between 0 and 255

  • blue – the blue value between 0 and 255

Returns

Returns true if command written successfully, false otherwise

Return type

bool

Example Two - Single Pixel

examples/qwiic_led_stick_ex2_single_pixel.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex2_single_pixel.py
 4#
 5# This example will alternate blinking two single LEDs 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 2
39
40from __future__ import print_function
41import qwiic_led_stick
42import time
43import sys
44
45def run_example():
46
47    print("\nSparkFun Qwiic LED Stick Example 2")
48    my_stick = qwiic_led_stick.QwiicLEDStick()
49
50    if my_stick.begin() == False:
51        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
52            file=sys.stderr)
53        return
54    print("\nLED Stick ready!")
55
56    while True:
57
58        # Turn all LEDs off
59        my_stick.LED_off()
60        # TODO: make sure this numbering matches up with the silkscreen
61        # Turn on LED #4, red
62        my_stick.set_single_LED_color(4, 255, 0, 0)
63        time.sleep(1)
64        # Turn all LEDs off
65        my_stick.LED_off()
66        # Turn on LED #6, red
67        my_stick.set_single_LED_color(6, 255, 0, 0)
68        time.sleep(1)
69
70if __name__ == '__main__':
71    try:
72        run_example()
73    except (KeyboardInterrupt, SystemExit) as exErr:
74        print("\nEnding Example 2")
75        sys.exit(0)

Example Three - Unique Pixels

examples/qwiic_led_stick_ex3_single_pixel2.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex3_single_pixel2.py
 4#
 5# This example changes each LED of the LED Stick to an arbitrary color.
 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 3
39
40from __future__ import print_function
41import qwiic_led_stick
42import time
43import sys
44
45def run_example():
46
47    print("\nSparkFun Qwiic LED Stick Example 3")
48    my_stick = qwiic_led_stick.QwiicLEDStick()
49
50    if my_stick.begin() == False:
51        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
52            file=sys.stderr)
53        return
54    print("\nLED Stick ready!")
55
56    # Create 3 lists of the same length as the LED Stick, initialize with arbitrary values
57    # Pixel_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
58    red_list = [214, 78, 183, 198, 59, 134, 15, 209, 219, 186]
59    green_list = [59, 216, 170, 21, 114, 63, 226, 92, 155, 175]
60    blue_list = [214, 147, 25, 124, 153, 163, 188, 33, 175, 221]
61
62    my_stick.set_all_LED_unique_color(red_list, green_list, blue_list, 10)
63
64if __name__ == '__main__':
65    try:
66        run_example()
67    except (KeyboardInterrupt, SystemExit) as exErr:
68        print("\nEnding Example 3")
69        sys.exit(0)

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)

Example Five - Binary Counter

examples/qwiic_led_stick_ex5_binary_counter.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex5_binary_counter.py
 4#
 5# This example counts up from 0 to 1023 and displays the number in binary on the 
 6# LED 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 5
40
41from __future__ import print_function
42import qwiic_led_stick
43import time
44import sys
45
46def binary_LED_display(LED_stick, count, LED_length):
47    # Create color arrays because we want to turn on whole string of LEDs at one time
48    red_list = [0] * LED_length
49    green_list = [0] * LED_length
50    blue_list = [0] * LED_length
51    
52    # This for loop will repeat for each pixel of the LED Stick
53    for i in range(0, LED_length):
54        # For ith_bit, we use the bitshift operator. count >> i takes the binary
55        # representation of count and shifts it to the right i times. For example, 
56        # if count was 10, 0b1010, and i was 2, we get 0b10. This aligns with the 
57        # ith bit of count to the 0th bit of ith_bit
58        ith_bit = count >> i
59        # This will resolve to the oth bit of ith_bit
60        ith_bit_true = ith_bit & 0b1
61        # Write the color red to the current LED if the ith_bit_true
62        # LED_stick.set_single_LED_color(10 - i, 255 * ith_bit_true, 0, 0)
63        red_list[LED_length - i - 1] = 255 * ith_bit_true
64    
65    LED_stick.set_all_LED_unique_color(red_list, green_list, blue_list, LED_length)
66
67def binary_serial_display(count, bit_length):
68    print(str(count) + "\t" + str(bin(count)))
69
70def run_example():
71
72    print("\nSparkFun Qwiic LED Stick Example 5")
73    my_stick = qwiic_led_stick.QwiicLEDStick()
74
75    if my_stick.begin() == False:
76        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
77            file=sys.stderr)
78        return
79    print("\nLED Stick ready!")
80    
81    # Reset the state of LEDs
82    my_stick.LED_off()
83
84    while True:
85        # This loop counts from 0 to 1023 and displays the binary over the 
86        # serial port and the LED stick
87        for i in range(0, 1024):
88            binary_LED_display(my_stick, i, 10)
89            binary_serial_display(i, 10)
90            time.sleep(1)
91    
92if __name__ == '__main__':
93    try:
94        run_example()
95    except (KeyboardInterrupt, SystemExit) as exErr:
96        print("\nEnding Example 5")
97        sys.exit(0)

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)

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)

Example Eight - Walking Rainbow

examples/qwiic_led_stick_ex8_walking_rainbow.py
  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)

Example Nine - Change Length

examples/qwiic_led_stick_ex9_change_length.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex9_change_length.py
 4#
 5# This example changes the length of the attached LED strip and shows the results 
 6# by writing the whole strip white. Length will not reset on restart, change back
 7# to 10 if necessary with my_stick.change_length(10);
 8# If you add LEDs to the end of the sitck, you must change the length to be able to
 9# use them.
10# --------------------------------------------------------------------------------
11#
12# Written by Priyanka Makin @ SparkFun Electronics, June 2021
13# 
14# This python library supports the SpakrFun Electronics qwiic sensor/
15# board ecosystem on a Raspberry Pi (and compatible) board computers.
16#
17# More information on qwiic is at https://www.sparkfun.com/qwiic
18#
19# Do you like this library? Help support SparkFun by buying a board!
20#
21#==================================================================================
22# Copyright (c) 2019 SparkFun Electronics
23#
24# Permission is hereby granted, free of charge, to any person obtaining a copy 
25# of this software and associated documentation files (the "Software"), to deal 
26# in the Software without restriction, including without limitation the rights 
27# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
28# copies of the Software, and to permit persons to whom the Software is 
29# furnished to do so, subject to the following conditions:
30#
31# The above copyright notice and this permission notice shall be included in all 
32# copies or substantial portions of the Software.
33#
34# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
35# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
36# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
37# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
38# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
39# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
40# SOFTWARE.
41#==================================================================================
42# Example 9
43
44from __future__ import print_function
45import qwiic_led_stick
46import time
47import sys
48
49def run_example():
50
51    print("\nSparkFun Qwiic LED Stick Example 9")
52    my_stick = qwiic_led_stick.QwiicLEDStick()
53
54    if my_stick.begin() == False:
55        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
56            file=sys.stderr)
57        return
58    print("\nLED Stick ready!")
59    
60    # First, turn all LEDs off
61    my_stick.LED_off()
62    # Give stick time to turn all LEDs off
63    time.sleep(0.5)
64    
65    # Change LED length to 5
66    # This will allow you to write to a maximum of 5 LEDs
67    my_stick.change_length(5)
68    # Set all LEDs to dim white, notice only 5 are lit.
69    my_stick.set_all_LED_color(10, 10, 10)
70
71if __name__ == '__main__':
72    try:
73        run_example()
74    except (KeyboardInterrupt, SystemExit) as exErr:
75        print("\nEnding Example 1")
76        sys.exit(0)

Example Ten - Change Address

examples/qwiic_led_stick_ex10_change_address.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex10_change_address.py
 4#
 5# This example changes the address of the LED stick and shows the results by writing
 6# the whole strip white. Address will not reset on restart. Change the address back 
 7# to default with my_stick.change_address(0x23).
 8# --------------------------------------------------------------------------------
 9#
10# Written by Priyanka Makin @ SparkFun Electronics, June 2021
11# 
12# This python library supports the SpakrFun Electronics qwiic sensor/
13# board ecosystem on a Raspberry Pi (and compatible) board computers.
14#
15# More information on qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun by buying a board!
18#
19#==================================================================================
20# Copyright (c) 2019 SparkFun Electronics
21#
22# Permission is hereby granted, free of charge, to any person obtaining a copy 
23# of this software and associated documentation files (the "Software"), to deal 
24# in the Software without restriction, including without limitation the rights 
25# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
26# copies of the Software, and to permit persons to whom the Software is 
27# furnished to do so, subject to the following conditions:
28#
29# The above copyright notice and this permission notice shall be included in all 
30# copies or substantial portions of the Software.
31#
32# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
33# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
34# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
35# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
36# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
37# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
38# SOFTWARE.
39#==================================================================================
40# Example 10
41
42from __future__ import print_function
43import qwiic_led_stick
44import time
45import sys
46
47def run_example():
48
49    print("\nSparkFun Qwiic LED Stick Example 10")
50    my_stick = qwiic_led_stick.QwiicLEDStick()
51
52    if my_stick.begin() == False:
53        print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
54            file=sys.stderr)
55        return
56    print("\nLED Stick ready!")
57
58    print("\nEnter a new I2C address for the Qwiic LED Stick to use.")
59    print("\nDon't use the 0x prefix. For instance, if you wanted to")
60    print("\nchange the address to 0x5B, you would type 5B and hit enter.")
61
62    new_address = raw_input("\nNew address: ")
63    new_address = int(new_address, 16)
64
65    # Check if the user entered a valid address
66    if new_address > 0x08 and new_address < 0x77:
67        print("\nCharacters received and new address is valid!")
68        print("\nAttempting to set Qwiic LED Stick address...")
69
70        if my_stick.change_address(new_address) == True:
71            print("\nAddress successfully changed!")
72        # Check that the Qwiic LED Stick acknowledges on the new address
73        time.sleep(0.02)
74        if my_stick.begin() == False:
75            print("\nThe Qwiic LED Stick isn't connected to the system. Please check your connection", \
76            file=sys.stderr)
77        else:
78            print("\nLED Stick acknowledged on new address!")
79    else:
80        print("\nAddress entered not a valid I2C address.")
81
82if __name__ == '__main__':
83    try:
84        run_example()
85    except (KeyboardInterrupt, SystemExit) as exErr:
86        print("\nEnding Example 10")
87        sys.exit(0)

Example Eleven - 2 LED Sticks

examples/qwiic_led_stick_ex11_2_led_sticks.py
 1# !/usr/bin/env python
 2# ---------------------------------------------------------------------------------
 3# qwiic_led_stick_ex11_2_led_sticks.py
 4#
 5# This example shows how to use two LED Sticks on the same I2C bus.
 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 11
39
40from __future__ import print_function
41import qwiic_led_stick
42import time
43import sys
44
45def run_example():
46
47    print("\nSparkFun Qwiic LED Stick Example 11")
48    my_stick1 = qwiic_led_stick.QwiicLEDStick()
49    my_stick2 = qwiic_led_stick.QwiicLEDStick(0x29)
50
51    if my_stick1.begin() == False:
52        print("\nThe Qwiic LED Stick 1 isn't connected to the system. Please check your connection", \
53            file=sys.stderr)
54        return
55    print("\nLED Stick 1 ready!")
56
57    if my_stick2.begin() == False:
58        print("\nThe Qwiic LED Stick 2 isn't connected to the system. Please check your connection", \
59            file=sys.stderr)
60        return
61    print("\nLED Stick 2 ready!")
62
63    # Set all of LED Stick 1 to white
64    my_stick1.set_all_LED_color(10, 10, 10)
65    # Set all of LED Stick 2 to red
66    my_stick2.set_all_LED_color(255, 0, 0)
67
68if __name__ == '__main__':
69    try:
70        run_example()
71    except (KeyboardInterrupt, SystemExit) as exErr:
72        print("\nEnding Example 11")
73        sys.exit(0)

Indices and tables