-
Notifications
You must be signed in to change notification settings - Fork 8
/
ds1820.cpp
215 lines (184 loc) · 4.62 KB
/
ds1820.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Copyright 2003-2010 Mike Wakerly <opensource@hoho.com>
*
* This file is part of the Kegbot package of the Kegbot project.
* For more information on Kegbot, see http://kegbot.org/
*
* Kegbot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Kegbot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Kegbot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <inttypes.h>
#include "ds1820.h"
#include "OneWire.h"
#define REFRESH_MS 5000
DS1820Sensor::DS1820Sensor() {
m_bus = 0;
m_initialized = false;
Reset();
}
void DS1820Sensor::Reset() {
m_converting = false;
m_conversion_start_clock = 0;
m_temp = INVALID_TEMPERATURE_VALUE;
m_temp_is_valid = false;
for (int i=0; i<8; i++) {
m_addr[i] = 0;
}
}
void DS1820Sensor::Initialize(OneWire* bus, uint8_t* addr)
{
m_bus = bus;
for (int i=0; i<8;i++) {
m_addr[i] = addr[i];
}
m_initialized = true;
}
bool DS1820Sensor::Initialized() {
return m_initialized;
}
bool DS1820Sensor::Update(unsigned long clock)
{
if (!m_initialized) {
return false;
}
if (clock < m_conversion_start_clock) { // overflow of clock
m_conversion_start_clock = 0;
}
if (!m_converting) {
// we're not converting, and it is time to start
m_converting = true;
m_conversion_start_clock = clock;
StartConversion();
} else if (m_converting && (clock - m_conversion_start_clock) >= 1000) {
// we're converting and it is time to fetch
m_converting = false;
if (FetchConversion()) {
m_temp_is_valid = true;
} else {
m_temp_is_valid = false;
}
return true;
} else {
// we're either in the middle of a conversion, or it is too soon to start
// the next cycle.
}
return false;
}
bool DS1820Sensor::ResetAndSelect()
{
if (!m_bus->reset()) {
return false;
}
m_bus->select(m_addr);
return true;
}
bool DS1820Sensor::StartConversion()
{
if (!ResetAndSelect())
return false;
m_bus->write(0x44, 1);
return true;
}
bool DS1820Sensor::FetchConversion()
{
if (!ResetAndSelect()) {
return false;
}
uint8_t data[9];
m_bus->write(0xBE); // read scratchpad
bool null_data = true;
for (int i = 0; i < 9; i++) {
data[i] = m_bus->read();
if (data[i] != 0) {
null_data = false;
}
}
if (null_data) {
return false;
}
if (OneWire::crc8(data, 8) != data[8]) {
// bad CRC, drop reading.
return false;
}
m_temp = ((data[1] << 8) | data[0]);
return true;
}
long DS1820Sensor::GetTemp(void)
{
if (!m_temp_is_valid)
return INVALID_TEMPERATURE_VALUE;
// The value returned by the DS18B20 is a 16-bit 2's complement sign-extended
// value. The value is the temperature, either increments of 1/16th deg C
// (DS18B20 default) or 1/2 deg C (DS18S20 default).
//
// This method returns the temperature in 1/10^6 deg C, so the value is scaled
// up by (10^6/16) = 62500 or (10^6/2) = 500000.
long res = (long)m_temp;
switch (m_addr[0]) {
case ONEWIRE_FAMILY_DS18B20:
return res * 62500L;
case ONEWIRE_FAMILY_DS18S20:
return res * 500000L;
default:
return INVALID_TEMPERATURE_VALUE;
}
}
void DS1820Sensor::GetTempC(char* buf) {
if (!m_temp_is_valid) {
buf[0] = '\0';
return;
}
int temp = m_temp;
int temp_c;
if (temp & 0x8000) {
*buf = '-';
buf++;
temp = (temp ^ 0xffff) + 1;
}
temp_c = (6 * temp) + temp / 4; // multiply by (100 * 0.0625) or 6.25
sprintf(buf, "%i.%i", temp_c/100, temp_c % 100);
}
bool DS1820Sensor::Busy() {
return m_converting;
}
int DS1820Sensor::CompareId(uint8_t* other) {
for (int i = 0; i < 8; i++) {
if (m_addr[i] == other[i]) {
continue;
} else {
return (m_addr[i] < other[i]) ? -1 : 1;
}
}
return 0;
}
void DS1820Sensor::PrintTemp(void)
{
long temp = GetTemp() / 1000000;
Serial.print(temp);
}
static void byteToChars(uint8_t byte, char* out) {
for (int i=0; i<2; i++) {
uint8_t val = (byte >> (4*i)) & 0xf;
if (val < 10) {
out[1-i] = (char) ('0' + val);
} else if (val < 16) {
out[1-i] = (char) ('a' + (val - 10));
}
}
}
void DS1820Sensor::GetName(char* buf) {
buf[16] = '\0';
for (int i=7; i>=0; i--) {
byteToChars(m_addr[i], buf + (i * 2));
}
}