arduino-audio-tools
PWMAudioMBED.h
1 
2 #pragma once
3 #if defined(ARDUINO_ARCH_MBED)
4 #include "AudioPWM/PWMAudioBase.h"
5 #include "AudioTimer/AudioTimer.h"
6 #include "mbed.h"
7 
8 namespace audio_tools {
9 
10 // forward declaration
11 class PWMDriverMBED;
16 using PWMDriver = PWMDriverMBED;
17 
25 class PWMDriverMBED : public DriverPWMBase {
26  public:
27  PWMDriverMBED() = default;
28 
29  // Ends the output
30  virtual void end() override {
31  TRACED();
32  ticker.end(); // it does not hurt to call this even if it has not been
33  // started
34  is_timer_started = false;
35 
36  // stop and release pins
37  for (int j = 0; j < audio_config.channels; j++) {
38  if (pins[j] != nullptr) {
39  pins[j]->suspend();
40  delete pins[j];
41  pins[j] = nullptr;
42  }
43  }
44  pins.clear();
45  // pins.shrink_to_fit();
46  deleteBuffer();
47  }
48 
49  protected:
51  TimerAlarmRepeating ticker; // calls a callback repeatedly with a timeout
52 
55  virtual void startTimer() override {
56  if (!is_timer_started) {
57  TRACED();
58  long wait_time = 1000000l / audio_config.sample_rate;
59  ticker.setCallbackParameter(this);
60  ticker.begin(defaultPWMAudioOutputCallback, wait_time, US);
61  is_timer_started = true;
62  }
63  }
64 
66  virtual void setupPWM() {
67  TRACED();
68  unsigned long period =
69  1000000l / audio_config.pwm_frequency; // -> 30.517578125 microseconds
70  pins.resize(audio_config.channels);
71  for (int j = 0; j < audio_config.channels; j++) {
72  LOGD("Processing channel %d", j);
73  auto gpio = audio_config.pins()[j];
74  mbed::PwmOut* pin = new mbed::PwmOut(digitalPinToPinName(gpio));
75  LOGI("PWM Pin: %d", gpio);
76  pin->period_us(period);
77  pin->write(0.0f); // 0% duty cycle ->
78  pin->resume(); // in case it was suspended before
79  pins[j] = pin;
80  }
81  }
82 
84  virtual void setupTimer() {}
85 
87  virtual int maxChannels() { return 16; };
88 
90  virtual int maxOutputValue() { return 1000; }
91 
94  virtual void pwmWrite(int channel, int value) {
95  float float_value = static_cast<float>(value) / maxOutputValue();
96  pins[channel]->write(float_value); // pwm the value is between 0.0 and 1.0
97  }
98 
100  static void defaultPWMAudioOutputCallback(void* obj) {
101  PWMDriverMBED* accessAudioPWM = (PWMDriverMBED*)obj;
102  if (accessAudioPWM != nullptr) {
103  accessAudioPWM->playNextFrame();
104  }
105  }
106 };
107 
108 } // namespace audio_tools
109 
110 #endif
Base Class for all PWM drivers.
Definition: PWMAudioBase.h:105
void playNextFrame()
writes the next frame to the output pins
Definition: PWMAudioBase.h:248
Audio output to PWM pins for MBED based Arduino implementations.
Definition: PWMAudioMBED.h:25
virtual void startTimer() override
Definition: PWMAudioMBED.h:55
virtual int maxChannels()
Maximum supported channels.
Definition: PWMAudioMBED.h:87
virtual void setupPWM()
Setup PWM Pins.
Definition: PWMAudioMBED.h:66
virtual int maxOutputValue()
provides the max value for the configured resulution
Definition: PWMAudioMBED.h:90
virtual void pwmWrite(int channel, int value)
Definition: PWMAudioMBED.h:94
virtual void setupTimer()
not used -> see startTimer();
Definition: PWMAudioMBED.h:84
static void defaultPWMAudioOutputCallback(void *obj)
timer callback: write the next frame to the pins
Definition: PWMAudioMBED.h:100
Common Interface definition for TimerAlarmRepeating.
Definition: AudioTimer.h:25
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AnalogAudio.h:10
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition: AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition: AudioTypes.h:55