arduino-audio-tools
Loading...
Searching...
No Matches
AudioTimerESP32.h
1#pragma once
2
3#if defined(ESP32) && defined(ARDUINO)
4#include <esp_task_wdt.h>
5
6#include "AudioTools/CoreAudio/AudioTimer/AudioTimerBase.h"
7
8namespace audio_tools {
9
18class TimerAlarmRepeatingDriverESP32
19 : public TimerAlarmRepeatingDriverBase {
20 public:
21 virtual ~TimerAlarmRepeatingDriverESP32() {
22 end();
23 LOGD("esp_timer_delete");
24 esp_timer_delete(rtsp_timer);
25 }
26
27 void setIsSave(bool is_save) override {
28 setTimerFunction(is_save ? TimerCallbackInThread : DirectTimerCallback);
29 }
30
31 void setTimerFunction(TimerFunction function) override {
32 timer_function = function;
33 }
34
36 bool begin(repeating_timer_callback_t callback_f, uint32_t time,
37 TimeUnit unit = MS) override {
38 TRACED();
39
40 // we determine the time in microseconds
41 switch (unit) {
42 case MS:
43 time_us = time * 1000;
44 break;
45 case US:
46 time_us = time;
47 break;
48 case HZ:
49 // convert hz to time in us
50 time_us = AudioTime::toTimeUs(time);
51 break;
52 }
53 LOGI("Timer every: %u us", time_us);
54
55 if (!initialized) {
56 esp_timer_create_args_t timer_args;
57 timer_args.callback = callback_f;
58 timer_args.name = "rtsp_timer";
59 timer_args.arg = callbackParameter();
60 // select the dispatch method
61 if (timer_function == DirectTimerCallback) {
62#if CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD
63 timer_args.dispatch_method = ESP_TIMER_ISR;
64#endif
65 } else {
66 timer_args.dispatch_method = ESP_TIMER_TASK;
67 }
68
69 LOGD("esp_timer_create: %p", timer_args.arg);
70 esp_err_t rc = esp_timer_create(&timer_args, &rtsp_timer);
71 if (rc != ESP_OK) {
72 LOGE("Could not create timer: %d", rc);
73 return false;
74 }
75 }
76
77 esp_err_t rc = ESP_OK;
78 if (!started) {
79 LOGD("esp_timer_start_periodic");
80 rc = esp_timer_start_periodic(rtsp_timer, time_us);
81 } else {
82 LOGD("esp_timer_restart");
83 rc = esp_timer_restart(rtsp_timer, time_us);
84 }
85 if (rc != ESP_OK) {
86 LOGE("Could not start timer: %d", rc);
87 return false;
88 }
89
90 initialized = true;
91 started = true;
92 return true;
93 }
94
96 bool end() override {
97 TRACED();
98 if (started) {
99 LOGD("esp_timer_stop");
100 esp_timer_stop(rtsp_timer);
101 }
102 started = false;
103 return true;
104 }
105
106 protected:
107 esp_timer_handle_t rtsp_timer = nullptr;
108 bool started = false;
109 bool initialized = false;
110 uint64_t time_us = 0;
111 TimerFunction timer_function = TimerCallbackInThread;
112};
113
115using TimerAlarmRepeatingDriver = TimerAlarmRepeatingDriverESP32;
116
117
118} // namespace audio_tools
119
120#endif
static uint32_t toTimeUs(uint32_t samplingRate, uint8_t limit=10)
converts sampling rate to delay in microseconds (μs)
Definition AudioTypes.h:240
TimeUnit
Time Units.
Definition AudioTypes.h:46
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
TimerAlarmRepeatingDriverAVR TimerAlarmRepeatingDriver
use TimerAlarmRepeating!
Definition AudioTimerAVR.h:94