diff options
author | Joey Castillo <jose.castillo@gmail.com> | 2021-08-08 19:56:33 -0400 |
---|---|---|
committer | Joey Castillo <jose.castillo@gmail.com> | 2021-08-08 20:17:08 -0400 |
commit | 616e4fb1054fb2d500e4720a38b02501c8214030 (patch) | |
tree | 40e3044e4136066989f49756f91eb3cb3be0642d /watch-library/watch | |
parent | ca96db1ef5729b8b14d5964acbf5eed7b34e1b29 (diff) | |
download | Sensor-Watch-616e4fb1054fb2d500e4720a38b02501c8214030.tar.gz Sensor-Watch-616e4fb1054fb2d500e4720a38b02501c8214030.tar.bz2 Sensor-Watch-616e4fb1054fb2d500e4720a38b02501c8214030.zip |
add API for playing sounds from the buzzer
Diffstat (limited to 'watch-library/watch')
-rw-r--r-- | watch-library/watch/notes.h | 114 | ||||
-rw-r--r-- | watch-library/watch/watch.c | 36 | ||||
-rw-r--r-- | watch-library/watch/watch.h | 43 |
3 files changed, 192 insertions, 1 deletions
diff --git a/watch-library/watch/notes.h b/watch-library/watch/notes.h new file mode 100644 index 00000000..95f2abbf --- /dev/null +++ b/watch-library/watch/notes.h @@ -0,0 +1,114 @@ +////< @file notes.h + +/// @brief 108 notes for use with watch_buzzer_play_note +typedef enum BuzzerNote { + BUZZER_NOTE_C0 = 0, ///< 16.35 Hz + BUZZER_NOTE_C0SHARP_D0FLAT, ///< 17.32 Hz + BUZZER_NOTE_D0, ///< 18.35 Hz + BUZZER_NOTE_D0SHARP_E0FLAT, ///< 19.45 Hz + BUZZER_NOTE_E0, ///< 20.60 Hz + BUZZER_NOTE_F0, ///< 21.83 Hz + BUZZER_NOTE_F0SHARP_G0FLAT, ///< 23.12 Hz + BUZZER_NOTE_G0, ///< 24.50 Hz + BUZZER_NOTE_G0SHARP_A0FLAT, ///< 25.96 Hz + BUZZER_NOTE_A0, ///< 27.50 Hz + BUZZER_NOTE_A0SHARP_B0FLAT, ///< 29.14 Hz + BUZZER_NOTE_B0, ///< 30.87 Hz + BUZZER_NOTE_C1, ///< 32.70 Hz + BUZZER_NOTE_C1SHARP_D1FLAT, ///< 34.65 Hz + BUZZER_NOTE_D1, ///< 36.71 Hz + BUZZER_NOTE_D1SHARP_E1FLAT, ///< 38.89 Hz + BUZZER_NOTE_E1, ///< 41.20 Hz + BUZZER_NOTE_F1, ///< 43.65 Hz + BUZZER_NOTE_F1SHARP_G1FLAT, ///< 46.25 Hz + BUZZER_NOTE_G1, ///< 49.00 Hz + BUZZER_NOTE_G1SHARP_A1FLAT, ///< 51.91 Hz + BUZZER_NOTE_A1, ///< 55.00 Hz + BUZZER_NOTE_A1SHARP_B1FLAT, ///< 58.27 Hz + BUZZER_NOTE_B1, ///< 61.74 Hz + BUZZER_NOTE_C2, ///< 65.41 Hz + BUZZER_NOTE_C2SHARP_D2FLAT, ///< 69.30 Hz + BUZZER_NOTE_D2, ///< 73.42 Hz + BUZZER_NOTE_D2SHARP_E2FLAT, ///< 77.78 Hz + BUZZER_NOTE_E2, ///< 82.41 Hz + BUZZER_NOTE_F2, ///< 87.31 Hz + BUZZER_NOTE_F2SHARP_G2FLAT, ///< 92.50 Hz + BUZZER_NOTE_G2, ///< 98.00 Hz + BUZZER_NOTE_G2SHARP_A2FLAT, ///< 103.83 Hz + BUZZER_NOTE_A2, ///< 110.00 Hz + BUZZER_NOTE_A2SHARP_B2FLAT, ///< 116.54 Hz + BUZZER_NOTE_B2, ///< 123.47 Hz + BUZZER_NOTE_C3, ///< 130.81 Hz + BUZZER_NOTE_C3SHARP_D3FLAT, ///< 138.59 Hz + BUZZER_NOTE_D3, ///< 146.83 Hz + BUZZER_NOTE_D3SHARP_E3FLAT, ///< 155.56 Hz + BUZZER_NOTE_E3, ///< 164.81 Hz + BUZZER_NOTE_F3, ///< 174.61 Hz + BUZZER_NOTE_F3SHARP_G3FLAT, ///< 185.00 Hz + BUZZER_NOTE_G3, ///< 196.00 Hz + BUZZER_NOTE_G3SHARP_A3FLAT, ///< 207.65 Hz + BUZZER_NOTE_A3, ///< 220.00 Hz + BUZZER_NOTE_A3SHARP_B3FLAT, ///< 233.08 Hz + BUZZER_NOTE_B3, ///< 246.94 Hz + BUZZER_NOTE_C4, ///< 261.63 Hz + BUZZER_NOTE_C4SHARP_D4FLAT, ///< 277.18 Hz + BUZZER_NOTE_D4, ///< 293.66 Hz + BUZZER_NOTE_D4SHARP_E4FLAT, ///< 311.13 Hz + BUZZER_NOTE_E4, ///< 329.63 Hz + BUZZER_NOTE_F4, ///< 349.23 Hz + BUZZER_NOTE_F4SHARP_G4FLAT, ///< 369.99 Hz + BUZZER_NOTE_G4, ///< 392.00 Hz + BUZZER_NOTE_G4SHARP_A4FLAT, ///< 415.30 Hz + BUZZER_NOTE_A4, ///< 440.00 Hz + BUZZER_NOTE_A4SHARP_B4FLAT, ///< 466.16 Hz + BUZZER_NOTE_B4, ///< 493.88 Hz + BUZZER_NOTE_C5, ///< 523.25 Hz + BUZZER_NOTE_C5SHARP_D5FLAT, ///< 554.37 Hz + BUZZER_NOTE_D5, ///< 587.33 Hz + BUZZER_NOTE_D5SHARP_E5FLAT, ///< 622.25 Hz + BUZZER_NOTE_E5, ///< 659.25 Hz + BUZZER_NOTE_F5, ///< 698.46 Hz + BUZZER_NOTE_F5SHARP_G5FLAT, ///< 739.99 Hz + BUZZER_NOTE_G5, ///< 783.99 Hz + BUZZER_NOTE_G5SHARP_A5FLAT, ///< 830.61 Hz + BUZZER_NOTE_A5, ///< 880.00 Hz + BUZZER_NOTE_A5SHARP_B5FLAT, ///< 932.33 Hz + BUZZER_NOTE_B5, ///< 987.77 Hz + BUZZER_NOTE_C6, ///< 1046.50 Hz + BUZZER_NOTE_C6SHARP_D6FLAT, ///< 1108.73 Hz + BUZZER_NOTE_D6, ///< 1174.66 Hz + BUZZER_NOTE_D6SHARP_E6FLAT, ///< 1244.51 Hz + BUZZER_NOTE_E6, ///< 1318.51 Hz + BUZZER_NOTE_F6, ///< 1396.91 Hz + BUZZER_NOTE_F6SHARP_G6FLAT, ///< 1479.98 Hz + BUZZER_NOTE_G6, ///< 1567.98 Hz + BUZZER_NOTE_G6SHARP_A6FLAT, ///< 1661.22 Hz + BUZZER_NOTE_A6, ///< 1760.00 Hz + BUZZER_NOTE_A6SHARP_B6FLAT, ///< 1864.66 Hz + BUZZER_NOTE_B6, ///< 1975.53 Hz + BUZZER_NOTE_C7, ///< 2093.00 Hz + BUZZER_NOTE_C7SHARP_D7FLAT, ///< 2217.46 Hz + BUZZER_NOTE_D7, ///< 2349.32 Hz + BUZZER_NOTE_D7SHARP_E7FLAT, ///< 2489.02 Hz + BUZZER_NOTE_E7, ///< 2637.02 Hz + BUZZER_NOTE_F7, ///< 2793.83 Hz + BUZZER_NOTE_F7SHARP_G7FLAT, ///< 2959.96 Hz + BUZZER_NOTE_G7, ///< 3135.96 Hz + BUZZER_NOTE_G7SHARP_A7FLAT, ///< 3322.44 Hz + BUZZER_NOTE_A7, ///< 3520.00 Hz + BUZZER_NOTE_A7SHARP_B7FLAT, ///< 3729.31 Hz + BUZZER_NOTE_B7, ///< 3951.07 Hz + BUZZER_NOTE_C8, ///< 4186.01 Hz + BUZZER_NOTE_C8SHARP_D8FLAT, ///< 4434.92 Hz + BUZZER_NOTE_D8, ///< 4698.63 Hz + BUZZER_NOTE_D8SHARP_E8FLAT, ///< 4978.03 Hz + BUZZER_NOTE_E8, ///< 5274.04 Hz + BUZZER_NOTE_F8, ///< 5587.65 Hz + BUZZER_NOTE_F8SHARP_G8FLAT, ///< 5919.91 Hz + BUZZER_NOTE_G8, ///< 6271.93 Hz + BUZZER_NOTE_G8SHARP_A8FLAT, ///< 6644.88 Hz + BUZZER_NOTE_A8, ///< 7040.00 Hz + BUZZER_NOTE_A8SHARP_B8FLAT, ///< 7458.62 Hz + BUZZER_NOTE_B8, ///< 7902.13 Hz + BUZZER_NOTE_REST ///< no sound +} BuzzerNote; diff --git a/watch-library/watch/watch.c b/watch-library/watch/watch.c index 170c4747..d3e32d5c 100644 --- a/watch-library/watch/watch.c +++ b/watch-library/watch/watch.c @@ -172,6 +172,9 @@ inline void watch_clear_pixel(uint8_t com, uint8_t seg) { } void watch_display_character(uint8_t character, uint8_t position) { + // handle lowercase 7 if needed + if (character == '7' && (position == 4 || position == 6)) character = '&'; + uint64_t segmap = Segment_Map[position]; uint64_t segdata = Character_Set[character - 0x20]; @@ -319,6 +322,39 @@ void watch_set_led_off() { } } + +////////////////////////////////////////////////////////////////////////////////////////// +// Buzzer + +inline void watch_enable_buzzer() { + PWM_1_init(); +} + +inline void watch_set_buzzer_period(uint32_t period) { + pwm_set_parameters(&PWM_1, period, period / 2); +} + +inline void watch_set_buzzer_on() { + pwm_enable(&PWM_1); +} + +inline void watch_set_buzzer_off() { + pwm_disable(&PWM_1); +} + +const uint16_t NotePeriods[108] = {31047, 29301, 27649, 26079, 24617, 23224, 21923, 20683, 19515, 18418, 17377, 16399, 15477, 14603, 13780, 13004, 12272, 11580, 10926, 10311, 9730, 9181, 8664, 8175, 7714, 7280, 6869, 6483, 6117, 5772, 5447, 5140, 4850, 4577, 4319, 4076, 3846, 3629, 3425, 3232, 3050, 2878, 2715, 2562, 2418, 2282, 2153, 2032, 1917, 1809, 1707, 1611, 1520, 1435, 1354, 1277, 1205, 1137, 1073, 1013, 956, 902, 851, 803, 758, 715, 675, 637, 601, 567, 535, 505, 476, 450, 424, 400, 378, 357, 336, 317, 300, 283, 267, 252, 238, 224, 212, 200, 188, 178, 168, 158, 149, 141, 133, 125, 118, 112, 105, 99, 94, 89, 84, 79, 74, 70, 66, 63}; + +void watch_buzzer_play_note(BuzzerNote note, uint16_t duration_ms) { + if (note == BUZZER_NOTE_REST) { + watch_set_buzzer_off(); + } else { + pwm_set_parameters(&PWM_1, NotePeriods[note], NotePeriods[note] / 2); + watch_set_buzzer_on(); + } + delay_ms(duration_ms); + watch_set_buzzer_off(); +} + ////////////////////////////////////////////////////////////////////////////////////////// // Real-time Clock diff --git a/watch-library/watch/watch.h b/watch-library/watch/watch.h index 43f582a3..66a0cc27 100644 --- a/watch-library/watch/watch.h +++ b/watch-library/watch/watch.h @@ -6,6 +6,7 @@ #include "driver_init.h" #include "hpl_calendar.h" #include "hal_ext_irq.h" +#include "notes.h" /** @mainpage Sensor Watch Documentation * @brief This documentation covers most of the functions you will use to interact with the Sensor Watch @@ -14,12 +15,13 @@ Sensor Watch app. - @ref slcd - This section covers functions related to the Segment LCD display driver, which is responsible for displaying strings of characters and indicators on the main watch display. + - @ref buttons - This section covers functions related to the three buttons: Light, Mode and Alarm. - @ref led - This section covers functions related to the bi-color red/green LED mounted behind the LCD. + - @ref buzzer - This section covers functions related to the piezo buzzer. - @ref rtc - This section covers functions related to the SAM L22's real-time clock peripheral, including date, time and alarm functions. - @ref adc - This section covers functions related to the SAM L22's analog-to-digital converter, as well as configuring and reading values from the three analog-capable pins on the 9-pin connector. - - @ref buttons - This section covers functions related to the three buttons: Light, Mode and Alarm. - @ref gpio - This section covers functions related to general-purpose input and output signals. - @ref i2c - This section covers functions related to the SAM L22's built-I2C driver, including configuring the I2C bus, putting values directly on the bus and reading data from registers on I2C devices. @@ -215,6 +217,45 @@ void watch_set_led_off(); /// @} +/** @addtogroup buzzer Buzzer + * @brief This section covers functions related to the piezo buzzer embedded in the F-91W's back plate. + */ +/// @{ +/** @brief Enables the TCC peripheral, which drives the buzzer. + */ +void watch_enable_buzzer(); + +/** @brief Sets the period of the buzzer. + * @param period The period of a single cycle for the PWM peripheral. You can use the following formula to + * convert a desired frequency to a period for this function: period = 513751 * (freq^−1.0043) + */ +void watch_set_buzzer_period(uint32_t period); + +/** @brief Turns the buzzer output on. It will emit a continuous sound at the given frequency. + * @note The TCC peripheral that drives the buzzer does not run in standby mode; if you wish for buzzer + * output to continue, you should prevent your app from going to sleep. + */ +void watch_set_buzzer_on(); + +/** @brief Turns the buzzer output off. + */ +void watch_set_buzzer_off(); + +/** @brief Plays the given note for a set duration. + * @param note The note you wish to play, or BUZZER_NOTE_REST to disable output for the given duration. + * @param duration_ms The duration of the note. + * @note Note that this will block your UI for the duration of the note's play time, and it will + * after this call, the buzzer period will be set to the period of this note. + */ +void watch_buzzer_play_note(BuzzerNote note, uint16_t duration_ms); + +/** @brief An array of periods for all the notes on a piano, corresponding to the names in BuzzerNote. + */ +extern const uint16_t NotePeriods[108]; + +/// @} + + /** @addtogroup rtc Real-Time Clock * @brief This section covers functions related to the SAM L22's real-time clock peripheral, including * date, time and alarm functions. |