1 /* RMT example -- Musical Buzzer
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include "esp_log.h"
10 #include "driver/rmt.h"
11 #include "musical_buzzer.h"
12
13 static const char *TAG = "example";
14
15 #define RMT_TX_CHANNEL RMT_CHANNEL_0
16 #define RMT_TX_GPIO_NUM (4)
17
18 /**
19 * @brief Musical Notation: Beethoven's Ode to joy
20 *
21 */
22 static const musical_buzzer_notation_t notation[] = {
23 {740, 400}, {740, 600}, {784, 400}, {880, 400},
24 {880, 400}, {784, 400}, {740, 400}, {659, 400},
25 {587, 400}, {587, 400}, {659, 400}, {740, 400},
26 {740, 400}, {740, 200}, {659, 200}, {659, 800},
27
28 {740, 400}, {740, 600}, {784, 400}, {880, 400},
29 {880, 400}, {784, 400}, {740, 400}, {659, 400},
30 {587, 400}, {587, 400}, {659, 400}, {740, 400},
31 {659, 400}, {659, 200}, {587, 200}, {587, 800},
32
33 {659, 400}, {659, 400}, {740, 400}, {587, 400},
34 {659, 400}, {740, 200}, {784, 200}, {740, 400}, {587, 400},
35 {659, 400}, {740, 200}, {784, 200}, {740, 400}, {659, 400},
36 {587, 400}, {659, 400}, {440, 400}, {440, 400},
37
38 {740, 400}, {740, 600}, {784, 400}, {880, 400},
39 {880, 400}, {784, 400}, {740, 400}, {659, 400},
40 {587, 400}, {587, 400}, {659, 400}, {740, 400},
41 {659, 400}, {659, 200}, {587, 200}, {587, 800},
42 };
43
app_main(void)44 void app_main(void)
45 {
46 // Apply default RMT configuration
47 rmt_config_t dev_config = RMT_DEFAULT_CONFIG_TX(RMT_TX_GPIO_NUM, RMT_TX_CHANNEL);
48 dev_config.tx_config.loop_en = true; // Enable loop mode
49
50 // Install RMT driver
51 ESP_ERROR_CHECK(rmt_config(&dev_config));
52 ESP_ERROR_CHECK(rmt_driver_install(RMT_TX_CHANNEL, 0, 0));
53
54 // This example take the RMT channel number as the device handle
55 musical_buzzer_config_t buzzer_config = MUSICAL_BUZZER_DEFAULT_CONFIG((musical_buzzer_dev_t)RMT_TX_CHANNEL);
56 musical_buzzer_t *buzzer = NULL;
57 // Install buzzer driver
58 ESP_ERROR_CHECK(musical_buzzer_create_rmt(&buzzer_config, &buzzer));
59
60 ESP_LOGI(TAG, "Playing Beethoven's Ode to joy");
61
62 ESP_ERROR_CHECK(buzzer->play(buzzer, notation, sizeof(notation) / sizeof(notation[0])));
63 }
64