Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: lib_SHT25.cpp
- Revision:
- 3:81324c19d870
- Parent:
- 2:34e14b4cd981
- Child:
- 4:21a50575cb79
--- a/lib_SHT25.cpp Mon Jun 18 12:10:49 2018 +0000
+++ b/lib_SHT25.cpp Tue Dec 11 13:09:18 2018 +0000
@@ -12,16 +12,20 @@
* #include "lib_SHT25.h"
*
* Serial pc(USBTX, USBRX);
-* SHT25 sensor(p28, p27);
+* SHT25 sensor(I2C_SDA, I2C_SCL);
*
* int main()
* {
* float temperature, humidity;
* while(1)
* {
+* temperature = sensor.getTemperature();
+* humidity = sensor.getTemperature();
+* pc.printf("\r\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
+* sensor.waitSafeHeat();
* sensor.getData(&temperature, &humidity);
-* pc.printf("\rtemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
-* wait(0.5);
+* pc.printf("\ntemperature = %6.2f%cC -|- humidity = %6.2f%%RH", temperature, 248, humidity);
+* sensor.waitSafeHeat();
* }
* }
* @endcode
@@ -36,15 +40,16 @@
wait_ms(SHT_WAIT_SET);
softReset();
setPrecision(SHT_PREC_1214);
- _selfHeat = true;
+ _temperature = _humidity = NAN;
+ _selfHeatTemperature = _selfHeatHumidity = true;
}
float SHT25::getTemperature(void)
{
- if(_selfHeat)
+ if(_selfHeatTemperature)
{
- _selfHeat = false;
- _t.attach(callback(this, &SHT25::keepSelf), SHT_SELF_HEATING);
+ _selfHeatTemperature = false;
+ _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
return _temperature = readTemperature();
}
return _temperature;
@@ -65,10 +70,10 @@
float SHT25::getHumidity(void)
{
- if(_selfHeat)
+ if(_selfHeatHumidity)
{
- _selfHeat = false;
- _t.attach(callback(this, &SHT25::keepSelf), SHT_SELF_HEATING);
+ _selfHeatHumidity = false;
+ _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING);
return _humidity = readHumidity();
}
return _humidity;
@@ -89,10 +94,11 @@
void SHT25::getData(float *tempC, float *relHumidity)
{
- if(_selfHeat)
+ if(_selfHeatTemperature && _selfHeatHumidity)
{
- _selfHeat = false;
- _t.attach(callback(this, &SHT25::keepSelf), SHT_SELF_HEATING);
+ _selfHeatTemperature = _selfHeatHumidity = false;
+ _t.attach(callback(this, &SHT25::keepSafeTemperature), SHT_SELF_HEATING);
+ _h.attach(callback(this, &SHT25::keepSafeHumidity), SHT_SELF_HEATING);
readData(tempC, relHumidity);
}
else
@@ -132,7 +138,18 @@
return false;
}
-void SHT25::keepSelf(void)
+void SHT25::waitSafeHeat(void)
+{
+ while(!_selfHeatTemperature || !_selfHeatHumidity)
+ wait_ms(SHT_WAIT_SET);
+}
+
+void SHT25::keepSafeTemperature(void)
{
- _selfHeat = true;
+ _selfHeatTemperature = true;
+}
+
+void SHT25::keepSafeHumidity(void)
+{
+ _selfHeatHumidity = true;
}
\ No newline at end of file