arduino-audio-tools
AudioTimerMBED.h
1 #pragma once
2 
3 #if defined(ARDUINO_ARCH_MBED)
4 #include "AudioTools/CoreAudio/AudioTimer/AudioTimerBase.h"
5 #include "mbed.h"
6 
7 namespace audio_tools {
8 
9 class TimerAlarmRepeatingDriverMBED;
10 static TimerAlarmRepeatingDriverMBED *timerAlarmRepeating = nullptr;
11 
21  public:
22  TimerAlarmRepeatingDriverMBED() { timerAlarmRepeating = this; }
23 
27  bool begin(repeating_timer_callback_t callback_f, uint32_t time,
28  TimeUnit unit = MS) override {
29  callback = callback_f;
30 
31  // we determine the time in microseconds
32  switch (unit) {
33  case MS:
34  ticker.attach(tickerCallback, std::chrono::microseconds(time * 1000));
35  break;
36  case US:
37  ticker.attach(tickerCallback, std::chrono::microseconds(time));
38  break;
39  case HZ:
40  // convert hz to time in us
41  uint64_t time_us = AudioTime::toTimeUs(time);
42  ticker.attach(tickerCallback, std::chrono::microseconds(time_us));
43  break;
44  }
45  return true;
46  }
47 
48  // ends the timer and if necessary the task
49  bool end() {
50  ticker.detach();
51  return true;
52  }
53 
54  protected:
55  mbed::Ticker ticker;
56  repeating_timer_callback_t callback;
57 
58  inline static void tickerCallback() {
59  timerAlarmRepeating->callback(timerAlarmRepeating->object);
60  }
61 };
62 
64 using TimerAlarmRepeatingDriver = TimerAlarmRepeatingDriverMBED;
65 
66 } // namespace audio_tools
67 
68 #endif
static uint32_t toTimeUs(uint32_t samplingRate, uint8_t limit=10)
converts sampling rate to delay in microseconds (μs)
Definition: AudioTypes.h:256
Definition: AudioTimerBase.h:22
Repeating Timer functions for repeated execution: Plaease use the typedef TimerAlarmRepeating.
Definition: AudioTimerMBED.h:20
bool begin(repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS) override
Definition: AudioTimerMBED.h:27
TimeUnit
Time Units.
Definition: AudioTypes.h:45
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868
TimerAlarmRepeatingDriverAVR TimerAlarmRepeatingDriver
use TimerAlarmRepeating!
Definition: AudioTimerAVR.h:94