-
Notifications
You must be signed in to change notification settings - Fork 839
/
Copy pathir_Daikin.cpp
3929 lines (3520 loc) · 146 KB
/
ir_Daikin.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 2016 sillyfrog
// Copyright 2017 sillyfrog, crankyoldgit
// Copyright 2018-2022 crankyoldgit
// Copyright 2019 pasna (IRDaikin160 class / Daikin176 class)
/// @file
/// @brief Support for Daikin A/C protocols.
/// @see Daikin http://harizanov.com/2012/02/control-daikin-air-conditioner-over-the-internet/
/// @see Daikin https://github.com/mharizanov/Daikin-AC-remote-control-over-the-Internet/tree/master/IRremote
/// @see Daikin http://rdlab.cdmt.vn/project-2013/daikin-ir-protocol
/// @see Daikin https://github.com/blafois/Daikin-IR-Reverse
/// @see Daikin128 https://github.com/crankyoldgit/IRremoteESP8266/issues/827
/// @see Daikin152 https://github.com/crankyoldgit/IRremoteESP8266/issues/873
/// @see Daikin152 https://github.com/ToniA/arduino-heatpumpir/blob/master/DaikinHeatpumpARC480A14IR.cpp
/// @see Daikin152 https://github.com/ToniA/arduino-heatpumpir/blob/master/DaikinHeatpumpARC480A14IR.h
/// @see Daikin160 https://github.com/crankyoldgit/IRremoteESP8266/issues/731
/// @see Daikin2 https://docs.google.com/spreadsheets/d/1f8EGfIbBUo2B-CzUFdrgKQprWakoYNKM80IKZN4KXQE/edit#gid=236366525&range=B25:D32
/// @see Daikin2 https://github.com/crankyoldgit/IRremoteESP8266/issues/582
/// @see Daikin2 https://github.com/crankyoldgit/IRremoteESP8266/issues/1535
/// @see Daikin2 https://www.daikin.co.nz/sites/default/files/daikin-split-system-US7-FTXZ25-50NV1B.pdf
/// @see Daikin216 https://github.com/crankyoldgit/IRremoteESP8266/issues/689
/// @see Daikin216 https://github.com/danny-source/Arduino_DY_IRDaikin
/// @see Daikin64 https://github.com/crankyoldgit/IRremoteESP8266/issues/1064
/// @see Daikin200 https://github.com/crankyoldgit/IRremoteESP8266/issues/1802
#include "ir_Daikin.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::addDayToString;
using irutils::addIntToString;
using irutils::addLabeledString;
using irutils::addModeToString;
using irutils::addSwingHToString;
using irutils::addSwingVToString;
using irutils::addTempToString;
using irutils::addTempFloatToString;
using irutils::addFanToString;
using irutils::bcdToUint8;
using irutils::minsToString;
using irutils::setBit;
using irutils::setBits;
using irutils::sumNibbles;
using irutils::uint8ToBcd;
#if SEND_DAIKIN
/// Send a Daikin 280-bit A/C formatted message.
/// Status: STABLE
/// @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/mharizanov/Daikin-AC-remote-control-over-the-Internet/tree/master/IRremote
/// @see https://github.com/blafois/Daikin-IR-Reverse
void IRsend::sendDaikin(const unsigned char data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kDaikinStateLengthShort)
return; // Not enough bytes to send a proper message.
for (uint16_t r = 0; r <= repeat; r++) {
uint16_t offset = 0;
// Send the header, 0b00000
sendGeneric(0, 0, // No header for the header
kDaikinBitMark, kDaikinOneSpace, kDaikinBitMark,
kDaikinZeroSpace, kDaikinBitMark, kDaikinZeroSpace + kDaikinGap,
static_cast<uint64_t>(0b00000), kDaikinHeaderLength, 38, false,
0, 50);
// Data #1
if (nbytes < kDaikinStateLength) { // Are we using the legacy size?
// Do this as a constant to save RAM and keep in flash memory
sendGeneric(kDaikinHdrMark, kDaikinHdrSpace, kDaikinBitMark,
kDaikinOneSpace, kDaikinBitMark, kDaikinZeroSpace,
kDaikinBitMark, kDaikinZeroSpace + kDaikinGap,
kDaikinFirstHeader64, 64, 38, false, 0, 50);
} else { // We are using the newer/more correct size.
sendGeneric(kDaikinHdrMark, kDaikinHdrSpace, kDaikinBitMark,
kDaikinOneSpace, kDaikinBitMark, kDaikinZeroSpace,
kDaikinBitMark, kDaikinZeroSpace + kDaikinGap,
data, kDaikinSection1Length, 38, false, 0, 50);
offset += kDaikinSection1Length;
}
// Data #2
sendGeneric(kDaikinHdrMark, kDaikinHdrSpace, kDaikinBitMark,
kDaikinOneSpace, kDaikinBitMark, kDaikinZeroSpace,
kDaikinBitMark, kDaikinZeroSpace + kDaikinGap,
data + offset, kDaikinSection2Length, 38, false, 0, 50);
offset += kDaikinSection2Length;
// Data #3
sendGeneric(kDaikinHdrMark, kDaikinHdrSpace, kDaikinBitMark,
kDaikinOneSpace, kDaikinBitMark, kDaikinZeroSpace,
kDaikinBitMark, kDaikinZeroSpace + kDaikinGap,
data + offset, nbytes - offset, 38, false, 0, 50);
}
}
#endif // SEND_DAIKIN
/// 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?
IRDaikinESP::IRDaikinESP(const uint16_t pin, const bool inverted,
const bool use_modulation)
: _irsend(pin, inverted, use_modulation) { stateReset(); }
/// Set up hardware to be able to send a message.
void IRDaikinESP::begin(void) { _irsend.begin(); }
#if SEND_DAIKIN
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRDaikinESP::send(const uint16_t repeat) {
_irsend.sendDaikin(getRaw(), kDaikinStateLength, repeat);
}
#endif // SEND_DAIKIN
/// 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 IRDaikinESP::validChecksum(uint8_t state[], const uint16_t length) {
// Data #1
if (length < kDaikinSection1Length ||
state[kDaikinByteChecksum1] != sumBytes(state, kDaikinSection1Length - 1))
return false;
// Data #2
if (length < kDaikinSection1Length + kDaikinSection2Length ||
state[kDaikinByteChecksum2] != sumBytes(state + kDaikinSection1Length,
kDaikinSection2Length - 1))
return false;
// Data #3
if (length < kDaikinSection1Length + kDaikinSection2Length + 2 ||
state[length - 1] != sumBytes(state + kDaikinSection1Length +
kDaikinSection2Length,
length - (kDaikinSection1Length +
kDaikinSection2Length) - 1))
return false;
return true;
}
/// Calculate and set the checksum values for the internal state.
void IRDaikinESP::checksum(void) {
_.Sum1 = sumBytes(_.raw, kDaikinSection1Length - 1);
_.Sum2 = sumBytes(_.raw + kDaikinSection1Length, kDaikinSection2Length - 1);
_.Sum3 = sumBytes(_.raw + kDaikinSection1Length + kDaikinSection2Length,
kDaikinSection3Length - 1);
}
/// Reset the internal state to a fixed known good state.
void IRDaikinESP::stateReset(void) {
for (uint8_t i = 0; i < kDaikinStateLength; i++) _.raw[i] = 0x0;
_.raw[0] = 0x11;
_.raw[1] = 0xDA;
_.raw[2] = 0x27;
_.raw[4] = 0xC5;
// _.raw[7] is a checksum byte, it will be set by checksum().
_.raw[8] = 0x11;
_.raw[9] = 0xDA;
_.raw[10] = 0x27;
_.raw[12] = 0x42;
// _.raw[15] is a checksum byte, it will be set by checksum().
_.raw[16] = 0x11;
_.raw[17] = 0xDA;
_.raw[18] = 0x27;
_.raw[21] = 0x49;
_.raw[22] = 0x1E;
_.raw[24] = 0xB0;
_.raw[27] = 0x06;
_.raw[28] = 0x60;
_.raw[31] = 0xC0;
// _.raw[34] is a checksum byte, it will be set by checksum().
checksum();
}
/// 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 *IRDaikinESP::getRaw(void) {
checksum(); // Ensure correct settings before sending.
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 IRDaikinESP::setRaw(const uint8_t new_code[], const uint16_t length) {
uint8_t offset = 0;
if (length == kDaikinStateLengthShort) { // Handle the "short" length case.
offset = kDaikinStateLength - kDaikinStateLengthShort;
stateReset();
}
for (uint8_t i = 0; i < length && i < kDaikinStateLength; i++)
_.raw[i + offset] = new_code[i];
}
/// Change the power setting to On.
void IRDaikinESP::on(void) { setPower(true); }
/// Change the power setting to Off.
void IRDaikinESP::off(void) { setPower(false); }
/// Change the power setting.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setPower(const bool on) {
_.Power = on;
}
/// Get the value of the current power setting.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getPower(void) const {
return _.Power;
}
/// Set the temperature.
/// @param[in] temp The temperature in degrees celsius.
void IRDaikinESP::setTemp(const float temp) {
float degrees = std::max(temp, static_cast<float>(kDaikinMinTemp));
degrees = std::min(degrees, static_cast<float>(kDaikinMaxTemp));
_.Temp = degrees * 2.0f;
}
/// Get the current temperature setting.
/// @return The current setting for temp. in degrees celsius.
float IRDaikinESP::getTemp(void) const { return _.Temp / 2.0f; }
/// Set the speed of the fan.
/// @param[in] fan The desired setting.
/// @note 1-5 or kDaikinFanAuto or kDaikinFanQuiet
void IRDaikinESP::setFan(const uint8_t fan) {
// Set the fan speed bits, leave low 4 bits alone
uint8_t fanset;
if (fan == kDaikinFanQuiet || fan == kDaikinFanAuto)
fanset = fan;
else if (fan < kDaikinFanMin || fan > kDaikinFanMax)
fanset = kDaikinFanAuto;
else
fanset = 2 + fan;
_.Fan = fanset;
}
/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRDaikinESP::getFan(void) const {
uint8_t fan = _.Fan;
if (fan != kDaikinFanQuiet && fan != kDaikinFanAuto) fan -= 2;
return fan;
}
/// Get the operating mode setting of the A/C.
/// @return The current operating mode setting.
uint8_t IRDaikinESP::getMode(void) const {
return _.Mode;
}
/// Set the operating mode of the A/C.
/// @param[in] mode The desired operating mode.
void IRDaikinESP::setMode(const uint8_t mode) {
switch (mode) {
case kDaikinAuto:
case kDaikinCool:
case kDaikinHeat:
case kDaikinFan:
case kDaikinDry:
_.Mode = mode;
break;
default:
_.Mode = kDaikinAuto;
}
}
/// Set the Vertical Swing mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setSwingVertical(const bool on) {
_.SwingV = (on ? kDaikinSwingOn : kDaikinSwingOff);
}
/// Get the Vertical Swing mode of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getSwingVertical(void) const {
return _.SwingV;
}
/// Set the Horizontal Swing mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setSwingHorizontal(const bool on) {
_.SwingH = (on ? kDaikinSwingOn : kDaikinSwingOff);
}
/// Get the Horizontal Swing mode of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getSwingHorizontal(void) const {
return _.SwingH;
}
/// Set the Quiet mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setQuiet(const bool on) {
_.Quiet = on;
// Powerful & Quiet mode being on are mutually exclusive.
if (on) setPowerful(false);
}
/// Get the Quiet mode status of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getQuiet(void) const {
return _.Quiet;
}
/// Set the Powerful (Turbo) mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setPowerful(const bool on) {
_.Powerful = on;
if (on) {
// Powerful, Quiet, & Econo mode being on are mutually exclusive.
setQuiet(false);
setEcono(false);
}
}
/// Get the Powerful (Turbo) mode of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getPowerful(void) const {
return _.Powerful;
}
/// Set the Sensor mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setSensor(const bool on) {
_.Sensor = on;
}
/// Get the Sensor mode of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getSensor(void) const {
return _.Sensor;
}
/// Set the Economy mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setEcono(const bool on) {
_.Econo = on;
// Powerful & Econo mode being on are mutually exclusive.
if (on) setPowerful(false);
}
/// Get the Economical mode of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getEcono(void) const {
return _.Econo;
}
/// Set the Mould mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setMold(const bool on) {
_.Mold = on;
}
/// Get the Mould mode status of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getMold(void) const {
return _.Mold;
}
/// Set the Comfort mode of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setComfort(const bool on) {
_.Comfort = on;
}
/// Get the Comfort mode of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getComfort(void) const {
return _.Comfort;
}
/// Set the enable status & time of the On Timer.
/// @param[in] starttime The number of minutes past midnight.
void IRDaikinESP::enableOnTimer(const uint16_t starttime) {
_.OnTimer = true;
_.OnTime = starttime;
}
/// Clear and disable the On timer.
void IRDaikinESP::disableOnTimer(void) {
_.OnTimer = false;
_.OnTime = kDaikinUnusedTime;
}
/// Get the On Timer time to be sent to the A/C unit.
/// @return The number of minutes past midnight.
uint16_t IRDaikinESP::getOnTime(void) const {
return _.OnTime;
}
/// Get the enable status of the On Timer.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getOnTimerEnabled(void) const {
return _.OnTimer;
}
/// Set the enable status & time of the Off Timer.
/// @param[in] endtime The number of minutes past midnight.
void IRDaikinESP::enableOffTimer(const uint16_t endtime) {
_.OffTimer = true;
_.OffTime = endtime;
}
/// Clear and disable the Off timer.
void IRDaikinESP::disableOffTimer(void) {
_.OffTimer = false;
_.OffTime = kDaikinUnusedTime;
}
/// Get the Off Timer time to be sent to the A/C unit.
/// @return The number of minutes past midnight.
uint16_t IRDaikinESP::getOffTime(void) const {
return _.OffTime;
}
/// Get the enable status of the Off Timer.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getOffTimerEnabled(void) const {
return _.OffTimer;
}
/// Set the clock on the A/C unit.
/// @param[in] mins_since_midnight Nr. of minutes past midnight.
void IRDaikinESP::setCurrentTime(const uint16_t mins_since_midnight) {
uint16_t mins = mins_since_midnight;
if (mins > 24 * 60) mins = 0; // If > 23:59, set to 00:00
_.CurrentTime = mins;
}
/// Get the clock time to be sent to the A/C unit.
/// @return The number of minutes past midnight.
uint16_t IRDaikinESP::getCurrentTime(void) const {
return _.CurrentTime;
}
/// Set the current day of the week to be sent to the A/C unit.
/// @param[in] day_of_week The numerical representation of the day of the week.
/// @note 1 is SUN, 2 is MON, ..., 7 is SAT
void IRDaikinESP::setCurrentDay(const uint8_t day_of_week) {
_.CurrentDay = day_of_week;
}
/// Get the current day of the week to be sent to the A/C unit.
/// @return The numerical representation of the day of the week.
/// @note 1 is SUN, 2 is MON, ..., 7 is SAT
uint8_t IRDaikinESP::getCurrentDay(void) const {
return _.CurrentDay;
}
/// Set the enable status of the Weekly Timer.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikinESP::setWeeklyTimerEnable(const bool on) {
// Bit is cleared for `on`.
_.WeeklyTimer = !on;
}
/// Get the enable status of the Weekly Timer.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikinESP::getWeeklyTimerEnable(void) const {
return !_.WeeklyTimer;
}
/// 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 IRDaikinESP::convertMode(const stdAc::opmode_t mode) {
switch (mode) {
case stdAc::opmode_t::kCool: return kDaikinCool;
case stdAc::opmode_t::kHeat: return kDaikinHeat;
case stdAc::opmode_t::kDry: return kDaikinDry;
case stdAc::opmode_t::kFan: return kDaikinFan;
default: return kDaikinAuto;
}
}
/// 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 IRDaikinESP::convertFan(const stdAc::fanspeed_t speed) {
switch (speed) {
case stdAc::fanspeed_t::kMin: return kDaikinFanQuiet;
case stdAc::fanspeed_t::kLow: return kDaikinFanMin;
case stdAc::fanspeed_t::kMedium: return kDaikinFanMed;
case stdAc::fanspeed_t::kHigh: return kDaikinFanMax - 1;
case stdAc::fanspeed_t::kMax: return kDaikinFanMax;
default: return kDaikinFanAuto;
}
}
/// 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 IRDaikinESP::toCommonMode(const uint8_t mode) {
switch (mode) {
case kDaikinCool: return stdAc::opmode_t::kCool;
case kDaikinHeat: return stdAc::opmode_t::kHeat;
case kDaikinDry: return stdAc::opmode_t::kDry;
case kDaikinFan: 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 IRDaikinESP::toCommonFanSpeed(const uint8_t speed) {
switch (speed) {
case kDaikinFanMax: return stdAc::fanspeed_t::kMax;
case kDaikinFanMax - 1: return stdAc::fanspeed_t::kHigh;
case kDaikinFanMed:
case kDaikinFanMin + 1: return stdAc::fanspeed_t::kMedium;
case kDaikinFanMin: return stdAc::fanspeed_t::kLow;
case kDaikinFanQuiet: 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 IRDaikinESP::toCommon(void) const {
stdAc::state_t result{};
result.protocol = decode_type_t::DAIKIN;
result.model = -1; // No models used.
result.power = _.Power;
result.mode = toCommonMode(_.Mode);
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;
result.quiet = _.Quiet;
result.turbo = _.Powerful;
result.clean = _.Mold;
result.econo = _.Econo;
// Not supported.
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 IRDaikinESP::toString(void) const {
String result = "";
result.reserve(230); // Reserve some heap for the string to reduce fragging.
result += addBoolToString(_.Power, kPowerStr, false);
result += addModeToString(_.Mode, kDaikinAuto, kDaikinCool, kDaikinHeat,
kDaikinDry, kDaikinFan);
result += addTempFloatToString(getTemp());
result += addFanToString(getFan(), kDaikinFanMax, kDaikinFanMin,
kDaikinFanAuto, kDaikinFanQuiet, kDaikinFanMed);
result += addBoolToString(_.Powerful, kPowerfulStr);
result += addBoolToString(_.Quiet, kQuietStr);
result += addBoolToString(getSensor(), kSensorStr);
result += addBoolToString(_.Mold, kMouldStr);
result += addBoolToString(_.Comfort, kComfortStr);
result += addBoolToString(_.SwingH, kSwingHStr);
result += addBoolToString(_.SwingV, kSwingVStr);
result += addLabeledString(minsToString(_.CurrentTime), kClockStr);
result += addDayToString(_.CurrentDay, -1);
result += addLabeledString(_.OnTimer
? minsToString(_.OnTime) : kOffStr,
kOnTimerStr);
result += addLabeledString(_.OffTimer
? minsToString(_.OffTime) : kOffStr,
kOffTimerStr);
result += addBoolToString(getWeeklyTimerEnable(), kWeeklyTimerStr);
return result;
}
#if DECODE_DAIKIN
/// Decode the supplied Daikin 280-bit message. (DAIKIN)
/// Status: STABLE / Reported as working.
/// @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.
/// @see https://github.com/mharizanov/Daikin-AC-remote-control-over-the-Internet/tree/master/IRremote
bool IRrecv::decodeDaikin(decode_results *results, uint16_t offset,
const uint16_t nbits, const bool strict) {
// Is there enough data to match successfully?
if (results->rawlen < (2 * (nbits + kDaikinHeaderLength) +
kDaikinSections * (kHeader + kFooter) + kFooter - 1) +
offset)
return false;
// Compliance
if (strict && nbits != kDaikinBits) return false;
match_result_t data_result;
// Header #1 - Doesn't count as data.
data_result = matchData(&(results->rawbuf[offset]), kDaikinHeaderLength,
kDaikinBitMark, kDaikinOneSpace,
kDaikinBitMark, kDaikinZeroSpace,
kDaikinTolerance, kDaikinMarkExcess, false);
offset += data_result.used;
if (data_result.success == false) return false; // Fail
if (data_result.data) return false; // The header bits should be zero.
// Footer
if (!matchMark(results->rawbuf[offset++], kDaikinBitMark,
kDaikinTolerance, kDaikinMarkExcess)) return false;
if (!matchSpace(results->rawbuf[offset++], kDaikinZeroSpace + kDaikinGap,
kDaikinTolerance, kDaikinMarkExcess)) return false;
// Sections
const uint8_t ksectionSize[kDaikinSections] = {
kDaikinSection1Length, kDaikinSection2Length, kDaikinSection3Length};
uint16_t pos = 0;
for (uint8_t section = 0; section < kDaikinSections; section++) {
uint16_t used;
// Section Header + Section Data (7 bytes) + Section Footer
used = matchGeneric(results->rawbuf + offset, results->state + pos,
results->rawlen - offset, ksectionSize[section] * 8,
kDaikinHdrMark, kDaikinHdrSpace,
kDaikinBitMark, kDaikinOneSpace,
kDaikinBitMark, kDaikinZeroSpace,
kDaikinBitMark, kDaikinZeroSpace + kDaikinGap,
section >= kDaikinSections - 1,
kDaikinTolerance, kDaikinMarkExcess, false);
if (used == 0) return false;
offset += used;
pos += ksectionSize[section];
}
// Compliance
if (strict) {
// Re-check we got the correct size/length due to the way we read the data.
if (pos * 8 != kDaikinBits) return false;
// Validate the checksum.
if (!IRDaikinESP::validChecksum(results->state)) return false;
}
// Success
results->decode_type = DAIKIN;
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_DAIKIN
#if SEND_DAIKIN2
/// Send a Daikin2 (312-bit) A/C formatted message.
/// 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.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/582
void IRsend::sendDaikin2(const unsigned char data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kDaikin2Section1Length)
return; // Not enough bytes to send a partial message.
for (uint16_t r = 0; r <= repeat; r++) {
// Leader
sendGeneric(kDaikin2LeaderMark, kDaikin2LeaderSpace,
0, 0, 0, 0, 0, 0, static_cast<uint64_t>(0), // No data payload.
0, kDaikin2Freq, false, 0, 50);
// Section #1
sendGeneric(kDaikin2HdrMark, kDaikin2HdrSpace, kDaikin2BitMark,
kDaikin2OneSpace, kDaikin2BitMark, kDaikin2ZeroSpace,
kDaikin2BitMark, kDaikin2Gap, data, kDaikin2Section1Length,
kDaikin2Freq, false, 0, 50);
// Section #2
sendGeneric(kDaikin2HdrMark, kDaikin2HdrSpace, kDaikin2BitMark,
kDaikin2OneSpace, kDaikin2BitMark, kDaikin2ZeroSpace,
kDaikin2BitMark, kDaikin2Gap, data + kDaikin2Section1Length,
nbytes - kDaikin2Section1Length,
kDaikin2Freq, false, 0, 50);
}
}
#endif // SEND_DAIKIN2
/// 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?
IRDaikin2::IRDaikin2(const uint16_t pin, const bool inverted,
const bool use_modulation)
: _irsend(pin, inverted, use_modulation) { stateReset(); }
/// Set up hardware to be able to send a message.
void IRDaikin2::begin(void) { _irsend.begin(); }
#if SEND_DAIKIN2
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRDaikin2::send(const uint16_t repeat) {
_irsend.sendDaikin2(getRaw(), kDaikin2StateLength, repeat);
}
#endif // SEND_DAIKIN2
/// 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 IRDaikin2::validChecksum(uint8_t state[], const uint16_t length) {
// Validate the checksum of section #1.
if (length <= kDaikin2Section1Length - 1 ||
state[kDaikin2Section1Length - 1] != sumBytes(state,
kDaikin2Section1Length - 1))
return false;
// Validate the checksum of section #2 (a.k.a. the rest)
if (length <= kDaikin2Section1Length + 1 ||
state[length - 1] != sumBytes(state + kDaikin2Section1Length,
length - kDaikin2Section1Length - 1))
return false;
return true;
}
/// Calculate and set the checksum values for the internal state.
void IRDaikin2::checksum(void) {
_.Sum1 = sumBytes(_.raw, kDaikin2Section1Length - 1);
_.Sum2 = sumBytes(_.raw + kDaikin2Section1Length, kDaikin2Section2Length - 1);
}
/// Reset the internal state to a fixed known good state.
void IRDaikin2::stateReset(void) {
for (uint8_t i = 0; i < kDaikin2StateLength; i++) _.raw[i] = 0x0;
_.raw[0] = 0x11;
_.raw[1] = 0xDA;
_.raw[2] = 0x27;
_.raw[4] = 0x01;
_.raw[6] = 0xC0;
_.raw[7] = 0x70;
_.raw[8] = 0x08;
_.raw[9] = 0x0C;
_.raw[10] = 0x80;
_.raw[11] = 0x04;
_.raw[12] = 0xB0;
_.raw[13] = 0x16;
_.raw[14] = 0x24;
_.raw[17] = 0xBE;
_.raw[18] = 0xD0;
// _.raw[19] is a checksum byte, it will be set by checksum().
_.raw[20] = 0x11;
_.raw[21] = 0xDA;
_.raw[22] = 0x27;
_.raw[25] = 0x08;
_.raw[28] = 0xA0;
_.raw[35] = 0xC1;
_.raw[36] = 0x80;
_.raw[37] = 0x60;
// _.raw[38] is a checksum byte, it will be set by checksum().
disableOnTimer();
disableOffTimer();
disableSleepTimer();
checksum();
}
/// 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 *IRDaikin2::getRaw(void) {
checksum(); // Ensure correct settings before sending.
return _.raw;
}
/// Set the internal state from a valid code for this protocol.
/// @param[in] new_code A valid code for this protocol.
void IRDaikin2::setRaw(const uint8_t new_code[]) {
std::memcpy(_.raw, new_code, kDaikin2StateLength);
}
/// Change the power setting to On.
void IRDaikin2::on(void) { setPower(true); }
/// Change the power setting to Off.
void IRDaikin2::off(void) { setPower(false); }
/// Change the power setting.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRDaikin2::setPower(const bool on) {
_.Power = on;
_.Power2 = !on;
}
/// Get the value of the current power setting.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikin2::getPower(void) const { return _.Power && !_.Power2; }
/// Get the operating mode setting of the A/C.
/// @return The current operating mode setting.
uint8_t IRDaikin2::getMode(void) const { return _.Mode; }
/// Set the operating mode of the A/C.
/// @param[in] desired_mode The desired operating mode.
void IRDaikin2::setMode(const uint8_t desired_mode) {
uint8_t mode = desired_mode;
switch (mode) {
case kDaikinCool:
case kDaikinHeat:
case kDaikinFan:
case kDaikinDry: break;
default: mode = kDaikinAuto;
}
_.Mode = mode;
// Redo the temp setting as Cool mode has a different min temp.
if (mode == kDaikinCool) setTemp(getTemp());
setHumidity(getHumidity()); // Make sure the humidity is okay for this mode.
}
/// Set the temperature.
/// @param[in] desired The temperature in degrees celsius.
void IRDaikin2::setTemp(const uint8_t desired) {
// The A/C has a different min temp if in cool mode.
uint8_t temp = std::max(
(_.Mode == kDaikinCool) ? kDaikin2MinCoolTemp : kDaikinMinTemp,
desired);
_.Temp = std::min(kDaikinMaxTemp, temp);
// If the humidity setting is in use, the temp is a fixed value.
if (_.HumidOn) _.Temp = kDaikinMaxTemp;
}
/// Get the current temperature setting.
/// @return The current setting for temp. in degrees celsius.
uint8_t IRDaikin2::getTemp(void) const { return _.Temp; }
/// Set the speed of the fan.
/// @param[in] fan The desired setting.
/// @note 1-5 or kDaikinFanAuto or kDaikinFanQuiet
void IRDaikin2::setFan(const uint8_t fan) {
uint8_t fanset;
if (fan == kDaikinFanQuiet || fan == kDaikinFanAuto)
fanset = fan;
else if (fan < kDaikinFanMin || fan > kDaikinFanMax)
fanset = kDaikinFanAuto;
else
fanset = 2 + fan;
_.Fan = fanset;
}
/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRDaikin2::getFan(void) const {
const uint8_t fan = _.Fan;
switch (fan) {
case kDaikinFanAuto:
case kDaikinFanQuiet: return fan;
default: return fan - 2;
}
}
/// Set the Vertical Swing mode of the A/C.
/// @param[in] position The position/mode to set the swing to.
void IRDaikin2::setSwingVertical(const uint8_t position) {
switch (position) {
case kDaikin2SwingVHighest:
case kDaikin2SwingVHigh:
case kDaikin2SwingVUpperMiddle:
case kDaikin2SwingVLowerMiddle:
case kDaikin2SwingVLow:
case kDaikin2SwingVLowest:
case kDaikin2SwingVOff:
case kDaikin2SwingVBreeze:
case kDaikin2SwingVCirculate:
case kDaikin2SwingVAuto:
_.SwingV = position;
}
}
/// Get the Vertical Swing mode of the A/C.
/// @return The native position/mode setting.
uint8_t IRDaikin2::getSwingVertical(void) const { return _.SwingV; }
/// Convert a stdAc::swingv_t enum into it's native setting.
/// @param[in] position The enum to be converted.
/// @return The native equivalent of the enum.
uint8_t IRDaikin2::convertSwingV(const stdAc::swingv_t position) {
switch (position) {
case stdAc::swingv_t::kHighest:
case stdAc::swingv_t::kHigh:
case stdAc::swingv_t::kMiddle:
case stdAc::swingv_t::kLow:
case stdAc::swingv_t::kLowest:
return (uint8_t)position + kDaikin2SwingVHighest;
case stdAc::swingv_t::kOff:
return kDaikin2SwingVOff;
default:
return kDaikin2SwingVAuto;
}
}
/// Convert a native vertical swing postion to it's common equivalent.
/// @param[in] setting A native position to convert.
/// @return The common vertical swing position.
stdAc::swingv_t IRDaikin2::toCommonSwingV(const uint8_t setting) {
switch (setting) {
case kDaikin2SwingVHighest: return stdAc::swingv_t::kHighest;
case kDaikin2SwingVHigh: return stdAc::swingv_t::kHigh;
case kDaikin2SwingVUpperMiddle:
case kDaikin2SwingVLowerMiddle: return stdAc::swingv_t::kMiddle;
case kDaikin2SwingVLow: return stdAc::swingv_t::kLow;
case kDaikin2SwingVLowest: return stdAc::swingv_t::kLowest;
case kDaikin2SwingVOff: return stdAc::swingv_t::kOff;
default: return stdAc::swingv_t::kAuto;
}
}
/// Set the Horizontal Swing mode of the A/C.
/// @param[in] position The position/mode to set the swing to.
void IRDaikin2::setSwingHorizontal(const uint8_t position) {
_.SwingH = position;
}
/// Get the Horizontal Swing mode of the A/C.
/// @return The native position/mode setting.
uint8_t IRDaikin2::getSwingHorizontal(void) const { return _.SwingH; }
/// Set the clock on the A/C unit.
/// @param[in] numMins Nr. of minutes past midnight.
void IRDaikin2::setCurrentTime(const uint16_t numMins) {
uint16_t mins = numMins;
if (numMins > 24 * 60) mins = 0; // If > 23:59, set to 00:00
_.CurrentTime = mins;
}
/// Get the clock time to be sent to the A/C unit.
/// @return The number of minutes past midnight.
uint16_t IRDaikin2::getCurrentTime(void) const { return _.CurrentTime; }
/// Set the enable status & time of the On Timer.
/// @param[in] starttime The number of minutes past midnight.
/// @note Timer location is shared with sleep timer.
void IRDaikin2::enableOnTimer(const uint16_t starttime) {
clearSleepTimerFlag();
_.OnTimer = true;
_.OnTime = starttime;
}
/// Clear the On Timer flag.
void IRDaikin2::clearOnTimerFlag(void) { _.OnTimer = false; }
/// Disable the On timer.
void IRDaikin2::disableOnTimer(void) {
_.OnTime = kDaikinUnusedTime;
clearOnTimerFlag();
clearSleepTimerFlag();
}
/// Get the On Timer time to be sent to the A/C unit.
/// @return The number of minutes past midnight.
uint16_t IRDaikin2::getOnTime(void) const { return _.OnTime; }
/// Get the enable status of the On Timer.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikin2::getOnTimerEnabled(void) const { return _.OnTimer; }
/// Set the enable status & time of the Off Timer.
/// @param[in] endtime The number of minutes past midnight.
void IRDaikin2::enableOffTimer(const uint16_t endtime) {
// Set the Off Timer flag.
_.OffTimer = true;
_.OffTime = endtime;
}
/// Disable the Off timer.
void IRDaikin2::disableOffTimer(void) {
_.OffTime = kDaikinUnusedTime;
// Clear the Off Timer flag.
_.OffTimer = false;
}
/// Get the Off Timer time to be sent to the A/C unit.
/// @return The number of minutes past midnight.
uint16_t IRDaikin2::getOffTime(void) const { return _.OffTime; }
/// Get the enable status of the Off Timer.
/// @return true, the setting is on. false, the setting is off.
bool IRDaikin2::getOffTimerEnabled(void) const { return _.OffTimer; }
/// Get the Beep status of the A/C.
/// @return true, the setting is on. false, the setting is off.
uint8_t IRDaikin2::getBeep(void) const { return _.Beep; }
/// Set the Beep mode of the A/C.
/// @param[in] beep true, the setting is on. false, the setting is off.
void IRDaikin2::setBeep(const uint8_t beep) { _.Beep = beep; }