-
Notifications
You must be signed in to change notification settings - Fork 835
/
ir_Goodweather.cpp
499 lines (453 loc) · 16.7 KB
/
ir_Goodweather.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
// Copyright 2019 ribeirodanielf
// Copyright 2019 David Conran
/// @file
/// @brief Support for Goodweather compatible HVAC protocols.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/697
#include "ir_Goodweather.h"
#include <algorithm>
#ifndef ARDUINO
#include <string>
#endif
#include "IRrecv.h"
#include "IRremoteESP8266.h"
#include "IRsend.h"
#include "IRtext.h"
#include "IRutils.h"
using irutils::addBoolToString;
using irutils::addIntToString;
using irutils::addLabeledString;
using irutils::addModeToString;
using irutils::addFanToString;
using irutils::addTempToString;
using irutils::addToggleToString;
#if SEND_GOODWEATHER
/// Send a Goodweather HVAC formatted message.
/// Status: BETA / Needs testing on real device.
/// @param[in] data The message to be sent.
/// @param[in] nbits The number of bits of message to be sent.
/// @param[in] repeat The number of times the command is to be repeated.
void IRsend::sendGoodweather(const uint64_t data, const uint16_t nbits,
const uint16_t repeat) {
if (nbits != kGoodweatherBits)
return; // Wrong nr. of bits to send a proper message.
// Set IR carrier frequency
enableIROut(38);
for (uint16_t r = 0; r <= repeat; r++) {
// Header
mark(kGoodweatherHdrMark);
space(kGoodweatherHdrSpace);
// Data
for (int16_t i = 0; i < nbits; i += 8) {
uint16_t chunk = (data >> i) & 0xFF; // Grab a byte at a time.
chunk = (~chunk) << 8 | chunk; // Prepend a inverted copy of the byte.
sendData(kGoodweatherBitMark, kGoodweatherOneSpace,
kGoodweatherBitMark, kGoodweatherZeroSpace,
chunk, 16, false);
}
// Footer
mark(kGoodweatherBitMark);
space(kGoodweatherHdrSpace);
mark(kGoodweatherBitMark);
space(kDefaultMessageGap);
}
}
#endif // SEND_GOODWEATHER
/// 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?
IRGoodweatherAc::IRGoodweatherAc(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 IRGoodweatherAc::stateReset(void) { _.raw = kGoodweatherStateInit; }
/// Set up hardware to be able to send a message.
void IRGoodweatherAc::begin(void) { _irsend.begin(); }
#if SEND_GOODWEATHER
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRGoodweatherAc::send(const uint16_t repeat) {
_irsend.sendGoodweather(getRaw(), kGoodweatherBits, repeat);
}
#endif // SEND_GOODWEATHER
/// Get a copy of the internal state as a valid code for this protocol.
/// @return A valid code for this protocol based on the current internal state.
uint64_t IRGoodweatherAc::getRaw(void) { return _.raw; }
/// Set the internal state from a valid code for this protocol.
/// @param[in] state A valid code for this protocol.
void IRGoodweatherAc::setRaw(const uint64_t state) { _.raw = state; }
/// Change the power setting to On.
void IRGoodweatherAc::on(void) { setPower(true); }
/// Change the power setting to Off.
void IRGoodweatherAc::off(void) { setPower(false); }
/// Change the power setting.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRGoodweatherAc::setPower(const bool on) {
_.Command = kGoodweatherCmdPower;
_.Power = on;
}
/// Get the value of the current power setting.
/// @return true, the setting is on. false, the setting is off.
bool IRGoodweatherAc::getPower(void) const {
return _.Power;
}
/// Set the temperature.
/// @param[in] temp The temperature in degrees celsius.
void IRGoodweatherAc::setTemp(const uint8_t temp) {
uint8_t new_temp = std::max(kGoodweatherTempMin, temp);
new_temp = std::min(kGoodweatherTempMax, new_temp);
if (new_temp > getTemp()) _.Command = kGoodweatherCmdUpTemp;
if (new_temp < getTemp()) _.Command = kGoodweatherCmdDownTemp;
_.Temp = new_temp - kGoodweatherTempMin;
}
/// Get the current temperature setting.
/// @return The current setting for temp. in degrees celsius.
uint8_t IRGoodweatherAc::getTemp(void) const {
return _.Temp + kGoodweatherTempMin;
}
/// Set the speed of the fan.
/// @param[in] speed The desired setting.
void IRGoodweatherAc::setFan(const uint8_t speed) {
_.Command = kGoodweatherCmdFan;
switch (speed) {
case kGoodweatherFanAuto:
case kGoodweatherFanLow:
case kGoodweatherFanMed:
case kGoodweatherFanHigh:
_.Fan = speed;
break;
default:
_.Fan = kGoodweatherFanAuto;
}
}
/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRGoodweatherAc::getFan(void) const {
return _.Fan;
}
/// Set the operating mode of the A/C.
/// @param[in] mode The desired operating mode.
void IRGoodweatherAc::setMode(const uint8_t mode) {
_.Command = kGoodweatherCmdMode;
switch (mode) {
case kGoodweatherAuto:
case kGoodweatherDry:
case kGoodweatherCool:
case kGoodweatherFan:
case kGoodweatherHeat:
_.Mode = mode;
break;
default:
_.Mode = kGoodweatherAuto;
}
}
/// Get the operating mode setting of the A/C.
/// @return The current operating mode setting.
uint8_t IRGoodweatherAc::getMode(void) const {
return _.Mode;
}
/// Set the Light (LED) Toggle setting of the A/C.
/// @param[in] toggle true, the setting is on. false, the setting is off.
void IRGoodweatherAc::setLight(const bool toggle) {
_.Command = kGoodweatherCmdLight;
_.Light = toggle;
}
/// Get the Light (LED) Toggle setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRGoodweatherAc::getLight(void) const {
return _.Light;
}
/// Set the Sleep Toggle setting of the A/C.
/// @param[in] toggle true, the setting is on. false, the setting is off.
void IRGoodweatherAc::setSleep(const bool toggle) {
_.Command = kGoodweatherCmdSleep;
_.Sleep = toggle;
}
/// Get the Sleep Toggle setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRGoodweatherAc::getSleep(void) const {
return _.Sleep;
}
/// Set the Turbo Toggle setting of the A/C.
/// @param[in] toggle true, the setting is on. false, the setting is off.
void IRGoodweatherAc::setTurbo(const bool toggle) {
_.Command = kGoodweatherCmdTurbo;
_.Turbo = toggle;
}
/// Get the Turbo Toggle setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRGoodweatherAc::getTurbo(void) const {
return _.Turbo;
}
/// Set the Vertical Swing speed of the A/C.
/// @param[in] speed The speed to set the swing to.
void IRGoodweatherAc::setSwing(const uint8_t speed) {
_.Command = kGoodweatherCmdSwing;
switch (speed) {
case kGoodweatherSwingOff:
case kGoodweatherSwingSlow:
case kGoodweatherSwingFast:
_.Swing = speed;
break;
default:
_.Swing = kGoodweatherSwingOff;
}
}
/// Get the Vertical Swing speed of the A/C.
/// @return The native swing speed setting.
uint8_t IRGoodweatherAc::getSwing(void) const {
return _.Swing;
}
/// Set the remote Command type/button pressed.
/// @param[in] cmd The command/button that was issued/pressed.
void IRGoodweatherAc::setCommand(const uint8_t cmd) {
if (cmd <= kGoodweatherCmdLight)
_.Command = cmd;
}
/// Get the Command type/button pressed from the current settings
/// @return The command/button that was issued/pressed.
uint8_t IRGoodweatherAc::getCommand(void) const {
return _.Command;
}
/// 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 IRGoodweatherAc::convertMode(const stdAc::opmode_t mode) {
switch (mode) {
case stdAc::opmode_t::kCool: return kGoodweatherCool;
case stdAc::opmode_t::kHeat: return kGoodweatherHeat;
case stdAc::opmode_t::kDry: return kGoodweatherDry;
case stdAc::opmode_t::kFan: return kGoodweatherFan;
default: return kGoodweatherAuto;
}
}
/// 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 IRGoodweatherAc::convertFan(const stdAc::fanspeed_t speed) {
switch (speed) {
case stdAc::fanspeed_t::kMin:
case stdAc::fanspeed_t::kLow: return kGoodweatherFanLow;
case stdAc::fanspeed_t::kMedium: return kGoodweatherFanMed;
case stdAc::fanspeed_t::kHigh:
case stdAc::fanspeed_t::kMax: return kGoodweatherFanHigh;
default: return kGoodweatherFanAuto;
}
}
/// Convert a stdAc::swingv_t enum into it's native setting.
/// @param[in] swingv The enum to be converted.
/// @return The native equivalent of the enum.
uint8_t IRGoodweatherAc::convertSwingV(const stdAc::swingv_t swingv) {
switch (swingv) {
case stdAc::swingv_t::kHighest:
case stdAc::swingv_t::kHigh:
case stdAc::swingv_t::kMiddle: return kGoodweatherSwingFast;
case stdAc::swingv_t::kLow:
case stdAc::swingv_t::kLowest:
case stdAc::swingv_t::kAuto: return kGoodweatherSwingSlow;
default: return kGoodweatherSwingOff;
}
}
/// 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 IRGoodweatherAc::toCommonMode(const uint8_t mode) {
switch (mode) {
case kGoodweatherCool: return stdAc::opmode_t::kCool;
case kGoodweatherHeat: return stdAc::opmode_t::kHeat;
case kGoodweatherDry: return stdAc::opmode_t::kDry;
case kGoodweatherFan: return stdAc::opmode_t::kFan;
default: return stdAc::opmode_t::kAuto;
}
}
/// 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 IRGoodweatherAc::toCommonFanSpeed(const uint8_t speed) {
switch (speed) {
case kGoodweatherFanHigh: return stdAc::fanspeed_t::kMax;
case kGoodweatherFanMed: return stdAc::fanspeed_t::kMedium;
case kGoodweatherFanLow: return stdAc::fanspeed_t::kMin;
default: return stdAc::fanspeed_t::kAuto;
}
}
/// Convert the current internal state into its stdAc::state_t equivalent.
/// @return The stdAc equivalent of the native settings.
stdAc::state_t IRGoodweatherAc::toCommon(void) const {
stdAc::state_t result{};
result.protocol = decode_type_t::GOODWEATHER;
result.power = _.Power;
result.mode = toCommonMode(_.Mode);
result.celsius = true;
result.degrees = getTemp();
result.fanspeed = toCommonFanSpeed(_.Fan);
result.swingv = (_.Swing == kGoodweatherSwingOff ?
stdAc::swingv_t::kOff : stdAc::swingv_t::kAuto);
result.turbo = _.Turbo;
result.light = _.Light;
result.sleep = _.Sleep ? 0: -1;
// Not supported.
result.model = -1;
result.swingh = stdAc::swingh_t::kOff;
result.quiet = false;
result.econo = false;
result.filter = 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 IRGoodweatherAc::toString(void) const {
String result = "";
result.reserve(150); // Reserve some heap for the string to reduce fragging.
result += addBoolToString(_.Power, kPowerStr, false);
result += addModeToString(_.Mode, kGoodweatherAuto, kGoodweatherCool,
kGoodweatherHeat, kGoodweatherDry, kGoodweatherFan);
result += addTempToString(getTemp());
result += addFanToString(_.Fan, kGoodweatherFanHigh, kGoodweatherFanLow,
kGoodweatherFanAuto, kGoodweatherFanAuto,
kGoodweatherFanMed);
result += addToggleToString(_.Turbo, kTurboStr);
result += addToggleToString(_.Light, kLightStr);
result += addToggleToString(_.Sleep, kSleepStr);
result += addIntToString(_.Swing, kSwingStr);
result += kSpaceLBraceStr;
switch (_.Swing) {
case kGoodweatherSwingFast:
result += kFastStr;
break;
case kGoodweatherSwingSlow:
result += kSlowStr;
break;
case kGoodweatherSwingOff:
result += kOffStr;
break;
default:
result += kUnknownStr;
}
result += ')';
result += addIntToString(_.Command, kCommandStr);
result += kSpaceLBraceStr;
switch (_.Command) {
case kGoodweatherCmdPower:
result += kPowerStr;
break;
case kGoodweatherCmdMode:
result += kModeStr;
break;
case kGoodweatherCmdUpTemp:
result += kTempUpStr;
break;
case kGoodweatherCmdDownTemp:
result += kTempDownStr;
break;
case kGoodweatherCmdSwing:
result += kSwingStr;
break;
case kGoodweatherCmdFan:
result += kFanStr;
break;
case kGoodweatherCmdTimer:
result += kTimerStr;
break;
case kGoodweatherCmdAirFlow:
result += kAirFlowStr;
break;
case kGoodweatherCmdHold:
result += kHoldStr;
break;
case kGoodweatherCmdSleep:
result += kSleepStr;
break;
case kGoodweatherCmdTurbo:
result += kTurboStr;
break;
case kGoodweatherCmdLight:
result += kLightStr;
break;
default:
result += kUnknownStr;
}
result += ')';
return result;
}
#if DECODE_GOODWEATHER
/// Decode the supplied Goodweather message.
/// Status: BETA / Probably works.
/// @param[in,out] results Ptr to the data to decode & where to store the decode
/// result.
/// @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::decodeGoodweather(decode_results* results, uint16_t offset,
const uint16_t nbits,
const bool strict) {
if (results->rawlen < 2 * (2 * nbits) + kHeader + 2 * kFooter - 1 + offset)
return false; // Can't possibly be a valid Goodweather message.
if (strict && nbits != kGoodweatherBits)
return false; // Not strictly a Goodweather message.
uint64_t dataSoFar = 0;
uint16_t dataBitsSoFar = 0;
match_result_t data_result;
// Header
if (!matchMark(results->rawbuf[offset++], kGoodweatherHdrMark)) return false;
if (!matchSpace(results->rawbuf[offset++], kGoodweatherHdrSpace))
return false;
// Data
for (; offset <= results->rawlen - 32 && dataBitsSoFar < nbits;
dataBitsSoFar += 8) {
DPRINT("DEBUG: Attempting Byte #");
DPRINTLN(dataBitsSoFar / 8);
// Read in a byte at a time.
// Normal first.
data_result = matchData(&(results->rawbuf[offset]), 8,
kGoodweatherBitMark, kGoodweatherOneSpace,
kGoodweatherBitMark, kGoodweatherZeroSpace,
_tolerance + kGoodweatherExtraTolerance,
kMarkExcess, false);
if (data_result.success == false) return false;
DPRINTLN("DEBUG: Normal byte read okay.");
offset += data_result.used;
uint8_t data = (uint8_t)data_result.data;
// Then inverted.
data_result = matchData(&(results->rawbuf[offset]), 8,
kGoodweatherBitMark, kGoodweatherOneSpace,
kGoodweatherBitMark, kGoodweatherZeroSpace,
_tolerance + kGoodweatherExtraTolerance,
kMarkExcess, false);
if (data_result.success == false) return false;
DPRINTLN("DEBUG: Inverted byte read okay.");
offset += data_result.used;
uint8_t inverted = (uint8_t)data_result.data;
DPRINT("DEBUG: data = ");
DPRINTLN((uint16_t)data);
DPRINT("DEBUG: inverted = ");
DPRINTLN((uint16_t)inverted);
if (data != (inverted ^ 0xFF)) return false; // Data integrity failed.
dataSoFar |= (uint64_t)data << dataBitsSoFar;
}
// Footer.
if (!matchMark(results->rawbuf[offset++], kGoodweatherBitMark,
_tolerance + kGoodweatherExtraTolerance)) return false;
if (!matchSpace(results->rawbuf[offset++], kGoodweatherHdrSpace))
return false;
if (!matchMark(results->rawbuf[offset++], kGoodweatherBitMark,
_tolerance + kGoodweatherExtraTolerance)) return false;
if (offset <= results->rawlen &&
!matchAtLeast(results->rawbuf[offset], kGoodweatherHdrSpace))
return false;
// Compliance
if (strict && (dataBitsSoFar != kGoodweatherBits)) return false;
// Success
results->decode_type = decode_type_t::GOODWEATHER;
results->bits = dataBitsSoFar;
results->value = dataSoFar;
results->address = 0;
results->command = 0;
return true;
}
#endif // DECODE_GOODWEATHER