forked from ToniA/Raw-IR-decoder-for-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Carrier.cpp
237 lines (206 loc) · 5.9 KB
/
Carrier.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
#include <Arduino.h>
#define BYTETOBINARYPATTERN "%d%d%d%d%d%d%d%d"
#define BYTETOBINARY(byte) \
(byte & 0x80 ? 1 : 0), \
(byte & 0x40 ? 1 : 0), \
(byte & 0x20 ? 1 : 0), \
(byte & 0x10 ? 1 : 0), \
(byte & 0x08 ? 1 : 0), \
(byte & 0x04 ? 1 : 0), \
(byte & 0x02 ? 1 : 0), \
(byte & 0x01 ? 1 : 0)
byte bitReverse(byte x)
{
// 01010101 | 10101010
x = ((x >> 1) & 0x55) | ((x << 1) & 0xaa);
// 00110011 | 11001100
x = ((x >> 2) & 0x33) | ((x << 2) & 0xcc);
// 00001111 | 11110000
x = ((x >> 4) & 0x0f) | ((x << 4) & 0xf0);
return x;
}
bool decodeCarrier1(byte *bytes, int byteCount)
{
// If this looks like a Carrier code...
if ( byteCount == 18 && bytes[0] == 0x4F && bytes[1] == 0xB0 && (memcmp(bytes, bytes+9, 9) == 0)) {
Serial.println(F("Looks like a Carrier protocol #1"));
// Check if the checksum matches
byte checksum = 0;
checksum = bitReverse(bytes[0]) +
bitReverse(bytes[1]) +
bitReverse(bytes[2]) +
bitReverse(bytes[3]) +
bitReverse(bytes[4]) +
bitReverse(bytes[5]) +
bitReverse(bytes[6]) +
bitReverse(bytes[7]);
switch (bytes[6] & 0x0F) {
case 0x00:
Serial.println(F("FAN: AUTO"));
break;
case 0x02:
Serial.println(F("FAN: 1"));
break;
case 0x06:
Serial.println(F("FAN: 2"));
break;
case 0x01:
Serial.println(F("FAN: 3"));
break;
case 0x05:
Serial.println(F("FAN: 4"));
break;
case 0x03:
Serial.println(F("FAN: 5"));
break;
}
switch (bytes[6] & 0xF0) {
case 0xE0:
Serial.println(F("MODE: OFF"));
break;
case 0x00:
Serial.println(F("MODE: AUTO"));
checksum += 0x02;
switch (bytes[6] & 0x0F) {
case 0x02: // FAN1
case 0x03: // FAN5
case 0x06: // FAN2
checksum += 0x80;
break;
}
break;
case 0x80:
Serial.println(F("MODE: COOL"));
break;
case 0x40:
Serial.println(F("MODE: DRY"));
checksum += 0x02;
break;
case 0xC0:
Serial.println(F("MODE: HEAT"));
switch (bytes[6] & 0x0F) {
case 0x05: // FAN4
case 0x06: // FAN2
checksum += 0xC0;
break;
}
break;
case 0x20:
Serial.println(F("MODE: FAN"));
checksum += 0x02;
switch (bytes[6] & 0x0F) {
case 0x02: // FAN1
case 0x03: // FAN5
case 0x06: // FAN2
checksum += 0x80;
break;
}
break;
}
checksum = bitReverse(checksum);
const byte temperatures[] = { 17, 25, 21, 29, 19, 27, 23, 00, 18, 26, 22, 30, 20, 28, 24 };
Serial.print(F("Temperature: "));
Serial.println(temperatures[bytes[5]]);
char bin1[9];
char bin2[9];
char bin3[9];
snprintf(bin1, sizeof(bin1), BYTETOBINARYPATTERN, BYTETOBINARY(checksum));
snprintf(bin2, sizeof(bin2), BYTETOBINARYPATTERN, BYTETOBINARY(bytes[8]));
snprintf(bin3, sizeof(bin3), BYTETOBINARYPATTERN, BYTETOBINARY(bytes[6]));
Serial.print(F("ModeFan "));
Serial.println(bin3);
Serial.print(F("Checksum "));
Serial.print(bin1);
if (checksum == bytes[8]) {
Serial.println(F(" matches"));
} else {
Serial.println(F(" does not match real"));
Serial.print(F("checksum "));
Serial.println(bin2);
}
return true;
}
return false;
}
bool decodeCarrier2(byte *bytes, int byteCount)
{
// If this looks like a Carrier code...
if ( byteCount == 12 && ((bytes[0] == 0x4D && bytes[1] == 0xB2) || (bytes[0] == 0xAD && bytes[1] == 0x52)) && (memcmp(bytes, bytes+6, 6) == 0)) {
Serial.println(F("Looks like a Carrier protocol #2"));
if (bytes[0] == 0xAD && bytes[1] == 0x52)
{
if (bytes[4] == 0x55)
{
Serial.println(F("MODE: Frost guard"));
}
else
{
Serial.println(F("MODE: Turbo mode"));
}
}
else
{
switch (bytes[2] & 0x20) {
case 0x00:
Serial.println(F("POWER: OFF"));
break;
case 0x20:
Serial.println(F("POWER: ON"));
break;
}
switch (bytes[2] & 0x07) {
case 0x05:
Serial.println(F("FAN: AUTO"));
break;
case 0x00:
Serial.println(F("FAN: AUTO/DRY AUTO"));
break;
case 0x01:
Serial.println(F("FAN: 1"));
break;
case 0x02:
Serial.println(F("FAN: 2"));
break;
case 0x04:
Serial.println(F("FAN: 3"));
break;
}
switch (bytes[4] & 0x30) {
case 0x10:
Serial.println(F("MODE: AUTO"));
break;
case 0x00:
Serial.println(F("MODE: COOL"));
break;
case 0x30:
Serial.println(F("MODE: HEAT"));
break;
case 0x20:
if ((bytes[4] & 0x0F) == 0x07) {
Serial.println(F("MODE: FAN"));
} else {
Serial.println(F("MODE: DRY"));
}
break;
}
const byte temperatures[] = { 17, 28, 24, 25, 20, 29, 21, 31, 18, 27, 23, 26, 19, 30, 22 };
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Serial.print(F("Temperature: "));
Serial.println(temperatures[bytes[4] & 0x0F]);
}
// Check if the checksum matches
uint8_t checksum1 = ~bytes[2];
uint8_t checksum2 = ~bytes[4];
if (checksum1 == bytes[3] && checksum2 == bytes[5]) {
Serial.println(F("Checksum matches"));
} else {
Serial.println(F("Checksum does not match"));
}
return true;
}
return false;
}
bool decodeCarrier(byte *bytes, int byteCount)
{
return decodeCarrier1(bytes, byteCount) || decodeCarrier2(bytes, byteCount);
}