Y SI / lib_SHT25

Dependents:   lib_SHT25_example

Revision:
2:34e14b4cd981
Parent:
1:ebd95757fba4
Child:
3:81324c19d870
diff -r ebd95757fba4 -r 34e14b4cd981 lib_SHT25.cpp
--- a/lib_SHT25.cpp	Mon Jun 18 08:28:52 2018 +0000
+++ b/lib_SHT25.cpp	Mon Jun 18 12:10:49 2018 +0000
@@ -33,8 +33,8 @@
 
 SHT25::SHT25(PinName sda, PinName scl) : _i2c(sda, scl)
 {
+    wait_ms(SHT_WAIT_SET);
     softReset();
-    wait_ms(100);
     setPrecision(SHT_PREC_1214);
     _selfHeat = true;
 }
@@ -45,7 +45,7 @@
     {
         _selfHeat = false;
         _t.attach(callback(this, &SHT25::keepSelf), SHT_SELF_HEATING);
-        return readTemperature();
+        return _temperature = readTemperature();
     }
     return _temperature;
 }
@@ -54,11 +54,13 @@
 {
     char command[1] = {SHT_TRIG_TEMP}, rx[3] = {0xFF, 0xFF, 0xFF};
     
-    _i2c.write(SHT_I2C_ADDR, command, 1, false);
-    wait_ms(85);
-    _i2c.read(SHT_I2C_ADDR, rx, 3, false);
-    
-    return _temperature = -46.85 + 175.72 * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0);
+    if(!_i2c.write(SHT_I2C_ADDR, command, 1, false))
+    {
+        wait_ms(SHT_WAIT_TEMP);
+        if(!_i2c.read(SHT_I2C_ADDR, rx, 3, false))
+            return -46.85 + 175.72 * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0);
+    }
+    return NAN;
 }
 
 float SHT25::getHumidity(void)
@@ -67,7 +69,7 @@
     {
         _selfHeat = false;
         _t.attach(callback(this, &SHT25::keepSelf), SHT_SELF_HEATING);
-        return readHumidity();
+        return _humidity = readHumidity();
     }
     return _humidity;
 }
@@ -76,11 +78,13 @@
 {
     char command[1] = {SHT_TRIG_RH}, rx[3] = {0xFF, 0xFF, 0xFF};
     
-    _i2c.write(SHT_I2C_ADDR, command, 1, false);
-    wait_ms(29);
-    _i2c.read(SHT_I2C_ADDR, rx, 3, false);
-    
-    return _humidity = -6.0 + 125.0 * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0);
+    if(!_i2c.write(SHT_I2C_ADDR, command, 1, false))
+    {
+        wait_ms(SHT_WAIT_RH);
+        if(!_i2c.read(SHT_I2C_ADDR, rx, 3, false))
+            return -6.0 + 125.0 * ((((rx[0] << 8) | rx[1]) & 0xFFFC) / 65536.0);
+    }
+    return NAN;
 }
 
 void SHT25::getData(float *tempC, float *relHumidity)
@@ -91,32 +95,41 @@
         _t.attach(callback(this, &SHT25::keepSelf), SHT_SELF_HEATING);
         readData(tempC, relHumidity);
     }
+    else
+    {
+        *tempC = _temperature;
+        *relHumidity = _humidity;
+    }
 }
 
 void SHT25::readData(float *tempC, float *relHumidity)
 {
-    *tempC = readTemperature();
-    *relHumidity = readHumidity();
+    *tempC = _temperature = readTemperature();
+    *relHumidity = _humidity = readHumidity();
 }
 
-int SHT25::setPrecision(char precision)
+bool SHT25::setPrecision(char precision)
 {
     char command[2] = {SHT_WRITE_REG, precision};
 
-    return _i2c.write(SHT_I2C_ADDR, command, 2, false);
+    if(!_i2c.write(SHT_I2C_ADDR, command, 2, false))
+    {
+        wait_ms(SHT_WAIT_SET);
+        return true;
+    }
+    return false;
 }
 
 bool SHT25::softReset()
 {
     char command[1] = {SHT_SOFT_RESET};
     
-    if (_i2c.write(SHT_I2C_ADDR, command, 1, false) != 0)
+    if (!_i2c.write(SHT_I2C_ADDR, command, 1, false))
     {
-        wait_ms(15);
-        return false;
+        wait_ms(SHT_WAIT_SET);
+        return true;
     }
-    
-    return true;
+    return false;
 }
 
 void SHT25::keepSelf(void)