-
Notifications
You must be signed in to change notification settings - Fork 838
/
Copy pathir_Hitachi.cpp
1996 lines (1802 loc) · 74.2 KB
/
ir_Hitachi.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2019 David Conran
/// @file
/// @brief Support for Hitachi A/C protocols.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/417
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/453
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/973
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1056
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1060
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1134
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1757
#include "ir_Hitachi.h"
#include <algorithm>
#include <cstring>
#ifndef ARDUINO
#include <string>
#endif
#include "IRrecv.h"
#include "IRremoteESP8266.h"
#include "IRsend.h"
#include "IRtext.h"
#include "IRutils.h"
// Constants
const uint16_t kHitachiAcHdrMark = 3300;
const uint16_t kHitachiAcHdrSpace = 1700;
const uint16_t kHitachiAc1HdrMark = 3400;
const uint16_t kHitachiAc1HdrSpace = 3400;
const uint16_t kHitachiAcBitMark = 400;
const uint16_t kHitachiAcOneSpace = 1250;
const uint16_t kHitachiAcZeroSpace = 500;
const uint32_t kHitachiAcMinGap = kDefaultMessageGap; // Just a guess.
// Support for HitachiAc424 protocol
const uint16_t kHitachiAc424LdrMark = 29784; // Leader
const uint16_t kHitachiAc424LdrSpace = 49290; // Leader
const uint16_t kHitachiAc424HdrMark = 3416; // Header
const uint16_t kHitachiAc424HdrSpace = 1604; // Header
const uint16_t kHitachiAc424BitMark = 463;
const uint16_t kHitachiAc424OneSpace = 1208;
const uint16_t kHitachiAc424ZeroSpace = 372;
// Support for HitachiAc3 protocol
const uint16_t kHitachiAc3HdrMark = 3400; // Header
const uint16_t kHitachiAc3HdrSpace = 1660; // Header
const uint16_t kHitachiAc3BitMark = 460;
const uint16_t kHitachiAc3OneSpace = 1250;
const uint16_t kHitachiAc3ZeroSpace = 410;
using irutils::addBoolToString;
using irutils::addIntToString;
using irutils::addLabeledString;
using irutils::addModeToString;
using irutils::addModelToString;
using irutils::addFanToString;
using irutils::addTempToString;
using irutils::checkInvertedBytePairs;
using irutils::invertBytePairs;
using irutils::minsToString;
#if (SEND_HITACHI_AC || SEND_HITACHI_AC2 || SEND_HITACHI_AC264 || \
SEND_HITACHI_AC344)
/// Send a Hitachi 28-byte/224-bit A/C formatted message. (HITACHI_AC)
/// Status: STABLE / Working.
/// @param[in] data The message to be sent.
/// @param[in] nbytes The number of bytes of message to be sent.
/// @param[in] repeat The number of times the command is to be repeated.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/417
void IRsend::sendHitachiAC(const unsigned char data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kHitachiAcStateLength)
return; // Not enough bytes to send a proper message.
bool MSBfirst = true;
switch (nbytes) {
case kHitachiAc264StateLength:
case kHitachiAc296StateLength:
case kHitachiAc344StateLength:
MSBfirst = false;
}
sendGeneric(kHitachiAcHdrMark, kHitachiAcHdrSpace, kHitachiAcBitMark,
kHitachiAcOneSpace, kHitachiAcBitMark, kHitachiAcZeroSpace,
kHitachiAcBitMark, kHitachiAcMinGap, data, nbytes, 38, MSBfirst,
repeat, 50);
}
#endif // (SEND_HITACHI_AC || SEND_HITACHI_AC2 || SEND_HITACHI_AC264 ||
// SEND_HITACHI_AC344)
#if SEND_HITACHI_AC1
/// Send a Hitachi 13 byte/224-bit A/C formatted message. (HITACHI_AC1)
/// Status: STABLE / Confirmed Working.
/// @param[in] data The message to be sent.
/// @param[in] nbytes The number of bytes of message to be sent.
/// @param[in] repeat The number of times the command is to be repeated.
/// @note Basically the same as sendHitachiAC() except different size & header.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/453
void IRsend::sendHitachiAC1(const unsigned char data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kHitachiAc1StateLength)
return; // Not enough bytes to send a proper message.
sendGeneric(kHitachiAc1HdrMark, kHitachiAc1HdrSpace, kHitachiAcBitMark,
kHitachiAcOneSpace, kHitachiAcBitMark, kHitachiAcZeroSpace,
kHitachiAcBitMark, kHitachiAcMinGap, data, nbytes, kHitachiAcFreq,
true, repeat, kDutyDefault);
}
#endif // SEND_HITACHI_AC1
#if SEND_HITACHI_AC2
/// Send a Hitachi 53 byte/424-bit A/C formatted message. (HITACHI_AC2)
/// Basically the same as sendHitachiAC() except different size.
/// Status: STABLE / Expected to work.
/// @param[in] data The message to be sent.
/// @param[in] nbytes The number of bytes of message to be sent.
/// @param[in] repeat The number of times the command is to be repeated.
void IRsend::sendHitachiAC2(const unsigned char data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kHitachiAc2StateLength)
return; // Not enough bytes to send a proper message.
sendHitachiAC(data, nbytes, repeat);
}
#endif // SEND_HITACHI_AC2
#if SEND_HITACHI_AC344
/// Send a Hitachi A/C 43-byte/344-bit message. (HITACHI_AC344)
/// Basically the same as sendHitachiAC() except different size.
/// Status: Beta / Probably works.
/// @param[in] data An array of bytes containing the IR command.
/// @param[in] nbytes Nr. of bytes of data in the array.
/// @param[in] repeat Nr. of times the message is to be repeated. (Default = 0).
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1134
void IRsend::sendHitachiAc344(const unsigned char data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kHitachiAc344StateLength)
return; // Not enough bytes to send a proper message.
sendHitachiAC(data, nbytes, repeat);
}
#endif // SEND_HITACHI_AC344
/// 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?
IRHitachiAc::IRHitachiAc(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 IRHitachiAc::stateReset(void) {
_.raw[0] = 0x80;
_.raw[1] = 0x08;
_.raw[2] = 0x0C;
_.raw[3] = 0x02;
_.raw[4] = 0xFD;
_.raw[5] = 0x80;
_.raw[6] = 0x7F;
_.raw[7] = 0x88;
_.raw[8] = 0x48;
_.raw[9] = 0x10;
for (uint8_t i = 10; i < kHitachiAcStateLength; i++) _.raw[i] = 0x00;
_.raw[14] = 0x60;
_.raw[15] = 0x60;
_.raw[24] = 0x80;
setTemp(23);
}
/// Set up hardware to be able to send a message.
void IRHitachiAc::begin(void) { _irsend.begin(); }
/// Calculate the checksum for a given state.
/// @param[in] state The value to calc the checksum of.
/// @param[in] length The size/length of the state.
/// @return The calculated checksum value.
uint8_t IRHitachiAc::calcChecksum(const uint8_t state[],
const uint16_t length) {
uint8_t sum = 62;
for (uint16_t i = 0; i < length - 1; i++) sum -= reverseBits(state[i], 8);
return reverseBits(sum, 8);
}
/// Calculate and set the checksum values for the internal state.
/// @param[in] length The size/length of the state.
void IRHitachiAc::checksum(const uint16_t length) {
_.Sum = calcChecksum(_.raw, length);
}
/// Verify the checksum is valid for a given state.
/// @param[in] state The array to verify the checksum of.
/// @param[in] length The length of the state array.
/// @return true, if the state has a valid checksum. Otherwise, false.
bool IRHitachiAc::validChecksum(const uint8_t state[], const uint16_t length) {
if (length < 2) return true; // Assume true for lengths that are too short.
return (state[length - 1] == calcChecksum(state, length));
}
/// Get a PTR to the internal state/code for this protocol.
/// @return PTR to a code for this protocol based on the current internal state.
uint8_t *IRHitachiAc::getRaw(void) {
checksum();
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 The length of the new_code array.
void IRHitachiAc::setRaw(const uint8_t new_code[], const uint16_t length) {
std::memcpy(_.raw, new_code, std::min(length, kHitachiAcStateLength));
}
#if SEND_HITACHI_AC
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRHitachiAc::send(const uint16_t repeat) {
_irsend.sendHitachiAC(getRaw(), kHitachiAcStateLength, repeat);
}
#endif // SEND_HITACHI_AC
/// Get the value of the current power setting.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc::getPower(void) const {
return _.Power;
}
/// Change the power setting.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRHitachiAc::setPower(const bool on) {
_.Power = on;
}
/// Change the power setting to On.
void IRHitachiAc::on(void) { setPower(true); }
/// Change the power setting to Off.
void IRHitachiAc::off(void) { setPower(false); }
/// Get the operating mode setting of the A/C.
/// @return The current operating mode setting.
uint8_t IRHitachiAc::getMode(void) const { return reverseBits(_.Mode, 8); }
/// Set the operating mode of the A/C.
/// @param[in] mode The desired operating mode.
void IRHitachiAc::setMode(const uint8_t mode) {
uint8_t newmode = mode;
switch (mode) {
// Fan mode sets a special temp.
case kHitachiAcFan: setTemp(64); break;
case kHitachiAcAuto:
case kHitachiAcHeat:
case kHitachiAcCool:
case kHitachiAcDry: break;
default: newmode = kHitachiAcAuto;
}
_.Mode = reverseBits(newmode, 8);
if (mode != kHitachiAcFan) setTemp(_previoustemp);
setFan(getFan()); // Reset the fan speed after the mode change.
}
/// Get the current temperature setting.
/// @return The current setting for temp. in degrees celsius.
uint8_t IRHitachiAc::getTemp(void) const {
return reverseBits(_.Temp, 8) >> 1;
}
/// Set the temperature.
/// @param[in] celsius The temperature in degrees celsius.
void IRHitachiAc::setTemp(const uint8_t celsius) {
uint8_t temp;
if (celsius != 64) _previoustemp = celsius;
switch (celsius) {
case 64:
temp = celsius;
break;
default:
temp = std::min(celsius, kHitachiAcMaxTemp);
temp = std::max(temp, kHitachiAcMinTemp);
}
_.Temp = reverseBits(temp << 1, 8);
if (temp == kHitachiAcMinTemp)
_.raw[9] = 0x90;
else
_.raw[9] = 0x10;
}
/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRHitachiAc::getFan(void) const { return reverseBits(_.Fan, 8); }
/// Set the speed of the fan.
/// @param[in] speed The desired setting.
void IRHitachiAc::setFan(const uint8_t speed) {
uint8_t fanmin = kHitachiAcFanAuto;
uint8_t fanmax = kHitachiAcFanHigh;
switch (getMode()) {
case kHitachiAcDry: // Only 2 x low speeds in Dry mode.
fanmin = kHitachiAcFanLow;
fanmax = kHitachiAcFanLow + 1;
break;
case kHitachiAcFan:
fanmin = kHitachiAcFanLow; // No Auto in Fan mode.
break;
}
uint8_t newspeed = std::max(speed, fanmin);
newspeed = std::min(newspeed, fanmax);
_.Fan = reverseBits(newspeed, 8);
}
/// Get the Vertical Swing setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc::getSwingVertical(void) const {
return _.SwingV;
}
/// Set the Vertical Swing setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRHitachiAc::setSwingVertical(const bool on) {
_.SwingV = on;
}
/// Get the Horizontal Swing setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc::getSwingHorizontal(void) const {
return _.SwingH;
}
/// Set the Horizontal Swing setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRHitachiAc::setSwingHorizontal(const bool on) {
_.SwingH = on;
}
/// 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 IRHitachiAc::convertMode(const stdAc::opmode_t mode) {
switch (mode) {
case stdAc::opmode_t::kCool: return kHitachiAcCool;
case stdAc::opmode_t::kHeat: return kHitachiAcHeat;
case stdAc::opmode_t::kDry: return kHitachiAcDry;
case stdAc::opmode_t::kFan: return kHitachiAcFan;
default: return kHitachiAcAuto;
}
}
/// 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 IRHitachiAc::convertFan(const stdAc::fanspeed_t speed) {
switch (speed) {
case stdAc::fanspeed_t::kMin:
case stdAc::fanspeed_t::kLow: return kHitachiAcFanLow;
case stdAc::fanspeed_t::kMedium: return kHitachiAcFanLow + 1;
case stdAc::fanspeed_t::kHigh: return kHitachiAcFanHigh - 1;
case stdAc::fanspeed_t::kMax: return kHitachiAcFanHigh;
default: return kHitachiAcFanAuto;
}
}
/// 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 IRHitachiAc::toCommonMode(const uint8_t mode) {
switch (mode) {
case kHitachiAcCool: return stdAc::opmode_t::kCool;
case kHitachiAcHeat: return stdAc::opmode_t::kHeat;
case kHitachiAcDry: return stdAc::opmode_t::kDry;
case kHitachiAcFan: 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 IRHitachiAc::toCommonFanSpeed(const uint8_t speed) {
switch (speed) {
case kHitachiAcFanHigh: return stdAc::fanspeed_t::kMax;
case kHitachiAcFanHigh - 1: return stdAc::fanspeed_t::kHigh;
case kHitachiAcFanLow + 1: return stdAc::fanspeed_t::kMedium;
case kHitachiAcFanLow: return stdAc::fanspeed_t::kLow;
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 IRHitachiAc::toCommon(void) const {
stdAc::state_t result{};
result.protocol = decode_type_t::HITACHI_AC;
result.model = -1; // No models used.
result.power = _.Power;
result.mode = toCommonMode(getMode());
result.celsius = true;
result.degrees = getTemp();
result.fanspeed = toCommonFanSpeed(getFan());
result.swingv = (_.SwingV ? stdAc::swingv_t::kAuto : stdAc::swingv_t::kOff);
result.swingh = (_.SwingH ? stdAc::swingh_t::kAuto : stdAc::swingh_t::kOff);
// Not supported.
result.quiet = false;
result.turbo = false;
result.clean = false;
result.econo = false;
result.filter = false;
result.light = false;
result.beep = false;
result.sleep = -1;
result.clock = -1;
return result;
}
/// Convert the current internal state into a human readable string.
/// @return A human readable string.
String IRHitachiAc::toString(void) const {
String result = "";
result.reserve(110); // Reserve some heap for the string to reduce fragging.
result += addBoolToString(_.Power, kPowerStr, false);
result += addModeToString(getMode(), kHitachiAcAuto, kHitachiAcCool,
kHitachiAcHeat, kHitachiAcDry, kHitachiAcFan);
result += addTempToString(getTemp());
result += addFanToString(getFan(), kHitachiAcFanHigh, kHitachiAcFanLow,
kHitachiAcFanAuto, kHitachiAcFanAuto,
kHitachiAcFanMed);
result += addBoolToString(_.SwingV, kSwingVStr);
result += addBoolToString(_.SwingH, kSwingHStr);
return result;
}
/// 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?
IRHitachiAc1::IRHitachiAc1(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 IRHitachiAc1::stateReset(void) {
for (uint8_t i = 0; i < kHitachiAc1StateLength; i++) _.raw[i] = 0x00;
// Copy in a known good state.
_.raw[0] = 0xB2;
_.raw[1] = 0xAE;
_.raw[2] = 0x4D;
_.raw[3] = 0x91;
_.raw[4] = 0xF0;
_.raw[5] = 0xE1;
_.raw[6] = 0xA4;
_.raw[11] = 0x61;
_.raw[12] = 0x24;
}
/// Set up hardware to be able to send a message.
void IRHitachiAc1::begin(void) { _irsend.begin(); }
/// Calculate the checksum for a given state.
/// @param[in] state The value to calc the checksum of.
/// @param[in] length The size/length of the state.
/// @return The calculated checksum value.
uint8_t IRHitachiAc1::calcChecksum(const uint8_t state[],
const uint16_t length) {
uint8_t sum = 0;
for (uint16_t i = kHitachiAc1ChecksumStartByte; i < length - 1; i++) {
sum += reverseBits(GETBITS8(state[i], kLowNibble, kNibbleSize),
kNibbleSize);
sum += reverseBits(GETBITS8(state[i], kHighNibble, kNibbleSize),
kNibbleSize);
}
return reverseBits(sum, 8);
}
/// Calculate and set the checksum values for the internal state.
/// @param[in] length The size/length of the state.
void IRHitachiAc1::checksum(const uint16_t length) {
_.Sum = calcChecksum(_.raw, length);
}
/// Verify the checksum is valid for a given state.
/// @param[in] state The array to verify the checksum of.
/// @param[in] length The length of the state array.
/// @return true, if the state has a valid checksum. Otherwise, false.
bool IRHitachiAc1::validChecksum(const uint8_t state[], const uint16_t length) {
if (length < 2) return true; // Assume true for lengths that are too short.
return (state[length - 1] == calcChecksum(state, length));
}
/// Get a PTR to the internal state/code for this protocol.
/// @return PTR to a code for this protocol based on the current internal state.
uint8_t *IRHitachiAc1::getRaw(void) {
checksum();
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 The length of the new_code array.
void IRHitachiAc1::setRaw(const uint8_t new_code[], const uint16_t length) {
std::memcpy(_.raw, new_code, std::min(length, kHitachiAc1StateLength));
}
#if SEND_HITACHI_AC
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRHitachiAc1::send(const uint16_t repeat) {
_irsend.sendHitachiAC1(getRaw(), kHitachiAc1StateLength, repeat);
// Clear the toggle bits as we have actioned them by sending them.
setPowerToggle(false);
setSwingToggle(false);
}
#endif // SEND_HITACHI_AC
/// Get/Detect the model of the A/C.
/// @return The enum of the compatible model.
hitachi_ac1_remote_model_t IRHitachiAc1::getModel(void) const {
switch (_.Model) {
case kHitachiAc1Model_B: return hitachi_ac1_remote_model_t::R_LT0541_HTA_B;
default: return hitachi_ac1_remote_model_t::R_LT0541_HTA_A;
}
}
/// Set the model of the A/C to emulate.
/// @param[in] model The enum of the appropriate model.
void IRHitachiAc1::setModel(const hitachi_ac1_remote_model_t model) {
uint8_t value = 0;
switch (model) {
case hitachi_ac1_remote_model_t::R_LT0541_HTA_B:
value = kHitachiAc1Model_B;
break;
default:
value = kHitachiAc1Model_A; // i.e. 'A' mode.
}
_.Model = value;
}
/// Get the value of the current power setting.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc1::getPower(void) const {
return _.Power;
}
/// Change the power setting.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRHitachiAc1::setPower(const bool on) {
// If the power changes, set the power toggle bit.
if (on != _.Power) setPowerToggle(true);
_.Power = on;
}
/// Get the value of the current power toggle setting.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc1::getPowerToggle(void) const {
return _.PowerToggle;
}
/// Change the power toggle setting.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRHitachiAc1::setPowerToggle(const bool on) {
_.PowerToggle = on;
}
/// Change the power setting to On.
void IRHitachiAc1::on(void) { setPower(true); }
/// Change the power setting to Off.
void IRHitachiAc1::off(void) { setPower(false); }
/// Get the operating mode setting of the A/C.
/// @return The current operating mode setting.
uint8_t IRHitachiAc1::getMode(void) const {
return _.Mode;
}
/// Set the operating mode of the A/C.
/// @param[in] mode The desired operating mode.
void IRHitachiAc1::setMode(const uint8_t mode) {
switch (mode) {
case kHitachiAc1Auto:
setTemp(kHitachiAc1TempAuto);
// FALL THRU
case kHitachiAc1Fan:
case kHitachiAc1Heat:
case kHitachiAc1Cool:
case kHitachiAc1Dry:
_.Mode = mode;
break;
default:
setTemp(kHitachiAc1TempAuto);
_.Mode = kHitachiAc1Auto;
break;
}
setSleep(_.Sleep); // Correct the sleep mode if required.
setFan(_.Fan); // Correct the fan speed if required.
}
/// Get the current temperature setting.
/// @return The current setting for temp. in degrees celsius.
uint8_t IRHitachiAc1::getTemp(void) const {
return reverseBits(_.Temp, kHitachiAc1TempSize) + kHitachiAc1TempDelta;
}
/// Set the temperature.
/// @param[in] celsius The temperature in degrees celsius.
void IRHitachiAc1::setTemp(const uint8_t celsius) {
if (_.Mode == kHitachiAc1Auto) return; // Can't change temp in Auto mode.
uint8_t temp = std::min(celsius, kHitachiAcMaxTemp);
temp = std::max(temp, kHitachiAcMinTemp);
temp -= kHitachiAc1TempDelta;
temp = reverseBits(temp, kHitachiAc1TempSize);
_.Temp = temp;
}
/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRHitachiAc1::getFan(void) const {
return _.Fan;
}
/// Set the speed of the fan.
/// @param[in] speed The desired setting.
/// @param[in] force Deprecated
void IRHitachiAc1::setFan(const uint8_t speed, const bool /*force*/) {
// restrictions
switch (_.Mode) {
case kHitachiAc1Dry:
_.Fan = kHitachiAc1FanLow; // Dry is locked to Low speed.
return;
case kHitachiAc1Auto:
_.Fan = kHitachiAc1FanAuto; // Auto is locked to Auto speed.
return;
case kHitachiAc1Heat:
case kHitachiAc1Fan: // Auto speed not allowed in these modes.
if (speed == kHitachiAc1FanAuto || _.Fan == kHitachiAc1FanAuto)
_.Fan = kHitachiAc1FanLow;
return;
}
switch (speed) {
case kHitachiAc1FanAuto:
case kHitachiAc1FanHigh:
case kHitachiAc1FanMed:
case kHitachiAc1FanLow:
_.Fan = speed;
break;
default: _.Fan = kHitachiAc1FanAuto;
}
}
/// Get the Swing Toggle setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc1::getSwingToggle(void) const {
return _.SwingToggle;
}
/// Set the Swing toggle setting of the A/C.
/// @param[in] toggle true, the setting is on. false, the setting is off.
void IRHitachiAc1::setSwingToggle(const bool toggle) {
_.SwingToggle = toggle;
}
/// Get the Vertical Swing setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc1::getSwingV(void) const {
return _.SwingV;
}
/// Set the Vertical Swing setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRHitachiAc1::setSwingV(const bool on) {
_.SwingV = on;
}
/// Get the Horizontal Swing setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRHitachiAc1::getSwingH(void) const {
return _.SwingH;
}
/// Set the Horizontal Swing setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRHitachiAc1::setSwingH(const bool on) {
_.SwingH = on;
}
/// Get the Sleep setting of the A/C.
/// @return The currently configured sleep mode.
/// @note Sleep modes only available in Auto & Cool modes, otherwise it's off.
uint8_t IRHitachiAc1::getSleep(void) const {
return _.Sleep;
}
/// Set the Sleep setting of the A/C.
/// @param[in] mode The mode of sleep to set the A/C to.
/// @note Sleep modes only available in Auto & Cool modes, otherwise it's off.
void IRHitachiAc1::setSleep(const uint8_t mode) {
switch (_.Mode) {
case kHitachiAc1Auto:
case kHitachiAc1Cool:
_.Sleep = std::min(mode, kHitachiAc1Sleep4);
break;
default:
_.Sleep = kHitachiAc1SleepOff;
}
}
/// Set the On Timer time.
/// @param[in] mins The time expressed in total number of minutes.
void IRHitachiAc1::setOnTimer(const uint16_t mins) {
const uint16_t mins_lsb = reverseBits(mins, kHitachiAc1TimerSize);
_.OnTimerLow = GETBITS16(mins_lsb, 8, 8);
_.OnTimerHigh = GETBITS16(mins_lsb, 0, 8);
}
/// Get the On Timer vtime of the A/C.
/// @return Nr of minutes the timer is set to.
uint16_t IRHitachiAc1::getOnTimer(void) const {
return reverseBits(
(_.OnTimerLow << 8) | _.OnTimerHigh, kHitachiAc1TimerSize);
}
/// Set the Off Timer time.
/// @param[in] mins The time expressed in total number of minutes.
void IRHitachiAc1::setOffTimer(const uint16_t mins) {
const uint16_t mins_lsb = reverseBits(mins, kHitachiAc1TimerSize);
_.OffTimerLow = GETBITS16(mins_lsb, 8, 8);
_.OffTimerHigh = GETBITS16(mins_lsb, 0, 8);
}
/// Get the Off Timer vtime of the A/C.
/// @return Nr of minutes the timer is set to.
uint16_t IRHitachiAc1::getOffTimer(void) const {
return reverseBits(
(_.OffTimerLow << 8) | _.OffTimerHigh, kHitachiAc1TimerSize);
}
/// 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 IRHitachiAc1::convertMode(const stdAc::opmode_t mode) {
switch (mode) {
case stdAc::opmode_t::kCool: return kHitachiAc1Cool;
case stdAc::opmode_t::kHeat: return kHitachiAc1Heat;
case stdAc::opmode_t::kDry: return kHitachiAc1Dry;
case stdAc::opmode_t::kFan: return kHitachiAc1Fan;
default: return kHitachiAc1Auto;
}
}
/// 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 IRHitachiAc1::convertFan(const stdAc::fanspeed_t speed) {
switch (speed) {
case stdAc::fanspeed_t::kMin:
case stdAc::fanspeed_t::kLow: return kHitachiAc1FanLow;
case stdAc::fanspeed_t::kMedium: return kHitachiAc1FanMed;
case stdAc::fanspeed_t::kHigh:
case stdAc::fanspeed_t::kMax: return kHitachiAc1FanHigh;
default: return kHitachiAc1FanAuto;
}
}
/// 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 IRHitachiAc1::toCommonMode(const uint8_t mode) {
switch (mode) {
case kHitachiAc1Cool: return stdAc::opmode_t::kCool;
case kHitachiAc1Heat: return stdAc::opmode_t::kHeat;
case kHitachiAc1Dry: return stdAc::opmode_t::kDry;
case kHitachiAc1Fan: 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 IRHitachiAc1::toCommonFanSpeed(const uint8_t speed) {
switch (speed) {
case kHitachiAc1FanHigh: return stdAc::fanspeed_t::kMax;
case kHitachiAc1FanMed: return stdAc::fanspeed_t::kMedium;
case kHitachiAc1FanLow: return stdAc::fanspeed_t::kLow;
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 IRHitachiAc1::toCommon(void) const {
stdAc::state_t result{};
result.protocol = decode_type_t::HITACHI_AC1;
result.model = getModel();
result.power = _.Power;
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.swingh = _.SwingH ? stdAc::swingh_t::kAuto :
stdAc::swingh_t::kOff;
result.sleep = _.Sleep ? 0 : -1;
// Not supported.
result.quiet = false;
result.turbo = false;
result.clean = false;
result.econo = false;
result.filter = false;
result.light = 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 IRHitachiAc1::toString(void) const {
String result = "";
result.reserve(170); // Reserve some heap for the string to reduce fragging.
result += addModelToString(decode_type_t::HITACHI_AC1, getModel(), false);
result += addBoolToString(_.Power, kPowerStr);
result += addBoolToString(_.PowerToggle, kPowerToggleStr);
result += addModeToString(_.Mode, kHitachiAc1Auto, kHitachiAc1Cool,
kHitachiAc1Heat, kHitachiAc1Dry, kHitachiAc1Fan);
result += addTempToString(getTemp());
result += addFanToString(_.Fan, kHitachiAc1FanHigh, kHitachiAc1FanLow,
kHitachiAc1FanAuto, kHitachiAc1FanAuto,
kHitachiAc1FanMed);
result += addBoolToString(_.SwingToggle, kSwingVToggleStr);
result += addBoolToString(_.SwingV, kSwingVStr);
result += addBoolToString(_.SwingH, kSwingHStr);
result += addLabeledString(_.Sleep ? uint64ToString(_.Sleep) : kOffStr,
kSleepStr);
result += addLabeledString(getOnTimer() ? minsToString(getOnTimer())
: kOffStr,
kOnTimerStr);
result += addLabeledString(getOffTimer() ? minsToString(getOffTimer())
: kOffStr,
kOffTimerStr);
return result;
}
#if (DECODE_HITACHI_AC || DECODE_HITACHI_AC1 || DECODE_HITACHI_AC2 || \
DECODE_HITACHI_AC344 || DECODE_HITACHI_AC264)
/// Decode the supplied Hitachi A/C message.
/// Status: STABLE / Expected to work.
/// @param[in,out] results Ptr to the data to decode & where to store the 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.
/// Typically kHitachiAcBits, kHitachiAc1Bits, kHitachiAc2Bits,
/// kHitachiAc344Bits, kHitachiAc264Bits
/// @param[in] strict Flag indicating if we should perform strict matching.
/// @param[in] MSBfirst Is the data per byte stored in MSB First (true) or
/// LSB First order(false)?
/// @return True if it can decode it, false if it can't.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/417
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/453
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1134
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1729
bool IRrecv::decodeHitachiAC(decode_results *results, uint16_t offset,
const uint16_t nbits, const bool strict,
const bool MSBfirst) {
const uint8_t k_tolerance = _tolerance + 5;
if (strict) {
switch (nbits) {
case kHitachiAcBits:
case kHitachiAc1Bits:
case kHitachiAc2Bits:
case kHitachiAc264Bits:
case kHitachiAc344Bits:
break; // Okay to continue.
default:
return false; // Not strictly a Hitachi message.
}
}
uint16_t hmark;
uint32_t hspace;
if (nbits == kHitachiAc1Bits) {
hmark = kHitachiAc1HdrMark;
hspace = kHitachiAc1HdrSpace;
} else {
hmark = kHitachiAcHdrMark;
hspace = kHitachiAcHdrSpace;
}
// Match Header + Data + Footer
if (!matchGeneric(results->rawbuf + offset, results->state,
results->rawlen - offset, nbits,
hmark, hspace,
kHitachiAcBitMark, kHitachiAcOneSpace,
kHitachiAcBitMark, kHitachiAcZeroSpace,
kHitachiAcBitMark, kHitachiAcMinGap, true,
k_tolerance, kMarkExcess, MSBfirst)) return false;
// Compliance
if (strict) {
const uint16_t nbytes = nbits / 8;
switch (nbytes) {
case kHitachiAcStateLength:
if (!IRHitachiAc::validChecksum(results->state, nbytes)) return false;
break;
case kHitachiAc1StateLength:
if (!IRHitachiAc1::validChecksum(results->state, nbytes)) return false;
break;
case kHitachiAc264StateLength:
case kHitachiAc344StateLength:
if (!IRHitachiAc3::hasInvertedStates(results->state, nbytes))
return false;
break;
}
}
// Success
switch (nbits) {
case kHitachiAc1Bits:
results->decode_type = decode_type_t::HITACHI_AC1;
break;
case kHitachiAc2Bits:
results->decode_type = decode_type_t::HITACHI_AC2;
break;
case kHitachiAc264Bits:
results->decode_type = decode_type_t::HITACHI_AC264;
break;
case kHitachiAc344Bits:
results->decode_type = decode_type_t::HITACHI_AC344;
break;
case kHitachiAcBits:
default:
results->decode_type = decode_type_t::HITACHI_AC;
}
results->bits = nbits;
// No need to record the state as we stored it as we decoded it.
// As we use result->state, we don't record value, address, or command as it
// is a union data type.
return true;
}
#endif // (DECODE_HITACHI_AC || DECODE_HITACHI_AC1 || DECODE_HITACHI_AC2 ||
// DECODE_HITACHI_AC344 || DECODE_HITACHI_AC264)
#if SEND_HITACHI_AC424
/// Send a Hitachi 53-byte/424-bit A/C formatted message. (HITACHI_AC424)
/// Status: STABLE / Reported as working.
/// @param[in] data The message to be sent.
/// @param[in] nbytes The number of bytes of message to be sent.
/// @param[in] repeat The number of times the command is to be repeated.
/// @note This protocol is almost exactly the same as HitachiAC2 except this
/// variant has a leader section as well, and subtle timing differences.
/// It is also in LSBF order (per byte), rather than MSBF order.
void IRsend::sendHitachiAc424(const uint8_t data[], const uint16_t nbytes,
const uint16_t repeat) {
enableIROut(kHitachiAcFreq);
for (uint16_t r = 0; r <= repeat; r++) {
// Leader
mark(kHitachiAc424LdrMark);
space(kHitachiAc424LdrSpace);
// Header + Data + Footer
sendGeneric(kHitachiAc424HdrMark, kHitachiAc424HdrSpace,
kHitachiAc424BitMark, kHitachiAc424OneSpace,
kHitachiAc424BitMark, kHitachiAc424ZeroSpace,
kHitachiAc424BitMark, kHitachiAcMinGap,
data, nbytes, // Bytes
kHitachiAcFreq, false, kNoRepeat, kDutyDefault);
}
}
#endif // SEND_HITACHI_AC424
#if DECODE_HITACHI_AC424
/// Decode the supplied Hitachi 53-byte/424-bit A/C message.
/// Status: STABLE / Reported as working.
/// @param[in,out] results Ptr to the data to decode & where to store the 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 True if it can decode it, false if it can't.
/// @note This protocol is almost exactly the same as HitachiAC2 except this
/// variant has a leader section as well, and subtle timing differences.
/// It is also in LSBF order (per byte), rather than MSBF order.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/973
/// @see (Japanese Manual) https://kadenfan.hitachi.co.jp/support/raj/item/docs/ras_aj22h_a_tori.pdf
bool IRrecv::decodeHitachiAc424(decode_results *results, uint16_t offset,
const uint16_t nbits,
const bool strict) {
if (results->rawlen < 2 * nbits + kHeader + kHeader + kFooter - 1 + offset)
return false; // Too short a message to match.
if (strict && nbits != kHitachiAc424Bits)
return false;
uint16_t used;
// Leader
if (!matchMark(results->rawbuf[offset++], kHitachiAc424LdrMark))
return false;
if (!matchSpace(results->rawbuf[offset++], kHitachiAc424LdrSpace))
return false;
// Header + Data + Footer
used = matchGeneric(results->rawbuf + offset, results->state,
results->rawlen - offset, nbits,
kHitachiAc424HdrMark, kHitachiAc424HdrSpace,
kHitachiAc424BitMark, kHitachiAc424OneSpace,
kHitachiAc424BitMark, kHitachiAc424ZeroSpace,
kHitachiAc424BitMark, kHitachiAcMinGap, true,