Example Three - Buzz Duration

examples/qwiic_buzzer_ex3_buzz_duration.py
 1#!/usr/bin/env python
 2#-------------------------------------------------------------------------------
 3# qwiic_buzzer_ex3_buzz_duration.py
 4#
 5# This example shows how to control the buzzer using frequency and duration.
 6#
 7# This example shows how to turn the buzzer on and off.
 8# Much like the classic "blink LED sketch" this will buzz
 9# the buzzer once every second.
10#-------------------------------------------------------------------------------
11# Written by SparkFun Electronics, January 2024
12#
13# This python library supports the SparkFun Electroncis Qwiic ecosystem
14#
15# More information on Qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun. Buy a board!
18#
19# https://www.sparkfun.com/products/24474
20#
21#===============================================================================
22# Copyright (c) 2023 SparkFun Electronics
23#
24# Permission is hereby granted, free of charge, to any person obtaining a copy 
25# of this software and associated documentation files (the "Software"), to deal 
26# in the Software without restriction, including without limitation the rights 
27# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
28# copies of the Software, and to permit persons to whom the Software is 
29# furnished to do so, subject to the following conditions:
30#
31# The above copyright notice and this permission notice shall be included in all 
32# copies or substantial portions of the Software.
33#
34# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
35# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
36# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
37# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
38# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
39# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
40# SOFTWARE.
41#===============================================================================
42
43import qwiic_buzzer
44import sys
45import time
46
47def runExample():
48	print("\nQwiic Buzzer Example 3 - Buzz Duration\n")
49
50	# Create instance of device
51	my_buzzer = qwiic_buzzer.QwiicBuzzer()
52
53	# Initialize the device
54	if my_buzzer.begin() == False:
55		print("The device isn't connected to the system. Please check your connection", \
56			file=sys.stderr)
57		return
58
59	print("\nQwiic Buzzer ready!")
60	
61	# Loop forever
62	while True:
63		my_buzzer.configure(2730, 100) # frequency: 2.73KHz, duration: 100ms
64		my_buzzer.on()
65		time.sleep(1)     
66
67		my_buzzer.configure(1000, 500) # frequency: 1K, duration: 500ms
68		my_buzzer.on()
69		time.sleep(1)  
70
71		# Note, we dont' have to call buzzer.off(), because it will automatically turn
72		# off after the duration of each tone is completed.
73
74if __name__ == '__main__':
75	try:
76		runExample()
77	except (KeyboardInterrupt, SystemExit) as exErr:
78		print("\nEnding Example")
79		sys.exit(0)