grove_temp_hum

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers grove_temp_hum.cpp Source File

grove_temp_hum.cpp

00001 
00002 
00003 #include "suli2.h"
00004 #include "grove_temp_hum.h"
00005 
00006 
00007 
00008 //local functions
00009 static bool _read(IO_T *io);
00010 static float grove_temp_hum_convertCtoF(float c);
00011 
00012 
00013 
00014 //local variables
00015 static uint8_t _type, _count;
00016 static bool firstreading;
00017 static unsigned long _lastreadtime;
00018 static uint8_t data[6];
00019 
00020 
00021 
00022 void grove_temp_hum_init(IO_T *io, int pin)
00023 {
00024     suli_pin_init(io, pin, SULI_INPUT);
00025 }
00026 
00027 bool grove_temp_hum_write_setup(IO_T *io, uint8_t type, uint8_t count)
00028 {
00029     _type = type;
00030     _count = count;
00031     firstreading = true;
00032     return true;
00033 }
00034 static bool _read(IO_T *io)
00035 {
00036   uint8_t laststate = SULI_HIGH;
00037   uint8_t counter = 0;
00038   uint8_t j = 0, i;
00039   unsigned long currenttime;
00040 
00041   // pull the pin high and wait 250 milliseconds
00042   //digitalWrite(_pin, SULI_HIGH);
00043   suli_pin_write(io, SULI_HIGH);
00044   suli_delay_ms(250);
00045 
00046   currenttime = suli_millis();
00047   if (currenttime < _lastreadtime) {
00048     // ie there was a rollover
00049     _lastreadtime = 0;
00050   }
00051   if (!firstreading && ((currenttime - _lastreadtime) < 2000)) {
00052     return true; // return last correct measurement
00053     //delay(2000 - (currenttime - _lastreadtime));
00054   }
00055   firstreading = false;
00056 
00057   _lastreadtime = suli_millis();
00058 
00059   data[0] = data[1] = data[2] = data[3] = data[4] = 0;
00060   
00061   // now pull it low for ~20 milliseconds
00062   //pinMode(_pin, OUTPUT);
00063   suli_pin_dir(io, SULI_OUTPUT)
00064   //digitalWrite(_pin, LOW);
00065   suli_pin_write(io, SULI_LOW);
00066   suli_delay_ms(20);
00067   //cli();
00068   //digitalWrite(_pin, SULI_HIGH);
00069   suli_pin_write(io, SULI_HIGH);
00070   //delayMicroseconds(40);
00071   suli_delay_us(40);
00072   //pinMode(_pin, INPUT);
00073   suli_pin_dir(io, SULI_INPUT)
00074   // read in timings
00075   for ( i=0; i< MAXTIMINGS; i++) {
00076     counter = 0;
00077     //while (digitalRead(_pin) == laststate) {
00078     while (suli_pin_read(io) == laststate) {
00079       counter++;
00080       //delayMicroseconds(1);
00081       suli_delay_us(1);
00082       if (counter == 255) {
00083         break;
00084       }
00085     }
00086     //laststate = digitalRead(&_pin);
00087     laststate = suli_pin_read(io);
00088 
00089     if (counter == 255) break;
00090 
00091     // ignore first 3 transitions
00092     if ((i >= 4) && (i%2 == 0)) {
00093       // shove each bit into the storage bytes
00094       data[j/8] <<= 1;
00095       if (counter > _count)//
00096         data[j/8] |= 1;
00097       j++;
00098     }
00099 
00100   }
00101 
00102   // check we read 40 bits and that the checksum matches
00103   if ((j >= 40) && 
00104       (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) ) {
00105     return true;
00106   }
00107   
00108 
00109   return false;
00110 
00111 }
00112 
00113 static float grove_temp_hum_convertCtoF(float c) {
00114     return c * 9 / 5 + 32;
00115 }
00116 
00117 //boolean S == Scale.  True == Farenheit; False == Celcius
00118 bool grove_temp_hum_readtemperature(IO_T *io, bool S, float *temperature)
00119 {
00120   float f;
00121 
00122   if (_read(io)) {
00123     switch (_type) {
00124     case DHT11:
00125       f = data[2];
00126       if(S)
00127         f = grove_temp_hum_convertCtoF(f);
00128         *temperature = f;
00129       return true;
00130     case DHT22:
00131     case DHT21:
00132       f = data[2] & 0x7F;
00133       f *= 256;
00134       f += data[3];
00135       f /= 10;
00136       if (data[2] & 0x80)
00137     f *= -1;
00138       if(S)
00139     f = grove_temp_hum_convertCtoF(f);
00140       *temperature = f;
00141       return true;
00142     }
00143   }
00144   //Serial.print("Read fail");
00145   return false;
00146 }
00147 
00148 bool grove_temp_hum_readhumidity(IO_T *io,  float *humidity)
00149 {
00150   float f;
00151   if (_read(io)) {
00152     switch (_type) {
00153     case DHT11:
00154       f = data[0];
00155       *humidity = f;
00156       return true;
00157     case DHT22:
00158     case DHT21:
00159       f = data[0];
00160       f *= 256;
00161       f += data[1];
00162       f /= 10;
00163        *humidity = f;
00164       return true;
00165     }
00166   }
00167   //Serial.print("Read fail");
00168   return false;
00169 }