A simple example adopting SHT21 Arduino library to mbed.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
odtulumbedder
Date:
Wed Sep 13 13:36:04 2017 +0000
Parent:
0:062188c0bb61
Commit message:
file order fixed.

Changed in this revision

SHT21.cpp Show annotated file Show diff for this revision Revisions of this file
SHT21.hpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 062188c0bb61 -r f4a1942b4486 SHT21.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SHT21.cpp	Wed Sep 13 13:36:04 2017 +0000
@@ -0,0 +1,102 @@
+#include "SHT21.hpp"
+
+const uint16_t SHT21::POLYNOMIAL = 0x131;
+
+SHT21::SHT21(PinName sda, PinName scl, char addr) :
+    addr_(addr),
+    pi2c_(new I2C(sda, scl)),
+    i2c_(*pi2c_) {
+}
+
+SHT21::SHT21(I2C &i2c, char addr) :
+    addr_(addr),
+    pi2c_(NULL),
+    i2c_(i2c) {
+}
+
+float SHT21::temperature() {
+    uint16_t r = readRaw(TRIGGER_T_MEASUREMENT_HM);
+    r &= ~0x0003;
+    return (-46.85 + 175.72/65536 * (float) r);
+}
+
+float SHT21::humidity() {
+    uint16_t r = readRaw(TRIGGER_RH_MEASUREMENT_HM);
+    r &= ~0x0003;
+    return (-6.0 + 125.0/65536 * (float) r);
+}
+
+void SHT21::reset() {
+    uint8_t pcmd [] = { SOFT_RESET };
+    i2c_.write(addr_, (const char *) pcmd, 1);
+    wait_ms(15);
+}
+
+void SHT21::serialNumber(uint8_t *serialNumber) {
+    uint8_t data[8];
+    uint8_t pcmd0[2] = { 0xFA, 0x0F };
+    uint8_t pcmd1[2] = { 0xFC, 0xC9 };
+    uint8_t i = 0;
+    i2c_.write(addr_, (const char *) pcmd0, 2);
+    i2c_.read(addr_, (char *) data, 8);
+    
+    serialNumber[5] = data[i++];
+    i++;
+    serialNumber[4] = data[i++];
+    i++;
+    serialNumber[3] = data[i++];
+    i++;
+    serialNumber[2] = data[i++];
+    i = 0;
+    
+    i2c_.write(addr_, (const char *) pcmd1, 2);
+    i2c_.read(addr_, (char *) data, 6);
+    
+    serialNumber[1] = data[i++];
+    serialNumber[0] = data[i++];
+    i++;
+    serialNumber[7] = data[i++];
+    serialNumber[8] = data[i++];
+    i = 0;
+}
+
+SHT21::~SHT21() {
+    if (pi2c_) delete pi2c_;
+}
+
+uint16_t SHT21::readRaw(uint8_t cmd) {
+    uint8_t checksum;
+    uint8_t data[2];
+    uint16_t result;
+
+    uint8_t pcmd [] = { cmd };
+
+    i2c_.write(addr_, (const char *) pcmd, 1);
+    i2c_.read(addr_, (char *) data, 2);
+    i2c_.read(addr_, (char *) &checksum, 1);
+
+    result =    data[0] << 8;
+    result +=   data[1];
+
+    if (crcChecksum(data, 2, checksum)) {
+        reset();
+        return 1;
+    }
+    return result;
+}
+
+bool SHT21::crcChecksum(const uint8_t *data, uint8_t sz, uint8_t checksum) {
+    uint8_t crc = 0;
+    
+    for (uint8_t i = 0; i < sz; ++i) {
+        crc ^= data[i];
+        for (uint8_t j /* bit index */ = 8; j > 0; --j) {
+            if (crc & 0x80)
+                crc = (crc << 1) ^ POLYNOMIAL;
+            else
+                crc = (crc << 1);
+        }
+    }
+    
+    return crc == checksum;
+}
diff -r 062188c0bb61 -r f4a1942b4486 SHT21.hpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SHT21.hpp	Wed Sep 13 13:36:04 2017 +0000
@@ -0,0 +1,39 @@
+#ifndef SHT21_HPP_INCLUDED
+#define  SHT21_HPP_INCLUDED
+
+#include "mbed.h"
+
+class SHT21 {
+public:
+    SHT21(PinName sda, PinName scl, char addr = (0x40 << 1));
+    SHT21(I2C &i2c,  char addr = (0x40 << 1));
+    
+    float temperature();
+    float humidity();
+    
+    void reset();
+    void serialNumber(uint8_t *serialNumber);
+    
+    ~SHT21();
+private:
+    enum commands {
+        TRIGGER_T_MEASUREMENT_HM    = 0xE3,
+        TRIGGER_RH_MEASUREMENT_HM   = 0xE5,
+        TRIGGER_T_MEASUREMENT_NHM   = 0xF3,
+        TRIGGER_RH_MEASUREMENT_NHM  = 0xF5,
+        USER_REGISTER_W             = 0xE6,
+        USER_REGISTER_R             = 0xE7,
+        SOFT_RESET                  = 0xFE
+    };
+
+    static const uint16_t POLYNOMIAL;
+
+    char addr_;
+    I2C *pi2c_;
+    I2C &i2c_;
+    
+    uint16_t readRaw(uint8_t cmd);
+    static bool crcChecksum(const uint8_t *data, uint8_t sz, uint8_t checksum);
+};
+
+#endif // SHT21_HPP_INCLUDED
diff -r 062188c0bb61 -r f4a1942b4486 main.cpp
--- a/main.cpp	Thu Aug 10 13:25:37 2017 +0000
+++ b/main.cpp	Wed Sep 13 13:36:04 2017 +0000
@@ -1,143 +1,5 @@
 #include "mbed.h"
