Example One - Buzz

examples/qwiic_buzzer_ex1_buzz.py
 1#!/usr/bin/env python
 2#-------------------------------------------------------------------------------
 3# qwiic_buzzer_ex1_buzz.py
 4#
 5# This example shows how to turn the buzzer on and off.
 6# Much like the classic "blink LED sketch" this will buzz
 7# the buzzer once every second.
 8#-------------------------------------------------------------------------------
 9# Written by SparkFun Electronics, January 2024
10#
11# This python library supports the SparkFun Electroncis Qwiic ecosystem
12#
13# More information on Qwiic is at https://www.sparkfun.com/qwiic
14#
15# Do you like this library? Help support SparkFun. Buy a board!
16#
17# https://www.sparkfun.com/products/24474
18#
19#===============================================================================
20# Copyright (c) 2023 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
41import qwiic_buzzer
42import sys
43import time
44
45def runExample():
46	print("\nQwiic Buzzer Example 1 - Buzz\n")
47
48	# Create instance of device
49	my_buzzer = qwiic_buzzer.QwiicBuzzer()
50
51	# Initialize the device
52	if my_buzzer.begin() == False:
53		print("The device isn't connected to the system. Please check your connection", \
54			file=sys.stderr)
55		return
56
57	print("\nQwiic Buzzer ready!")
58	
59	# Loop forever
60	while True:
61		my_buzzer.on()
62		time.sleep(1)
63		my_buzzer.off()
64		time.sleep(1)     
65
66if __name__ == '__main__':
67	try:
68		runExample()
69	except (KeyboardInterrupt, SystemExit) as exErr:
70		print("\nEnding Example")
71		sys.exit(0)