Files
esp-pwm/main/un.c

123 lines
3.1 KiB
C

#include <stdint.h>
#include "esp_log.h"
#include "driver/usb_serial_jtag.h"
#include "freertos/FreeRTOS.h" // IWYU pragma: keep
#include "freertos/task.h"
#include "un.h"
#include "buzzer.h"
#include "arraylen.h"
extern const uint32_t init_buzzer_freq;
static const char* TAG = "UN_C";
const char* NoteFreqToStr(NoteFreq note) {
switch (note) {
case C:
return "C";
case D:
return "D";
case E:
return "E";
case F:
return "F";
case G:
return "G";
case P:
return "P";
default:
return "INVALID NOTEFREQ";
}
}
void play_note(Note* note) {
ESP_LOGI(TAG, "Playing note: %s %d", NoteFreqToStr(note->note),
note->length);
if (note->note != P) {
set_frequency(note->note);
buzzer_resume();
}
vTaskDelay(pdMS_TO_TICKS(note->length));
buzzer_pause();
}
void un_task(void* param) {
usb_serial_jtag_driver_config_t jtag_config = {.rx_buffer_size = 1024,
.tx_buffer_size = 1024};
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&jtag_config));
buzzer_init(init_buzzer_freq);
const uint32_t quarter = 250;
const uint32_t half = 500;
const uint32_t whole = 1000;
// clang-format off
Note sequence[] = {
{.note = C, .length = quarter},
{.note = C, .length = quarter},
{.note = C, .length = quarter},
{.note = E, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = F, .length = quarter},
{.note = E, .length = quarter},
{.note = E, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = C, .length = half},
{.note = P, .length = half},
{.note = E, .length = quarter},
{.note = E, .length = quarter},
{.note = E, .length = quarter},
{.note = E, .length = quarter},
{.note = G, .length = half},
{.note = F, .length = half},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = F, .length = half},
{.note = E, .length = half},
{.note = C, .length = quarter},
{.note = C, .length = quarter},
{.note = C, .length = quarter},
{.note = E, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = F, .length = quarter},
{.note = E, .length = quarter},
{.note = E, .length = quarter},
{.note = D, .length = quarter},
{.note = D, .length = quarter},
{.note = C, .length = whole},
};
// clang-format on
char* buf[4] = {0};
buzzer_pause();
while (true) {
ESP_LOGI(TAG, "Press any button to play");
usb_serial_jtag_read_bytes(buf, ARRAY_LEN(buf) - 1, portMAX_DELAY);
for (size_t i = 0; i < ARRAY_LEN(sequence); i++) {
play_note(&sequence[i]);
}
}
}