Example Four - Buzz Volume

examples/qwiic_buzzer_ex4_buzz_volume.py
 1#!/usr/bin/env python
 2#-------------------------------------------------------------------------------
 3# qwiic_buzzer_ex4_buzz_volume.py
 4#
 5# This example shows how to control the buzzer to sound at different volumes.
 6#
 7# Note, the "configure()" function accepts three arguments, and you must send all three
 8# in order to access the volume control.
 9#
10# configure(frequency, duration, volume);
11#
12# This example shows how to turn the buzzer on and off.
13# Much like the classic "blink LED sketch" this will buzz
14# the buzzer once every second at different volumes.
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
52def runExample():
53	print("\nQwiic Buzzer Example 4 - Buzz Volume\n")
54
55	# Create instance of device
56	my_buzzer = qwiic_buzzer.QwiicBuzzer()
57
58	# Initialize the device
59	if my_buzzer.begin() == False:
60		print("The device isn't connected to the system. Please check your connection", \
61			file=sys.stderr)
62		return
63
64	print("\nQwiic Buzzer ready!")
65	
66	# Loop forever
67	while True:
68		print("\nVolume: MIN (1)")
69		my_buzzer.configure(2730, 100, my_buzzer.VOLUME_MIN) # frequency: 2.73KHz, duration: 100ms, volume: MIN
70		my_buzzer.on()
71		time.sleep(1)     
72
73		print("\nVolume: LOW (2)")
74		my_buzzer.configure(2730, 100, my_buzzer.VOLUME_LOW) # frequency: 2.73KHz, duration: 100ms, volume: LOW
75		my_buzzer.on()
76		time.sleep(1)   
77
78		print("\nVolume: MID (3)")
79		my_buzzer.configure(2730, 100, my_buzzer.VOLUME_MID) # frequency: 2.73KHz, duration: 100ms, volume: MID
80		my_buzzer.on()
81		time.sleep(1)   
82
83		print("\nVolume: MAX (4)")
84		my_buzzer.configure(2730, 100, my_buzzer.VOLUME_MAX) # frequency: 2.73KHz, duration: 100ms, volume: MAX
85		my_buzzer.on()
86		time.sleep(1)   						
87
88		# Note, we dont' have to call buzzer.off(), because it will automatically turn
89		# off after the duration of each tone is completed.
90
91if __name__ == '__main__':
92	try:
93		runExample()
94	except (KeyboardInterrupt, SystemExit) as exErr:
95		print("\nEnding Example")
96		sys.exit(0)