1#!/usr/bin/env python
2#-------------------------------------------------------------------------------
3# qwiic_buzzer_ex5_change_address.py
4#
5# Demonstrates how to change the address of the Qwiic Buzzer
6#-------------------------------------------------------------------------------
7# Written by SparkFun Electronics, January 2024
8#
9# This python library supports the SparkFun Electroncis Qwiic ecosystem
10#
11# More information on Qwiic is at https://www.sparkfun.com/qwiic
12#
13# Do you like this library? Help support SparkFun. Buy a board!
14#
15# https://www.sparkfun.com/products/24474
16#
17#===============================================================================
18# Copyright (c) 2023 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
39import qwiic_buzzer
40import sys
41import time
42
43# The default address is 0x34, change this if your buzzer currently has a different address!
44initial_address = qwiic_buzzer.QwiicBuzzer.DEFAULT_ADDRESS
45
46def runExample():
47 print("\nQwiic Buzzer Example 5 - Change Address\n")
48
49 # Create instance of device
50 my_buzzer = qwiic_buzzer.QwiicBuzzer(address=initial_address)
51
52 # Initialize the device
53 if my_buzzer.begin() == False:
54 print("The device isn't connected to the system. Please check your connection", \
55 file=sys.stderr)
56 return
57
58 print("\nQwiic Buzzer ready!")
59
60 # Repeat until the address has been successfully changed
61 addressChanged = False
62 while not addressChanged:
63 # Print instructions
64 print()
65 print("Please enter a new address for the sensor.")
66 print("Any value between 0x08 and 0x77 is valid.")
67 print("Enter the address in hexadecimal without the `0x`.")
68 print()
69
70 # Read input from user
71 newAddress = input("New address: ")
72
73 try:
74 # Parse input using int() function
75 newAddress = int(newAddress, 16)
76
77 print("Parsed address:", hex(newAddress))
78
79 # Check if the address is valid
80 if newAddress < 0x08 or newAddress > 0x77:
81 print("Invalid address!")
82 continue
83
84 # Address is valid, attempt to change it on the device
85 result = my_buzzer.change_address(newAddress)
86
87 # Check whether the address was changed successfully
88 if result == False:
89 print("Failed to change address!")
90 continue
91
92 # Success, we're done here!
93 addressChanged = True
94
95 except ValueError:
96 print("Invalid address format!")
97
98 print("Address changed successfully! Continuing...")
99
100 # Wait a moment so user can read the messages
101 time.sleep(1)
102
103 # Loop forever
104 while True:
105 my_buzzer.on()
106 time.sleep(1)
107 my_buzzer.off()
108 time.sleep(1)
109
110if __name__ == '__main__':
111 try:
112 runExample()
113 except (KeyboardInterrupt, SystemExit) as exErr:
114 print("\nEnding Example")
115 sys.exit(0)