Skip to content

Commit 4d39e30

Browse files
committed
add methods for % and ration; max value macro; change default ID macro to SHOUTING
1 parent dd096ca commit 4d39e30

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/sfeTk/sfeDevSoilMoisture.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ uint16_t sfeDevSoilMoisture::readMoistureValue(void)
6565
return value;
6666
}
6767

68+
//----------------------------------------------------------------------------------------
69+
// Returns the moisture ratio from the sensor (0 - 1.0)
70+
float sfeDevSoilMoisture::readMoistureRatio(void)
71+
{
72+
if (_theBus == nullptr)
73+
return 0.0;
74+
75+
return (((float)SFE_SOIL_MOISTURE_MAX_VALUE - (float)readMoistureValue()) / (float)SFE_SOIL_MOISTURE_MAX_VALUE);
76+
}
77+
78+
//----------------------------------------------------------------------------------------
79+
// Returns the moisture percentage from the sensor (0 - 100%)
80+
float sfeDevSoilMoisture::readMoisturePercentage(void)
81+
{
82+
return readMoistureRatio() * 100.0;
83+
}
6884
//----------------------------------------------------------------------------------------
6985
sfeTkError_t sfeDevSoilMoisture::changeSensorAddress(uint8_t newAddress)
7086
{

src/sfeTk/sfeDevSoilMoisture.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
#include "sfeTk/sfeTkII2C.h"
1616
#include <sfeTk/sfeToolKit.h>
1717

18-
#define kSfeDevSoilMoistureDefaultI2CAddress 0x28
18+
// Default I2C address for the sensor
19+
#define SFE_SOIL_MOISTURE_DEFAULT_I2C_ADDRESS 0x28
20+
21+
// Max value for the sensor value (10-bit ADC) 2^10 = 1024-1
22+
#define SFE_SOIL_MOISTURE_MAX_VALUE 1023
1923

2024
class sfeDevSoilMoisture
2125
{
@@ -43,6 +47,14 @@ class sfeDevSoilMoisture
4347
/// @return The moisture value - a resistance reading between 0 and 1023
4448
uint16_t readMoistureValue(void);
4549

50+
/// @brief Reads the sensor value and returns the moisture ratio from the sensor (0 - 1.0)
51+
/// @return The moisture ratio
52+
float readMoistureRatio(void);
53+
54+
/// @brief Reads the sensor value and returns the moisture percentage from the sensor (0 - 100%)
55+
/// @return The moisture percentage
56+
float readMoisturePercentage(void);
57+
4658
/// @brief Changes the I2C address of the sensor
4759
/// @param newAddress The new I2C address to assign to the sensor
4860
/// @return kSTkErrOk if successful, otherwise an error code

0 commit comments

Comments
 (0)