un.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdint.h>
  2. #include "esp_log.h"
  3. #include "driver/usb_serial_jtag.h"
  4. #include "freertos/FreeRTOS.h" // IWYU pragma: keep
  5. #include "freertos/task.h"
  6. #include "un.h"
  7. #include "buzzer.h"
  8. #include "arraylen.h"
  9. extern const uint32_t init_buzzer_freq;
  10. static const char* TAG = "UN_C";
  11. const char* NoteFreqToStr(NoteFreq note) {
  12. switch (note) {
  13. case C:
  14. return "C";
  15. case D:
  16. return "D";
  17. case E:
  18. return "E";
  19. case F:
  20. return "F";
  21. case G:
  22. return "G";
  23. case P:
  24. return "P";
  25. default:
  26. return "INVALID NOTEFREQ";
  27. }
  28. }
  29. void play_note(Note* note) {
  30. ESP_LOGI(TAG, "Playing note: %s %d", NoteFreqToStr(note->note),
  31. note->length);
  32. if (note->note != P) {
  33. set_frequency(note->note);
  34. buzzer_resume();
  35. }
  36. vTaskDelay(pdMS_TO_TICKS(note->length));
  37. buzzer_pause();
  38. }
  39. void un_task(void* param) {
  40. usb_serial_jtag_driver_config_t jtag_config = {.rx_buffer_size = 1024,
  41. .tx_buffer_size = 1024};
  42. ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&jtag_config));
  43. buzzer_init(init_buzzer_freq);
  44. const uint32_t quarter = 250;
  45. const uint32_t half = 500;
  46. const uint32_t whole = 1000;
  47. // clang-format off
  48. Note sequence[] = {
  49. {.note = C, .length = quarter},
  50. {.note = C, .length = quarter},
  51. {.note = C, .length = quarter},
  52. {.note = E, .length = quarter},
  53. {.note = D, .length = quarter},
  54. {.note = D, .length = quarter},
  55. {.note = D, .length = quarter},
  56. {.note = F, .length = quarter},
  57. {.note = E, .length = quarter},
  58. {.note = E, .length = quarter},
  59. {.note = D, .length = quarter},
  60. {.note = D, .length = quarter},
  61. {.note = C, .length = half},
  62. {.note = P, .length = half},
  63. {.note = E, .length = quarter},
  64. {.note = E, .length = quarter},
  65. {.note = E, .length = quarter},
  66. {.note = E, .length = quarter},
  67. {.note = G, .length = half},
  68. {.note = F, .length = half},
  69. {.note = D, .length = quarter},
  70. {.note = D, .length = quarter},
  71. {.note = D, .length = quarter},
  72. {.note = D, .length = quarter},
  73. {.note = F, .length = half},
  74. {.note = E, .length = half},
  75. {.note = C, .length = quarter},
  76. {.note = C, .length = quarter},
  77. {.note = C, .length = quarter},
  78. {.note = E, .length = quarter},
  79. {.note = D, .length = quarter},
  80. {.note = D, .length = quarter},
  81. {.note = D, .length = quarter},
  82. {.note = F, .length = quarter},
  83. {.note = E, .length = quarter},
  84. {.note = E, .length = quarter},
  85. {.note = D, .length = quarter},
  86. {.note = D, .length = quarter},
  87. {.note = C, .length = whole},
  88. };
  89. // clang-format on
  90. char* buf[4] = {0};
  91. buzzer_pause();
  92. while (true) {
  93. ESP_LOGI(TAG, "Press any button to play");
  94. usb_serial_jtag_read_bytes(buf, ARRAY_LEN(buf) - 1, portMAX_DELAY);
  95. for (size_t i = 0; i < ARRAY_LEN(sequence); i++) {
  96. play_note(&sequence[i]);
  97. }
  98. }
  99. }