arduino-audio-tools
PWMAudioRenesas.h
1 #pragma once
2 #if defined(ARDUINO_ARCH_RENESAS)
3 #include "AudioTools/CoreAudio/AudioPWM/PWMAudioBase.h"
4 #include "AudioTools/CoreAudio/AudioTimer/AudioTimer.h"
5 #include "pwm.h"
6 
7 namespace audio_tools {
8 
9 // forward declaration
10 class PWMDriverRenesas;
15 using PWMDriver = PWMDriverRenesas;
16 
25  public:
26  PWMDriverRenesas() { LOGD("PWMDriverRenesas"); }
27 
28  virtual PWMConfig defaultConfig() {
29  PWMConfig cfg;
30  Pins pwm_pins;
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();
55  deleteBuffer();
56  }
57 
58  protected:
59  Vector<PwmOut*> pins;
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();
68  ticker.setCallbackParameter(this);
69  int sample_rate = effectiveOutputSampleRate();
70  if (isDecimateActive()) {
71  LOGI("Using reduced sample rate: %d", effectiveOutputSampleRate());
72  }
73  ticker.begin(defaultPWMAudioOutputCallback, sample_rate, HZ);
74  is_timer_started = true;
75  }
76  }
77 
79  virtual void setupPWM() {
80  TRACED();
81  pins.resize(audio_config.channels);
82  for (int j = 0; j < audio_config.channels; j++) {
83  LOGD("Processing channel %d", j);
84  auto gpio = audio_config.pins()[j];
85  PwmOut* pin = new PwmOut(gpio);
86  LOGI("PWM Pin: %d", gpio);
87  pin->begin(20000.0f, 0.0f); // 50: 20000hz at 0%
88  pins[j] = pin;
89  }
90  }
91 
93  virtual void setupTimer() {}
94 
95  virtual int maxChannels() { return PIN_PWM_COUNT; };
96 
98  virtual int maxOutputValue() {
99  return 100; // percent
100  }
101 
104  virtual void pwmWrite(int channel, int value) {
105  pins[channel]->pulse_perc(value);
106  }
107 
109  static void defaultPWMAudioOutputCallback(void* obj) {
110  PWMDriverRenesas* accessAudioPWM = (PWMDriverRenesas*)obj;
111  if (accessAudioPWM != nullptr) {
112  accessAudioPWM->playNextFrame();
113  }
114  }
115 
116  int maxSampleRate() override { return ANALOG_MAX_SAMPLE_RATE; }
117 
118  int decimation() override {
119  for (int j = 1; j < 5; j++) {
120  if (j % 2 == 0 && audio_config.sample_rate / j <= maxSampleRate()) {
121  return j;
122  }
123  }
124  return 5;
125  }
126 };
127 
128 } // namespace audio_tools
129 
130 #endif
Base Class for all PWM drivers.
Definition: PWMAudioBase.h:105
virtual int effectiveOutputSampleRate()
Provides the effective sample rate.
Definition: PWMAudioBase.h:321
void playNextFrame()
writes the next frame to the output pins
Definition: PWMAudioBase.h:250
virtual bool isDecimateActive()
Definition: PWMAudioBase.h:316
Audio output to PWM pins for Renesas based Arduino implementations.
Definition: PWMAudioRenesas.h:24
virtual void startTimer() override
Definition: PWMAudioRenesas.h:64
int maxSampleRate() override
Provides the max working sample rate.
Definition: PWMAudioRenesas.h:116
virtual void setupPWM()
Setup PWM Pins.
Definition: PWMAudioRenesas.h:79
virtual int maxOutputValue()
provides the max value for the configured resulution
Definition: PWMAudioRenesas.h:98
virtual void pwmWrite(int channel, int value)
Definition: PWMAudioRenesas.h:104
int decimation() override
Decimation factor to reduce the sample rate.
Definition: PWMAudioRenesas.h:118
virtual void setupTimer()
not used -> see startTimer();
Definition: PWMAudioRenesas.h:93
static void defaultPWMAudioOutputCallback(void *obj)
timer callback: write the next frame to the pins
Definition: PWMAudioRenesas.h:109
Common Interface definition for TimerAlarmRepeating.
Definition: AudioTimer.h:25
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868
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
Configuration data for PWM audio output.
Definition: PWMAudioBase.h:32