@@ -406,15 +406,16 @@ I2cController::~I2cController() {
406
406
*/
407
407
/* **********************************************************************/
408
408
bool I2cController::RemoveDriver (uint32_t address) {
409
- for (drvBase* driver : _i2c_drivers) {
409
+ for (drvBase * driver : _i2c_drivers) {
410
410
if (driver == nullptr )
411
411
continue ;
412
412
413
413
if (driver->GetAddress () != address)
414
414
continue ;
415
415
416
416
delete driver;
417
- _i2c_drivers.erase (std::find (_i2c_drivers.begin (), _i2c_drivers.end (), driver));
417
+ _i2c_drivers.erase (
418
+ std::find (_i2c_drivers.begin (), _i2c_drivers.end (), driver));
418
419
return true ;
419
420
}
420
421
return false ;
@@ -504,29 +505,63 @@ bool I2cController::Handle_I2cDeviceRemove(pb_istream_t *stream) {
504
505
return false ;
505
506
}
506
507
508
+ bool did_remove = true ;
509
+
507
510
// Check for default bus
508
511
if (strlen (msgRemove->i2c_device_description .i2c_bus_scl ) == 0 &&
509
512
strlen (msgRemove->i2c_device_description .i2c_bus_sda ) == 0 ) {
510
513
WS_DEBUG_PRINTLN (" [i2c] Removing device from default bus..." );
511
514
if (!_i2c_bus_default->HasMux ()) {
512
515
// TODO: Implement remove, straightforward
513
516
if (!RemoveDriver (msgRemove->i2c_device_description .i2c_device_address )) {
514
- WS_DEBUG_PRINTLN (" [i2c] ERROR: Failed to remove i2c device from default bus!" );
515
- return false ;
517
+ WS_DEBUG_PRINTLN (
518
+ " [i2c] ERROR: Failed to remove i2c device from default bus!" );
519
+ did_remove = false ;
516
520
}
517
521
} else {
518
522
// Bus has a I2C MUX attached
519
523
// Case 1: Is the I2C device connected to a MUX?
520
- if (msgRemove->i2c_device_description .i2c_mux_address != 0xFFFF && msgRemove->i2c_device_description .i2c_mux_channel >= 0 ) {
524
+ if (msgRemove->i2c_device_description .i2c_mux_address != 0xFFFF &&
525
+ msgRemove->i2c_device_description .i2c_mux_channel >= 0 ) {
521
526
// TODO: Remove the device from the mux's channel and delete the driver
527
+ _i2c_bus_default->SelectMuxChannel (
528
+ msgRemove->i2c_device_description .i2c_mux_channel );
529
+ if (!RemoveDriver (
530
+ msgRemove->i2c_device_description .i2c_device_address )) {
531
+ WS_DEBUG_PRINTLN (
532
+ " [i2c] ERROR: Failed to remove i2c device from default bus!" );
533
+ did_remove = false ;
534
+ }
522
535
}
523
536
// Case 2: Is the I2C device a MUX?
524
- if (msgRemove->i2c_device_description .i2c_device_address == msgRemove->i2c_device_description .i2c_mux_address ) {
525
- // TODO: Remove the MUX from the i2c bus
537
+ if (msgRemove->i2c_device_description .i2c_device_address ==
538
+ msgRemove->i2c_device_description .i2c_mux_address ) {
539
+ // TODO: Scan the mux to see what drivers are attached?
540
+ wippersnapper_i2c_I2cBusScanned scan_results;
541
+ _i2c_bus_default->ScanMux (&scan_results);
542
+ // DEBUG - TODO REMOVE - Print out the scan results
543
+ for (int i = 0 ; i < scan_results.i2c_bus_found_devices_count ; i++) {
544
+ WS_DEBUG_PRINT (" [i2c] Found device at address: " );
545
+ WS_DEBUG_PRINT (
546
+ scan_results.i2c_bus_found_devices [i].i2c_device_address , HEX);
547
+ WS_DEBUG_PRINT (" on mux channel #: " );
548
+ WS_DEBUG_PRINTLN (
549
+ scan_results.i2c_bus_found_devices [i].i2c_mux_channel );
550
+ // Select the channel and remove the device
551
+ _i2c_bus_default->SelectMuxChannel (
552
+ scan_results.i2c_bus_found_devices [i].i2c_mux_channel );
553
+ RemoveDriver (
554
+ scan_results.i2c_bus_found_devices [i].i2c_device_address );
555
+ }
556
+ _i2c_bus_default->RemoveMux ();
526
557
}
527
558
}
528
559
}
529
560
561
+ // TODO: Check for Alt. I2C Bus
562
+
563
+ // Publush with did_remove to the response
564
+
530
565
return true ;
531
566
}
532
567
0 commit comments