-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTT34.cpp
More file actions
127 lines (104 loc) · 2.43 KB
/
TT34.cpp
File metadata and controls
127 lines (104 loc) · 2.43 KB
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
#include "TT34.h"
TT34::TT34(unsigned int pin, unsigned int timeout)
{
_pin = pin;
_timeout = timeout;
pinMode(pin, INPUT);
}
float TT34::getTemperature()
{
if (waitForChange(_timeout) < 0)
{
return -1.0;
}
readValues();
const int shift = findShift();
const unsigned int toggle = findToggle(shift);
const float temperature = parse(shift, toggle);
if (0 < temperature && temperature < 250)
{
return temperature;
}
else
{
return -2.0;
}
}
int TT34::waitForChange(unsigned int timeout)
{
unsigned int start = micros();
unsigned int counter = 0;
unsigned int t0 = 0;
unsigned int duration = 0;
unsigned int initial = 0;
const unsigned int requiredCounts = 6;
while (counter < requiredCounts && start <= micros() && micros() <= start + timeout)
{
t0 = micros();
initial = digitalRead(_pin);
while (digitalRead(_pin) == initial && start <= micros() && micros() <= start + timeout)
{
}
duration = micros() - t0;
if ((940 < duration) and (duration < 1140))
{
++counter;
}
else
{
counter = 0;
}
}
if (counter < requiredCounts)
{
return -1;
}
else
{
return 0;
}
}
void TT34::readValues()
{
delayMicroseconds(1038 / 2);
for (int i = 0; i < numValues; ++i)
{
_values[i] = digitalRead(_pin);
delayMicroseconds(1038 - 4);
}
}
int TT34::findShift()
{
for (int i = 1; i < numValues; ++i)
{
if (_values[i] == _values[i - 1])
{
return i - 7;
}
}
}
unsigned int TT34::findToggle(int shift)
{
return _values[shift + 7];
}
float TT34::parse(int shift, unsigned int toggle)
{
float result = 0.0;
for (int i = 0; i < 4; ++i)
{
result += 0.100 * float((toggle ^ _values[27 + i + shift]) << i);
}
for (int i = 0; i < 4; ++i)
{
result += 1.000 * float((toggle ^ _values[31 + i + shift]) << i);
}
for (int i = 0; i < 4; ++i)
{
result += 10.00 * float((toggle ^ _values[37 + i + shift]) << i);
}
for (int i = 0; i < 4; ++i)
{
result += 100.0 * float((toggle ^ _values[41 + i + shift]) << i);
}
return result;
}