arduino-audio-tools
Loading...
Searching...
No Matches
PWMDriverMBED.h
Go to the documentation of this file.
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
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();
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;
62 is_timer_started = true;
63 }
64 }
65
67 virtual void setupPWM() {
68 TRACED();
71 }
72
73 unsigned long period =
74 1000000l / audio_config.pwm_frequency; // -> 30.517578125 microseconds
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
107 if (accessAudioPWM != nullptr) {
109 }
110 }
111};
112
113} // namespace audio_tools
114
115#endif
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define PWM_AUDIO_FREQUENCY
Definition AudioToolsConfig.h:168
Base Class for all PWM drivers.
Definition PWMDriverBase.h:114
void playNextFrame()
writes the next frame to the output pins
Definition PWMDriverBase.h:274
bool is_timer_started
Definition PWMDriverBase.h:260
PWMConfig audio_config
Definition PWMDriverBase.h:251
void deleteBuffer()
Definition PWMDriverBase.h:265
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
TimerAlarmRepeating ticker
Definition PWMDriverMBED.h:52
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
Vector< mbed::PwmOut * > pins
Definition PWMDriverMBED.h:51
virtual void end() override
Definition PWMDriverMBED.h:31
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:29
void setCallbackParameter(void *obj)
Definition AudioTimer.h:56
bool begin(repeating_timer_callback_t callback_f, uint32_t time, TimeUnit unit=MS)
Definition AudioTimer.h:43
bool end()
Definition AudioTimer.h:51
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(int newSize, T value)
Definition Vector.h:266
void clear()
Definition Vector.h:176
@ US
Definition AudioTypes.h:48
PWMDriverAVR PWMDriver
Platform-specific PWM driver alias for AVR.
Definition PWMDriverAVR.h:13
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
void defaultPWMAudioOutputCallback()
Definition PWMDriverAVR.h:134
static PWMDriverAVR * accessAudioPWM
Definition PWMDriverAVR.h:14
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint32_t pwm_frequency
additinal info which might not be used by all processors
Definition PWMDriverBase.h:46