Example Seven - Buzz Melody

examples/qwiic_buzzer_ex7_buzz_melody.py
 1#!/usr/bin/env python
 2#-------------------------------------------------------------------------------
 3# qwiic_buzzer_ex7_buzz_melody.py
 4#
 5# This example shows how to buzz a melody on 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
43def runExample():
44	print("\nQwiic Buzzer Example 7 - Buzz Melody\n")
45
46	# Create instance of device
47	my_buzzer = qwiic_buzzer.QwiicBuzzer()
48
49	# Initialize the device
50	if my_buzzer.begin() == False:
51		print("The device isn't connected to the system. Please check your connection", \
52			file=sys.stderr)
53		return
54
55	print("\nQwiic Buzzer ready!")
56	
57	# notes in the melody
58	melody = [
59		my_buzzer.NOTE_C4, 
60		my_buzzer.NOTE_G3, 
61		my_buzzer.NOTE_G3, 
62		my_buzzer.NOTE_A3, 
63		my_buzzer.NOTE_G3, 
64		my_buzzer.NOTE_REST, 
65		my_buzzer.NOTE_B3, 
66		my_buzzer.NOTE_C4
67	]
68		
69	# note durations: 4 = quarter note, 8 = eighth note, etc.:
70	noteDurations = [4, 8, 8, 4, 4, 4, 4, 4]
71
72	# iterate over the notes of the melody:
73	for thisNote in range(8):
74		# to calculate the note duration, take one second divided by the note type.
75		#e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
76		noteDuration = round(1000 / noteDurations[thisNote])
77		my_buzzer.configure(melody[thisNote], noteDuration, my_buzzer.VOLUME_MAX)
78		my_buzzer.on()
79
80		# to distinguish the notes, set a minimum time between them.
81		# the note's duration + 30% seems to work well:
82		pauseBetweenNotes = noteDuration * 1.30
83		time.sleep(pauseBetweenNotes * 0.001)
84
85if __name__ == '__main__':
86	try:
87		runExample()
88	except (KeyboardInterrupt, SystemExit) as exErr:
89		print("\nEnding Example")
90		sys.exit(0)