COSA
An Object-Oriented Platform for Arduino Programming
DHT.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_DHT_HH
22 #define COSA_DHT_HH
23 
24 #include "Cosa/Types.h"
26 #include "Cosa/IOStream.hh"
27 
34 class DHT : public ExternalInterrupt {
35 public:
37  static const int16_t INIT_HUMIDITY_SAMPLE = 1000;
38 
40  static const int16_t INIT_TEMPERATURE_SAMPLE = 850;
41 
48  m_state(INIT),
49  m_start(0),
50  m_value(0),
51  m_bits(0),
52  m_ix(0),
53  m_humidity(INIT_HUMIDITY_SAMPLE),
54  m_temperature(INIT_TEMPERATURE_SAMPLE)
55  {}
56 
61  int16_t humidity() const
62  {
63  return (m_humidity);
64  }
65 
70  int16_t temperature() const
71  {
72  return (m_temperature);
73  }
74 
80  bool sample_request();
81 
87  bool sample_await();
88 
94  bool sample()
95  __attribute__((always_inline))
96  {
97  return (sample_request() && sample_await());
98  }
99 
107  bool sample(int16_t& humidity, int16_t& temperature)
108  __attribute__((always_inline))
109  {
110  bool res = sample();
113  return (res);
114  }
115 
116 protected:
122  virtual void on_interrupt(uint16_t arg = 0);
123 
131  virtual void on_sample_completed(bool valid)
132  {
133  UNUSED(valid);
134  }
135 
141  bool is_valid();
142 
149  virtual void adjust_data() = 0;
150 
151  // States
152  enum {
159  } __attribute__((packed));
160 
162  static const uint16_t MIN_PERIOD = 2048;
163 
165  static const uint16_t LOW_THRESHOLD = 50;
166  static const uint16_t BIT_THRESHOLD = 100;
167  static const uint16_t HIGH_THRESHOLD = 200;
168 
170  static const uint8_t DATA_MAX = 5;
171 
173  static const uint8_t DATA_LAST = DATA_MAX - 1;
174 
179  union data_t {
180  uint8_t as_byte[DATA_MAX];
181  struct {
182  int16_t humidity;
183  int16_t temperature;
184  uint8_t chksum;
185  };
186  };
187 
189  volatile uint8_t m_state;
190 
192  uint16_t m_start;
193 
195  uint8_t m_value;
196 
198  uint8_t m_bits;
199 
201  uint8_t m_ix;
202 
205 
206  /* Latest valid humidity reading. */
207  int16_t m_humidity;
208 
209  /* Latest valid temperature reading. */
210  int16_t m_temperature;
211 
219  friend IOStream& operator<<(IOStream& outs, DHT& dht);
220 };
221 
244 class DHT11 : public DHT {
245 public:
251  DHT(pin)
252  {
253  }
254 
255 protected:
261  virtual void adjust_data();
262 };
263 
286 class DHT22 : public DHT {
287 public:
293  DHT(pin)
294  {
295  }
296 
297 protected:
303  virtual void adjust_data();
304 };
305 #endif
bool sample()
Definition: DHT.hh:94
DHT22(Board::ExternalInterruptPin pin=Board::EXT0)
Definition: DHT.hh:292
uint8_t chksum
Definition: DHT.hh:184
uint8_t m_ix
Definition: DHT.hh:201
bool sample_await()
Definition: DHT.cpp:102
Waiting for response.
Definition: DHT.hh:156
bool is_valid()
Definition: DHT.cpp:116
uint8_t pin() const
Definition: Pin.hh:103
DHT(Board::ExternalInterruptPin pin=Board::EXT0)
Definition: DHT.hh:46
data_t m_data
Definition: DHT.hh:204
uint8_t m_bits
Definition: DHT.hh:198
DHT11(Board::ExternalInterruptPin pin=Board::EXT0)
Definition: DHT.hh:250
int16_t humidity() const
Definition: DHT.hh:61
static const int16_t INIT_HUMIDITY_SAMPLE
Definition: DHT.hh:37
friend IOStream & operator<<(IOStream &outs, DHT &dht)
Definition: DHT.cpp:142
static const uint8_t DATA_MAX
Definition: DHT.hh:170
Initial state.
Definition: DHT.hh:153
static const uint16_t BIT_THRESHOLD
Definition: DHT.hh:166
Collecting samples.
Definition: DHT.hh:157
static const int16_t INIT_TEMPERATURE_SAMPLE
Definition: DHT.hh:40
bool sample_request()
Definition: DHT.cpp:83
bool sample(int16_t &humidity, int16_t &temperature)
Definition: DHT.hh:107
int16_t humidity
Definition: DHT.hh:182
static const uint16_t LOW_THRESHOLD
Definition: DHT.hh:165
virtual void adjust_data()=0
static const uint16_t HIGH_THRESHOLD
Definition: DHT.hh:167
volatile uint8_t m_state
Definition: DHT.hh:189
ExternalInterruptPin
Definition: ATmega1284P.hh:190
Data transfer completed.
Definition: DHT.hh:158
Periodic wait.
Definition: DHT.hh:154
#define UNUSED(x)
Definition: ATmega328P.hh:31
Definition: DHT.hh:244
static const uint8_t DATA_LAST
Definition: DHT.hh:173
virtual void on_interrupt(uint16_t arg=0)
Definition: DHT.cpp:35
uint16_t m_start
Definition: DHT.hh:192
uint8_t as_byte[DATA_MAX]
Definition: DHT.hh:180
Issued a request.
Definition: DHT.hh:155
Definition: DHT.hh:34
int16_t temperature() const
Definition: DHT.hh:70
int16_t m_humidity
Definition: DHT.hh:207
uint8_t m_value
Definition: DHT.hh:195
virtual void on_sample_completed(bool valid)
Definition: DHT.hh:131
static const uint16_t MIN_PERIOD
Definition: DHT.hh:162
int16_t temperature
Definition: DHT.hh:183
Definition: DHT.hh:286
int16_t m_temperature
Definition: DHT.hh:210