zero_tracker.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include "zero_tracker.h"
  4. /**
  5. * @brief Note record
  6. *
  7. * AH AL
  8. * FEDCBA98 76543210
  9. * nnnnnnee eedddddd
  10. * -------- --------
  11. * nnnnnn = [0] note kill, [1..60] note number, [61..63] service values
  12. * ee ee = [0..F] effect
  13. * 111222 = [0..63] or [0..7, 0..7] effect data
  14. */
  15. typedef uint16_t NoteRecord;
  16. uint8_t record_get_note(NoteRecord note) {
  17. return note & 0x3F;
  18. }
  19. uint8_t record_get_effect(NoteRecord note) {
  20. return (note >> 6) & 0xF;
  21. }
  22. uint8_t record_get_effect_data(NoteRecord note) {
  23. return (note >> 10) & 0x3F;
  24. }
  25. #define PATTERN_SIZE 64
  26. typedef struct {
  27. NoteRecord notes[PATTERN_SIZE];
  28. } NoteRow;
  29. typedef struct {
  30. uint8_t row_count;
  31. NoteRow* rows;
  32. } NotePattern;
  33. #define FURI_HAL_SPEAKER_TIMER TIM16
  34. #define FURI_HAL_SPEAKER_CHANNEL LL_TIM_CHANNEL_CH1
  35. #define FURI_HAL_SPEAKER_PRESCALER 500
  36. void tracker_speaker_play(float frequency, float pwm) {
  37. uint32_t autoreload = (SystemCoreClock / FURI_HAL_SPEAKER_PRESCALER / frequency) - 1;
  38. if(autoreload < 2) {
  39. autoreload = 2;
  40. } else if(autoreload > UINT16_MAX) {
  41. autoreload = UINT16_MAX;
  42. }
  43. if(pwm < 0) pwm = 0;
  44. if(pwm > 1) pwm = 1;
  45. uint32_t compare_value = pwm * autoreload;
  46. if(compare_value == 0) {
  47. compare_value = 1;
  48. }
  49. LL_TIM_SetAutoReload(FURI_HAL_SPEAKER_TIMER, autoreload);
  50. LL_TIM_OC_SetCompareCH1(FURI_HAL_SPEAKER_TIMER, compare_value);
  51. LL_TIM_EnableAllOutputs(FURI_HAL_SPEAKER_TIMER);
  52. }
  53. void tracker_speaker_stop() {
  54. LL_TIM_DisableAllOutputs(FURI_HAL_SPEAKER_TIMER);
  55. }
  56. void tracker_speaker_init() {
  57. furi_hal_speaker_start(200.0f, 0.01f);
  58. tracker_speaker_stop();
  59. }
  60. void tracker_speaker_deinit() {
  61. furi_hal_speaker_stop();
  62. }
  63. void tracker_interrupt_init(FuriHalInterruptISR isr, void* context) {
  64. furi_hal_interrupt_set_isr(FuriHalInterruptIdTIM2, isr, context);
  65. // setup TIM2 to genereate update event approximately 120 times per second
  66. // Timer: base
  67. LL_TIM_InitTypeDef TIM_InitStruct = {0};
  68. // Prescaler to get 1kHz clock
  69. TIM_InitStruct.Prescaler = SystemCoreClock / 1000000 - 1;
  70. TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  71. // Auto reload to get 120Hz interrupt
  72. TIM_InitStruct.Autoreload = 8333 - 1;
  73. TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  74. LL_TIM_Init(TIM2, &TIM_InitStruct);
  75. LL_TIM_EnableIT_UPDATE(TIM2);
  76. LL_TIM_EnableAllOutputs(TIM2);
  77. LL_TIM_EnableCounter(TIM2);
  78. }
  79. void tracker_interrupt_deinit() {
  80. FURI_CRITICAL_ENTER();
  81. LL_TIM_DeInit(TIM2);
  82. FURI_CRITICAL_EXIT();
  83. }
  84. void tracker_interrupt_cb(void* context) {
  85. UNUSED(context);
  86. if(LL_TIM_IsActiveFlag_UPDATE(TIM2)) {
  87. LL_TIM_ClearFlag_UPDATE(TIM2);
  88. }
  89. }
  90. int32_t zero_tracker_app(void* p) {
  91. UNUSED(p);
  92. tracker_speaker_init();
  93. tracker_interrupt_init(tracker_interrupt_cb, NULL);
  94. while(1) {
  95. furi_delay_ms(1000);
  96. }
  97. return 0;
  98. }