@@ -24,8 +24,21 @@ class I2SDriverRP2040 {
24
24
I2SConfigStd c (mode);
25
25
return c;
26
26
}
27
- // / Potentially updates the sample rate (if supported)
28
- bool setAudioInfo (AudioInfo) { return false ; }
27
+ // / Potentially updates values dynamically
28
+ bool setAudioInfo (AudioInfo info) {
29
+ if (info.sample_rate != cfg.sample_rate && !i2s.setFrequency (info.sample_rate )) {
30
+ LOGI (" i2s.setFrequency %d failed" , info.sample_rate );
31
+ return false ;
32
+ }
33
+ if (info.bits_per_sample != cfg.bits_per_sample && !i2s.setBitsPerSample (info.bits_per_sample )) {
34
+ LOGI (" i2s.setBitsPerSample %d failed" , info.bits_per_sample );
35
+ return false ;
36
+ }
37
+ cfg.sample_rate = info.sample_rate ;
38
+ cfg.bits_per_sample = info.bits_per_sample ;
39
+ cfg.channels = info.channels ;
40
+ return true ;
41
+ }
29
42
30
43
// / starts the DAC with the default config in TX Mode
31
44
bool begin (RxTxMode mode = TX_MODE) {
@@ -36,6 +49,8 @@ class I2SDriverRP2040 {
36
49
// / starts the DAC
37
50
bool begin (I2SConfigStd cfg) {
38
51
TRACEI ();
52
+ // prevent multiple begins w/o calling end
53
+ if (is_active) end ();
39
54
this ->cfg = cfg;
40
55
cfg.logInfo ();
41
56
switch (cfg.rx_tx_mode ) {
@@ -137,13 +152,15 @@ class I2SDriverRP2040 {
137
152
LOGE (" Could not start I2S" );
138
153
return false ;
139
154
}
155
+ is_active = true ;
140
156
return true ;
141
157
}
142
158
143
159
// / stops the I2C and uninstalls the driver
144
160
void end () {
145
161
flush ();
146
162
i2s.end ();
163
+ is_active = false ;
147
164
}
148
165
149
166
// / provides the actual configuration
@@ -204,6 +221,7 @@ class I2SDriverRP2040 {
204
221
I2SConfigStd cfg;
205
222
I2S i2s;
206
223
bool has_input[2 ];
224
+ bool is_active = false ;
207
225
208
226
// / writes 1 channel to I2S while expanding it to 2 channels
209
227
// returns amount of bytes written from src to i2s
0 commit comments