A library for STMicroelectronics STTS751 I2C temperature sensor

Dependents:   STTS751_Demo

Revision:
3:f9d3008f7e8f
Parent:
2:3116fe4a0079
diff -r 3116fe4a0079 -r f9d3008f7e8f STTS751.cpp
--- a/STTS751.cpp	Sun Jan 19 03:13:38 2014 +0000
+++ b/STTS751.cpp	Wed Feb 05 04:24:36 2014 +0000
@@ -16,21 +16,24 @@
 
 #include "STTS751.h"
 
-#define SET_BITS(x,m,b) (((x) & !(m)) | (b))
+#define SET_BITS(x,m,b) (((x) & ~(m)) | (b))
 
-STTS751::STTS751(PinName sda, PinName scl, Address addr, Model model): _i2c(sda, scl), _addr(addr | model)
-{
+STTS751::STTS751(PinName sda, PinName scl, bool standby, Address addr, Model model) :
+        _i2c(sda, scl), _addr(addr | model), _standby(standby) {
     init();
 }
 
-STTS751::STTS751(I2C &i2c, Address addr, Model model): _i2c(i2c), _addr(addr | model)
-{
+STTS751::STTS751(I2C &i2c, bool standby, Address addr, Model model) :
+        _i2c(i2c), _addr(addr | model), _standby(standby) {
     init();
 }
 
-void STTS751::init()
-{
-    write8(REG_CONFIGURATION, 0x00);
+void STTS751::init() {
+    char conf = 0x00;
+    if (_standby)
+        conf |= CONF_RUNSTOP;
+    write8(REG_CONFIGURATION, conf);
+    // conversion rate = 1/sec
     write8(REG_CONV_RATE, 0x04);
 }
 
@@ -40,25 +43,57 @@
 
 STTS751::Resolution STTS751::resolution() {
     char conf = read8(REG_CONFIGURATION);
-    return (STTS751::Resolution)(conf & RES_MASK);
+    return (STTS751::Resolution) (conf & CONF_RES_MASK);
 }
 
 void STTS751::setResolution(STTS751::Resolution res) {
     char conf = read8(REG_CONFIGURATION);
-    conf = SET_BITS(conf, RES_MASK, res);
+    conf = SET_BITS(conf, CONF_RES_MASK, res);
     write8(REG_CONFIGURATION, conf);
 }
 
-float STTS751::temp()
-{
+int STTS751::conversionRate() {
+    char conv = read8(REG_CONV_RATE);
+    return conv & CONV_RATE_MASK;
+}
+
+void STTS751::setConversionRate(int rate) {
+    write8(REG_CONV_RATE, (char)(rate & CONV_RATE_MASK));
+}
+
+void STTS751::setStandbyMode(bool standby) {
+    _standby = standby;
+    char conf = read8(REG_CONFIGURATION);
+    if (_standby)
+        conf |= CONF_RUNSTOP;
+    else
+        conf &= ~CONF_RUNSTOP;
+    write8(REG_CONFIGURATION, conf);
+}
+
+void STTS751::start() {
+    if (ready())
+        write8(REG_ONESHOT, 1);
+}
+
+bool STTS751::ready() {
+    char status = read8(REG_STATUS);
+    return (status & STATUS_BUSY) == 0;
+}
+
+float STTS751::temp(bool nowait) {
+    if (_standby && !nowait) {
+        start();
+        while (!ready())
+            wait(0.01);
+    }
     signed char h = read8(REG_TEMPERATURE_H);
     unsigned char l = read8(REG_TEMPERATURE_L);
     return ((h << 8) | l) / 256.0;
 }
 
 #ifdef MBED_OPERATORS
-STTS751::operator float()
-{
+STTS751::operator float() {
     return temp();
 }
 #endif