diff --git a/examples/Example_01_Buzz/Example_01_Buzz.ino b/examples/Example_01_Buzz/Example_01_Buzz.ino index f351590..b1cac1f 100644 --- a/examples/Example_01_Buzz/Example_01_Buzz.ino +++ b/examples/Example_01_Buzz/Example_01_Buzz.ino @@ -8,11 +8,11 @@ By Pete Lewis @ SparkFun Electronics December 2023 - Based on code orginally written by Fischer Moseley @ SparkFun Electronics + Based on code originally written by Fischer Moseley @ SparkFun Electronics Original Creation Date: June 28, 2019 - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Connect QWIIC cable from Arduino to Qwiic Buzzer @@ -28,12 +28,12 @@ void setup() { Serial.println("Qwiic Buzzer Example_01_Buzz"); Wire.begin(); //Join I2C bus - //check if buzzer will acknowledge over I2C + //check if buzzer will connect over I2C if (buzzer.begin() == false) { - Serial.println("Device did not acknowledge! Freezing."); + Serial.println("Device did not connect! Freezing."); while (1); } - Serial.println("Buzzer acknowledged."); + Serial.println("Buzzer connected."); } void loop() { diff --git a/examples/Example_02_Buzz_Frequency/Example_02_Buzz_Frequency.ino b/examples/Example_02_Buzz_Frequency/Example_02_Buzz_Frequency.ino index df18e96..b75ddd9 100644 --- a/examples/Example_02_Buzz_Frequency/Example_02_Buzz_Frequency.ino +++ b/examples/Example_02_Buzz_Frequency/Example_02_Buzz_Frequency.ino @@ -13,8 +13,8 @@ Based on code originally written by Fischer Moseley @ SparkFun Electronics Original Creation Date: June 28, 2019 - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Connect QWIIC cable from Arduino to Qwiic Buzzer @@ -30,22 +30,26 @@ void setup() { Serial.println("Qwiic Buzzer Example_02_Buzz_Frequency"); Wire.begin(); //Join I2C bus - //check if buzzer will acknowledge over I2C + //check if buzzer will connect over I2C if (buzzer.begin() == false) { - Serial.println("Device did not acknowledge! Freezing."); + Serial.println("Device did not connect! Freezing."); while (1); } - Serial.println("Buzzer acknowledged."); + Serial.println("Buzzer connected."); } void loop() { - buzzer.on(2730); // resonant frequency is 2.73KHz + // Configure with desired settings + // Resonant frequency is 2.73KHz + buzzer.configureBuzzer(SFE_QWIIC_BUZZER_RESONANT_FREQUENCY); + buzzer.on(); delay(100); buzzer.off(); delay(1000); - buzzer.on(1000); // try out 1KHz for fun + buzzer.configureBuzzer(1000); // set frequency to 1KHz + buzzer.on(); delay(100); buzzer.off(); diff --git a/examples/Example_03_Buzz_Duration/Example_03_Buzz_Duration.ino b/examples/Example_03_Buzz_Duration/Example_03_Buzz_Duration.ino index 0b170b8..16f3aec 100644 --- a/examples/Example_03_Buzz_Duration/Example_03_Buzz_Duration.ino +++ b/examples/Example_03_Buzz_Duration/Example_03_Buzz_Duration.ino @@ -13,8 +13,8 @@ Based on code originally written by Fischer Moseley @ SparkFun Electronics Original Creation Date: June 28, 2019 - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Connect QWIIC cable from Arduino to Qwiic Buzzer @@ -30,19 +30,21 @@ void setup() { Serial.println("Qwiic Buzzer Example_03_Buzz_Duration"); Wire.begin(); //Join I2C bus - //check if buzzer will acknowledge over I2C + //check if buzzer will connect over I2C if (buzzer.begin() == false) { - Serial.println("Device did not acknowledge! Freezing."); + Serial.println("Device did not connect! Freezing."); while (1); } - Serial.println("Buzzer acknowledged."); + Serial.println("Buzzer connected."); } void loop() { - buzzer.on(2730, 100); // frequency: 2.73KHz, duration: 100ms + buzzer.configureBuzzer(2730, 100); // frequency: 2.73KHz, duration: 100ms + buzzer.on(); delay(1000); - buzzer.on(1000, 500); // frequency: 1K, duration: 500ms + buzzer.configureBuzzer(1000, 500); // frequency: 1K, duration: 500ms + buzzer.on(); delay(1000); // Note, we dont' have to call buzzer.off(), because it will automatically turn diff --git a/examples/Example_04_Buzz_Volume/Example_04_Buzz_Volume.ino b/examples/Example_04_Buzz_Volume/Example_04_Buzz_Volume.ino index 6a9dc91..0f1a5df 100644 --- a/examples/Example_04_Buzz_Volume/Example_04_Buzz_Volume.ino +++ b/examples/Example_04_Buzz_Volume/Example_04_Buzz_Volume.ino @@ -3,10 +3,10 @@ This example shows how to control the buzzer to sound at different volumes. - Note, the "on()" function accepts three arguments, and you must send all three + Note, the "configureBuzzer()" function accepts three arguments, and you must send all three in order to access the volume control. - on(frequency, duration, volume); + configureBuzzer(frequency, duration, volume); It turns the buzzer on and off. Much like the classic "blink LED sketch" this will buzz @@ -18,8 +18,8 @@ Based on code originally written by Fischer Moseley @ SparkFun Electronics Original Creation Date: June 28, 2019 - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Connect QWIIC cable from Arduino to Qwiic Buzzer @@ -35,29 +35,33 @@ void setup() { Serial.println("Qwiic Buzzer Example_04_Buzz_Volume"); Wire.begin(); //Join I2C bus - //check if buzzer will acknowledge over I2C + //check if buzzer will connect over I2C if (buzzer.begin() == false) { - Serial.println("Device did not acknowledge! Freezing."); + Serial.println("Device did not connect! Freezing."); while (1); } - Serial.println("Buzzer acknowledged."); + Serial.println("Buzzer connected."); } void loop() { - Serial.println("Volume: Quietest (1)"); - buzzer.on(2730, 100, 1); // frequency: 2.73KHz, duration: 100ms, volume: 1 + Serial.println("Volume: MIN (1)"); + buzzer.configureBuzzer(2730, 100, SFE_QWIIC_BUZZER_VOLUME_MIN); // frequency: 2.73KHz, duration: 100ms, volume: MIN + buzzer.on(); delay(1000); - Serial.println("Volume: Mid-low (2)"); - buzzer.on(2730, 100, 2); // frequency: 2.73KHz, duration: 100ms, volume: 2 + Serial.println("Volume: LOW (2)"); + buzzer.configureBuzzer(2730, 100, SFE_QWIIC_BUZZER_VOLUME_LOW); // frequency: 2.73KHz, duration: 100ms, volume: LOW + buzzer.on(); delay(1000); - Serial.println("Volume: Mid-high (3)"); - buzzer.on(2730, 100, 3); // frequency: 2.73KHz, duration: 100ms, volume: 3 + Serial.println("Volume: MID (3)"); + buzzer.configureBuzzer(2730, 100, SFE_QWIIC_BUZZER_VOLUME_MID); // frequency: 2.73KHz, duration: 100ms, volume: MID + buzzer.on(); delay(1000); - Serial.println("Volume: Loudest (4)"); - buzzer.on(2730, 100, 4); // frequency: 2.73KHz, duration: 100ms, volume: 4 + Serial.println("Volume: MAX (4)"); + buzzer.configureBuzzer(2730, 100, SFE_QWIIC_BUZZER_VOLUME_MAX); // frequency: 2.73KHz, duration: 100ms, volume: MAX + buzzer.on(); delay(1000); // Note, we dont' have to use buzzer.off(), because it will automatically turn diff --git a/examples/Example_05_ChangeI2CAddress/Example_05_ChangeI2CAddress.ino b/examples/Example_05_ChangeI2CAddress/Example_05_ChangeI2CAddress.ino index 5aaa53f..1724a3c 100644 --- a/examples/Example_05_ChangeI2CAddress/Example_05_ChangeI2CAddress.ino +++ b/examples/Example_05_ChangeI2CAddress/Example_05_ChangeI2CAddress.ino @@ -5,8 +5,8 @@ Fischer Moseley @ SparkFun Electronics Original Creation Date: July 30, 2019 - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Attach the Qwiic Buzzer to your Redboard via Qwiic Cable @@ -18,17 +18,20 @@ #include QwiicBuzzer buzzer; +// The default address is 0x34, change this if your buzzer currently has a different address! +uint8_t initialAddress = SFE_QWIIC_BUZZER_DEFAULT_ADDRESS; + void setup() { Serial.begin(115200); Serial.println("Qwiic Buzzer example - I2C Address Change"); Wire.begin(); //Join I2C bus - //check if device will acknowledge over I2C - if (buzzer.begin(0x5B) == false) { - Serial.println("Device did not acknowledge! Running scanner."); + //check if device will connect over I2C + if (buzzer.begin(initialAddress) == false) { + Serial.println("Device did not connect! Running scanner."); } else{ - Serial.println("Device acknowledged!"); + Serial.println("Device connected!"); Serial.println(); Serial.println("Enter a new I2C address for the Qwiic Buzzer to use!"); @@ -51,11 +54,11 @@ void setup() { if (success) { if (newAddress > 0x08 && newAddress < 0x77) { - Serial.println("Character recieved, and device address is valid!"); + Serial.println("Character received, and device address is valid!"); Serial.print("Attempting to set device address to 0x"); Serial.println(newAddress, HEX); - if (buzzer.setI2Caddress(newAddress) == true) { + if (buzzer.setAddress(newAddress) == true) { Serial.println("Device address set succeeded!"); } @@ -66,16 +69,16 @@ void setup() { delay(100); //give the hardware time to do whatever configuration it needs to do if (buzzer.isConnected()) { - Serial.println("Device will acknowledge on new I2C address!"); + Serial.println("Device will connect on new I2C address!"); } else { - Serial.println("Device will not acknowledge on new I2C address."); + Serial.println("Device will not connect on new I2C address."); } } else { - Serial.println("Address out of range! Try an adress between 0x08 and 0x77"); + Serial.println("Address out of range! Try an address between 0x08 and 0x77"); } } @@ -99,7 +102,7 @@ void loop() { for (address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of - // the Write.endTransmisstion to see if + // the Write.endTransmission to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); diff --git a/examples/Example_06_SaveSettings/Example_06_SaveSettings.ino b/examples/Example_06_SaveSettings/Example_06_SaveSettings.ino index 9efe31b..005046a 100644 --- a/examples/Example_06_SaveSettings/Example_06_SaveSettings.ino +++ b/examples/Example_06_SaveSettings/Example_06_SaveSettings.ino @@ -23,8 +23,8 @@ Qwiic buzzer up. Then you can use the TRIGGER header to cause the buzzer to buzz at your saved settings. - Note, the "on()" function accepts three arguments: - on(frequency, duration, volume); + Note, the "configureBuzzer()" function accepts three arguments: + configureBuzzer(frequency, duration, volume); By Pete Lewis @ SparkFun Electronics December 2023 @@ -32,8 +32,8 @@ Based on code originally written by Fischer Moseley @ SparkFun Electronics Original Creation Date: June 28, 2019 - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Connect QWIIC cable from Arduino to Qwiic Buzzer @@ -49,23 +49,24 @@ void setup() { Serial.println("Qwiic Buzzer Example_06_SaveSettings"); Wire.begin(); //Join I2C bus - //check if buzzer will acknowledge over I2C + //check if buzzer will connect over I2C if (buzzer.begin() == false) { - Serial.println("Device did not acknowledge! Freezing."); + Serial.println("Device did not connect! Freezing."); while (1); } - Serial.println("Buzzer acknowledged."); + Serial.println("Buzzer connected."); Serial.println("Buzzing at 1K, volume 3"); // Comment/Un-Comment the following "buzzer.on()" example lines to try different settings: // "MOMENTARY" SETUP - buzzer.on(1000, 0, 3); // frequency: 1KHz, duration: 0 (aka forever), volume: 3 + buzzer.configureBuzzer(1000, 0, SFE_QWIIC_BUZZER_VOLUME_MID); // frequency: 1KHz, duration: 0 (aka forever), volume: MID // "ONE-SHOT" Setup (aka adding in a duration amount). - // buzzer.on(1000, 100, 3); // frequency: 1KHz, duration: 100ms, volume: 3 + // buzzer.configureBuzzer(1000, 100, SFE_QWIIC_BUZZER_VOLUME_MID); // frequency: 1KHz, duration: 100ms, volume: MID + buzzer.on(); delay(1000); Serial.println("Buzzer OFF"); diff --git a/examples/Example_07_Melody/Example_07_Melody.ino b/examples/Example_07_Melody/Example_07_Melody.ino index 5ca509b..dba7a60 100644 --- a/examples/Example_07_Melody/Example_07_Melody.ino +++ b/examples/Example_07_Melody/Example_07_Melody.ino @@ -6,11 +6,15 @@ By Pete Lewis @ SparkFun Electronics December 2023 + Based on original source code written by Tom Igeo in Jan 2010: + https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody + http://www.arduino.cc/en/Tutorial/Tone + Based on code originally written by Fischer Moseley @ SparkFun Electronics Original Creation Date: June 28, 2019 - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Connect QWIIC cable from Arduino to Qwiic Buzzer @@ -23,7 +27,14 @@ QwiicBuzzer buzzer; // notes in the melody: int melody[] = { - NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4 + SFE_QWIIC_BUZZER_NOTE_C4, + SFE_QWIIC_BUZZER_NOTE_G3, + SFE_QWIIC_BUZZER_NOTE_G3, + SFE_QWIIC_BUZZER_NOTE_A3, + SFE_QWIIC_BUZZER_NOTE_G3, + SFE_QWIIC_BUZZER_NOTE_REST, // silence (aka "rest") + SFE_QWIIC_BUZZER_NOTE_B3, + SFE_QWIIC_BUZZER_NOTE_C4 }; // note durations: 4 = quarter note, 8 = eighth note, etc.: @@ -31,20 +42,17 @@ int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4 }; -#define BUZZER_VOLUME 4 // loudest!! -//#define BUZZER_VOLUME 3 // pretty good volume for most things - void setup() { Serial.begin(115200); Serial.println("Qwiic Buzzer Example_07_Buzz_Melody"); Wire.begin(); //Join I2C bus - //check if buzzer will acknowledge over I2C + //check if buzzer will connect over I2C if (buzzer.begin() == false) { - Serial.println("Device did not acknowledge! Freezing."); + Serial.println("Device did not connect! Freezing."); while (1); } - Serial.println("Buzzer acknowledged."); + Serial.println("Buzzer connected."); Serial.println("Buzzing Melody now..."); play_melody(); @@ -63,13 +71,12 @@ void play_melody() // to calculate the note duration, take one second divided by the note type. //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc. int noteDuration = 1000 / noteDurations[thisNote]; - buzzer.on(melody[thisNote], noteDuration, BUZZER_VOLUME); + buzzer.configureBuzzer(melody[thisNote], noteDuration, SFE_QWIIC_BUZZER_VOLUME_MAX); + buzzer.on(); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = noteDuration * 1.30; delay(pauseBetweenNotes); - // stop the tone playing: - buzzer.off(); } } \ No newline at end of file diff --git a/examples/Example_08_Sound_Effects/Example_08_Sound_Effects.ino b/examples/Example_08_Sound_Effects/Example_08_Sound_Effects.ino index 89aabcd..262aeb6 100644 --- a/examples/Example_08_Sound_Effects/Example_08_Sound_Effects.ino +++ b/examples/Example_08_Sound_Effects/Example_08_Sound_Effects.ino @@ -3,6 +3,49 @@ This example demos the sound effects included in this library. + This includes the following sound effects: + 0 (aka "Siren") + Intended to sound like a siren, starting at a low frequency, and then + increasing rapidly up and then back down. This sound effect does a + single "up and down" cycle. + + 1 (aka "3 Fast Sirens") + Similar to the "Siren" sound effect, only faster and goes three times. + + 2 (aka "robot saying 'Yes'") + Intended to sound like a robot saying the word "yes". + It starts at a low frequency and quickly ramps up to a high frequency, + then stops. This can be interpreted by most to be an affirmative + sound to any question you may ask your buzzing robot. + + 3 (aka "robot yelling 'YES!'" - faster) + Same as the "Yes" sound effect, only faster. + When done more quickly, it can add enthusiasm to the buzzing sound. + + 4 (aka "robot saying 'No'") + Intended to sound like a robot saying the word "no". + It starts at a high frequency and quickly ramps down to a low frequency, + then stops. This can be interpreted by most to be an negative + sound to any question you may ask your buzzing robot. + + 5 (aka "robot yelling 'NO!'" - faster) + Same as the "No" sound effect, only faster. + When done more quickly, it can add enthusiasm to the buzzing sound. + + 6 (aka "Laughing Robot") + Intended to sound like your robot is laughing at you. + + 7 (aka "Laughing Robot Faster") + Same as the "Laugh" sound effect, only faster. + When done more quickly, it can add enthusiasm to the buzzing sound. + + 8 (aka "Crying Robot") + Intended to sound like a robot is crying and sad. + + 9 (aka "Crying Robot Faster") + Same as the "Cry" sound effect, only faster. + When done more quickly, it can add enthusiasm to the buzzing sound. + By Pete Lewis @ SparkFun Electronics December 2023 @@ -21,8 +64,8 @@ by Magician/hacker Jeff Haas. Thanks Jeff!! https://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds - This code is Lemonadeware; if you see me (or any other SparkFun employee) at the - local, and you've found our code helpful, please buy us a round! + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. Hardware Connections: Connect QWIIC cable from Arduino to Qwiic Buzzer @@ -33,26 +76,35 @@ #include QwiicBuzzer buzzer; -//#define BUZZER_VOLUME 4 // loudest!! -#define BUZZER_VOLUME 3 // pretty good volume for most things +#define BUZZER_VOLUME 4 // loudest!! +//#define BUZZER_VOLUME 3 // pretty good volume for most things +bool err; // used for checking for errors void setup() { Serial.begin(115200); Serial.println("Qwiic Buzzer Example_08_Sound_Effects"); Wire.begin(); //Join I2C bus + Wire.setClock(400000); // sound effects require changing configuration quickly - //check if buzzer will acknowledge over I2C + //check if buzzer will connect over I2C if (buzzer.begin() == false) { - Serial.println("Device did not acknowledge! Freezing."); + Serial.println("Device did not connect! Freezing."); while (1); } - Serial.println("Buzzer acknowledged."); + Serial.println("Buzzer connected."); for(int i = 0 ; i <= 9 ; i++) { Serial.print("Sound Effect: "); Serial.println(i); - buzzer.playSoundEffect(i, BUZZER_VOLUME); + err = buzzer.playSoundEffect(i, BUZZER_VOLUME); + + // Check whether the playSoundEffect() was successful + if (err == false) + { + Serial.println("error!"); + break; + } delay(2000); } } diff --git a/examples/Example_09_FirmwareVersion/Example_09_FirmwareVersion.ino b/examples/Example_09_FirmwareVersion/Example_09_FirmwareVersion.ino new file mode 100644 index 0000000..42d57ef --- /dev/null +++ b/examples/Example_09_FirmwareVersion/Example_09_FirmwareVersion.ino @@ -0,0 +1,71 @@ +/****************************************************************************** + Example_09_FirmwareVersion + + This example shows how to read the firmware version from the Qwiic Buzzer + + By Pete Lewis @ SparkFun Electronics + December 2023 + + Based on code originally written by Fischer Moseley @ SparkFun Electronics + Original Creation Date: June 28, 2019 + + SparkFun code, firmware, and software is released under the MIT License. + Please see LICENSE.md for further details. + + Hardware Connections: + Connect QWIIC cable from Arduino to Qwiic Buzzer + + Distributed as-is; no warranty is given. +******************************************************************************/ + +#include +QwiicBuzzer buzzer; + +// variables to store the firmware version (major and Minor), +// read back from the Qwiic Buzzer +uint8_t firmwareVersionMajor; +uint8_t firmwareVersionMinor; + +bool err; // used for checking for errors + +void setup() { + Serial.begin(115200); + Serial.println("Qwiic Buzzer Example_09_FirmwareVersion"); + Wire.begin(); //Join I2C bus + + //check if buzzer will connect over I2C + if (buzzer.begin() == false) { + Serial.println("Device did not connect! Freezing."); + while (1); + } + Serial.println("Buzzer connected."); + + err = buzzer.firmwareVersionMajor(firmwareVersionMajor); + + // Check whether the firmware read was successful + if (err == false) + { + Serial.println("Could not read firmware version Major. Freezing."); + while (1); + } + + err = buzzer.firmwareVersionMinor(firmwareVersionMinor); + + // Check whether the firmware read was successful + if (err == false) + { + Serial.println("Could not read firmware version Minor. Freezing."); + while (1); + } + + Serial.println("Firmware Version read successfully!"); + + Serial.print("Firmware Version Major: "); + Serial.println(firmwareVersionMajor); + Serial.print("Firmware Version Minor: "); + Serial.println(firmwareVersionMinor); +} + +void loop() { +// do nothing here +} diff --git a/keywords.txt b/keywords.txt index d48d663..d873eb5 100644 --- a/keywords.txt +++ b/keywords.txt @@ -4,21 +4,133 @@ # Class ###################################################################### -QwiicBuzzer KEYWORD1 +QwiicBuzzer KEYWORD1 ###################################################################### # Methods and Functions ###################################################################### -begin KEYWORD2 -isConnected KEYWORD2 -deviceID KEYWORD2 -checkDeviceID KEYWORD2 -getDeviceType KEYWORD2 -getFirmwareVersion KEYWORD2 -setI2Caddress KEYWORD2 -getI2Caddress KEYWORD2 -BUZZERconfig KEYWORD2 -off KEYWORD2 -on KEYWORD2 -saveSettings KEYWORD2 \ No newline at end of file +begin KEYWORD2 +isConnected KEYWORD2 +setAddress KEYWORD2 +address KEYWORD2 +firmwareVersionMajor KEYWORD2 +firmwareVersionMinor KEYWORD2 +configureBuzzer KEYWORD2 +on KEYWORD2 +off KEYWORD2 +saveSettings KEYWORD2 +playSoundEffect KEYWORD2 + +######################################################### +# Constants +######################################################### + +SFE_QWIIC_BUZZER_DEFAULT_ADDRESS LITERAL1 +SFE_QWIIC_BUZZER_DEVICE_ID LITERAL1 +kSfeQwiicBuzzerRegId LITERAL1 +kSfeQwiicBuzzerRegFirmwareMinor LITERAL1 +kSfeQwiicBuzzerRegFirmwareMajor LITERAL1 +kSfeQwiicBuzzerRegToneFrequencyMsb LITERAL1 +kSfeQwiicBuzzerRegToneFrequencyLsb LITERAL1 +kSfeQwiicBuzzerRegVolume LITERAL1 +kSfeQwiicBuzzerRegDurationMsb LITERAL1 +kSfeQwiicBuzzerRegDurationLsb LITERAL1 +kSfeQwiicBuzzerRegActive LITERAL1 +kSfeQwiicBuzzerRegSaveSettings LITERAL1 +kSfeQwiicBuzzerRegI2cAddress LITERAL1 +SFE_QWIIC_BUZZER_VOLUME_OFF LITERAL1 +SFE_QWIIC_BUZZER_VOLUME_MIN LITERAL1 +SFE_QWIIC_BUZZER_VOLUME_LOW LITERAL1 +SFE_QWIIC_BUZZER_VOLUME_MID LITERAL1 +SFE_QWIIC_BUZZER_VOLUME_MAX LITERAL1 + +SFE_QWIIC_BUZZER_NOTE_B0 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_E1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_F1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_FS1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_G1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_GS1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_A1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_AS1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_B1 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_E2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_F2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_FS2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_G2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_GS2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_A2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_AS2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_B2 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_E3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_F3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_FS3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_G3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_GS3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_A3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_AS3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_B3 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_E4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_F4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_FS4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_G4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_GS4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_A4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_AS4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_B4 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_E5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_F5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_FS5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_G5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_GS5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_A5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_AS5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_B5 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_E6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_F6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_FS6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_G6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_GS6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_A6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_AS6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_B6 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_E7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_F7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_FS7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_G7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_GS7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_A7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_AS7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_B7 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_C8 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_CS8 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_D8 LITERAL1 +SFE_QWIIC_BUZZER_NOTE_DS8 LITERAL1 \ No newline at end of file diff --git a/library.properties b/library.properties index 973cefc..9a8cc96 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,10 @@ name=SparkFun Qwiic Buzzer Library -version=0.0.1 +version=1.0.0 author=SparkFun Electronics maintainer=SparkFun Electronics -sentence=Communicates and configures the SparkFun Qwiic Buzzer +sentence=Communicates with and configures the SparkFun Qwiic Buzzer paragraph=This library allows the user to control the buzzer frequency/duration, and change its I2C addresses. category=Outputs -url=https://sparkfun.com/products/24474 +url=https://github.com/sparkfun/SparkFun_Qwiic_Buzzer_Arduino_Library architectures=* +depends=SparkFun Toolkit diff --git a/src/SparkFun_Qwiic_Buzzer_Arduino_Library.cpp b/src/SparkFun_Qwiic_Buzzer_Arduino_Library.cpp deleted file mode 100644 index e21f955..0000000 --- a/src/SparkFun_Qwiic_Buzzer_Arduino_Library.cpp +++ /dev/null @@ -1,528 +0,0 @@ -/****************************************************************************** -SparkFun_Qwiic_Buzzer_Arduino_Library.cpp -SparkFun Qwiic Buzzer Library Source File - -by Pete Lewis @SparkFun Electronics -Jan 2024 - -Based on original source code written by -Fischer Moseley @ SparkFun Electronics -Original Creation Date: July 24, 2019 - -Sound effects based on the following work: - Jan 21st, 2020 - Snake In A Can Controller - Written by: Pete Lewis, with contributions from Jeff Haas - A collaboration with Mario the Maker Magician - https://www.mariothemagician.com/ - - January, 2021 - Cry, Laugh Functions were adapted from Adafruit animal sounds - by Magician/hacker Jeff Haas. Thanks Jeff!! - https://learn.adafruit.com/adafruit-trinket-modded-stuffed-animal/animal-sounds - -This file implements the QwiicBuzzer class, prototyped in SparkFun_Qwiic_Buzzer_Arduino_Library.h - -Development environment specifics: - IDE: Arduino 2.2.1 - Hardware Platform: Arduino Uno/SparkFun Redboard - Qwiic Buzzer Version: v10 - -This code is Lemonadeware; if you see me (or any other SparkFun employee) at the -local, and you've found our code helpful, please buy us a round! - -Distributed as-is; no warranty is given. -******************************************************************************/ - -#include -#include - -#if defined(ARDUINO) && ARDUINO >= 100 -#include "Arduino.h" -#else -#include "WProgram.h" -#endif - -/*-------------------------------- Device Status ------------------------*/ - -bool QwiicBuzzer::begin(uint8_t address, TwoWire &wirePort) -{ - _deviceAddress = address; //grab the address that the sensor is on - _i2cPort = &wirePort; //grab the port that the user wants to use - - //return true if the device is connected and the device ID is what we expect - return (isConnected() && checkDeviceID()); -} - -bool QwiicBuzzer::isConnected() -{ - _i2cPort->beginTransmission(_deviceAddress); - if (_i2cPort->endTransmission() == 0) - return true; - return false; -} - -uint8_t QwiicBuzzer::deviceID() -{ - return readSingleRegister(SFE_QWIIC_BUZZER_ID); //read and return the value in the ID register -} - -bool QwiicBuzzer::checkDeviceID() -{ - return (deviceID() == SFE_QWIIC_BUZZER_DEV_ID); //Return true if the device ID matches -} - -uint8_t QwiicBuzzer::getDeviceType() -{ - if (isConnected()) - { //only try to get the device ID if the device will acknowledge - uint8_t id = deviceID(); - if (id == SFE_QWIIC_BUZZER_DEV_ID) - return 1; - } - return 0; -} - -uint16_t QwiicBuzzer::getFirmwareVersion() -{ - uint16_t version = (readSingleRegister(SFE_QWIIC_BUZZER_FIRMWARE_MAJOR)) << 8; - version |= readSingleRegister(SFE_QWIIC_BUZZER_FIRMWARE_MINOR); - return version; -} - -bool QwiicBuzzer::setI2Caddress(uint8_t address) -{ - if (address < 0x08 || address > 0x77) - { - Serial.println("Error1"); - return 1; //error immediately if the address is out of legal range - } - - bool success = writeSingleRegister(SFE_QWIIC_BUZZER_I2C_ADDRESS, address); - - if (success == true) - { - _deviceAddress = address; - return true; - } - - else - { - Serial.println("Error2"); - return false; - } -} - -uint8_t QwiicBuzzer::getI2Caddress() -{ - return _deviceAddress; -} - - - -/*------------------------ BUZZER Configuration ------------------------ */ -bool QwiicBuzzer::BUZZERconfig(uint16_t toneFrequency, uint16_t duration, uint8_t volume) -{ - bool success = writeSingleRegister(SFE_QWIIC_BUZZER_VOLUME, volume); - - uint8_t toneFrequencyMSB = ((toneFrequency & 0xFF00) >> 8 ); - uint8_t toneFrequencyLSB = (toneFrequency & 0x00FF); - success &= writeSingleRegister(SFE_QWIIC_BUZZER_TONE_FREQUENCY_MSB, toneFrequencyMSB); - success &= writeSingleRegister(SFE_QWIIC_BUZZER_TONE_FREQUENCY_LSB, toneFrequencyLSB); - - uint8_t durationMSB = ((duration & 0xFF00) >> 8 ); - uint8_t durationLSB = (duration & 0x00FF); - success &= writeSingleRegister(SFE_QWIIC_BUZZER_DURATION_MSB, durationMSB); - success &= writeSingleRegister(SFE_QWIIC_BUZZER_DURATION_LSB, durationLSB); - - if(volume > 0) - { - success &= setBuzzerActiveReg(); - } - else - { - success &= clearBuzzerActiveReg(); - } - - return success; -} - -bool QwiicBuzzer::on(uint16_t toneFrequency, uint16_t duration, uint8_t volume) -{ - return BUZZERconfig(toneFrequency, duration, volume); -} - -bool QwiicBuzzer::off() -{ - return clearBuzzerActiveReg(); -} - -bool QwiicBuzzer::saveSettings() -{ - return writeSingleRegister(SFE_QWIIC_BUZZER_SAVE_SETTINGS, 0x01); -} - -bool QwiicBuzzer::setBuzzerActiveReg() -{ - return writeSingleRegister(SFE_QWIIC_BUZZER_ACTIVE, 0x01); -} - -bool QwiicBuzzer::clearBuzzerActiveReg() -{ - return writeSingleRegister(SFE_QWIIC_BUZZER_ACTIVE, 0x00); -} - -/*------------------------- Internal I2C Abstraction ---------------- */ - -uint8_t QwiicBuzzer::readSingleRegister(Qwiic_Buzzer_Register reg) -{ - _i2cPort->beginTransmission(_deviceAddress); - _i2cPort->write(reg); - _i2cPort->endTransmission(); - - //typecasting the 1 parameter in requestFrom so that the compiler - //doesn't give us a warning about multiple candidates - if (_i2cPort->requestFrom(_deviceAddress, static_cast(1)) != 0) - { - return _i2cPort->read(); - } - return 0; -} - -uint16_t QwiicBuzzer::readDoubleRegister(Qwiic_Buzzer_Register reg) -{ //little endian - _i2cPort->beginTransmission(_deviceAddress); - _i2cPort->write(reg); - _i2cPort->endTransmission(); - - //typecasting the 2 parameter in requestFrom so that the compiler - //doesn't give us a warning about multiple candidates - if (_i2cPort->requestFrom(_deviceAddress, static_cast(2)) != 0) - { - uint16_t data = _i2cPort->read(); - data |= (_i2cPort->read() << 8); - return data; - } - return 0; -} - -unsigned long QwiicBuzzer::readQuadRegister(Qwiic_Buzzer_Register reg) -{ - _i2cPort->beginTransmission(_deviceAddress); - _i2cPort->write(reg); - _i2cPort->endTransmission(); - - union databuffer { - uint8_t array[4]; - unsigned long integer; - }; - - databuffer data; - - //typecasting the 4 parameter in requestFrom so that the compiler - //doesn't give us a warning about multiple candidates - if (_i2cPort->requestFrom(_deviceAddress, static_cast(4)) != 0) - { - for (uint8_t i = 0; i < 4; i++) - { - data.array[i] = _i2cPort->read(); - } - } - return data.integer; -} - -bool QwiicBuzzer::writeSingleRegister(Qwiic_Buzzer_Register reg, uint8_t data) -{ - _i2cPort->beginTransmission(_deviceAddress); - _i2cPort->write(reg); - _i2cPort->write(data); - if (_i2cPort->endTransmission() == 0) - return true; - return false; -} - -bool QwiicBuzzer::writeDoubleRegister(Qwiic_Buzzer_Register reg, uint16_t data) -{ - _i2cPort->beginTransmission(_deviceAddress); - _i2cPort->write(reg); - _i2cPort->write(lowByte(data)); - _i2cPort->write(highByte(data)); - if (_i2cPort->endTransmission() == 0) - return true; - return false; -} - -uint8_t QwiicBuzzer::writeSingleRegisterWithReadback(Qwiic_Buzzer_Register reg, uint8_t data) -{ - if (writeSingleRegister(reg, data)) - return 1; - if (readSingleRegister(reg) != data) - return 2; - return 0; -} - -uint16_t QwiicBuzzer::writeDoubleRegisterWithReadback(Qwiic_Buzzer_Register reg, uint16_t data) -{ - if (writeDoubleRegister(reg, data)) - return 1; - if (readDoubleRegister(reg) != data) - return 2; - return 0; -} - -/*------------------------- Sound Effects ---------------- */ - -void QwiicBuzzer::playSoundEffect(uint8_t soundEffectNumber, uint8_t volume) -{ - switch (soundEffectNumber) { - case 0: - soundEffect0(volume); - break; - case 1: - soundEffect1(volume); - break; - case 2: - soundEffect2(volume); - break; - case 3: - soundEffect3(volume); - break; - case 4: - soundEffect4(volume); - break; - case 5: - soundEffect5(volume); - break; - case 6: - soundEffect6(volume); - break; - case 7: - soundEffect7(volume); - break; - case 8: - soundEffect8(volume); - break; - case 9: - soundEffect9(volume); - break; - } -} - -// SIREN SLOW X1 -void QwiicBuzzer::soundEffect0(uint8_t volume) -{ - for (int note = 150 ; note < 4000 ; note += 150) - { - on(note, 10, volume); - delay(10); - } - for (int note = 4000 ; note > 150 ; note -= 150) - { - on(note, 10, volume); - delay(10); - } - off(); -} - -// SIREN FAST X3 -void QwiicBuzzer::soundEffect1(uint8_t volume) -{ - for (int i = 0 ; i <= 2 ; i++) - { - for (int note = 150 ; note < 4000 ; note += 150) - { - on(note, 2, volume); - delay(2); - } - for (int note = 4000 ; note > 150 ; note -= 150) - { - on(note, 2, volume); - delay(2); - } - } - off(); -} - -// YES SLOW -void QwiicBuzzer::soundEffect2(uint8_t volume) -{ - for (int note = 150 ; note < 4000 ; note += 150) - { - on(note, 50, volume); - delay(40); - } - off(); -} - -// YES FAST -void QwiicBuzzer::soundEffect3(uint8_t volume) -{ - for (int note = 150 ; note < 4000 ; note += 150) - { - on(note, 10, volume); - delay(10); - } - off(); -} - -// NO SLOW -void QwiicBuzzer::soundEffect4(uint8_t volume) -{ - for (int note = 4000 ; note > 150 ; note -= 150) - { - on(note, 50, volume); - delay(40); - } - off(); -} - -// NO FAST -void QwiicBuzzer::soundEffect5(uint8_t volume) -{ - for (int note = 4000 ; note > 150 ; note -= 150) - { - on(note, 10, volume); - delay(10); - } - off(); -} - -// LAUGH -void QwiicBuzzer::soundEffect6(uint8_t volume) -{ - int laughdelay = 400; - int laughstep = 10; - uint16_t i; - - //for (i = 650; i > 525; i -= 3) // vary up //1538, 1905 - for (i = 1538; i < 1905; i += laughstep) // vary up //1538, 1905 - { - on(i, 20, volume); - delay(10); - } - - delay(laughdelay); - - //for (i = 800; i > 660; i -= 3) // 1250, 1515 - for (i = 1250; i < 1515; i += laughstep) // 1250, 1515 - { - on(i, 20, volume); - delay(10); - } - delay(laughdelay); - - //for (i = 900; i > 745; i -= 3) // 1111, 1342 - for (i = 1111; i < 1342; i += laughstep) // 1111, 1342 - { - on(i, 20, volume); - delay(10); - } - delay(laughdelay); - - //for (i = 990; i > 850; i -= 3) // 1010, 1176 - for (i = 1010; i < 1176; i += laughstep) // 1010, 1176 - { - on(i, 20, volume); - delay(10); - } - off(); -} - -// LAUGH FAST -void QwiicBuzzer::soundEffect7(uint8_t volume) -{ - int laughdelay = 200; - int laughstep = 15; - uint16_t i; - - for (i = 1538; i < 1905; i += laughstep) // vary up //1538, 1905 - { - on(i, 20, volume); - delay(10); - } - - delay(laughdelay); - - for (i = 1250; i < 1515; i += laughstep) // 1250, 1515 - { - on(i, 20, volume); - delay(10); - } - delay(laughdelay); - - for (i = 1111; i < 1342; i += laughstep) // 1111, 1342 - { - on(i, 20, volume); - delay(10); - } - delay(laughdelay); - - for (i = 1010; i < 1176; i += laughstep) // 1010, 1176 - { - on(i, 20, volume); - delay(10); - } - off(); -} - -// CRY SLOW -void QwiicBuzzer::soundEffect8(uint8_t volume) -{ - int crydelay = 500; - int step = 10; - uint16_t i; - - for (i = 2000; i > 1429; i -= step) // vary down //2000, 1429 - { - on(i, 20, volume); - delay(10); - } - - delay(crydelay); - - for (i = 1667; i > 1250; i -= step) // 1667, 1250 - { - on(i, 20, volume); - delay(10); - } - delay(crydelay); - - //for (i = 900; i > 745; i -= 3) // 1111, 1342 - for (i = 1429; i > 1053; i -= step) // 1429, 1053 - { - on(i, 20, volume); - delay(10); - } - off(); -} - -// CRY FAST -void QwiicBuzzer::soundEffect9(uint8_t volume) -{ - int crydelay = 200; - int step = 20; - uint16_t i; - - for (i = 2000; i > 1429; i -= step) // vary down //2000, 1429 - { - on(i, 20, volume); - delay(10); - } - - delay(crydelay); - - for (i = 1667; i > 1250; i -= step) // 1667, 1250 - { - on(i, 20, volume); - delay(10); - } - delay(crydelay); - - //for (i = 900; i > 745; i -= 3) // 1111, 1342 - for (i = 1429; i > 1053; i -= step) // 1429, 1053 - { - on(i, 20, volume); - delay(10); - } - off(); -} \ No newline at end of file diff --git a/src/SparkFun_Qwiic_Buzzer_Arduino_Library.h b/src/SparkFun_Qwiic_Buzzer_Arduino_Library.h index 3c9de84..2276e86 100644 --- a/src/SparkFun_Qwiic_Buzzer_Arduino_Library.h +++ b/src/SparkFun_Qwiic_Buzzer_Arduino_Library.h @@ -1,83 +1,56 @@ /****************************************************************************** -SparkFun_Qwiic_Buzzer_Arduino_Library.h -SparkFun Qwiic Buzzer Library header file + SparkFun_Qwiic_Buzzer_Arduino_Library.h + SparkFun Qwiic Buzzer Library header file -by Pete Lewis @SparkFun Electronics -Jan 2024 + by Pete Lewis @SparkFun Electronics + January 2024 -Based on original source code written by -Fischer Moseley @ SparkFun Electronics -Original Creation Date: July 24, 2019 + Based on original source code written by + Fischer Moseley @ SparkFun Electronics + Original Creation Date: July 24, 2019 -This file implements the QwiicBuzzer class, prototyped in SparkFun_Qwiic_Buzzer_Arduino_Library.h + This file implements the QwiicBuzzer class, prototyped in SparkFun_Qwiic_Buzzer_Arduino_Library.h -Development environment specifics: - IDE: Arduino 2.2.1 - Hardware Platform: Arduino Uno/SparkFun Redboard - Qwiic Buzzer Version: v10 + Development environment specifics: + IDE: Arduino 2.2.1 + Hardware Platform: Arduino Uno/SparkFun Redboard + Qwiic Buzzer Version: v10 -This code is Lemonadeware; if you see me (or any other SparkFun employee) at the -local, and you've found our code helpful, please buy us a round! + SPDX-License-Identifier: MIT -Distributed as-is; no warranty is given. -******************************************************************************/ + Copyright (c) 2024 SparkFun Electronics -#ifndef __SparkFun_Qwiic_Buzzer_H__ -#define __SparkFun_Qwiic_Buzzer_H__ + Distributed as-is; no warranty is given. +******************************************************************************/ -#include -#include -#include "registers.h" -#include "buzzer_pitches.h" +#pragma once -#define SFE_QWIIC_BUZZER_DEFAULT_ADDRESS 0x34 //default I2C address of the buzzer -#define SFE_QWIIC_BUZZER_DEV_ID 0x5D //device ID of the Qwiic Buzzer +#include "sfeQwiicBuzzer.h" +#include -class QwiicBuzzer +class QwiicBuzzer : public sfeQwiicBuzzer { -private: - TwoWire *_i2cPort; //Generic connection to user's chosen I2C port - uint8_t _deviceAddress; //I2C address of the buzzer - -public: - //Device status - bool begin(uint8_t address = SFE_QWIIC_BUZZER_DEFAULT_ADDRESS, TwoWire &wirePort = Wire); //Sets device I2C address to a user-specified address, over whatever port the user specifies. - bool isConnected(); //Returns true if the device will acknowledge over I2C, and false otherwise - uint8_t deviceID(); //Return the 8-bit device ID of the attached device. - bool checkDeviceID(); //Returns true if the device ID matches that of the buzzer - uint8_t getDeviceType(); //Returns 1 if a buzzer is attached, 2 if a switch is attached. Returns 0 if there is no device attached. - uint16_t getFirmwareVersion(); //Returns the firmware version of the attached device as a 16-bit integer. The leftmost (high) byte is the major revision number, and the rightmost (low) byte is the minor revision number. - bool setI2Caddress(uint8_t address); //Configures the attached device to attach to the I2C bus using the specified address - uint8_t getI2Caddress(); //Returns the I2C address of the device. - - //Buzzer configuration - bool BUZZERconfig(uint16_t toneFrequency, uint16_t duration, - uint8_t volume); //Configures the LED with the given max brightness, granularity (1 is fine for most applications), cycle time, and off time. - bool on(uint16_t toneFrequency = 2730, uint16_t duration = 0, uint8_t volume = 4); - bool off(); - bool saveSettings(); // store settings to EEPROM - bool setBuzzerActiveReg(); - bool clearBuzzerActiveReg(); - - void playSoundEffect(uint8_t soundEffectNumber, uint8_t volume); - void soundEffect0(uint8_t volume); - void soundEffect1(uint8_t volume); - void soundEffect2(uint8_t volume); - void soundEffect3(uint8_t volume); - void soundEffect4(uint8_t volume); - void soundEffect5(uint8_t volume); - void soundEffect6(uint8_t volume); - void soundEffect7(uint8_t volume); - void soundEffect8(uint8_t volume); - void soundEffect9(uint8_t volume); - - //Internal I2C Abstraction - uint8_t readSingleRegister(Qwiic_Buzzer_Register reg); //Reads a single 8-bit register. - uint16_t readDoubleRegister(Qwiic_Buzzer_Register reg); //Reads a 16-bit register (little endian). - unsigned long readQuadRegister(Qwiic_Buzzer_Register reg); //Reads a 32-bit register (little endian). - bool writeSingleRegister(Qwiic_Buzzer_Register reg, uint8_t data); //Attempts to write data into a single 8-bit register. Does not check to make sure it was written successfully. Returns 0 if there wasn't an error on I2C transmission, and 1 otherwise. - bool writeDoubleRegister(Qwiic_Buzzer_Register reg, uint16_t data); //Attempts to write data into a double (two 8-bit) registers. Does not check to make sure it was written successfully. Returns 0 if there wasn't an error on I2C transmission, and 1 otherwise. - uint8_t writeSingleRegisterWithReadback(Qwiic_Buzzer_Register reg, uint8_t data); //Writes data into a single 8-bit register, and checks to make sure that the data was written successfully. Returns 0 on no error, 1 on I2C write fail, and 2 if the register doesn't read back the same value that was written. - uint16_t writeDoubleRegisterWithReadback(Qwiic_Buzzer_Register reg, uint16_t data); //Writes data into a double (two 8-bit) registers, and checks to make sure that the data was written successfully. Returns 0 on no error, 1 on I2C write fail, and 2 if the register doesn't read back the same value that was written. + public: + /// @brief Begins the Qwiic Buzzer + /// @param address I2C device address to use for the sensor + /// @param wirePort Wire port to use for I2C communication + /// @return True if successful, false otherwise + bool begin(const uint8_t address = SFE_QWIIC_BUZZER_DEFAULT_ADDRESS, TwoWire &wirePort = Wire) + { + // Setup Arudino I2C bus + _theI2CBus.init(wirePort, address); + + // Begin the sensor + return sfeQwiicBuzzer::begin(&_theI2CBus) == kSTkErrOk; + } + + /// @brief Checks if the Qwiic Buzzer is connected + /// @return True if the sensor is connected, false otherwise + bool isConnected() + { + return sfeQwiicBuzzer::isConnected() == kSTkErrOk; + } + + private: + sfeTkArdI2C _theI2CBus; }; -#endif diff --git a/src/buzzer_pitches.h b/src/buzzer_pitches.h deleted file mode 100644 index a0d69ac..0000000 --- a/src/buzzer_pitches.h +++ /dev/null @@ -1,93 +0,0 @@ -/************************************************* - Public Constants - *************************************************/ - -#define NOTE_B0 31 -#define NOTE_C1 33 -#define NOTE_CS1 35 -#define NOTE_D1 37 -#define NOTE_DS1 39 -#define NOTE_E1 41 -#define NOTE_F1 44 -#define NOTE_FS1 46 -#define NOTE_G1 49 -#define NOTE_GS1 52 -#define NOTE_A1 55 -#define NOTE_AS1 58 -#define NOTE_B1 62 -#define NOTE_C2 65 -#define NOTE_CS2 69 -#define NOTE_D2 73 -#define NOTE_DS2 78 -#define NOTE_E2 82 -#define NOTE_F2 87 -#define NOTE_FS2 93 -#define NOTE_G2 98 -#define NOTE_GS2 104 -#define NOTE_A2 110 -#define NOTE_AS2 117 -#define NOTE_B2 123 -#define NOTE_C3 131 -#define NOTE_CS3 139 -#define NOTE_D3 147 -#define NOTE_DS3 156 -#define NOTE_E3 165 -#define NOTE_F3 175 -#define NOTE_FS3 185 -#define NOTE_G3 196 -#define NOTE_GS3 208 -#define NOTE_A3 220 -#define NOTE_AS3 233 -#define NOTE_B3 247 -#define NOTE_C4 262 -#define NOTE_CS4 277 -#define NOTE_D4 294 -#define NOTE_DS4 311 -#define NOTE_E4 330 -#define NOTE_F4 349 -#define NOTE_FS4 370 -#define NOTE_G4 392 -#define NOTE_GS4 415 -#define NOTE_A4 440 -#define NOTE_AS4 466 -#define NOTE_B4 494 -#define NOTE_C5 523 -#define NOTE_CS5 554 -#define NOTE_D5 587 -#define NOTE_DS5 622 -#define NOTE_E5 659 -#define NOTE_F5 698 -#define NOTE_FS5 740 -#define NOTE_G5 784 -#define NOTE_GS5 831 -#define NOTE_A5 880 -#define NOTE_AS5 932 -#define NOTE_B5 988 -#define NOTE_C6 1047 -#define NOTE_CS6 1109 -#define NOTE_D6 1175 -#define NOTE_DS6 1245 -#define NOTE_E6 1319 -#define NOTE_F6 1397 -#define NOTE_FS6 1480 -#define NOTE_G6 1568 -#define NOTE_GS6 1661 -#define NOTE_A6 1760 -#define NOTE_AS6 1865 -#define NOTE_B6 1976 -#define NOTE_C7 2093 -#define NOTE_CS7 2217 -#define NOTE_D7 2349 -#define NOTE_DS7 2489 -#define NOTE_E7 2637 -#define NOTE_F7 2794 -#define NOTE_FS7 2960 -#define NOTE_G7 3136 -#define NOTE_GS7 3322 -#define NOTE_A7 3520 -#define NOTE_AS7 3729 -#define NOTE_B7 3951 -#define NOTE_C8 4186 -#define NOTE_CS8 4435 -#define NOTE_D8 4699 -#define NOTE_DS8 4978 diff --git a/src/registers.h b/src/registers.h deleted file mode 100644 index bb99699..0000000 --- a/src/registers.h +++ /dev/null @@ -1,35 +0,0 @@ -/****************************************************************************** -registers.h -Fischer Moseley @ SparkFun Electronics -Original Creation Date: July 24, 2019 - -This file defines the virtual memory map on the Qwiic Buzzer. The enum -provides a set of pointers for the various registers on the Qwiic -Buzzer. - -Development environment specifics: - IDE: Arduino 2.2.1 - Hardware Platform: Arduino Uno/SparkFun Redboard - Qwiic Buzzer Version: v10 - -This code is Lemonadeware; if you see me (or any other SparkFun employee) at the -local, and you've found our code helpful, please buy us a round! - -Distributed as-is; no warranty is given. -******************************************************************************/ - -//Register Pointer Map -enum Qwiic_Buzzer_Register : uint8_t -{ - SFE_QWIIC_BUZZER_ID = 0x00, - SFE_QWIIC_BUZZER_FIRMWARE_MINOR = 0x01, - SFE_QWIIC_BUZZER_FIRMWARE_MAJOR = 0x02, - SFE_QWIIC_BUZZER_TONE_FREQUENCY_MSB = 0x03, - SFE_QWIIC_BUZZER_TONE_FREQUENCY_LSB = 0x04, - SFE_QWIIC_BUZZER_VOLUME = 0x05, - SFE_QWIIC_BUZZER_DURATION_MSB = 0x06, - SFE_QWIIC_BUZZER_DURATION_LSB = 0x07, - SFE_QWIIC_BUZZER_ACTIVE = 0x08, - SFE_QWIIC_BUZZER_SAVE_SETTINGS = 0x09, - SFE_QWIIC_BUZZER_I2C_ADDRESS = 0x0A, -}; \ No newline at end of file diff --git a/src/sfeQwiicBuzzer.cpp b/src/sfeQwiicBuzzer.cpp new file mode 100644 index 0000000..7bdd329 --- /dev/null +++ b/src/sfeQwiicBuzzer.cpp @@ -0,0 +1,650 @@ +/****************************************************************************** + sfeQwiicBuzzer.h + SparkFun Qwiic Buzzer Library header file + + by Pete Lewis @SparkFun Electronics + January 2024 + + Based on original source code written by + Fischer Moseley @ SparkFun Electronics + Original Creation Date: July 24, 2019 + + Development environment specifics: + IDE: Arduino 2.2.1 + Hardware Platform: Arduino Uno/SparkFun Redboard + Qwiic Buzzer Version: v10 + + SPDX-License-Identifier: MIT + + Copyright (c) 2023 SparkFun Electronics + + Distributed as-is; no warranty is given. +******************************************************************************/ + +#include "sfeQwiicBuzzer.h" + +sfeTkError_t sfeQwiicBuzzer::begin(sfeTkII2C *theBus) +{ + // Nullptr check + if (theBus == nullptr) + return kSTkErrFail; + + // Set bus pointer + _theBus = theBus; + + sfeTkError_t err; + err = isConnected(); + // Check whether the ping was successful + if (err != kSTkErrOk) + return err; + + uint8_t readDeviceId; + err = deviceId(readDeviceId); + // Check whether the read was successful + if (err != kSTkErrOk) + return err; + + // check that device ID matches + if (readDeviceId != SFE_QWIIC_BUZZER_DEVICE_ID) + return kSTkErrFail; + + // Done! + return kSTkErrOk; +} + +sfeTkError_t sfeQwiicBuzzer::isConnected() +{ + // Just ping the device address + return _theBus->ping(); +} + +sfeTkError_t sfeQwiicBuzzer::deviceId(uint8_t &deviceId) +{ + return _theBus->readRegisterByte(kSfeQwiicBuzzerRegId, deviceId); +} + +bool sfeQwiicBuzzer::firmwareVersionMajor(uint8_t &versionMajor) +{ + sfeTkError_t err; + err = _theBus->readRegisterByte(kSfeQwiicBuzzerRegFirmwareMajor, versionMajor); + if (err == kSTkErrOk) + return true; + else + return false; +} + +bool sfeQwiicBuzzer::firmwareVersionMinor(uint8_t &versionMinor) +{ + sfeTkError_t err; + err = _theBus->readRegisterByte(kSfeQwiicBuzzerRegFirmwareMinor, versionMinor); + if (err == kSTkErrOk) + return true; + else + return false; +} + +sfeTkError_t sfeQwiicBuzzer::configureBuzzer(const uint16_t toneFrequency, const uint16_t duration, + const uint8_t volume) +{ + // All of the necessary configuration register addresses are in sequential order, + // starting at "kSfeQwiicBuzzerRegToneFrequencyMsb". + // We can write all of them in a single use of "writeRegisterRegion()". + + // kSfeQwiicBuzzerRegToneFrequencyMsb = 0x03, + // kSfeQwiicBuzzerRegToneFrequencyLsb = 0x04, + // kSfeQwiicBuzzerRegVolume = 0x05, + // kSfeQwiicBuzzerRegDurationMsb = 0x06, + // kSfeQwiicBuzzerRegDurationLsb = 0x07, + + // extract MSBs and LSBs from user passed in arguments + uint8_t toneFrequencyMSB = ((toneFrequency & 0xFF00) >> 8); + uint8_t toneFrequencyLSB = (toneFrequency & 0x00FF); + uint8_t durationMSB = ((duration & 0xFF00) >> 8); + uint8_t durationLSB = (duration & 0x00FF); + + size_t dataLength = 5; + uint8_t data[dataLength]; + + data[0] = toneFrequencyMSB; // kSfeQwiicBuzzerRegToneFrequencyMsb + data[1] = toneFrequencyLSB; // kSfeQwiicBuzzerRegToneFrequencyLsb + data[2] = volume; // kSfeQwiicBuzzerRegVolume + data[3] = durationMSB; // kSfeQwiicBuzzerRegDurationMsb + data[4] = durationLSB; // kSfeQwiicBuzzerRegDurationLsb + + return _theBus->writeRegisterRegion(kSfeQwiicBuzzerRegToneFrequencyMsb, data, dataLength); +} + +sfeTkError_t sfeQwiicBuzzer::on() +{ + return _theBus->writeRegisterByte(kSfeQwiicBuzzerRegActive, 1); +} + +sfeTkError_t sfeQwiicBuzzer::off() +{ + return _theBus->writeRegisterByte(kSfeQwiicBuzzerRegActive, 0); +} + +sfeTkError_t sfeQwiicBuzzer::saveSettings() +{ + return _theBus->writeRegisterByte(kSfeQwiicBuzzerRegSaveSettings, 1); +} + +sfeTkError_t sfeQwiicBuzzer::setAddress(const uint8_t &address) +{ + if (address < 0x08 || address > 0x77) + { + return kSTkErrFail; // error immediately if the address is out of legal range + } + + sfeTkError_t err = _theBus->writeRegisterByte(kSfeQwiicBuzzerRegI2cAddress, address); + + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + // Update the address in the bus + _theBus->setAddress(address); + + // Done! + return kSTkErrOk; +} + +uint8_t sfeQwiicBuzzer::address() +{ + return _theBus->address(); +} + +bool sfeQwiicBuzzer::playSoundEffect(const uint8_t soundEffectNumber, const uint8_t volume) +{ + sfeTkError_t err; + + switch (soundEffectNumber) + { + case 0: + err = soundEffect0(volume); + break; + case 1: + err = soundEffect1(volume); + break; + case 2: + err = soundEffect2(volume); + break; + case 3: + err = soundEffect3(volume); + break; + case 4: + err = soundEffect4(volume); + break; + case 5: + err = soundEffect5(volume); + break; + case 6: + err = soundEffect6(volume); + break; + case 7: + err = soundEffect7(volume); + break; + case 8: + err = soundEffect8(volume); + break; + case 9: + err = soundEffect9(volume); + break; + default: + err = kSTkErrFail; + } + + if (err == kSTkErrOk) + return true; + else + return false; +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect0(const uint8_t volume) +{ + sfeTkError_t err; + for (int note = 150; note < 4000; note += 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + for (int note = 4000; note > 150; note -= 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect1(const uint8_t volume) +{ + sfeTkError_t err; + for (int i = 0; i <= 2; i++) + { + for (int note = 150; note < 4000; note += 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(2); + } + for (int note = 4000; note > 150; note -= 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(2); + } + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect2(const uint8_t volume) +{ + sfeTkError_t err; + for (int note = 150; note < 4000; note += 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(40); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect3(const uint8_t volume) +{ + sfeTkError_t err; + for (int note = 150; note < 4000; note += 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect4(const uint8_t volume) +{ + sfeTkError_t err; + for (int note = 4000; note > 150; note -= 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(40); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect5(const uint8_t volume) +{ + sfeTkError_t err; + for (int note = 4000; note > 150; note -= 150) + { + err = configureBuzzer(note, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect6(const uint8_t volume) +{ + sfeTkError_t err; + int laughdelay = 400; + int laughstep = 10; + uint16_t i; + + for (i = 1538; i < 1905; i += laughstep) // vary up //1538, 1905 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(laughdelay); + + for (i = 1250; i < 1515; i += laughstep) // 1250, 1515 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(laughdelay); + + for (i = 1111; i < 1342; i += laughstep) // 1111, 1342 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(laughdelay); + + for (i = 1010; i < 1176; i += laughstep) // 1010, 1176 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect7(const uint8_t volume) +{ + sfeTkError_t err; + int laughdelay = 200; + int laughstep = 15; + uint16_t i; + + for (i = 1538; i < 1905; i += laughstep) // vary up //1538, 1905 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(laughdelay); + + for (i = 1250; i < 1515; i += laughstep) // 1250, 1515 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(laughdelay); + + for (i = 1111; i < 1342; i += laughstep) // 1111, 1342 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(laughdelay); + + for (i = 1010; i < 1176; i += laughstep) // 1010, 1176 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect8(const uint8_t volume) +{ + sfeTkError_t err; + int crydelay = 500; + int step = 10; + uint16_t i; + + for (i = 2000; i > 1429; i -= step) // vary down //2000, 1429 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(crydelay); + + for (i = 1667; i > 1250; i -= step) // 1667, 1250 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(crydelay); + + for (i = 1429; i > 1053; i -= step) // 1429, 1053 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + return off(); +} + +sfeTkError_t sfeQwiicBuzzer::soundEffect9(const uint8_t volume) +{ + sfeTkError_t err; + int crydelay = 200; + int step = 20; + uint16_t i; + + for (i = 2000; i > 1429; i -= step) // vary down //2000, 1429 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(crydelay); + + for (i = 1667; i > 1250; i -= step) // 1667, 1250 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + err = off(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + delay(crydelay); + + for (i = 1429; i > 1053; i -= step) // 1429, 1053 + { + err = configureBuzzer(i, 0, volume); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + err = on(); + // Check whether the write was successful + if (err != kSTkErrOk) + return err; + + delay(10); + } + return off(); +} \ No newline at end of file diff --git a/src/sfeQwiicBuzzer.h b/src/sfeQwiicBuzzer.h new file mode 100644 index 0000000..0deceea --- /dev/null +++ b/src/sfeQwiicBuzzer.h @@ -0,0 +1,197 @@ +/****************************************************************************** + sfeQwiicBuzzer.h + SparkFun Qwiic Buzzer Library header file + + by Pete Lewis @SparkFun Electronics + January 2024 + + Based on original source code written by + Fischer Moseley @ SparkFun Electronics + Original Creation Date: July 24, 2019 + + This file implements the QwiicBuzzer class, prototyped in SparkFun_Qwiic_Buzzer_Arduino_Library.h + + Development environment specifics: + IDE: Arduino 2.2.1 + Hardware Platform: Arduino Uno/SparkFun Redboard + Qwiic Buzzer Version: v10 + + SPDX-License-Identifier: MIT + + Copyright (c) 2023 SparkFun Electronics + + Distributed as-is; no warranty is given. +******************************************************************************/ + +#pragma once + +#include "sfeQwiicBuzzerPitches.h" +#include "sfeQwiicBuzzerRegisters.h" +#include +#include + +#define SFE_QWIIC_BUZZER_DEFAULT_ADDRESS 0x34 +#define SFE_QWIIC_BUZZER_DEVICE_ID 0x5E +#define SFE_QWIIC_BUZZER_RESONANT_FREQUENCY 2730 +#define SFE_QWIIC_BUZZER_VOLUME_OFF 0 +#define SFE_QWIIC_BUZZER_VOLUME_MIN 1 +#define SFE_QWIIC_BUZZER_VOLUME_LOW 2 +#define SFE_QWIIC_BUZZER_VOLUME_MID 3 +#define SFE_QWIIC_BUZZER_VOLUME_MAX 4 + +class sfeQwiicBuzzer +{ + public: + /// @brief Default constructor + sfeQwiicBuzzer() : _theBus{nullptr} + { + } + + /// @brief Begins the Qwiic Buzzer + /// @param theBus I2C bus to use for communication + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t begin(sfeTkII2C *theBus = nullptr); + + /// @brief Checks if the Qwiic Buzzer is connected + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t isConnected(); + + /// @brief Reads the Device ID of the Qwiic Buzzer + /// @param deviceId uint8_t variable where the read results will be stored + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t deviceId(uint8_t &deviceId); + + /// @brief Reads the Firmware Version Major of the Qwiic Buzzer + /// @param versionMajor Variable where the read results will be stored + /// @return 1 for succuss, 0 error + bool firmwareVersionMajor(uint8_t &versionMajor); + + /// @brief Reads the Firmware Version Minor of the Qwiic Buzzer + /// @param versionMinor Variable where the read results will be stored + /// @return 1 for succuss, 0 error + bool firmwareVersionMinor(uint8_t &versionMinor); + + /// @brief Configures the Qwiic Buzzer without causing the buzzer to buzz. + /// This allows configuration in silence (before you may want to buzz). + /// It is also useful in combination with saveSettings(), and then later + /// causing buzzing by using the physical TRIGGER pin. + /// To start buzzing (via Qwiic) with your desired configuration, use this + /// function, then call on(). + /// @param toneFrequency Frequency in Hz of buzzer tone + /// @param duration Duration in milliseconds (0 = forever) + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t configureBuzzer(const uint16_t toneFrequency = SFE_QWIIC_BUZZER_RESONANT_FREQUENCY, + const uint16_t duration = 0, const uint8_t volume = 4); + + /// @brief Turns on buzzer + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t on(); + + /// @brief Turns off buzzer + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t off(); + + /// @brief Stores settings to EEPROM + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t saveSettings(); + + /// @brief Changes the I2C address of the Qwiic Buzzer + /// @param address New address, must be in the range 0x08 to 0x77 + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t setAddress(const uint8_t &address); + + /// @brief Gets the current I2C address of the Qwiic Buzzer + /// @return The current I2C address, 7-bit unshifted + uint8_t address(); + + /// @brief Plays one of the sound effects included in this library + /// @param soundEffectNumber The sound effect you with to play + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 1 for succuss, 0 error + bool playSoundEffect(const uint8_t soundEffectNumber, const uint8_t volume); + + private: + /// @brief Plays sound effect 0 (aka "Siren") + /// Intended to sound like a siren, starting at a low frequency, and then + /// increasing rapidly up and then back down. This sound effect does a + /// single "up and down" cycle. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect0(const uint8_t volume); + + /// @brief Plays sound effect 1 (aka "3 Fast Sirens") + /// Intended to sound like a siren, starting at a low frequency, and then + /// increasing rapidly up and then back down. This sound effect does this + /// cycle of "up and down" three times rapidly. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect1(const uint8_t volume); + + /// @brief Plays sound effect 2 (aka "robot saying 'Yes'") + /// Intended to sound like a robot saying the word "yes". + /// It starts at a low frequency and quickly ramps up to a high frequency, + /// then stops. This can be interpreted by most to be an affirmative + /// sound to any question you may ask your buzzing robot. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect2(const uint8_t volume); + + /// @brief Plays sound effect 3 (aka "robot yelling 'YES!'" - faster) + /// Intended to sound like a robot saying the word "yes". + /// It starts at a low frequency and quickly ramps up to a high frequency, + /// then stops. This can be interpreted by most to be an affirmative + /// sound to any question you may ask your buzzing robot. As this sound + /// is done more quickly, it can add enthusiasm to the buzzing sound. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect3(const uint8_t volume); + + /// @brief Plays sound effect 4 (aka "robot saying 'No'") + /// Intended to sound like a robot saying the word "no". + /// It starts at a high frequency and quickly ramps down to a low frequency, + /// then stops. This can be interpreted by most to be an negative + /// sound to any question you may ask your buzzing robot. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect4(const uint8_t volume); + + /// @brief Plays sound effect 5 (aka "robot yelling 'NO!'" - faster) + /// Intended to sound like a robot saying the word "no". + /// It starts at a high frequency and quickly ramps down to a low frequency, + /// then stops. This can be interpreted by most to be an negative + /// sound to any question you may ask your buzzing robot. As this sound + /// is done more quickly, it can add enthusiasm to the buzzing sound. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect5(const uint8_t volume); + + /// @brief Plays sound effect 6 (aka "Laughing Robot") + /// Intended to sound like your robot is laughing at you. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect6(const uint8_t volume); + + /// @brief Plays sound effect 7 (aka "Laughing Robot Faster") + /// Intended to sound like your robot is laughing at you. As this sound + /// is done more quickly, it can add enthusiasm to the buzzing sound. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect7(const uint8_t volume); + + /// @brief Plays sound effect 8 (aka "Crying Robot") + /// Intended to sound like a robot is crying and sad. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect8(const uint8_t volume); + + /// @brief Plays sound effect 9 (aka "Crying Robot Faster") + /// Intended to sound like a robot is crying and sad. As this sound + /// is done more quickly, it can add enthusiasm to the buzzing sound. + /// @param volume Volume (4 settings; 0=off, 1=quiet... 4=loudest) + /// @return 0 for succuss, negative for errors, positive for warnings + sfeTkError_t soundEffect9(const uint8_t volume); + + protected: + sfeTkII2C *_theBus; +}; \ No newline at end of file diff --git a/src/sfeQwiicBuzzerPitches.h b/src/sfeQwiicBuzzerPitches.h new file mode 100644 index 0000000..0ecc23a --- /dev/null +++ b/src/sfeQwiicBuzzerPitches.h @@ -0,0 +1,205 @@ +/****************************************************************************** + sfeQwiicBuzzerPitches.h + SparkFun Qwiic Buzzer Library header file + This file contains a list of commonly found notes on a piano. + + by Pete Lewis @SparkFun Electronics + January 2024 + + Based on original source code written by Tom Igeo in Jan 2010: + https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody + http://www.arduino.cc/en/Tutorial/Tone + + SPDX-License-Identifier: MIT + + Copyright (c) 2023 SparkFun Electronics + + Distributed as-is; no warranty is given. +******************************************************************************/ + +/************************************************* + Public Constants +*************************************************/ + +#define SFE_QWIIC_BUZZER_NOTE_REST 0 +#define SFE_QWIIC_BUZZER_NOTE_B0 31 +#define SFE_QWIIC_BUZZER_NOTE_C1 33 +#define SFE_QWIIC_BUZZER_NOTE_CS1 35 +#define SFE_QWIIC_BUZZER_NOTE_D1 37 +#define SFE_QWIIC_BUZZER_NOTE_DS1 39 +#define SFE_QWIIC_BUZZER_NOTE_E1 41 +#define SFE_QWIIC_BUZZER_NOTE_F1 44 +#define SFE_QWIIC_BUZZER_NOTE_FS1 46 +#define SFE_QWIIC_BUZZER_NOTE_G1 49 +#define SFE_QWIIC_BUZZER_NOTE_GS1 52 +#define SFE_QWIIC_BUZZER_NOTE_A1 55 +#define SFE_QWIIC_BUZZER_NOTE_AS1 58 +#define SFE_QWIIC_BUZZER_NOTE_B1 62 +#define SFE_QWIIC_BUZZER_NOTE_C2 65 +#define SFE_QWIIC_BUZZER_NOTE_CS2 69 +#define SFE_QWIIC_BUZZER_NOTE_D2 73 +#define SFE_QWIIC_BUZZER_NOTE_DS2 78 +#define SFE_QWIIC_BUZZER_NOTE_E2 82 +#define SFE_QWIIC_BUZZER_NOTE_F2 87 +#define SFE_QWIIC_BUZZER_NOTE_FS2 93 +#define SFE_QWIIC_BUZZER_NOTE_G2 98 +#define SFE_QWIIC_BUZZER_NOTE_GS2 104 +#define SFE_QWIIC_BUZZER_NOTE_A2 110 +#define SFE_QWIIC_BUZZER_NOTE_AS2 117 +#define SFE_QWIIC_BUZZER_NOTE_B2 123 +#define SFE_QWIIC_BUZZER_NOTE_C3 131 +#define SFE_QWIIC_BUZZER_NOTE_CS3 139 +#define SFE_QWIIC_BUZZER_NOTE_D3 147 +#define SFE_QWIIC_BUZZER_NOTE_DS3 156 +#define SFE_QWIIC_BUZZER_NOTE_E3 165 +#define SFE_QWIIC_BUZZER_NOTE_F3 175 +#define SFE_QWIIC_BUZZER_NOTE_FS3 185 +#define SFE_QWIIC_BUZZER_NOTE_G3 196 +#define SFE_QWIIC_BUZZER_NOTE_GS3 208 +#define SFE_QWIIC_BUZZER_NOTE_A3 220 +#define SFE_QWIIC_BUZZER_NOTE_AS3 233 +#define SFE_QWIIC_BUZZER_NOTE_B3 247 +#define SFE_QWIIC_BUZZER_NOTE_C4 262 +#define SFE_QWIIC_BUZZER_NOTE_CS4 277 +#define SFE_QWIIC_BUZZER_NOTE_D4 294 +#define SFE_QWIIC_BUZZER_NOTE_DS4 311 +#define SFE_QWIIC_BUZZER_NOTE_E4 330 +#define SFE_QWIIC_BUZZER_NOTE_F4 349 +#define SFE_QWIIC_BUZZER_NOTE_FS4 370 +#define SFE_QWIIC_BUZZER_NOTE_G4 392 +#define SFE_QWIIC_BUZZER_NOTE_GS4 415 +#define SFE_QWIIC_BUZZER_NOTE_A4 440 +#define SFE_QWIIC_BUZZER_NOTE_AS4 466 +#define SFE_QWIIC_BUZZER_NOTE_B4 494 +#define SFE_QWIIC_BUZZER_NOTE_C5 523 +#define SFE_QWIIC_BUZZER_NOTE_CS5 554 +#define SFE_QWIIC_BUZZER_NOTE_D5 587 +#define SFE_QWIIC_BUZZER_NOTE_DS5 622 +#define SFE_QWIIC_BUZZER_NOTE_E5 659 +#define SFE_QWIIC_BUZZER_NOTE_F5 698 +#define SFE_QWIIC_BUZZER_NOTE_FS5 740 +#define SFE_QWIIC_BUZZER_NOTE_G5 784 +#define SFE_QWIIC_BUZZER_NOTE_GS5 831 +#define SFE_QWIIC_BUZZER_NOTE_A5 880 +#define SFE_QWIIC_BUZZER_NOTE_AS5 932 +#define SFE_QWIIC_BUZZER_NOTE_B5 988 +#define SFE_QWIIC_BUZZER_NOTE_C6 1047 +#define SFE_QWIIC_BUZZER_NOTE_CS6 1109 +#define SFE_QWIIC_BUZZER_NOTE_D6 1175 +#define SFE_QWIIC_BUZZER_NOTE_DS6 1245 +#define SFE_QWIIC_BUZZER_NOTE_E6 1319 +#define SFE_QWIIC_BUZZER_NOTE_F6 1397 +#define SFE_QWIIC_BUZZER_NOTE_FS6 1480 +#define SFE_QWIIC_BUZZER_NOTE_G6 1568 +#define SFE_QWIIC_BUZZER_NOTE_GS6 1661 +#define SFE_QWIIC_BUZZER_NOTE_A6 1760 +#define SFE_QWIIC_BUZZER_NOTE_AS6 1865 +#define SFE_QWIIC_BUZZER_NOTE_B6 1976 +#define SFE_QWIIC_BUZZER_NOTE_C7 2093 +#define SFE_QWIIC_BUZZER_NOTE_CS7 2217 +#define SFE_QWIIC_BUZZER_NOTE_D7 2349 +#define SFE_QWIIC_BUZZER_NOTE_DS7 2489 +#define SFE_QWIIC_BUZZER_NOTE_E7 2637 +#define SFE_QWIIC_BUZZER_NOTE_F7 2794 +#define SFE_QWIIC_BUZZER_NOTE_FS7 2960 +#define SFE_QWIIC_BUZZER_NOTE_G7 3136 +#define SFE_QWIIC_BUZZER_NOTE_GS7 3322 +#define SFE_QWIIC_BUZZER_NOTE_A7 3520 +#define SFE_QWIIC_BUZZER_NOTE_AS7 3729 +#define SFE_QWIIC_BUZZER_NOTE_B7 3951 +#define SFE_QWIIC_BUZZER_NOTE_C8 4186 +#define SFE_QWIIC_BUZZER_NOTE_CS8 4435 +#define SFE_QWIIC_BUZZER_NOTE_D8 4699 +#define SFE_QWIIC_BUZZER_NOTE_DS8 4978 + + +// Backwards compatibility with original "pitches.h" file written by Tim Igeo, 2010. +#define NOTE_B0 SFE_QWIIC_BUZZER_NOTE_B0 +#define NOTE_C1 SFE_QWIIC_BUZZER_NOTE_C1 +#define NOTE_CS1 SFE_QWIIC_BUZZER_NOTE_CS1 +#define NOTE_D1 SFE_QWIIC_BUZZER_NOTE_D1 +#define NOTE_DS1 SFE_QWIIC_BUZZER_NOTE_DS1 +#define NOTE_E1 SFE_QWIIC_BUZZER_NOTE_E1 +#define NOTE_F1 SFE_QWIIC_BUZZER_NOTE_F1 +#define NOTE_FS1 SFE_QWIIC_BUZZER_NOTE_FS1 +#define NOTE_G1 SFE_QWIIC_BUZZER_NOTE_G1 +#define NOTE_GS1 SFE_QWIIC_BUZZER_NOTE_GS1 +#define NOTE_A1 SFE_QWIIC_BUZZER_NOTE_A1 +#define NOTE_AS1 SFE_QWIIC_BUZZER_NOTE_AS1 +#define NOTE_B1 SFE_QWIIC_BUZZER_NOTE_B1 +#define NOTE_C2 SFE_QWIIC_BUZZER_NOTE_C2 +#define NOTE_CS2 SFE_QWIIC_BUZZER_NOTE_CS2 +#define NOTE_D2 SFE_QWIIC_BUZZER_NOTE_D2 +#define NOTE_DS2 SFE_QWIIC_BUZZER_NOTE_DS2 +#define NOTE_E2 SFE_QWIIC_BUZZER_NOTE_E2 +#define NOTE_F2 SFE_QWIIC_BUZZER_NOTE_F2 +#define NOTE_FS2 SFE_QWIIC_BUZZER_NOTE_FS2 +#define NOTE_G2 SFE_QWIIC_BUZZER_NOTE_G2 +#define NOTE_GS2 SFE_QWIIC_BUZZER_NOTE_GS2 +#define NOTE_A2 SFE_QWIIC_BUZZER_NOTE_A2 +#define NOTE_AS2 SFE_QWIIC_BUZZER_NOTE_AS2 +#define NOTE_B2 SFE_QWIIC_BUZZER_NOTE_B2 +#define NOTE_C3 SFE_QWIIC_BUZZER_NOTE_C3 +#define NOTE_CS3 SFE_QWIIC_BUZZER_NOTE_CS3 +#define NOTE_D3 SFE_QWIIC_BUZZER_NOTE_D3 +#define NOTE_DS3 SFE_QWIIC_BUZZER_NOTE_DS3 +#define NOTE_E3 SFE_QWIIC_BUZZER_NOTE_E3 +#define NOTE_F3 SFE_QWIIC_BUZZER_NOTE_F3 +#define NOTE_FS3 SFE_QWIIC_BUZZER_NOTE_FS3 +#define NOTE_G3 SFE_QWIIC_BUZZER_NOTE_G3 +#define NOTE_GS3 SFE_QWIIC_BUZZER_NOTE_GS3 +#define NOTE_A3 SFE_QWIIC_BUZZER_NOTE_A3 +#define NOTE_AS3 SFE_QWIIC_BUZZER_NOTE_AS3 +#define NOTE_B3 SFE_QWIIC_BUZZER_NOTE_B3 +#define NOTE_C4 SFE_QWIIC_BUZZER_NOTE_C4 +#define NOTE_CS4 SFE_QWIIC_BUZZER_NOTE_CS4 +#define NOTE_D4 SFE_QWIIC_BUZZER_NOTE_D4 +#define NOTE_DS4 SFE_QWIIC_BUZZER_NOTE_DS4 +#define NOTE_E4 SFE_QWIIC_BUZZER_NOTE_E4 +#define NOTE_F4 SFE_QWIIC_BUZZER_NOTE_F4 +#define NOTE_FS4 SFE_QWIIC_BUZZER_NOTE_FS4 +#define NOTE_G4 SFE_QWIIC_BUZZER_NOTE_G4 +#define NOTE_GS4 SFE_QWIIC_BUZZER_NOTE_GS4 +#define NOTE_A4 SFE_QWIIC_BUZZER_NOTE_A4 +#define NOTE_AS4 SFE_QWIIC_BUZZER_NOTE_AS4 +#define NOTE_B4 SFE_QWIIC_BUZZER_NOTE_B4 +#define NOTE_C5 SFE_QWIIC_BUZZER_NOTE_C5 +#define NOTE_CS5 SFE_QWIIC_BUZZER_NOTE_CS5 +#define NOTE_D5 SFE_QWIIC_BUZZER_NOTE_D5 +#define NOTE_DS5 SFE_QWIIC_BUZZER_NOTE_DS5 +#define NOTE_E5 SFE_QWIIC_BUZZER_NOTE_E5 +#define NOTE_F5 SFE_QWIIC_BUZZER_NOTE_F5 +#define NOTE_FS5 SFE_QWIIC_BUZZER_NOTE_FS5 +#define NOTE_G5 SFE_QWIIC_BUZZER_NOTE_G5 +#define NOTE_GS5 SFE_QWIIC_BUZZER_NOTE_GS5 +#define NOTE_A5 SFE_QWIIC_BUZZER_NOTE_A5 +#define NOTE_AS5 SFE_QWIIC_BUZZER_NOTE_AS5 +#define NOTE_B5 SFE_QWIIC_BUZZER_NOTE_B5 +#define NOTE_C6 SFE_QWIIC_BUZZER_NOTE_C6 +#define NOTE_CS6 SFE_QWIIC_BUZZER_NOTE_CS6 +#define NOTE_D6 SFE_QWIIC_BUZZER_NOTE_D6 +#define NOTE_DS6 SFE_QWIIC_BUZZER_NOTE_DS6 +#define NOTE_E6 SFE_QWIIC_BUZZER_NOTE_E6 +#define NOTE_F6 SFE_QWIIC_BUZZER_NOTE_F6 +#define NOTE_FS6 SFE_QWIIC_BUZZER_NOTE_FS6 +#define NOTE_G6 SFE_QWIIC_BUZZER_NOTE_G6 +#define NOTE_GS6 SFE_QWIIC_BUZZER_NOTE_GS6 +#define NOTE_A6 SFE_QWIIC_BUZZER_NOTE_A6 +#define NOTE_AS6 SFE_QWIIC_BUZZER_NOTE_AS6 +#define NOTE_B6 SFE_QWIIC_BUZZER_NOTE_B6 +#define NOTE_C7 SFE_QWIIC_BUZZER_NOTE_C7 +#define NOTE_CS7 SFE_QWIIC_BUZZER_NOTE_CS7 +#define NOTE_D7 SFE_QWIIC_BUZZER_NOTE_D7 +#define NOTE_DS7 SFE_QWIIC_BUZZER_NOTE_DS7 +#define NOTE_E7 SFE_QWIIC_BUZZER_NOTE_E7 +#define NOTE_F7 SFE_QWIIC_BUZZER_NOTE_F7 +#define NOTE_FS7 SFE_QWIIC_BUZZER_NOTE_FS7 +#define NOTE_G7 SFE_QWIIC_BUZZER_NOTE_G7 +#define NOTE_GS7 SFE_QWIIC_BUZZER_NOTE_GS7 +#define NOTE_A7 SFE_QWIIC_BUZZER_NOTE_A7 +#define NOTE_AS7 SFE_QWIIC_BUZZER_NOTE_AS7 +#define NOTE_B7 SFE_QWIIC_BUZZER_NOTE_B7 +#define NOTE_C8 SFE_QWIIC_BUZZER_NOTE_C8 +#define NOTE_CS8 SFE_QWIIC_BUZZER_NOTE_CS8 +#define NOTE_D8 SFE_QWIIC_BUZZER_NOTE_D8 +#define NOTE_DS8 SFE_QWIIC_BUZZER_NOTE_DS8 \ No newline at end of file diff --git a/src/sfeQwiicBuzzerRegisters.h b/src/sfeQwiicBuzzerRegisters.h new file mode 100644 index 0000000..e51b6df --- /dev/null +++ b/src/sfeQwiicBuzzerRegisters.h @@ -0,0 +1,45 @@ +/****************************************************************************** + sfeQwiicBuzzerRegisters.h + + by Pete Lewis @SparkFun Electronics + January 2024 + + Based on original source code written by + Fischer Moseley @ SparkFun Electronics + Original Creation Date: July 24, 2019 + + This file defines the virtual memory map on the Qwiic Buzzer. The enum + provides a set of pointers for the various registers on the Qwiic + Buzzer. + + Development environment specifics: + IDE: Arduino 2.2.1 + Hardware Platform: Arduino Uno/SparkFun Redboard + Qwiic Buzzer Version: v10 + + SPDX-License-Identifier: MIT + + Copyright (c) 2023 SparkFun Electronics + + Distributed as-is; no warranty is given. +******************************************************************************/ + +#pragma once + +#include + +// Register Pointer Map +enum Qwiic_Buzzer_Register : uint8_t +{ + kSfeQwiicBuzzerRegId = 0x00, + kSfeQwiicBuzzerRegFirmwareMinor, + kSfeQwiicBuzzerRegFirmwareMajor, + kSfeQwiicBuzzerRegToneFrequencyMsb, + kSfeQwiicBuzzerRegToneFrequencyLsb, + kSfeQwiicBuzzerRegVolume, + kSfeQwiicBuzzerRegDurationMsb, + kSfeQwiicBuzzerRegDurationLsb, + kSfeQwiicBuzzerRegActive, + kSfeQwiicBuzzerRegSaveSettings, + kSfeQwiicBuzzerRegI2cAddress, +}; \ No newline at end of file