-
-/**
- * A simple library for SHT21 humidity and temperature sensor.
- * Based on the Arduino library.
- */
-
-class SHT21 {
-public:
-    SHT21(PinName sda, PinName scl, char addr = (0x40 << 1));
-    SHT21(I2C &i2c,  char addr = (0x40 << 1));
-    
-    float temperature();
-    float humidity();
-    
-    void reset();
-    void serialNumber(uint8_t *serialNumber);
-    
-    ~SHT21();
-private:
-    enum commands {
-        TRIGGER_T_MEASUREMENT_HM    = 0xE3,
-        TRIGGER_RH_MEASUREMENT_HM   = 0xE5,
-        TRIGGER_T_MEASUREMENT_NHM   = 0xF3,
-        TRIGGER_RH_MEASUREMENT_NHM  = 0xF5,
-        USER_REGISTER_W             = 0xE6,
-        USER_REGISTER_R             = 0xE7,
-        SOFT_RESET                  = 0xFE
-    };
-
-    static const uint16_t POLYNOMIAL;
-
-    char addr_;
-    I2C *pi2c_;
-    I2C &i2c_;
-    
-    uint16_t readRaw(uint8_t cmd);
-    static bool crcChecksum(const uint8_t *data, uint8_t sz, uint8_t checksum);
-};
-
-const uint16_t SHT21::POLYNOMIAL = 0x131;
-
-SHT21::SHT21(PinName sda, PinName scl, char addr) :
-    addr_(addr),
-    pi2c_(new I2C(sda, scl)),
-    i2c_(*pi2c_) {
-}
-
-SHT21::SHT21(I2C &i2c, char addr) :
-    addr_(addr),
-    pi2c_(NULL),
-    i2c_(i2c) {
-}
-
-float SHT21::temperature() {
-    uint16_t r = readRaw(TRIGGER_T_MEASUREMENT_HM);
-    r &= ~0x0003;
-    return (-46.85 + 175.72/65536 * (float) r);
-}
-
-float SHT21::humidity() {
-    uint16_t r = readRaw(TRIGGER_RH_MEASUREMENT_HM);
-    r &= ~0x0003;
-    return (-6.0 + 125.0/65536 * (float) r);
-}
-
-void SHT21::reset() {
-    uint8_t pcmd [] = { SOFT_RESET };
-    i2c_.write(addr_, (const char *) pcmd, 1);
-    wait_ms(15);
-}
-
-void SHT21::serialNumber(uint8_t *serialNumber) {
-    uint8_t data[8];
-    uint8_t pcmd0[2] = { 0xFA, 0x0F };
-    uint8_t pcmd1[2] = { 0xFC, 0xC9 };
-    uint8_t i = 0;
-    i2c_.write(addr_, (const char *) pcmd0, 2);
-    i2c_.read(addr_, (char *) data, 8);
-    
-    serialNumber[5] = data[i++];
-    i++;
-    serialNumber[4] = data[i++];
-    i++;
-    serialNumber[3] = data[i++];
-    i++;
-    serialNumber[2] = data[i++];
-    i = 0;
-    
-    i2c_.write(addr_, (const char *) pcmd1, 2);
-    i2c_.read(addr_, (char *) data, 6);
-    
-    serialNumber[1] = data[i++];
-    serialNumber[0] = data[i++];
-    i++;
-    serialNumber[7] = data[i++];
-    serialNumber[8] = data[i++];
-    i = 0;
-}
-
-SHT21::~SHT21() {
-    if (pi2c_) delete pi2c_;
-}
-
-uint16_t SHT21::readRaw(uint8_t cmd) {
-    uint8_t checksum;
-    uint8_t data[2];
-    uint16_t result;
-
-    uint8_t pcmd [] = { cmd };
-
-    i2c_.write(addr_, (const char *) pcmd, 1);
-    i2c_.read(addr_, (char *) data, 2);
-    i2c_.read(addr_, (char *) &checksum, 1);
-
-    result =    data[0] << 8;
-    result +=   data[1];
-
-    if (crcChecksum(data, 2, checksum)) {
-        reset();
-        return 1;
-    }
-    return result;
-}
-
-bool SHT21::crcChecksum(const uint8_t *data, uint8_t sz, uint8_t checksum) {
-    uint8_t crc = 0;
-    
-    for (uint8_t i = 0; i < sz; ++i) {
-        crc ^= data[i];
-        for (uint8_t j /* bit index */ = 8; j > 0; --j) {
-            if (crc & 0x80)
-                crc = (crc << 1) ^ POLYNOMIAL;
-            else
-                crc = (crc << 1);
-        }
-    }
-    
-    return crc == checksum;
-}
+#include "SHT21.hpp"
 
 SHT21 sht21(PB_9 /* I2C_SDA */, PB_8 /* I2C_SCL */);
 Serial pc(USBTX, USBRX);