@@ -21,15 +21,22 @@ namespace audio_tools {
21
21
*/
22
22
class PWMAudioOutput : public AudioOutput {
23
23
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
+
24
31
~PWMAudioOutput () {
25
- if (pwm. isTimerStarted ()) {
32
+ if (p_driver && p_driver-> isTimerStarted ()) {
26
33
end ();
27
34
}
28
35
}
29
36
30
37
virtual PWMConfig defaultConfig (RxTxMode mode=TX_MODE) {
31
38
if (mode!=TX_MODE) LOGE (" mode not supported: using TX_MODE" );
32
- return pwm. defaultConfig ();
39
+ return p_driver-> defaultConfig ();
33
40
}
34
41
35
42
PWMConfig config () { return audio_config; }
@@ -52,7 +59,7 @@ class PWMAudioOutput : public AudioOutput {
52
59
53
60
AudioInfo audioInfoOut () override {
54
61
AudioInfo result = audioInfo ();
55
- result.sample_rate = pwm. effectiveOutputSampleRate ();
62
+ result.sample_rate = p_driver-> effectiveOutputSampleRate ();
56
63
return result;
57
64
}
58
65
@@ -66,33 +73,26 @@ class PWMAudioOutput : public AudioOutput {
66
73
bool begin () {
67
74
TRACED ();
68
75
AudioOutput::setAudioInfo (audio_config);
69
- return pwm. begin (audio_config);
76
+ return p_driver-> begin (audio_config);
70
77
}
71
78
72
- virtual void end () override { pwm. end (); }
79
+ virtual void end () override { if (p_driver) p_driver-> end (); }
73
80
74
- int availableForWrite () override { return pwm. availableForWrite (); }
81
+ int availableForWrite () override { return p_driver ? p_driver-> availableForWrite () : 0 ; }
75
82
76
- // blocking write for an array: we expect a singed value and convert it into a
77
- // unsigned
78
83
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 ; }
81
85
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 ; }
86
88
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); }
92
91
93
92
protected:
94
93
PWMConfig audio_config;
95
- PWMDriver pwm; // platform specific pwm
94
+ PWMDriver default_driver;
95
+ DriverPWMBase *p_driver = &default_driver;
96
96
};
97
97
98
98
0 commit comments