COSA
An Object-Oriented Platform for Arduino Programming
DS18B20.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_DS18B20_HH
22 #define COSA_DS18B20_HH
23 
24 #include <OWI.h>
25 
26 #include "Cosa/Types.h"
27 #include "Cosa/IOStream.hh"
28 
49 class DS18B20 : public OWI::Driver {
50 public:
54  class Search : public OWI::Search {
55  public:
61  Search(OWI* owi) : OWI::Search(owi, FAMILY_CODE) {}
62 
69  DS18B20* next();
70  };
71 
80  DS18B20(OWI* pin, const char* name = NULL) :
81  OWI::Driver(pin, name),
82  m_parasite(0),
83  m_start(0L),
84  m_converting(false)
85  {}
86 
97  DS18B20(OWI* pin, const uint8_t* rom, const char* name = NULL) :
98  OWI::Driver(pin, rom, name),
99  m_parasite(0),
100  m_start(0L),
101  m_converting(false)
102  {}
103 
111  bool connect(uint8_t index);
112 
118  void resolution(uint8_t bits);
119 
126  void set_trigger(int8_t low, int8_t high)
127  __attribute__((always_inline))
128  {
130  m_scratchpad.high_trigger = high;
131  }
132 
141  int16_t temperature() const
142  __attribute__((always_inline))
143  {
144  return (m_scratchpad.temperature);
145  }
146 
152  uint8_t resolution() const
153  __attribute__((always_inline))
154  {
155  if (m_rom[0] == 0) return (0);
156  return (9 + (m_scratchpad.configuration >> 5));
157  }
158 
165  void get_trigger(int8_t& low, int8_t& high) const
166  __attribute__((always_inline))
167  {
169  high = m_scratchpad.high_trigger;
170  }
171 
178  bool convert_request();
179 
192  static bool convert_request(OWI* owi,
193  uint8_t resolution = 12,
194  bool parasite = false);
195 
201  bool write_scratchpad();
202 
211  bool read_scratchpad(bool flag = true);
212 
219  bool copy_scratchpad();
220 
225  bool recall();
226 
234  bool read_power_supply();
235 
242  static void print(IOStream& outs, int16_t temp);
243 
244 protected:
248  enum {
249  FAMILY_CODE = 0x28,
250  CONVERT_T = 0x44,
254  RECALL_E = 0xB8,
256  } __attribute__((packed));
257 
261  struct scratchpad_t {
262  int16_t temperature;
263  int8_t high_trigger;
264  int8_t low_trigger;
265  uint8_t configuration;
266  uint8_t reserved[3];
267  uint8_t crc;
268  };
270 
272  static const uint8_t CONFIG_MAX = 3;
273 
275  uint8_t m_parasite;
276 
278  uint32_t m_start;
279 
281  uint8_t m_converting;
282 
284  static const uint16_t MAX_CONVERSION_TIME = 750;
285 
287  static const uint16_t MIN_COPY_PULLUP = 10;
288 
293  void power_off()
294  __attribute__((always_inline))
295  {
296  if (m_parasite) m_pin->power_off();
297  }
298 
299  friend IOStream& operator<<(IOStream& outs, DS18B20& thermometer);
300 };
301 
310 IOStream& operator<<(IOStream& outs, DS18B20& thermometer);
311 
312 #endif
uint8_t * rom()
Definition: OWI.hh:96
Definition: OWI.hh:36
int16_t temperature() const
Definition: DS18B20.hh:141
bool copy_scratchpad()
Definition: DS18B20.cpp:101
scratchpad_t m_scratchpad
Definition: DS18B20.hh:269
DS18B20(OWI *pin, const char *name=NULL)
Definition: DS18B20.hh:80
int16_t temperature
Temperature reading (9-12 bits).
Definition: DS18B20.hh:262
static void print(IOStream &outs, int16_t temp)
Definition: DS18B20.cpp:128
friend IOStream & operator<<(IOStream &outs, OWI &owi)
Definition: OWI.cpp:149
#define NULL
Definition: Types.h:101
int8_t low_trigger
Low temperature trigger.
Definition: DS18B20.hh:264
void power_off()
Definition: OWI.hh:274
void power_off()
Definition: DS18B20.hh:293
DS18B20 * next()
Definition: DS18B20.cpp:25
void get_trigger(int8_t &low, int8_t &high) const
Definition: DS18B20.hh:165
static const uint8_t CONFIG_MAX
Definition: DS18B20.hh:272
bool read_scratchpad(bool flag=true)
Definition: DS18B20.cpp:75
DS18B20(OWI *pin, const uint8_t *rom, const char *name=NULL)
Definition: DS18B20.hh:97
bool recall()
Definition: DS18B20.cpp:111
Search(OWI *owi)
Definition: DS18B20.hh:61
uint8_t crc
Check sum.
Definition: DS18B20.hh:267
uint8_t m_rom[ROM_MAX]
Definition: OWI.hh:162
bool convert_request()
Definition: DS18B20.cpp:50
uint8_t resolution() const
Definition: DS18B20.hh:152
int8_t high_trigger
High temperature trigger.
Definition: DS18B20.hh:263
uint8_t configuration
Configuration; resolution, alarm.
Definition: DS18B20.hh:265
uint8_t m_parasite
Definition: DS18B20.hh:275
bool write_scratchpad()
Definition: DS18B20.cpp:93
uint32_t m_start
Definition: DS18B20.hh:278
static const uint16_t MIN_COPY_PULLUP
Definition: DS18B20.hh:287
OWI * m_pin
Definition: OWI.hh:165
Driver(OWI *pin, const char *name=NULL)
Definition: OWI.hh:74
uint8_t m_converting
Definition: DS18B20.hh:281
bool connect(uint8_t family, uint8_t index)
Definition: OWI.cpp:272
void set_trigger(int8_t low, int8_t high)
Definition: DS18B20.hh:126
static const uint16_t MAX_CONVERSION_TIME
Definition: DS18B20.hh:284
bool read_power_supply()
Definition: DS18B20.cpp:119