Skip to content

Commit c715f19

Browse files
committed
PWMAudioOutput: accept driver in constructor
1 parent 8db500f commit c715f19

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/AudioTools/CoreAudio/AudioPWM/PWMAudioOutput.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,22 @@ namespace audio_tools {
2121
*/
2222
class PWMAudioOutput : public AudioOutput {
2323
public:
24+
// Default constructor with default driver
25+
PWMAudioOutput() = default;
26+
27+
// Inject external driver (must live longer than this object)
28+
PWMAudioOutput(DriverPWMBase &ext_driver)
29+
: p_driver(&ext_driver) {}
30+
2431
~PWMAudioOutput() {
25-
if (pwm.isTimerStarted()) {
32+
if (p_driver && p_driver->isTimerStarted()) {
2633
end();
2734
}
2835
}
2936

3037
virtual PWMConfig defaultConfig(RxTxMode mode=TX_MODE) {
3138
if (mode!=TX_MODE) LOGE("mode not supported: using TX_MODE");
32-
return pwm.defaultConfig();
39+
return p_driver->defaultConfig();
3340
}
3441

3542
PWMConfig config() { return audio_config; }
@@ -52,7 +59,7 @@ class PWMAudioOutput : public AudioOutput {
5259

5360
AudioInfo audioInfoOut() override {
5461
AudioInfo result = audioInfo();
55-
result.sample_rate = pwm.effectiveOutputSampleRate();
62+
result.sample_rate = p_driver->effectiveOutputSampleRate();
5663
return result;
5764
}
5865

@@ -66,33 +73,26 @@ class PWMAudioOutput : public AudioOutput {
6673
bool begin() {
6774
TRACED();
6875
AudioOutput::setAudioInfo(audio_config);
69-
return pwm.begin(audio_config);
76+
return p_driver->begin(audio_config);
7077
}
7178

72-
virtual void end() override { pwm.end(); }
79+
virtual void end() override { if (p_driver) p_driver->end(); }
7380

74-
int availableForWrite() override { return pwm.availableForWrite(); }
81+
int availableForWrite() override { return p_driver ? p_driver->availableForWrite() : 0; }
7582

76-
// blocking write for an array: we expect a singed value and convert it into a
77-
// unsigned
7883
size_t write(const uint8_t *data, size_t len) override {
79-
return pwm.write(data, len);
80-
}
84+
return p_driver ? p_driver->write(data, len) : 0; }
8185

82-
// When the timer does not have enough data we increase the underflow_count;
83-
uint32_t underflowsPerSecond() { return pwm.underflowsPerSecond(); }
84-
// provides the effectivly measured output frames per second
85-
uint32_t framesPerSecond() { return pwm.framesPerSecond(); }
86+
uint32_t underflowsPerSecond() { return p_driver ? p_driver->underflowsPerSecond() : 0; }
87+
uint32_t framesPerSecond() { return p_driver ? p_driver->framesPerSecond() : 0; }
8688

87-
/// Provides access to the driver
88-
PWMDriver *driver() { return &pwm; }
89-
/// You can assign your own custom buffer impelementation: must be allocated
90-
/// on the heap and will be cleaned up by this class
91-
void setBuffer(BaseBuffer<uint8_t> *buffer) { pwm.setBuffer(buffer); }
89+
DriverPWMBase *driver() { return p_driver; }
90+
void setBuffer(BaseBuffer<uint8_t> *buffer) { if (p_driver) p_driver->setBuffer(buffer); }
9291

9392
protected:
9493
PWMConfig audio_config;
95-
PWMDriver pwm; // platform specific pwm
94+
PWMDriver default_driver;
95+
DriverPWMBase *p_driver = &default_driver;
9696
};
9797

9898

0 commit comments

Comments
 (0)