Example Six - Save Settings

eexamples/qwiic_buzzer_ex6_save_settings.py
  1#!/usr/bin/env python
  2#-------------------------------------------------------------------------------
  3# qwiic_buzzer_ex6_save_settings.py
  4#
  5# This example shows how to save settings to the buzzer.
  6#
  7# It buzzes the buzzer once, and then commands it to save the settings.
  8#
  9# It then does nothing more in the main loop. It is up to you to trigger using
 10# the TRIGGER header pin.
 11#
 12# NOTE, TRIGGER PIN does utilize frequency, duration and volume. This means you can 
 13# set it up to be a "momentary" trigger button, or a "one-shot" button.
 14#
 15# When Duration=0, trigger is "momentary" trigger button.
 16#
 17# When Duration>0, trigger will fire a "one-shot" buzz of duration length.
 18#
 19# Note, this is most practically used when planning to trigger the buzzer using
 20# only the TRIGGER header pin.
 21#
 22# This is useful if you want to set the frequency, duration, volume to your 
 23# desired settings, and then have it remember them for next time you power your 
 24# Qwiic buzzer up. Then you can use the TRIGGER header to cause the buzzer to 
 25# buzz at your saved settings.
 26#
 27#-------------------------------------------------------------------------------
 28# Written by SparkFun Electronics, January 2024
 29#
 30# This python library supports the SparkFun Electroncis Qwiic ecosystem
 31#
 32# More information on Qwiic is at https://www.sparkfun.com/qwiic
 33#
 34# Do you like this library? Help support SparkFun. Buy a board!
 35#
 36# https://www.sparkfun.com/products/24474
 37#
 38#===============================================================================
 39# Copyright (c) 2023 SparkFun Electronics
 40#
 41# Permission is hereby granted, free of charge, to any person obtaining a copy 
 42# of this software and associated documentation files (the "Software"), to deal 
 43# in the Software without restriction, including without limitation the rights 
 44# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 45# copies of the Software, and to permit persons to whom the Software is 
 46# furnished to do so, subject to the following conditions:
 47#
 48# The above copyright notice and this permission notice shall be included in all 
 49# copies or substantial portions of the Software.
 50#
 51# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 52# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 53# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 54# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 55# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 56# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
 57# SOFTWARE.
 58#===============================================================================
 59
 60import qwiic_buzzer
 61import sys
 62import time
 63
 64def runExample():
 65	print("\nQwiic Buzzer Example 6 - Save Settings\n")
 66
 67	# Create instance of device
 68	my_buzzer = qwiic_buzzer.QwiicBuzzer()
 69
 70	# Initialize the device
 71	if my_buzzer.begin() == False:
 72		print("The device isn't connected to the system. Please check your connection", \
 73			file=sys.stderr)
 74		return
 75
 76	print("\nQwiic Buzzer ready!")
 77
 78	# Comment/Un-Comment the following "buzzer.configure()" example lines to try different settings:
 79  
 80	# "MOMENTARY" SETUP
 81	my_buzzer.configure(1000, 0, my_buzzer.VOLUME_MID); # frequency: 1KHz, duration: 0 (aka forever), volume: MID
 82
 83	# "ONE-SHOT" Setup (aka adding in a duration amount).
 84	# buzzer.configure(1000, 100, SFE_QWIIC_BUZZER_VOLUME_MID); # frequency: 1KHz, duration: 100ms, volume: MID	
 85	
 86	# Beep once to show settings
 87	my_buzzer.on()
 88	time.sleep(1)
 89	my_buzzer.off()
 90
 91	print("Saving settings now...")
 92	my_buzzer.save_settings()
 93
 94	print("Goodbye.")
 95
 96if __name__ == '__main__':
 97	try:
 98		runExample()
 99	except (KeyboardInterrupt, SystemExit) as exErr:
100		print("\nEnding Example")
101		sys.exit(0)