@@ -157,6 +157,7 @@ class CodecConfig : public codec_config_t {
157157class AudioDriver {
158158public:
159159 virtual bool begin (CodecConfig codecCfg, DriverPins &pins) {
160+ AD_LOGD (" AudioDriver::begin" );
160161 codec_cfg = codecCfg;
161162 p_pins = &pins;
162163 pins.setSPIActiveForSD (codecCfg.sd_active );
@@ -192,7 +193,7 @@ class AudioDriver {
192193 GpioPin pin = pins ().getPinID (PinFunction::PA);
193194 if (pin == -1 )
194195 return false ;
195- AD_LOGI (" setPAPower pin %d -> %d" , pin, enable);
196+ // AD_LOGI("setPAPower pin %d -> %d", pin, enable);
196197 digitalWrite (pin, enable ? HIGH : LOW);
197198 return true ;
198199 }
@@ -201,6 +202,16 @@ class AudioDriver {
201202 CodecConfig codec_cfg;
202203 DriverPins *p_pins = nullptr ;
203204
205+ // / Detemine the TwoWire object from the I2C config or use Wire
206+ TwoWire* getI2C () {
207+ if (p_pins == nullptr ) return &Wire;
208+ auto i2c = pins ().getI2CPins (PinFunction::CODEC);
209+ if (!i2c) {
210+ return &Wire;
211+ }
212+ return i2c.value ().p_wire ;
213+ }
214+
204215 virtual bool init (codec_config_t codec_cfg) { return false ; }
205216 virtual bool deinit () { return false ; }
206217 virtual bool controlState (codec_mode_t mode) { return false ; };
@@ -262,12 +273,7 @@ class AudioDriverAC101Class : public AudioDriver {
262273
263274protected:
264275 bool init (codec_config_t codec_cfg) {
265- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
266- if (!i2c) {
267- AD_LOGE (" i2c pins not defined" );
268- return false ;
269- }
270- ac101_set_i2c_handle (i2c.value ().p_wire );
276+ ac101_set_i2c_handle (getI2C ());
271277 return ac101_init (&codec_cfg) == RESULT_OK;
272278 };
273279 bool deinit () { return ac101_deinit () == RESULT_OK; }
@@ -293,15 +299,17 @@ class AudioDriverCS43l22Class : public AudioDriver {
293299 void setI2CAddress (uint16_t adr) { deviceAddr = adr; }
294300
295301 virtual bool begin (CodecConfig codecCfg, DriverPins &pins) {
302+ AD_LOGD (" AudioDriverCS43l22Class::begin" );
303+ p_pins = &pins;
296304 codec_cfg = codecCfg;
297305 // manage reset pin -> acive high
298306 setPAPower (true );
299307 delay (10 );
300- p_pins = &pins;
301308 int vol = map (volume, 0 , 100 , DEFAULT_VOLMIN, DEFAULT_VOLMAX);
302309 uint32_t freq = getFrequency (codec_cfg.i2s .rate );
303310 uint16_t outputDevice = getOutput (codec_cfg.output_device );
304- return cs43l22_Init (deviceAddr, outputDevice, vol, freq) == 0 ;
311+ AD_LOGD (" cs43l22_Init" );
312+ return cs43l22_Init (deviceAddr, outputDevice, vol, freq, getI2C ()) == 0 ;
305313 }
306314
307315 bool setMute (bool mute) {
@@ -382,12 +390,7 @@ class AudioDriverES7210Class : public AudioDriver {
382390 int volume;
383391
384392 bool init (codec_config_t codec_cfg) {
385- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
386- if (!i2c) {
387- AD_LOGE (" i2c pins not defined" );
388- return false ;
389- }
390- return es7210_adc_init (&codec_cfg, i2c.value ().p_wire ) == RESULT_OK;
393+ return es7210_adc_init (&codec_cfg, getI2C ()) == RESULT_OK;
391394 }
392395 bool deinit () { return es7210_adc_deinit () == RESULT_OK; }
393396
@@ -420,12 +423,7 @@ class AudioDriverES7243Class : public AudioDriver {
420423
421424protected:
422425 bool init (codec_config_t codec_cfg) {
423- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
424- if (!i2c) {
425- AD_LOGE (" i2c pins not defined" );
426- return false ;
427- }
428- return es7243_adc_init (&codec_cfg, i2c.value ().p_wire ) == RESULT_OK;
426+ return es7243_adc_init (&codec_cfg, getI2C ()) == RESULT_OK;
429427 }
430428 bool deinit () { return es7243_adc_deinit () == RESULT_OK; }
431429
@@ -462,12 +460,7 @@ class AudioDriverES7243eClass : public AudioDriver {
462460 int volume = 0 ;
463461
464462 bool init (codec_config_t codec_cfg) {
465- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
466- if (!i2c) {
467- AD_LOGE (" i2c pins not defined" );
468- return false ;
469- }
470- return es7243e_adc_init (&codec_cfg, i2c.value ().p_wire ) == RESULT_OK;
463+ return es7243e_adc_init (&codec_cfg, getI2C ()) == RESULT_OK;
471464 }
472465 bool deinit () { return es7243e_adc_deinit () == RESULT_OK; }
473466
@@ -501,13 +494,7 @@ class AudioDriverES8156Class : public AudioDriver {
501494
502495protected:
503496 bool init (codec_config_t codec_cfg) {
504- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
505- if (!i2c) {
506- AD_LOGE (" i2c pins not defined" );
507- return false ;
508- }
509-
510- return es8156_codec_init (&codec_cfg, i2c.value ().p_wire ) == RESULT_OK;
497+ return es8156_codec_init (&codec_cfg, getI2C ()) == RESULT_OK;
511498 }
512499 bool deinit () { return es8156_codec_deinit () == RESULT_OK; }
513500
@@ -538,15 +525,10 @@ class AudioDriverES8311Class : public AudioDriver {
538525
539526protected:
540527 bool init (codec_config_t codec_cfg) {
541- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
542- if (!i2c) {
543- AD_LOGE (" i2c pins not defined" );
544- return false ;
545- }
546528 int mclk_src = pins ().getPinID (PinFunction::MCLK_SOURCE);
547529 if (mclk_src == -1 )
548530 return false ;
549- return es8311_codec_init (&codec_cfg, i2c. value (). p_wire , mclk_src) ==
531+ return es8311_codec_init (&codec_cfg, getI2C () , mclk_src) ==
550532 RESULT_OK;
551533 }
552534 bool deinit () { return es8311_codec_deinit () == RESULT_OK; }
@@ -579,13 +561,8 @@ class AudioDriverES8374Class : public AudioDriver {
579561
580562protected:
581563 bool init (codec_config_t codec_cfg) {
582- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
583- if (!i2c) {
584- AD_LOGE (" i2c pins not defined" );
585- return false ;
586- }
587564 auto codec_mode = this ->codec_cfg .get_mode ();
588- return es8374_codec_init (&codec_cfg, codec_mode, i2c. value (). p_wire ) ==
565+ return es8374_codec_init (&codec_cfg, codec_mode, getI2C () ) ==
589566 RESULT_OK;
590567 }
591568 bool deinit () { return es8374_codec_deinit () == RESULT_OK; }
@@ -631,13 +608,7 @@ class AudioDriverES8388Class : public AudioDriver {
631608
632609protected:
633610 bool init (codec_config_t codec_cfg) {
634- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
635- if (!i2c) {
636- AD_LOGE (" i2c pins not defined" );
637- return false ;
638- }
639-
640- return es8388_init (&codec_cfg, i2c.value ().p_wire ) == RESULT_OK;
611+ return es8388_init (&codec_cfg, getI2C ()) == RESULT_OK;
641612 }
642613 bool deinit () { return es8388_deinit () == RESULT_OK; }
643614
@@ -669,12 +640,7 @@ class AudioDriverTAS5805MClass : public AudioDriver {
669640
670641protected:
671642 bool init (codec_config_t codec_cfg) {
672- auto i2c = pins ().getI2CPins (PinFunction::CODEC);
673- if (!i2c) {
674- AD_LOGE (" i2c pins not defined" );
675- return false ;
676- }
677- return tas5805m_init (&codec_cfg, i2c.value ().p_wire ) == RESULT_OK;
643+ return tas5805m_init (&codec_cfg, getI2C ()) == RESULT_OK;
678644 }
679645 bool deinit () { return tas5805m_deinit () == RESULT_OK; }
680646};
@@ -689,13 +655,8 @@ class AudioDriverWM8960Class : public AudioDriver {
689655 bool begin (CodecConfig codecCfg, DriverPins &pins) {
690656 codec_cfg = codecCfg;
691657
692- auto i2c = pins.getI2CPins (PinFunction::CODEC);
693- if (!i2c) {
694- AD_LOGE (" i2c pins not defined" );
695- return false ;
696- }
697658 // define wire object
698- mtb_wm8960_set_wire (i2c. value (). p_wire );
659+ mtb_wm8960_set_wire (getI2C () );
699660 mtb_wm8960_set_write_retry_count (i2c_retry_count);
700661
701662 // setup wm8960
@@ -881,14 +842,8 @@ class AudioDriverWM8994Class : public AudioDriver {
881842 uint32_t freq = codecCfg.getRateNumeric ();
882843 uint16_t outputDevice = getOutput (codec_cfg.output_device );
883844
884- auto i2c = pins.getI2CPins (PinFunction::CODEC);
885- if (!i2c) {
886- AD_LOGE (" i2c pins not defined" );
887- return false ;
888- }
889-
890845 return wm8994_Init (deviceAddr, outputDevice, vol, freq,
891- i2c. value (). p_wire ) == 0 ;
846+ getI2C () ) == 0 ;
892847 }
893848
894849 bool setMute (bool mute) {
0 commit comments