arduino-audio-tools
Loading...
Searching...
No Matches
AudioTimerESP32.h
Go to the documentation of this file.
1#pragma once
2
3#if defined(ESP32) && defined(ARDUINO)
4#include <esp_task_wdt.h>
5
7
8namespace audio_tools {
9
19 : public TimerAlarmRepeatingDriverBase {
20 public:
22 end();
23 LOGD("esp_timer_delete");
25 }
26
27 void setIsSave(bool is_save) override {
29 }
30
31 void setTimerFunction(TimerFunction function) override {
32 timer_function = function;
33 }
34
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
51 break;
52 }
53 LOGI("Timer every: %u us", time_us);
54
55 if (!initialized) {
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);
71 if (rc != ESP_OK) {
72 LOGE("Could not create timer: %d", rc);
73 return false;
74 }
75 }
76
78 if (!started) {
79 LOGD("esp_timer_start_periodic");
81 } else {
82 LOGD("esp_timer_restart");
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");
101 }
102 started = false;
103 return true;
104 }
105
106 protected:
108 bool started = false;
109 bool initialized = false;
110 uint64_t time_us = 0;
111 TimerFunction timer_function = TimerCallbackInThread;
112};
113
116
117
118} // namespace audio_tools
119
120#endif
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
static uint32_t toTimeUs(uint32_t samplingRate, uint8_t limit=10)
converts sampling rate to delay in microseconds (μs)
Definition AudioTypes.h:242
TimeUnit
Time Units.
Definition AudioTypes.h:48
@ US
Definition AudioTypes.h:48
@ HZ
Definition AudioTypes.h:48
@ MS
Definition AudioTypes.h:48
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
TimerFunction
Definition AudioTimerBase.h:16
@ TimerCallbackInThread
Definition AudioTimerBase.h:18
@ DirectTimerCallback
Definition AudioTimerBase.h:17
void(* repeating_timer_callback_t)(void *obj)
Definition AudioTimerAVR.h:7
TimerAlarmRepeatingDriverAVR TimerAlarmRepeatingDriver
use TimerAlarmRepeating!
Definition AudioTimerAVR.h:94
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
constexpr const _Ep * end(initializer_list< _Ep > __il) noexcept
Definition InitializerList.h:63
constexpr const _Ep * begin(initializer_list< _Ep > __il) noexcept
Definition InitializerList.h:55