@@ -455,42 +455,50 @@ I2cController::~I2cController() {
455
455
@brief Removes an I2C driver from the controller and frees memory
456
456
@param address
457
457
The desired I2C device's address.
458
+ @param is_output_device
459
+ True if the driver is an output device, False otherwise.
458
460
@returns True if the driver was removed, False otherwise.
459
461
*/
460
462
/* **********************************************************************/
461
- bool I2cController::RemoveDriver (uint32_t address) {
462
- // Safely remove the i2c sensor driver from the vector and free memory
463
- for (drvBase *driver : _i2c_drivers) {
464
- if (driver == nullptr )
465
- continue ;
463
+ bool I2cController::RemoveDriver (uint32_t address, bool is_output_device) {
464
+ if (!is_output_device) {
465
+ // Safely remove the i2c sensor driver from the vector and free memory
466
+ for (drvBase *driver : _i2c_drivers) {
467
+ if (driver == nullptr )
468
+ continue ;
466
469
467
- if (driver->GetAddress () != address)
468
- continue ;
470
+ if (driver->GetAddress () != address)
471
+ continue ;
469
472
470
- auto it = std::find (_i2c_drivers.begin (), _i2c_drivers.end (), driver);
471
- if (it != _i2c_drivers.end ()) {
472
- _i2c_drivers.erase (it);
473
+ auto it = std::find (_i2c_drivers.begin (), _i2c_drivers.end (), driver);
474
+ if (it != _i2c_drivers.end ()) {
475
+ _i2c_drivers.erase (it);
476
+ }
477
+ delete driver;
478
+ return true ;
473
479
}
474
- delete driver;
475
- return true ;
476
- }
477
-
478
- // This was an output driver type, safely remove the i2c output driver from the vector and free memory
479
- for (drvOutputBase *driver : _i2c_drivers_output) {
480
- if (driver == nullptr )
481
- continue ;
480
+ } else {
481
+ // This was an output driver type, safely remove the i2c output driver from
482
+ // the vector and free memory
483
+ for (drvOutputBase *driver : _i2c_drivers_output) {
484
+ if (driver == nullptr )
485
+ continue ;
482
486
483
- if (driver->GetAddress () != address)
484
- continue ;
487
+ if (driver->GetAddress () != address)
488
+ continue ;
485
489
486
- auto it = std::find (_i2c_drivers_output.begin (), _i2c_drivers_output.end (),
487
- driver);
488
- if (it != _i2c_drivers_output.end ()) {
489
- _i2c_drivers_output.erase (it);
490
+ auto it = std::find (_i2c_drivers_output.begin (),
491
+ _i2c_drivers_output.end (), driver);
492
+ if (it != _i2c_drivers_output.end ()) {
493
+ _i2c_drivers_output.erase (it);
494
+ }
495
+ delete driver;
496
+ return true ;
490
497
}
491
- delete driver;
492
- return true ;
493
498
}
499
+
500
+ // We didn't find the driver to remove
501
+ WS_DEBUG_PRINTLN (" [i2c] ERROR: Unable to find driver to remove!" );
494
502
return false ;
495
503
}
496
504
@@ -585,8 +593,8 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
585
593
strlen (msgRemove->i2c_device_description .i2c_bus_sda ) == 0 ) {
586
594
WS_DEBUG_PRINTLN (" [i2c] Removing device from default bus..." );
587
595
if (!_i2c_bus_default->HasMux ()) {
588
- // TODO: Implement remove, straightforward
589
- if (! RemoveDriver ( msgRemove->i2c_device_description . i2c_device_address )) {
596
+ if (! RemoveDriver (msgRemove-> i2c_device_description . i2c_device_address ,
597
+ msgRemove->is_output_device )) {
590
598
WS_DEBUG_PRINTLN (
591
599
" [i2c] ERROR: Failed to remove i2c device from default bus!" );
592
600
did_remove = false ;
@@ -596,11 +604,10 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
596
604
// Case 1: Is the I2C device connected to a MUX?
597
605
if (msgRemove->i2c_device_description .i2c_mux_address != 0xFFFF &&
598
606
msgRemove->i2c_device_description .i2c_mux_channel >= 0 ) {
599
- // TODO: Remove the device from the mux's channel and delete the driver
600
607
_i2c_bus_default->SelectMuxChannel (
601
608
msgRemove->i2c_device_description .i2c_mux_channel );
602
- if (!RemoveDriver (
603
- msgRemove->i2c_device_description . i2c_device_address )) {
609
+ if (!RemoveDriver (msgRemove-> i2c_device_description . i2c_device_address ,
610
+ msgRemove->is_output_device )) {
604
611
WS_DEBUG_PRINTLN (
605
612
" [i2c] ERROR: Failed to remove i2c device from default bus!" );
606
613
did_remove = false ;
@@ -609,16 +616,14 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
609
616
// Case 2: Is the I2C device a MUX?
610
617
if (msgRemove->i2c_device_description .i2c_device_address ==
611
618
msgRemove->i2c_device_description .i2c_mux_address ) {
612
- // TODO: Scan the mux to see what drivers are attached?
613
619
wippersnapper_i2c_I2cBusScanned scan_results;
614
620
_i2c_bus_default->ScanMux (&scan_results);
615
621
for (int i = 0 ; i < scan_results.i2c_bus_found_devices_count ; i++) {
616
- scan_results.i2c_bus_found_devices [i].i2c_mux_channel );
617
622
// Select the channel and remove the device
618
623
_i2c_bus_default->SelectMuxChannel (
619
624
scan_results.i2c_bus_found_devices [i].i2c_mux_channel );
620
- RemoveDriver (
621
- scan_results. i2c_bus_found_devices [i]. i2c_device_address );
625
+ RemoveDriver (scan_results. i2c_bus_found_devices [i]. i2c_device_address ,
626
+ msgRemove-> is_output_device );
622
627
}
623
628
_i2c_bus_default->RemoveMux ();
624
629
}
@@ -786,7 +791,8 @@ bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
786
791
" message!" );
787
792
return false ;
788
793
}
789
- wippersnapper_i2c_I2cDeviceDescriptor descriptor = _i2c_model->GetI2cDeviceOutputWriteMsg ()->i2c_device_description ;
794
+ wippersnapper_i2c_I2cDeviceDescriptor descriptor =
795
+ _i2c_model->GetI2cDeviceOutputWriteMsg ()->i2c_device_description ;
790
796
791
797
// Attempt to find the driver
792
798
drvOutputBase *driver = nullptr ;
@@ -816,8 +822,9 @@ bool I2cController::Handle_I2cDeviceOutputWrite(pb_istream_t *stream) {
816
822
817
823
// Determine which driver cb function to use
818
824
if (_i2c_model->GetI2cDeviceOutputWriteMsg ()->has_led_backpack_write ) {
819
- WS_DEBUG_PRINT (" [i2c] Writing message to LED backpack..." );
820
- if (!driver->LedBackpackWrite (&_i2c_model->GetI2cDeviceOutputWriteMsg ()->led_backpack_write )) {
825
+ WS_DEBUG_PRINTLN (" [i2c] Writing message to LED backpack..." );
826
+ if (!driver->LedBackpackWrite (
827
+ &_i2c_model->GetI2cDeviceOutputWriteMsg ()->led_backpack_write )) {
821
828
WS_DEBUG_PRINTLN (" [i2c] ERROR: Unable to write to LED backpack!" );
822
829
return false ;
823
830
}
@@ -942,12 +949,14 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
942
949
did_init = true ;
943
950
}
944
951
} else {
952
+ WS_DEBUG_PRINT (" [i2c] Creating an I2C output driver..." );
945
953
drv_out = CreateI2cOutputDrv (
946
954
device_name, bus, device_descriptor.i2c_device_address ,
947
955
device_descriptor.i2c_mux_channel , device_status);
948
956
if (drv_out != nullptr ) {
949
957
did_init = true ;
950
958
}
959
+ WS_DEBUG_PRINTLN (" OK!" );
951
960
}
952
961
953
962
if (!did_init) {
@@ -966,6 +975,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
966
975
if (!is_output) {
967
976
drv->SetMuxAddress (device_descriptor.i2c_mux_address );
968
977
} else {
978
+ WS_DEBUG_PRINTLN (" [i2c] Setting MUX address for output driver..." );
969
979
drv_out->SetMuxAddress (device_descriptor.i2c_mux_address );
970
980
}
971
981
WS_DEBUG_PRINTLN (" [i2c] Set driver to use MUX" );
@@ -977,6 +987,7 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
977
987
_i2c_model->GetI2cDeviceAddOrReplaceMsg ()
978
988
->i2c_device_description .i2c_bus_sda );
979
989
} else {
990
+ WS_DEBUG_PRINTLN (" [i2c] Setting alt. I2C bus for output driver..." );
980
991
drv_out->EnableAltI2CBus (_i2c_model->GetI2cDeviceAddOrReplaceMsg ()
981
992
->i2c_device_description .i2c_bus_scl ,
982
993
_i2c_model->GetI2cDeviceAddOrReplaceMsg ()
@@ -995,37 +1006,54 @@ bool I2cController::Handle_I2cDeviceAddOrReplace(pb_istream_t *stream) {
995
1006
drv->SetSensorPeriod (
996
1007
_i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_device_period );
997
1008
} else {
1009
+ WS_DEBUG_PRINTLN (" [i2c] Configuring output driver..." );
998
1010
// Configure Output-driver settings
999
1011
pb_size_t config =
1000
1012
_i2c_model->GetI2cDeviceAddOrReplaceMsg ()->i2c_output_add .which_config ;
1001
1013
if (config ==
1002
1014
wippersnapper_i2c_output_I2cOutputAdd_led_backpack_config_tag) {
1003
- // TODO: Configure LED Backpack
1015
+ WS_DEBUG_PRINTLN ( " [i2c] Configuring LED backpack... " );
1004
1016
wippersnapper_i2c_output_LedBackpackConfig cfg =
1005
1017
_i2c_model->GetI2cDeviceAddOrReplaceMsg ()
1006
1018
->i2c_output_add .config .led_backpack_config ;
1019
+ WS_DEBUG_PRINT (" [i2c] Got cfg, calling ConfigureLEDBackpack..." );
1007
1020
drv_out->ConfigureLEDBackpack (cfg.brightness , cfg.alignment );
1021
+ WS_DEBUG_PRINTLN (" OK!" );
1008
1022
} else if (config ==
1009
1023
wippersnapper_i2c_output_I2cOutputAdd_char_lcd_config_tag) {
1010
- // TODO: Configure Char LCD
1024
+ WS_DEBUG_PRINTLN ( " [i2c] Configuring char LCD... " );
1011
1025
} else {
1012
1026
WS_DEBUG_PRINTLN (
1013
1027
" [i2c] ERROR: Unknown config specified for output driver!" );
1014
1028
return false ;
1015
1029
}
1016
1030
}
1017
1031
1018
- if (!drv->begin ()) {
1019
- if (WsV2._sdCardV2 ->isModeOffline ()) {
1020
- WsV2.haltErrorV2 (" [i2c] Driver failed to initialize!\n\t Did you set "
1021
- " the correct value for i2cDeviceName?\n\t Did you set "
1022
- " the correct value for"
1023
- " i2cDeviceAddress?" ,
1024
- WS_LED_STATUS_ERROR_RUNTIME, false );
1032
+ if (!is_output) {
1033
+ if (!drv->begin ()) {
1034
+ if (WsV2._sdCardV2 ->isModeOffline ()) {
1035
+ WsV2.haltErrorV2 (" [i2c] Driver failed to initialize!\n\t Did you set "
1036
+ " the correct value for i2cDeviceName?\n\t Did you set "
1037
+ " the correct value for"
1038
+ " i2cDeviceAddress?" ,
1039
+ WS_LED_STATUS_ERROR_RUNTIME, false );
1040
+ }
1041
+ }
1042
+ _i2c_drivers.push_back (drv);
1043
+ } else {
1044
+ if (!drv_out->begin ()) {
1045
+ if (WsV2._sdCardV2 ->isModeOffline ()) {
1046
+ WsV2.haltErrorV2 (" [i2c] Driver failed to initialize!\n\t Did you set "
1047
+ " the correct value for i2cDeviceName?\n\t Did you set "
1048
+ " the correct value for"
1049
+ " i2cDeviceAddress?" ,
1050
+ WS_LED_STATUS_ERROR_RUNTIME, false );
1051
+ }
1025
1052
}
1053
+ _i2c_drivers_output.push_back (drv_out);
1026
1054
}
1027
1055
1028
- _i2c_drivers. push_back (drv);
1056
+
1029
1057
WS_DEBUG_PRINTLN (" [i2c] Driver initialized and added to controller: " );
1030
1058
WS_DEBUG_PRINTLN (device_name);
1031
1059
0 commit comments