COSA
An Object-Oriented Platform for Arduino Programming
DS18B20.cpp
Go to the documentation of this file.
1 
21 #include "DS18B20.hh"
22 #include "Cosa/Watchdog.hh"
23 
24 DS18B20*
26 {
27  DS18B20* dev = (DS18B20*) OWI::Search::next();
28  if (UNLIKELY(dev == NULL)) return (NULL);
29  dev->read_scratchpad(false);
30  return (dev);
31 }
32 
33 bool
34 DS18B20::connect(uint8_t index)
35 {
36  if (!OWI::Driver::connect(FAMILY_CODE, index)) return (false);
37  if (!read_scratchpad(false)) return (false);
39  return (true);
40 }
41 
42 void
43 DS18B20::resolution(uint8_t bits)
44 {
45  if (bits < 9) bits = 9; else if (bits > 12) bits = 12;
46  m_scratchpad.configuration = (((bits - 9) << 5) | 0x1f);
47 }
48 
49 bool
51 {
52  if (!match_rom()) return (false);
55  m_converting = true;
56  return (true);
57 }
58 
59 bool
60 DS18B20::convert_request(OWI* owi, uint8_t resolution, bool parasite)
61 {
62  if (!owi->reset()) return (false);
63  owi->write(OWI::SKIP_ROM);
64  owi->write(CONVERT_T, CHARBITS, parasite);
65  if (resolution == 0) return (true);
66  if (resolution < 9) resolution = 9;
67  else if (resolution > 12) resolution = 12;
68  uint16_t ms = (MAX_CONVERSION_TIME >> (12 - resolution));
69  delay(ms);
70  if (parasite) owi->power_off();
71  return (true);
72 }
73 
74 bool
76 {
77  if (m_converting) {
78  int32_t ms = Watchdog::millis() - m_start;
79  uint16_t conv_time = (MAX_CONVERSION_TIME >> (12 - resolution()));
80  if (ms < conv_time) {
81  ms = conv_time - ms;
82  delay(ms);
83  }
84  m_converting = false;
85  power_off();
86  }
87  if (flag && !match_rom()) return (false);
89  return (m_pin->read(&m_scratchpad, sizeof(m_scratchpad)));
90 }
91 
92 bool
94 {
95  if (!match_rom()) return (false);
97  return (true);
98 }
99 
100 bool
102 {
103  if (!match_rom()) return (false);
106  power_off();
107  return (true);
108 }
109 
110 bool
112 {
113  if (!match_rom()) return (false);
114  m_pin->write(RECALL_E);
115  return (true);
116 }
117 
118 bool
120 {
121  if (!match_rom()) return (false);
123  m_parasite = (m_pin->read(1) == 0);
124  return (m_parasite);
125 }
126 
127 void
128 DS18B20::print(IOStream& outs, int16_t temp)
129 {
130  if (temp < 0) {
131  temp = -temp;
132  outs << '-';
133  }
134  uint16_t fraction = (625 * (temp & 0xf)) / 100;
135  int16_t integer = (temp >> 4);
136  outs << integer << '.';
137  if (fraction < 10) outs << '0';
138  outs << fraction;
139 }
140 
141 IOStream& operator<<(IOStream& outs, DS18B20& thermometer)
142 {
143  if (thermometer.NAME != NULL) outs << thermometer.NAME << PSTR(" = ");
144  DS18B20::print(outs, thermometer.temperature());
145  return (outs);
146 }
147 
Definition: OWI.hh:36
int16_t temperature() const
Definition: DS18B20.hh:141
static uint32_t millis()
Definition: Watchdog.hh:51
bool copy_scratchpad()
Definition: DS18B20.cpp:101
scratchpad_t m_scratchpad
Definition: DS18B20.hh:269
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
void power_off()
Definition: OWI.hh:274
void power_off()
Definition: DS18B20.hh:293
Driver * next()
Definition: OWI.cpp:307
#define PSTR(s)
Definition: Types.h:202
DS18B20 * next()
Definition: DS18B20.cpp:25
bool reset()
Definition: OWI.cpp:29
void write(uint8_t value, uint8_t bits=CHARBITS, bool power=false)
Definition: OWI.cpp:90
static const uint8_t CONFIG_MAX
Definition: DS18B20.hh:272
bool read_scratchpad(bool flag=true)
Definition: DS18B20.cpp:75
bool connect(uint8_t index)
Definition: DS18B20.cpp:34
bool recall()
Definition: DS18B20.cpp:111
void(* delay)(uint32_t ms)
#define CHARBITS
Definition: Types.h:57
bool convert_request()
Definition: DS18B20.cpp:50
uint8_t resolution() const
Definition: DS18B20.hh:152
bool match_rom()
Definition: OWI.cpp:244
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
uint8_t m_converting
Definition: DS18B20.hh:281
bool connect(uint8_t family, uint8_t index)
Definition: OWI.cpp:272
uint8_t read(uint8_t bits=CHARBITS)
Definition: OWI.cpp:50
static const uint16_t MAX_CONVERSION_TIME
Definition: DS18B20.hh:284
#define UNLIKELY(x)
Definition: Types.h:153
bool read_power_supply()
Definition: DS18B20.cpp:119
str_P NAME
Definition: OWI.hh:67