little modification for best utilization

Fork of Sht31 by Andrea Corrado

Files at this revision

API Documentation at this revision

Comitter:
osilvam
Date:
Wed Oct 03 13:51:56 2018 +0000
Parent:
2:c84a60326ecf
Commit message:
first commit

Changed in this revision

Sht31.cpp Show annotated file Show diff for this revision Revisions of this file
Sht31.h Show annotated file Show diff for this revision Revisions of this file
diff -r c84a60326ecf -r c4bd8cd5d566 Sht31.cpp
--- a/Sht31.cpp	Mon Sep 11 13:51:03 2017 +0000
+++ b/Sht31.cpp	Wed Oct 03 13:51:56 2018 +0000
@@ -17,8 +17,8 @@
 #include "Sht31.h"
 #include "mbed.h"
 
-Sht31::Sht31(PinName sda, PinName scl) : _i2c(sda, scl) {
-    _i2caddr = (0x44 << 1);
+Sht31::Sht31(PinName sda, PinName scl, bool i2caddr) : _i2c(sda, scl) {
+    _i2caddr = !i2caddr ? (0x44 << 1) : (0x45 << 1);
     reset();
     readStatus();
 }
@@ -97,6 +97,50 @@
     return true;
 }
 
+
+bool Sht31::readTempHum(float &_temp, float &_humidity) {
+    char readbuffer[6];
+
+    writeCommand(SHT31_MEAS_HIGHREP);
+
+    wait_ms(500);
+    _i2c.read(_i2caddr, readbuffer, 6);
+    
+    uint16_t ST, SRH;
+    ST = readbuffer[0];
+    ST <<= 8;
+    ST |= readbuffer[1];
+
+    if (readbuffer[2] != crc8((uint8_t *) readbuffer, 2)) {
+        return false;
+    }
+
+    SRH = readbuffer[3];
+    SRH <<= 8;
+    SRH |= readbuffer[4];
+
+    if (readbuffer[5] != crc8((uint8_t *) readbuffer+3, 2)) {
+        return false;
+    }
+
+    double stemp = ST;
+    stemp *= 175;
+    stemp /= 0xffff;
+    stemp = -45 + stemp;
+    temp = stemp;
+    _temp = temp;
+
+    double shum = SRH;
+    shum *= 100;
+    shum /= 0xFFFF;
+
+    humidity = shum;
+    _humidity = humidity;
+
+    return true;
+}
+
+
 uint8_t Sht31::crc8(const uint8_t *data, int len) {
     const uint8_t POLYNOMIAL(0x31);
     uint8_t crc(0xFF);
diff -r c84a60326ecf -r c4bd8cd5d566 Sht31.h
--- a/Sht31.h	Mon Sep 11 13:51:03 2017 +0000
+++ b/Sht31.h	Wed Oct 03 13:51:56 2018 +0000
@@ -34,9 +34,10 @@
 
 class Sht31 {
 public:
-    Sht31(PinName sda, PinName scl);
+    Sht31(PinName sda, PinName scl, bool i2caddr);
     float readTemperature(void);
     float readHumidity(void);
+    bool readTempHum(float &_temp, float &_humidity);
 
 private:
     void reset(void);