-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathIHCtemperature.cpp
120 lines (101 loc) · 2.74 KB
/
IHCtemperature.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
/*
(C) 2015 dingus.dk J.Ø.N.
This file is part of ArduinoIHC.
ArduinoIHC 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 3 of the License, or
(at your option) any later version.
ArduinoIHC 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 ArduinoIHC. If not, see <http://www.gnu.org/licenses/>.
*/#include <Arduino.h>
#include "IHCOutput.h"
#include "IHCtemperature.h"
#define PULSELENGTH 122
#define PULSE0 41
#define PULSE1 81
IHCtemperature::IHCtemperature(int ch) {
channel = ch;
memset(&data, 0, sizeof(data));
bitpos = 0;
temperature = gulvtemp = humidity = 0;
gulvibrug = false;
next = NULL;
starttime = 0;
}
word NibbleChecksum(int n) {
return ((n >> 8) & 0x000f) + ((n >> 4) & 0x000f) + (n & 0x000f);
}
void IHCtemperature::Init() {
int rum = int(temperature * 10.0);
int h = int(humidity * 10.0);
int gulv = int(gulvtemp * 10.0);
word checksum_rum = NibbleChecksum(rum);
word checksum_h = NibbleChecksum(h);
word checksum_gulv = NibbleChecksum(gulv);
word checksum_5bit = ((checksum_rum + checksum_h + checksum_gulv) & 0x001F);
if (gulvibrug)
checksum_5bit = checksum_5bit & 0x000F;
else {
checksum_5bit |= 0x0010;
checksum_5bit ^= 0x0008;
}
memset(&data, 0, sizeof(data));
bitpos = 0;
AddBits(rum);
AddBits(h);
AddBits(gulv);
AddBits(checksum_5bit, 5);
starttime = millis();
ihcoutput->Set(channel, HIGH);
}
void IHCtemperature::Tick() {
if (starttime == 0) {
Init();
return;
}
unsigned long time = millis();
long dt = time - starttime;
int bitnr = dt / PULSELENGTH;
if (bitnr >= 41) {
// Wait 140ms sec until next update
if (dt > 5140) starttime = 0;
return;
}
int t = dt % PULSELENGTH;
int bp = 0;
if (t >= PULSE0) bp = 1;
if (t >= PULSE1) bp = 2;
switch (bp) {
case 0: ihcoutput->Set(channel, HIGH); break;
case 1: {
int bytenr = bitnr / 8;
int bit = bitnr % 8;
if ((data[bytenr] & (1 << bit)) == 0)
ihcoutput->Set(channel, LOW);
break;
}
case 2:
ihcoutput->Set(channel, LOW);
break;
}
}
void IHCtemperature::AddBits(word value, int bits) {
word valuemask = 0x01 << (bits - 1);
int i = bitpos / 8;
int bitofs = bitpos % 8;
uint8_t mask = 1 << bitofs;
do {
if (value & valuemask) {
data[i] |= mask;;
}
bitpos++;
if ((mask <<= 1) == 0) {
i++;
mask = 1;
}
} while (valuemask >>= 1);
}