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