grove_temp_hum

Files at this revision

API Documentation at this revision

Comitter:
JackyZhangFromSeeed
Date:
Tue Jun 09 10:19:38 2015 +0000
Commit message:
grove_temp_hum

Changed in this revision

grove_temp_hum.cpp Show annotated file Show diff for this revision Revisions of this file
grove_temp_hum.h Show annotated file Show diff for this revision Revisions of this file
grove_temp_hum_class.cpp Show annotated file Show diff for this revision Revisions of this file
grove_temp_hum_class.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r f0824fc8b273 grove_temp_hum.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/grove_temp_hum.cpp	Tue Jun 09 10:19:38 2015 +0000
@@ -0,0 +1,169 @@
+
+
+#include "suli2.h"
+#include "grove_temp_hum.h"
+
+
+
+//local functions
+static bool _read(IO_T *io);
+static float grove_temp_hum_convertCtoF(float c);
+
+
+
+//local variables
+static uint8_t _type, _count;
+static bool firstreading;
+static unsigned long _lastreadtime;
+static uint8_t data[6];
+
+
+
+void grove_temp_hum_init(IO_T *io, int pin)
+{
+    suli_pin_init(io, pin, SULI_INPUT);
+}
+
+bool grove_temp_hum_write_setup(IO_T *io, uint8_t type, uint8_t count)
+{
+    _type = type;
+    _count = count;
+    firstreading = true;
+    return true;
+}
+static bool _read(IO_T *io)
+{
+  uint8_t laststate = SULI_HIGH;
+  uint8_t counter = 0;
+  uint8_t j = 0, i;
+  unsigned long currenttime;
+
+  // pull the pin high and wait 250 milliseconds
+  //digitalWrite(_pin, SULI_HIGH);
+  suli_pin_write(io, SULI_HIGH);
+  suli_delay_ms(250);
+
+  currenttime = suli_millis();
+  if (currenttime < _lastreadtime) {
+    // ie there was a rollover
+    _lastreadtime = 0;
+  }
+  if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
+    return true; // return last correct measurement
+    //delay(2000 - (currenttime - _lastreadtime));
+  }
+  firstreading = false;
+
+  _lastreadtime = suli_millis();
+
+  data[0] = data[1] = data[2] = data[3] = data[4] = 0;
+  
+  // now pull it low for ~20 milliseconds
+  //pinMode(_pin, OUTPUT);
+  suli_pin_dir(io, SULI_OUTPUT)
+  //digitalWrite(_pin, LOW);
+  suli_pin_write(io, SULI_LOW);
+  suli_delay_ms(20);
+  //cli();
+  //digitalWrite(_pin, SULI_HIGH);
+  suli_pin_write(io, SULI_HIGH);
+  //delayMicroseconds(40);
+  suli_delay_us(40);
+  //pinMode(_pin, INPUT);
+  suli_pin_dir(io, SULI_INPUT)
+  // read in timings
+  for ( i=0; i< MAXTIMINGS; i++) {
+    counter = 0;
+    //while (digitalRead(_pin) == laststate) {
+    while (suli_pin_read(io) == laststate) {
+      counter++;
+      //delayMicroseconds(1);
+      suli_delay_us(1);
+      if (counter == 255) {
+        break;
+      }
+    }
+    //laststate = digitalRead(&_pin);
+    laststate = suli_pin_read(io);
+
+    if (counter == 255) break;
+
+    // ignore first 3 transitions
+    if ((i >= 4) && (i%2 == 0)) {
+      // shove each bit into the storage bytes
+      data[j/8] <<= 1;
+      if (counter > _count)//
+        data[j/8] |= 1;
+      j++;
+    }
+
+  }
+
+  // check we read 40 bits and that the checksum matches
+  if ((j >= 40) && 
+      (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
+    return true;
+  }
+  
+
+  return false;
+
+}
+
+static float grove_temp_hum_convertCtoF(float c) {
+    return c * 9 / 5 + 32;
+}
+
+//boolean S == Scale.  True == Farenheit; False == Celcius
+bool grove_temp_hum_readtemperature(IO_T *io, bool S, float *temperature)
+{
+  float f;
+
+  if (_read(io)) {
+    switch (_type) {
+    case DHT11:
+      f = data[2];
+      if(S)
+        f = grove_temp_hum_convertCtoF(f);
+        *temperature = f;
+      return true;
+    case DHT22:
+    case DHT21:
+      f = data[2] & 0x7F;
+      f *= 256;
+      f += data[3];
+      f /= 10;
+      if (data[2] & 0x80)
+    f *= -1;
+      if(S)
+    f = grove_temp_hum_convertCtoF(f);
+      *temperature = f;
+      return true;
+    }
+  }
+  //Serial.print("Read fail");
+  return false;
+}
+
+bool grove_temp_hum_readhumidity(IO_T *io,  float *humidity)
+{
+  float f;
+  if (_read(io)) {
+    switch (_type) {
+    case DHT11:
+      f = data[0];
+      *humidity = f;
+      return true;
+    case DHT22:
+    case DHT21:
+      f = data[0];
+      f *= 256;
+      f += data[1];
+      f /= 10;
+       *humidity = f;
+      return true;
+    }
+  }
+  //Serial.print("Read fail");
+  return false;
+}
diff -r 000000000000 -r f0824fc8b273 grove_temp_hum.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/grove_temp_hum.h	Tue Jun 09 10:19:38 2015 +0000
@@ -0,0 +1,26 @@
+
+
+
+#ifndef __GROVE_TEMP_HUM_H__
+#define __GROVE_TEMP_HUM_H__
+
+#include "suli2.h"
+
+// how many timing transitions we need to keep track of. 2 * number bits + extra
+#if defined(__MBED__)
+#define MAXTIMINGS 85
+#elif defined(ARDUINO)
+#define MAXTIMINGS 85
+#endif
+
+#define DHT11 11
+#define DHT22 22
+#define DHT21 21
+#define AM2301 21
+
+void grove_temp_hum_init(IO_T *io, int pin);
+bool grove_temp_hum_write_setup(IO_T *io, uint8_t type, uint8_t count);
+bool grove_temp_hum_readtemperature(IO_T *io, bool S, float *temperature);
+bool grove_temp_hum_readhumidity(IO_T *io, float *humidity);
+
+#endif
diff -r 000000000000 -r f0824fc8b273 grove_temp_hum_class.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/grove_temp_hum_class.cpp	Tue Jun 09 10:19:38 2015 +0000
@@ -0,0 +1,24 @@
+
+
+#include "grove_temp_hum_class.h"
+
+GroveTempHum::GroveTempHum(int pin)
+{
+    this->io = (IO_T *)malloc(sizeof(IO_T));
+    grove_temp_hum_init(this->io, pin);
+}
+
+bool GroveTempHum::write_setup(uint8_t type, uint8_t count)
+{
+    return grove_temp_hum_write_setup(this->io, type, count);
+}
+
+bool GroveTempHum::read_temperature(bool S, float *temperature)
+{
+    return grove_temp_hum_readtemperature(this->io, S, temperature);
+}
+
+bool GroveTempHum::read_humidity(float *humidity)
+{
+    return grove_temp_hum_readhumidity(this->io, humidity);
+}
diff -r 000000000000 -r f0824fc8b273 grove_temp_hum_class.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/grove_temp_hum_class.h	Tue Jun 09 10:19:38 2015 +0000
@@ -0,0 +1,25 @@
+
+
+
+#ifndef __GROVE_TEMP_HUM_CLASS_H__
+#define __GROVE_TEMP_HUM_CLASS_H__
+
+#include "grove_temp_hum.h"
+
+//GROVE_NAME        "Grove_TempHum"
+//IF_TYPE           GPIO
+//IMAGE_URL         http://www.seeedstudio.com/wiki/File:Temp%26Humi.jpg
+
+class GroveTempHum
+{
+public:
+    GroveTempHum(int pin);
+    bool write_setup(uint8_t type, uint8_t count);
+    bool read_temperature(bool S, float *temperature);
+    bool read_humidity(float *humidity);
+
+private:
+    IO_T *io;
+};
+
+#endif