Update sht3x

This commit is contained in:
Phat Nguyen
2024-02-07 09:18:02 +07:00
parent 0354c6e634
commit 05594441b8
3 changed files with 38 additions and 49 deletions

View File

@ -35,26 +35,6 @@ bool Sht3x::boardSupported(void) {
return true;
}
/**
* @brief Get temperature and humidity data
*
* @param temp Tempreature read out
* @param hum Humidity read out
* @return true Success
* @return false Failure
*/
bool Sht3x::measure(float &temp, float &hum) {
if (isBegin() == false) {
return false;
}
if (sht3x()->measureSingleShot(REPEATABILITY_MEDIUM, false, temp, hum) ==
NO_ERROR) {
return true;
}
return false;
}
/**
* @brief Construct a new Sht 3x:: Sht 3x object
*
@ -106,8 +86,15 @@ bool Sht3x::begin(TwoWire &wire) {
/** Create sensor and init */
_sensor = new SensirionI2cSht3x();
sht3x()->begin(wire, SHT30_I2C_ADDR_44);
if (sht3x()->softReset() != NO_ERROR) {
AgLog("Reset sensor fail, look like sensor is not on I2C bus");
sht3x()->stopMeasurement();
delay(1);
sht3x()->softReset();
delay(100);
uint16_t statusRegister = 0;
uint16_t err = sht3x()->readStatusRegister(statusRegister);
if (err != NO_ERROR) {
AgLog("Read status register invalid");
return false;
}
@ -139,25 +126,30 @@ void Sht3x::end(void) {
* @return float value <= 256.0f is invalid, that mean sensor has issue or
* communication to sensor not worked as well
*/
float Sht3x::getTemperature(void) {
float temp;
float hum;
if (measure(temp, hum)) {
return temp;
}
return -256.0f;
}
float Sht3x::getTemperature(void) { return temp; }
/**
* @brief Get humidity
*
* @return float Percent(0 - 100), value < 0 is invalid.
*/
float Sht3x::getRelativeHumidity(void) {
float temp;
float hum;
if (measure(temp, hum)) {
return hum;
float Sht3x::getRelativeHumidity(void) { return hum; }
/**
* @brief Measure temperature and humidity. Must call @getTemperature and
* @getRelativeHumidity after this function return success
*
* @return true Success
* @return false Failure
*/
bool Sht3x::measure(void) {
if (isBegin() == false) {
return false;
}
return -1.0f;
if (sht3x()->measureSingleShot(REPEATABILITY_MEDIUM, false, temp, hum) ==
NO_ERROR) {
return true;
}
return false;
}

View File

@ -20,10 +20,11 @@ private:
const char *TAG = "SHT3x";
#else
#endif
float temp;
float hum;
bool isBegin(void);
bool boardSupported(void);
bool measure(float &temp, float &hum);
public:
Sht3x(BoardType type);
@ -35,6 +36,7 @@ public:
#endif
bool begin(TwoWire &wire);
void end(void);
bool measure(void);
float getTemperature(void);
float getRelativeHumidity(void);
};