arduino-audio-tools
Loading...
Searching...
No Matches
AudioTimerZephyr.h
Go to the documentation of this file.
1#pragma once
2
3#include "AudioTimerBase.h"
4
5#if defined(IS_ZEPHYR) || defined(DOXYGEN)
6
7#include <zephyr/kernel.h>
8
9namespace audio_tools {
10
28 public:
30
32
35 TimeUnit unit = MS) override {
36 TRACED();
37
38 if (callback_f == nullptr || time == 0) {
39 LOGE("Invalid callback or time");
40 return false;
41 }
42
43 // Stop any existing timer
44 if (is_running) {
45 end();
46 }
47
48 // Convert time to microseconds for Zephyr
49 switch (unit) {
50 case MS:
51 period_us = static_cast<uint64_t>(time) * 1000ULL;
52 break;
53 case US:
54 period_us = time;
55 break;
56 case HZ:
57 // Convert Hz to microseconds: period = 1'000'000 / frequency
58 period_us = (time > 0) ? (1000000ULL / static_cast<uint64_t>(time))
59 : 1000ULL;
60 break;
61 }
62
63 if (period_us == 0) {
64 period_us = 1;
65 }
66
67 LOGI("Timer every: %llu us", period_us);
68
70
71 // Create the timer if needed
72 if (!timer_initialized) {
73 // Initialize timer and work item
77 timer_initialized = true;
78 }
79
80 // Start the timer: first expiration after period, then repeat every period
82 is_running = true;
83
84 LOGD("Timer started");
85 return true;
86 }
87
89 bool end() override {
90 TRACED();
91 if (is_running) {
93 is_running = false;
94 LOGD("Timer stopped");
95 }
96 return true;
97 }
98
102 LOGD("Timer function mode set to: %d", function);
103 }
104
109
110 protected:
111 struct k_timer timer = {};
112 struct k_work work = {};
113 bool timer_initialized = false;
114 bool is_running = false;
118
119 inline void executeCallback() {
120 if (callback != nullptr) {
122 }
123 }
124
125 #pragma GCC diagnostic push
126 #pragma GCC diagnostic ignored "-Winvalid-offsetof"
127
130 if (self != nullptr) {
131 self->executeCallback();
132 }
133 }
134
135 #pragma GCC diagnostic pop
136
137
142 auto* self = static_cast<AudioTimerDriverZephyr*>(
144 if (self == nullptr) return;
145
146 if (self->timer_function == TimerCallbackInThread) {
147 k_work_submit(&self->work);
148 } else {
149 self->executeCallback();
150 }
151 }
152};
153
155using AudioTimerDriver = AudioTimerDriverZephyr;
156
157} // namespace audio_tools
158
159#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
Definition AudioTimerBase.h:22
void * callbackParameter()
Definition AudioTimerBase.h:32
Repeating Timer driver for Zephyr using k_timer.
Definition AudioTimerZephyr.h:27
struct k_work work
Definition AudioTimerZephyr.h:112
TimerFunction timer_function
Definition AudioTimerZephyr.h:117
~AudioTimerDriverZephyr()
Definition AudioTimerZephyr.h:31
static void timerExpiredCallback(struct k_timer *timer_ptr)
Definition AudioTimerZephyr.h:141
static void workCallback(k_work *work_ptr)
Definition AudioTimerZephyr.h:128
void setIsSave(bool is_save) override
Sets whether timer callbacks should be safe (execute in thread context)
Definition AudioTimerZephyr.h:106
struct k_timer timer
Definition AudioTimerZephyr.h:111
bool is_running
Definition AudioTimerZephyr.h:114
bool timer_initialized
Definition AudioTimerZephyr.h:113
void setTimerFunction(TimerFunction function) override
Sets the callback execution mode.
Definition AudioTimerZephyr.h:100
uint64_t period_us
Definition AudioTimerZephyr.h:116
bool end() override
Stops the timer.
Definition AudioTimerZephyr.h:89
void executeCallback()
Definition AudioTimerZephyr.h:119
bool begin(repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS) override
Starts the repeating timer.
Definition AudioTimerZephyr.h:34
repeating_timer_callback_t callback
Definition AudioTimerZephyr.h:115
TimeUnit
Time Units.
Definition AudioTypes.h:44
@ US
Definition AudioTypes.h:44
@ HZ
Definition AudioTypes.h:44
@ MS
Definition AudioTypes.h:44
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
AudioTimerDriverAVR AudioTimerDriver
use AudioTimer!
Definition AudioTimerAVR.h:94
void(* repeating_timer_callback_t)(void *obj)
Definition AudioTimerAVR.h:7
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508