@@ -384,7 +384,7 @@ I2cController::I2cController() {
384384 _i2c_model = new I2cModel ();
385385 // Initialize the default I2C bus
386386 _i2c_bus_default = new I2cHardware ();
387- _i2c_bus_default->InitBus (is_default = true );
387+ _i2c_bus_default->InitBus (true );
388388}
389389
390390/* **********************************************************************/
@@ -494,9 +494,6 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
494494/* **********************************************************************/
495495bool I2cController::InitMux (const char *name, uint32_t address,
496496 bool is_alt_bus) {
497- if ((strcmp (name, " pca9546" ) != 0 ) || (strcmp (name, " pca9548" ) != 0 ))
498- return false ; // bail out, mux not specified as a device_name
499-
500497 WS_DEBUG_PRINTLN (" [i2c] Creating new MUX..." );
501498 if (is_alt_bus) {
502499 if (!_i2c_bus_alt->HasMux ()) {
@@ -576,10 +573,15 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
576573
577574 // I2C MUX (Case #1) - We are creating an I2C mux via the
578575 // I2cDeviceAddorReplace message
579- if (!InitMux (device_name, device_descriptor.i2c_mux_address , use_alt_bus)) {
580- // TODO [Online]: Publish back out to IO here!
581- WsV2.haltErrorV2 (" [i2c] Failed to initialize MUX driver!" ,
582- WS_LED_STATUS_ERROR_RUNTIME, false );
576+ if ((strcmp (device_name, " pca9546" ) == 0 ) ||
577+ (strcmp (device_name, " pca9548" ) == 0 )) {
578+ WS_DEBUG_PRINT (" [i2c] Initializing MUX driver..." );
579+ if (!InitMux (device_name, device_descriptor.i2c_mux_address , use_alt_bus)) {
580+ // TODO [Online]: Publish back out to IO here!
581+ WsV2.haltErrorV2 (" [i2c] Failed to initialize MUX driver!" ,
582+ WS_LED_STATUS_ERROR_RUNTIME, false );
583+ }
584+ WS_DEBUG_PRINTLN (" OK!" );
583585 }
584586
585587 // Mux case #2 - We are creating a new driver that USES THE MUX via
@@ -594,7 +596,8 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
594596 did_set_mux_ch = true ;
595597 }
596598
597- WS_DEBUG_PRINTLN (" Creating a new I2C driver obj" );
599+ WS_DEBUG_PRINTLN (" Creating a new I2C driver" );
600+ // Assign I2C bus
598601 TwoWire *bus = nullptr ;
599602 if (use_alt_bus) {
600603 bus = _i2c_bus_alt->GetBus ();
@@ -605,76 +608,62 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
605608 drvBase *drv = CreateI2CDriverByName (
606609 device_name, bus, device_descriptor.i2c_device_address ,
607610 device_descriptor.i2c_mux_channel , device_status);
608-
609- // TODO: Clean up for clarity - confusing checks and returns
610- if (drv != nullptr ) {
611- WS_DEBUG_PRINTLN (" OK! Configuring sensor types and period..." );
612- drv->EnableSensorReads (
613- _i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_sensor_types ,
614- _i2c_model->GetI2cDeviceAddOrReplaceMsg ()
615- ->i2c_device_sensor_types_count );
616- drv->SetSensorPeriod (
617- _i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_period );
618- if (did_set_mux_ch)
619- drv->SetMuxAddress (device_descriptor.i2c_mux_address );
620- if (use_alt_bus)
621- drv->EnableAltI2CBus (_i2c_model->GetI2cDeviceAddOrReplaceMsg ()
622- ->i2c_device_description .i2c_bus_scl ,
623- _i2c_model->GetI2cDeviceAddOrReplaceMsg ()
624- ->i2c_device_description .i2c_bus_sda );
625-
626- if (drv->begin ()) {
627- _i2c_drivers.push_back (drv);
628- WS_DEBUG_PRINTLN (
629- " [i2c] I2C driver initialized and added to controller: " );
630- WS_DEBUG_PRINTLN (device_name);
631-
632- // If we're using a MUX, clear the channel for any subsequent bus
633- // operations that may not involve the MUX
634- if (did_set_mux_ch) {
635- if (use_alt_bus) {
636- _i2c_bus_alt->ClearMuxChannel ();
637- } else {
638- _i2c_bus_default->ClearMuxChannel ();
639- }
640- }
641- } else {
642- WS_DEBUG_PRINTLN (" [i2c] ERROR: I2C driver failed to initialize!" );
643- device_status =
644- wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_INIT;
645-
646- if (WsV2._sdCardV2 ->isModeOffline ()) {
647- WsV2.haltErrorV2 (" [i2c] Driver failed to initialize!\n\t Did you set "
648- " the correct value for i2cDeviceName?\n\t Did you set "
649- " the correct value for"
650- " i2cDeviceAddress?" ,
651- WS_LED_STATUS_ERROR_RUNTIME, false );
652- }
653- if (!PublishI2cDeviceAddedorReplaced (device_descriptor, device_status))
654- return false ;
655- return true ;
656- }
657- } else {
611+ if (drv == nullptr ) {
658612 WS_DEBUG_PRINTLN (" [i2c] ERROR: I2C driver type not found or unsupported!" );
659- device_status =
660- wippersnapper_i2c_I2cDeviceStatus_I2C_DEVICE_STATUS_FAIL_UNSUPPORTED_SENSOR;
613+ if (WsV2._sdCardV2 ->isModeOffline ()) {
614+ WsV2.haltErrorV2 (" [i2c] Driver failed to initialize!\n\t Did you set "
615+ " the correct value for i2cDeviceName?\n\t Did you set "
616+ " the correct value for"
617+ " i2cDeviceAddress?" ,
618+ WS_LED_STATUS_ERROR_RUNTIME, false );
619+ }
620+ }
661621
622+ // Attempt to initialize the driver
623+ if (did_set_mux_ch)
624+ drv->SetMuxAddress (device_descriptor.i2c_mux_address );
625+ if (use_alt_bus)
626+ drv->EnableAltI2CBus (_i2c_model->GetI2cDeviceAddOrReplaceMsg ()
627+ ->i2c_device_description .i2c_bus_scl ,
628+ _i2c_model->GetI2cDeviceAddOrReplaceMsg ()
629+ ->i2c_device_description .i2c_bus_sda );
630+ if (!drv->begin ()) {
662631 if (WsV2._sdCardV2 ->isModeOffline ()) {
663632 WsV2.haltErrorV2 (" [i2c] Driver failed to initialize!\n\t Did you set "
664633 " the correct value for i2cDeviceName?\n\t Did you set "
665634 " the correct value for"
666635 " i2cDeviceAddress?" ,
667636 WS_LED_STATUS_ERROR_RUNTIME, false );
668637 }
638+ }
639+ // Configure the driver
640+ drv->EnableSensorReads (
641+ _i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_sensor_types ,
642+ _i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_sensor_types_count );
643+ drv->SetSensorPeriod (
644+ _i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_period );
645+ _i2c_drivers.push_back (drv);
646+ WS_DEBUG_PRINTLN (" [i2c] Driver initialized and added to controller: " );
647+ WS_DEBUG_PRINTLN (device_name);
648+
649+ // If we're using a MUX, clear the channel for any subsequent bus
650+ // operations that may not involve the MUX
651+ if (did_set_mux_ch) {
652+ if (use_alt_bus) {
653+ _i2c_bus_alt->ClearMuxChannel ();
654+ } else {
655+ _i2c_bus_default->ClearMuxChannel ();
656+ }
657+ }
669658
670- if (!PublishI2cDeviceAddedorReplaced (device_descriptor, device_status))
671- return false ;
672- return true ;
659+ if (WsV2._sdCardV2 ->isModeOffline () != true ) {
660+ // Create and publish the I2cDeviceAddedorReplaced message to the broker
661+ WS_DEBUG_PRINTLN (" [i2c] MQTT Publish I2cDeviceAddedorReplaced not yet "
662+ " implemented!" );
663+ /* if (!PublishI2cDeviceAddedorReplaced(device_descriptor,
664+ device_status)) return false; */
673665 }
674666
675- // Create and publish the I2cDeviceAddedorReplaced message to the broker
676- if (!PublishI2cDeviceAddedorReplaced (device_descriptor, device_status))
677- return false ;
678667 return true ;
679668}
680669
0 commit comments