@@ -88,7 +88,7 @@ float sfeDevSoilMoisture::readMoisturePercentage(void)
8888}
8989// ----------------------------------------------------------------------------------------
9090// Change the I2C address of the sensor
91- sfeTkError_t sfeDevSoilMoisture::changeSensorAddress (uint8_t newAddress)
91+ sfeTkError_t sfeDevSoilMoisture::setI2CAddress (uint8_t newAddress)
9292{
9393 if (_theBus == nullptr )
9494 return kSTkErrFail ;
@@ -97,6 +97,28 @@ sfeTkError_t sfeDevSoilMoisture::changeSensorAddress(uint8_t newAddress)
9797 if (newAddress < 0x07 || newAddress > 0x78 )
9898 return kSTkErrFail ;
9999
100- // Send the command to change the address
101- return _theBus->writeRegisterByte (kCommandChangeAddress , newAddress);
100+ // If in I2C mode, is the address the same as the current address?
101+ if (_theBus->type () == kBusTypeI2C && ((sfeTkII2C *)_theBus)->address () == newAddress)
102+ return kSTkErrOk ;
103+
104+ // Send the command to change the address. NOTE: Because of how the sensor works,
105+ // the following will return an error (since the sensor side resets the bus)
106+ (void ) _theBus->writeRegisterByte (kCommandChangeAddress , newAddress);
107+
108+ return kSTkErrOk ;
109+ }
110+ // ----------------------------------------------------------------------------------------
111+ // Return the address of the sensor bus. For I2C this is the address of the sensor, for
112+ // SPI this is the CS pin
113+ uint8_t sfeDevSoilMoisture::address (void )
114+ {
115+ if (_theBus == nullptr )
116+ return 0 ;
117+
118+ if (_theBus->type () == kBusTypeSPI )
119+ return ((sfeTkISPI *)_theBus)->cs ();
120+ else if (_theBus->type () == kBusTypeI2C )
121+ return ((sfeTkII2C *)_theBus)->address ();
122+
123+ return 0 ;
102124}
0 commit comments