-
Notifications
You must be signed in to change notification settings - Fork 852
/
Copy pathir_York.cpp
357 lines (318 loc) · 11.3 KB
/
ir_York.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2022 Daniele Gobbetti
/// @file
/// @brief Support for the York AC protocol (remote GRYLH2A)
// Note: Most of the code is autogenerated by the provided tools or assembled
// from other support classes
#include "ir_York.h"
#include <algorithm>
#include <cstring>
#ifndef ARDUINO
#include <string>
#endif
#include "IRrecv.h"
#include "IRremoteESP8266.h"
#include "IRsend.h"
#ifdef UNIT_TEST
#include "IRsend_test.h"
#endif
#include "IRtext.h"
#include "IRutils.h"
using irutils::addBoolToString;
using irutils::addModeToString;
using irutils::addFanToString;
using irutils::addTempToString;
using irutils::addLabeledString;
using irutils::minsToString;
// Constants
const uint16_t kYorkHdrMark = 4887;
const uint16_t kYorkBitMark = 612;
const uint16_t kYorkHdrSpace = 2267;
const uint16_t kYorkOneSpace = 1778;
const uint16_t kYorkZeroSpace = 579;
const uint16_t kYorkFreq = 38000; // Hz. (Guessing the most common frequency.)
#if SEND_YORK
/// Send a 17 Byte / 136 bit York A/C message.
/// Status: ALPHA / Untested.
/// @param[in] data An array of bytes containing the IR command.
/// @param[in] nbytes Nr. of bytes of data in the array. (>=kStateLength)
/// @param[in] repeat Nr. of times the message is to be repeated.
void IRsend::sendYork(const uint8_t data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kYorkStateLength)
return;
sendGeneric(kYorkHdrMark, kYorkHdrSpace,
kYorkBitMark, kYorkOneSpace,
kYorkBitMark, kYorkZeroSpace,
kYorkBitMark, kDefaultMessageGap,
data, nbytes, kYorkFreq,
false, repeat, kDutyDefault); // false == LSB
}
#endif // SEND_YORK
#if DECODE_YORK
/// Decode the supplied message.
/// Status: ALPHA / Tested, some values still are not mapped to the internal
/// state of AC
/// @param[in,out] results Ptr to the data to decode & where to store the decode
/// @param[in] offset The starting index to use when attempting to decode the
/// raw data. Typically/Defaults to kStartOffset.
/// @param[in] nbits The number of data bits to expect.
/// @param[in] strict Flag indicating if we should perform strict matching.
/// @return A boolean. True if it can decode it, false if it can't.
bool IRrecv::decodeYork(decode_results *results, uint16_t offset,
const uint16_t nbits, const bool strict) {
if (strict && nbits != kYorkBits)
return false;
uint16_t used = 0;
used = matchGeneric(results->rawbuf + offset, results->state,
results->rawlen - offset, nbits,
kYorkHdrMark, kYorkHdrSpace,
kYorkBitMark, kYorkOneSpace,
kYorkBitMark, kYorkZeroSpace,
kYorkBitMark, kDefaultMessageGap,
false, _tolerance, kMarkExcess,
false); // LSB
if (used == 0) return false; // We failed to find any data.
// Succes
results->decode_type = decode_type_t::YORK;
results->bits = nbits;
return true;
}
#endif // DECODE_YORK
//
//
/// Class constructor
/// @param[in] pin GPIO to be used when sending.
/// @param[in] inverted Is the output signal to be inverted?
/// @param[in] use_modulation Is frequency modulation to be used?
IRYorkAc::IRYorkAc(const uint16_t pin, const bool inverted,
const bool use_modulation)
: _irsend(pin, inverted, use_modulation) {
stateReset();
}
// Reset the internal state to a fixed known good state.
void IRYorkAc::stateReset() {
// This resets to a known-good state.
setRaw(kYorkKnownGoodState);
}
/// Set up hardware to be able to send a message.
void IRYorkAc::begin(void) { _irsend.begin(); }
/// Get the raw state of the object, suitable to be sent with the appropriate
/// IRsend object method.
/// @return A copy of the internal state.
uint8_t *IRYorkAc::getRaw(void) {
calcChecksum();
return _.raw;
}
/// Set the internal state from a valid code for this protocol.
/// @param[in] new_code A valid code for this protocol.
/// @param[in] length Length of the code in bytes.
void IRYorkAc::setRaw(const uint8_t new_code[], const uint16_t length) {
std::memcpy(_.raw, new_code, length);
}
#if SEND_YORK
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRYorkAc::send(const uint16_t repeat) {
_irsend.sendYork(getRaw(), kYorkStateLength, repeat);
}
#endif // SEND_YORK
/// Get the current operation mode setting.
/// @return The current operation mode.
uint8_t IRYorkAc::getMode(void) const {
return _.Mode;
}
/// Set the desired operation mode.
/// @param[in] mode The desired operation mode.
void IRYorkAc::setMode(const uint8_t mode) {
switch (mode) {
case kYorkFan:
case kYorkCool:
case kYorkHeat:
case kYorkDry:
_.Mode = mode;
break;
default:
_.Mode = kYorkAuto;
}
setFan(getFan()); // Ensure the fan is at the correct speed for the new mode.
}
/// Convert a stdAc::opmode_t enum into its native mode.
/// @param[in] mode The enum to be converted.
/// @return The native equivalent of the enum.
uint8_t IRYorkAc::convertMode(const stdAc::opmode_t mode) {
switch (mode) {
case stdAc::opmode_t::kCool: return kYorkCool;
case stdAc::opmode_t::kHeat: return kYorkHeat;
case stdAc::opmode_t::kDry: return kYorkDry;
case stdAc::opmode_t::kFan: return kYorkFan;
default: return kYorkAuto;
}
}
/// Convert a native mode into its stdAc equivalent.
/// @param[in] mode The native setting to be converted.
/// @return The stdAc equivalent of the native setting.
stdAc::opmode_t IRYorkAc::toCommonMode(const uint8_t mode) {
switch (mode) {
case kYorkCool: return stdAc::opmode_t::kCool;
case kYorkHeat: return stdAc::opmode_t::kHeat;
case kYorkDry: return stdAc::opmode_t::kDry;
case kYorkFan: return stdAc::opmode_t::kFan;
default: return stdAc::opmode_t::kAuto;
}
}
/// Set the speed of the fan.
/// @param[in] speed The desired setting.
/// @note The fan speed is locked to Low when in Dry mode, to auto when in auto
/// mode. "Fan" mode has no support for "auto" speed.
void IRYorkAc::setFan(const uint8_t speed) {
switch (getMode()) {
case kYorkDry:
_.Fan = kYorkFanLow;
break;
case kYorkFan:
_.Fan = std::min(speed, kYorkFanHigh);
break;
case kYorkAuto:
_.Fan = kYorkFanAuto;
break;
default:
_.Fan = std::min(speed, kYorkFanAuto);
}
}
/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRYorkAc::getFan(void) const {
return _.Fan;
}
/// Convert a stdAc::fanspeed_t enum into it's native speed.
/// @param[in] speed The enum to be converted.
/// @return The native equivalent of the enum.
uint8_t IRYorkAc::convertFan(const stdAc::fanspeed_t speed) {
switch (speed) {
case stdAc::fanspeed_t::kMin:
case stdAc::fanspeed_t::kLow:
return kYorkFanLow;
case stdAc::fanspeed_t::kMedium:
return kYorkFanMedium;
case stdAc::fanspeed_t::kHigh:
case stdAc::fanspeed_t::kMax:
return kYorkFanHigh;
default:
return kYorkFanAuto;
}
}
/// Convert a native fan speed into its stdAc equivalent.
/// @param[in] speed The native setting to be converted.
/// @return The stdAc equivalent of the native setting.
stdAc::fanspeed_t IRYorkAc::toCommonFanSpeed(const uint8_t speed) {
switch (speed) {
case kYorkFanHigh: return stdAc::fanspeed_t::kMax;
case kYorkFanMedium: return stdAc::fanspeed_t::kMedium;
case kYorkFanLow: return stdAc::fanspeed_t::kMin;
default: return stdAc::fanspeed_t::kAuto;
}
}
/// Set the temperature.
/// @param[in] degrees The temperature in degrees celsius.
void IRYorkAc::setTemp(const uint8_t degrees) {
_.Temp = std::min(kYorkMaxTemp, std::max(kYorkMinTemp, degrees));
}
/// Get the current temperature setting.
/// @return Get current setting for temp. in degrees celsius.
uint8_t IRYorkAc::getTemp(void) const {
return _.Temp;
}
/// Set the On Timer value of the A/C.
/// @param[in] nr_of_mins The number of minutes the timer should be.
/// @note The timer time only has a resolution of 10 mins.
/// @note Setting the On Timer active will cancel the Sleep timer/setting.
void IRYorkAc::setOnTimer(const uint16_t nr_of_mins) {
_.OnTimer = nr_of_mins / 10;
}
/// Set the Off Timer value of the A/C.
/// @param[in] nr_of_mins The number of minutes the timer should be.
/// @note The timer time only has a resolution of 10 mins.
/// @note Setting the Off Timer active will cancel the Sleep timer/setting.
void IRYorkAc::setOffTimer(const uint16_t nr_of_mins) {
_.OffTimer = nr_of_mins / 10;
}
/// Get the On Timer setting of the A/C.
/// @return The Nr. of minutes the On Timer is set for.
uint16_t IRYorkAc::getOnTimer(void) const {
return _.OnTimer * 10;
}
/// Get the Off Timer setting of the A/C.
/// @return The Nr. of minutes the Off Timer is set for.
/// @note Sleep & Off Timer share the same timer.
uint16_t IRYorkAc::getOffTimer(void) const {
return _.OffTimer * 10;
}
/// CRC16-16 (a.k.a. CRC-16-IBM)
void IRYorkAc::calcChecksum() {
uint8_t length = 14;
uint16_t reg_crc = 0x0000;
uint8_t* data = _.raw;
while (length--) {
reg_crc ^= *data++;
for (uint16_t index = 0; index < 8; index++) {
if (reg_crc & 0x01) {
reg_crc = (reg_crc>>1) ^ 0xA001;
} else {
reg_crc = reg_crc >>1;
}
}
}
_.Chk1 = (reg_crc & 0xff);
_.Chk2 = ((reg_crc >> 8) & 0x00ff);
}
/// Convert the current internal state into its stdAc::state_t equivalent.
/// @param[in] prev Ptr to the previous state if required.
/// @return The stdAc equivalent of the native settings.
stdAc::state_t IRYorkAc::toCommon(const stdAc::state_t *prev) const {
stdAc::state_t result{};
// Start with the previous state if given it.
if (prev != NULL) {
result = *prev;
} else {
// Set defaults for non-zero values that are not implicitly set for when
// there is no previous state.
// e.g. Any setting that toggles should probably go here.
result.power = false;
}
result.protocol = decode_type_t::YORK;
result.mode = toCommonMode(_.Mode);
result.celsius = true;
result.degrees = getTemp();
result.fanspeed = toCommonFanSpeed(_.Fan);
result.swingv = _.SwingV ? stdAc::swingv_t::kAuto : stdAc::swingv_t::kOff;
result.sleep = getOffTimer();
// Not supported.
result.model = -1;
result.turbo = false;
result.swingh = stdAc::swingh_t::kOff;
result.light = false;
result.filter = false;
result.econo = false;
result.quiet = false;
result.clean = false;
result.beep = false;
result.clock = -1;
return result;
}
/// Convert the current internal state into a human readable string.
/// @return A human readable string.
String IRYorkAc::toString(void) const {
String result = "";
result.reserve(70); // Reserve some heap for the string to reduce fragging.
result += addBoolToString(_.Power, kPowerStr, false);
result += addModeToString(_.Mode, kYorkAuto, kYorkCool,
kYorkHeat, kYorkDry, kYorkFan);
result += addFanToString(_.Fan, kYorkFanHigh, kYorkFanLow,
kYorkFanAuto, kYorkFanAuto,
kYorkFanMedium);
result += addTempToString(getTemp(), true);
result += addBoolToString(_.SwingV, kSwingVStr);
result += addLabeledString(minsToString(getOnTimer()), kOnTimerStr);
result += addLabeledString(minsToString(getOffTimer()), kOffTimerStr);
return result;
}