-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDS1820.cpp
265 lines (262 loc) · 7.1 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//-----------------------------------------------------------------------------
#include <stdlib.h>
#include <string.h>
#include "DS1820.h"
#include "DS1820_misc.h"
#include "../avr-debug/debug.h"
#include "../avr-oneWire/oneWire.h"
//-----------------------------------------------------------------------------
BYTE BusDS1820::searchDevices()
{
BYTE A, B;
QWORD address;
char dIndex;
BYTEstack dPos, dBit;
// clear known devices list
if(m_addresses)
{
free(m_addresses);
m_addresses = NULL;
}
m_devicesCount = 0;
while(m_devicesCount == 0 || dPos.size() > 0)
{
if(one_wire_reset())
{
// no devices found (no response from any)
break;
}
address = 0;
dIndex = -1;
one_wire_send_byte(CMD_ROM_SEARCH);
for(BYTE i = 0; i < 64; i++)
{
A = one_wire_read_bit();
B = one_wire_read_bit();
//DHEX8(2, A, B);
if(A == 1 && B == 1)
{
// a strange combination - we should not be here
return m_devicesCount;
}
address >>= 1;
if(A != B)
{
if(A)
{
address |= ((QWORD) 1 << 63);
one_wire_write_one();
}
else
{
one_wire_write_zero();
}
}
else
{
// discrepancy found
dIndex++;
if(dPos.size() == 0 || i > dPos.top())
{
// first time found discrepancy
dPos.push(i);
dBit.push(0);
one_wire_write_zero();
}
else
{
if(i == dPos.top())
{
// change route direction
dBit.pop();
dBit.push(1);
address |= ((QWORD) 1 << 63);
one_wire_write_one();
}
if(i < dPos.top())
{
// tracking the previous route
address |= ((QWORD) dBit[dIndex] << 63);
one_wire_write_bit(dBit[dIndex]);
}
}
}
}
while(dBit.size() && dBit.top())
{
// get rid of completely traced routes
dPos.pop();
dBit.pop();
}
// save the result
m_addresses = (QWORD*) realloc(m_addresses, sizeof(QWORD) * (m_devicesCount + 1));
m_addresses[m_devicesCount++] = address;
}
return m_devicesCount;
}
//-----------------------------------------------------------------------------
const QWORD& BusDS1820::getFirstDeviceAddress()
{
if(m_devicesCount)
{
m_nextID = 1;
return m_addresses[0];
}
return NULL;
}
//-----------------------------------------------------------------------------
const QWORD& BusDS1820::getNextDeviceAddress()
{
if(m_nextID < m_devicesCount)
{
return m_addresses[m_nextID++];
}
return NULL;
}
//-----------------------------------------------------------------------------
void* SensorDS1820::operator new(size_t count)
{
void* ptr = malloc(sizeof(SensorDS1820) * count);
return ptr;
}
//-----------------------------------------------------------------------------
void SensorDS1820::operator delete(void* ptr)
{
if(ptr)
{
free(ptr);
}
}
//-----------------------------------------------------------------------------
SensorDS1820::SensorDS1820(QWORD& address)
{
m_address = address;
}
//-----------------------------------------------------------------------------
QWORD& SensorDS1820::getAddress()
{
return m_address;
}
//-----------------------------------------------------------------------------
BYTE SensorDS1820::getModelCode()
{
return m_modelCode;
}
//-----------------------------------------------------------------------------
__inline void send_address(QWORD* address)
{
if(address)
{
BYTE* tmp = (BYTE*) address;
one_wire_send_byte(CMD_ROM_MATCH);
for(BYTE i = 0; i < 8; i++)
{
one_wire_send_byte(*tmp++);
}
}
else
{
one_wire_send_byte(CMD_ROM_SKIP);
}
}
//-----------------------------------------------------------------------------
__inline void start_conversion(QWORD* address = NULL)
{
if(one_wire_reset())
{
return;
}
send_address(address);
one_wire_send_byte(CMD_START_CONVERTION);
}
//-----------------------------------------------------------------------------
bool SensorDS1820::available()
{
if(one_wire_reset())
{
return false;
}
send_address(&m_address);
one_wire_send_byte(CMD_RAM_READ);
if(one_wire_read_byte() != 0xFF)
{
return true;
}
if(one_wire_read_byte() != 0xFF)
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
void AllSensorsDS1820::measure()
{
start_conversion();
}
//-----------------------------------------------------------------------------
void SensorDS1820::measure()
{
start_conversion(&m_address);
}
//-----------------------------------------------------------------------------
//__inline void set_resolution(__unused BYTE resolution, QWORD* address = NULL)
//{
// if(one_wire_reset())
// {
// return;
// }
// send_address(address);
// // TODO: set resolution command and data
//}
//-----------------------------------------------------------------------------
//void AllSensorsDS1820::setResolution(BYTE resolution)
//{
// set_resolution(resolution);
//}
//-----------------------------------------------------------------------------
//void SensorDS1820::setResolution(BYTE resolution)
//{
// set_resolution(resolution, &m_address);
//}
//-----------------------------------------------------------------------------
int SensorDS1820::readRawTemp()
{
if(one_wire_reset())
{
return BAD_TEMP;
}
send_address(&m_address);
one_wire_send_byte(CMD_RAM_READ);
WORD result = one_wire_read_word();
if(result == 0xFFFF)
{
return BAD_TEMP;
}
return (int) result;
}
//-----------------------------------------------------------------------------
int SensorDS1820::readTempC()
{
int result = readRawTemp();
if(result == (int) BAD_TEMP)
{
return result;
}
return ((result >> 3) + 1) / 2; // round to nearest integer;
}
//-----------------------------------------------------------------------------
int SensorDS1820::readTempF()
{
int result = readRawTemp();
if(result == (int) BAD_TEMP)
{
return result;
}
return ((result * 18 / 10 >> 3) + 1) / 2 + 32;
}
//-----------------------------------------------------------------------------
int SensorDS1820::readTempK()
{
return readTempC() + 273;
}
//-----------------------------------------------------------------------------