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)