arduino-audio-tools
Loading...
Searching...
No Matches
PWMDriverRenesas.h
Go to the documentation of this file.
1#pragma once
2#if defined(ARDUINO_ARCH_RENESAS)
5#include "pwm.h"
6
7namespace audio_tools {
8
9// forward declaration
10class PWMDriverRenesas;
15using PWMDriver = PWMDriverRenesas;
16
25 public:
26 PWMDriverRenesas() { LOGD("PWMDriverRenesas"); }
27
29 PWMConfig cfg;
31 // add default pins
32 for (int j = 2; j < 13; j += 2) pwm_pins.push_back(j);
33 cfg.setPins(pwm_pins);
34 return cfg;
35 }
36
37 // Ends the output
38 virtual void end() override {
39 TRACED();
40 ticker.end(); // it does not hurt to call this even if it has not been
41 // started
42 is_timer_started = false;
43
44 // stop and release pins
45 for (int j = 0; j < audio_config.channels; j++) {
46 if (pins[j] != nullptr) {
47 pins[j]->suspend();
48 pins[j]->end();
49 delete pins[j];
50 pins[j] = nullptr;
51 }
52 }
53 pins.clear();
54 // pins.shrink_to_fit();
56 }
57
58 protected:
60 TimerAlarmRepeating ticker; // calls a callback repeatedly with a timeout
61
64 virtual void startTimer() override {
65 TRACED();
66 if (!is_timer_started) {
67 TRACED();
69 int sample_rate = effectiveOutputSampleRate();
70 if (isDecimateActive()) {
71 LOGI("Using reduced sample rate: %d", effectiveOutputSampleRate());
72 }
74 is_timer_started = true;
75 }
76 }
77
79 virtual void setupPWM() {
80 TRACED();
83 }
84
86 for (int j = 0; j < audio_config.channels; j++) {
87 LOGD("Processing channel %d", j);
88 auto gpio = audio_config.pins()[j];
89 PwmOut* pin = new PwmOut(gpio);
90 LOGI("PWM Pin: %d", gpio);
91 pin->begin(20000.0f, 0.0f); // 50: 20000hz at 0%
92 pins[j] = pin;
93 }
94 }
95
97 virtual void setupTimer() {}
98
99 virtual int maxChannels() { return PIN_PWM_COUNT; };
100
102 virtual int maxOutputValue() {
103 return 100; // percent
104 }
105
108 virtual void pwmWrite(int channel, int value) {
109 pins[channel]->pulse_perc(value);
110 }
111
119
120 int maxSampleRate() override { return audio_config.max_sample_rate; }
121
122 int decimation() override {
123 for (int j = 1; j < 5; j++) {
124 if (j % 2 == 0 && audio_config.sample_rate / j <= maxSampleRate()) {
125 return j;
126 }
127 }
128 return 5;
129 }
130};
131
132} // namespace audio_tools
133
134#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
virtual int effectiveOutputSampleRate()
Provides the effective sample rate.
Definition PWMDriverBase.h:246
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
virtual bool isDecimateActive()
Definition PWMDriverBase.h:339
void deleteBuffer()
Definition PWMDriverBase.h:265
Audio output to PWM pins for Renesas based Arduino implementations.
Definition PWMDriverRenesas.h:24
PWMDriverRenesas()
Definition PWMDriverRenesas.h:26
virtual void startTimer() override
Definition PWMDriverRenesas.h:64
int maxSampleRate() override
Provides the max working sample rate.
Definition PWMDriverRenesas.h:120
virtual int maxChannels()
Definition PWMDriverRenesas.h:99
virtual PWMConfig defaultConfig()
Definition PWMDriverRenesas.h:28
TimerAlarmRepeating ticker
Definition PWMDriverRenesas.h:60
virtual void setupPWM()
Setup PWM Pins.
Definition PWMDriverRenesas.h:79
virtual int maxOutputValue()
provides the max value for the configured resulution
Definition PWMDriverRenesas.h:102
virtual void pwmWrite(int channel, int value)
Definition PWMDriverRenesas.h:108
int decimation() override
Decimation factor to reduce the sample rate.
Definition PWMDriverRenesas.h:122
virtual void setupTimer()
not used -> see startTimer();
Definition PWMDriverRenesas.h:97
Vector< PwmOut * > pins
Definition PWMDriverRenesas.h:59
virtual void end() override
Definition PWMDriverRenesas.h:38
static void defaultPWMAudioOutputCallback(void *obj)
timer callback: write the next frame to the pins
Definition PWMDriverRenesas.h:113
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
void push_back(T &&value)
Definition Vector.h:182
bool resize(int newSize, T value)
Definition Vector.h:266
void clear()
Definition Vector.h:176
iterator end()
Definition Vector.h:291
@ HZ
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
Configuration data for PWM audio output.
Definition PWMDriverBase.h:32
uint32_t pwm_frequency
additinal info which might not be used by all processors
Definition PWMDriverBase.h:46
uint32_t max_sample_rate
max sample sample rate that still produces good audio
Definition PWMDriverBase.h:56
#define PIN_PWM_COUNT
Definition unor4.h:13