Skip to content

Commit dfc54b6

Browse files
committed
setting address for I2C now working
1 parent ef92174 commit dfc54b6

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/sfeTk/sfeDevSoilMoisture.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/sfeTk/sfeDevSoilMoisture.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
// include the sparkfun toolkit headers
1717
#include <sfeTk/sfeToolKit.h>
1818

19+
// Bus interfaces
20+
#include <sfeTk/sfeTkII2C.h>
21+
#include <sfeTk/sfeTkISPI.h>
22+
1923
// Default I2C address for the sensor
2024
#define SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS 0x28
2125

@@ -59,7 +63,12 @@ class sfeDevSoilMoisture
5963
/// @brief Changes the I2C address of the sensor
6064
/// @param newAddress The new I2C address to assign to the sensor
6165
/// @return kSTkErrOk if successful, otherwise an error code
62-
sfeTkError_t changeSensorAddress(uint8_t newAddress);
66+
/// @note If communicating via I2C, the provided address is used for future communication
67+
sfeTkError_t setI2CAddress(uint8_t newAddress);
68+
69+
/// @brief Returns the current address of the sensor. I2C - bus address, SPI - CS Pin
70+
// @return The current I2C address of the sensor, or the CS pin for SPI
71+
uint8_t address(void);
6372

6473
protected:
6574
// The toolkit bus the sensor is connected to

0 commit comments

Comments
 (0)