Example Ten - Buzz Multiple

examples/qwiic_buzzer_ex10_buzz_multiple.py
  1#!/usr/bin/env python
  2#-------------------------------------------------------------------------------
  3# qwiic_buzzer_ex10_buzz_multiple.py
  4#
  5# This example shows how to control multiple buzzers.
  6#
  7# Be warned, FULL VOLUME can cause your micro to reset.
  8#
  9# Note, you must use the "change_address"" example to change the address of your
 10# second (or third, etc.) buzzers. Here, we are using "0x5B" for the address
 11# of buzzer2.
 12#
 13# It turns each buzzer on and off with their own unique frequencies in a 
 14# unique pattern.
 15#-------------------------------------------------------------------------------
 16# Written by SparkFun Electronics, January 2024
 17#
 18# This python library supports the SparkFun Electroncis Qwiic ecosystem
 19#
 20# More information on Qwiic is at https://www.sparkfun.com/qwiic
 21#
 22# Do you like this library? Help support SparkFun. Buy a board!
 23#
 24# https://www.sparkfun.com/products/24474
 25#
 26#===============================================================================
 27# Copyright (c) 2023 SparkFun Electronics
 28#
 29# Permission is hereby granted, free of charge, to any person obtaining a copy 
 30# of this software and associated documentation files (the "Software"), to deal 
 31# in the Software without restriction, including without limitation the rights 
 32# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 33# copies of the Software, and to permit persons to whom the Software is 
 34# furnished to do so, subject to the following conditions:
 35#
 36# The above copyright notice and this permission notice shall be included in all 
 37# copies or substantial portions of the Software.
 38#
 39# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 40# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 41# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 42# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 43# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 44# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 45# SOFTWARE.
 46#===============================================================================
 47
 48import qwiic_buzzer
 49import sys
 50import time
 51
 52buzzer_1_address = 0x34
 53buzzer_2_address = 0x5B
 54
 55def runExample():
 56	print("\nQwiic Buzzer Example 10 - Buzz Multiple\n")
 57
 58	# Create instance of device
 59	my_buzzer1 = qwiic_buzzer.QwiicBuzzer(address = buzzer_1_address)
 60	my_buzzer2 = qwiic_buzzer.QwiicBuzzer(address = buzzer_2_address)
 61
 62	# Initialize the device 1
 63	if my_buzzer1.begin() == False:
 64		print("The device 1 isn't connected to the system. Please check your connection", \
 65			file=sys.stderr)
 66		return
 67
 68	print("\nQwiic Buzzer 1 ready!")
 69
 70	# Initialize the device 2
 71	if my_buzzer2.begin() == False:
 72		print("The device 2 isn't connected to the system. Please check your connection", \
 73			file=sys.stderr)
 74		return
 75
 76	print("\nQwiic Buzzer 2 ready!")	
 77	
 78	# Loop forever
 79	while True:
 80		my_buzzer1.configure(my_buzzer1.NOTE_C4, 0, my_buzzer1.VOLUME_MID)
 81		my_buzzer1.on()
 82
 83		time.sleep(1)
 84
 85		my_buzzer2.configure(my_buzzer2.NOTE_E6, 0, my_buzzer2.VOLUME_MID)
 86		my_buzzer2.on()
 87
 88		time.sleep(1)		
 89
 90		my_buzzer1.off()
 91
 92		time.sleep(1)
 93		
 94		my_buzzer2.off()
 95
 96		time.sleep(1)		
 97
 98if __name__ == '__main__':
 99	try:
100		runExample()
101	except (KeyboardInterrupt, SystemExit) as exErr:
102		print("\nEnding Example")
103		sys.exit(0)