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)