-
Notifications
You must be signed in to change notification settings - Fork 835
/
ir_Panasonic.cpp
1350 lines (1240 loc) · 54.3 KB
/
ir_Panasonic.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 2015 Kristian Lauszus
// Copyright 2017, 2018 David Conran
/// @file
/// @brief Support for Panasonic protocols.
/// Panasonic protocol originally added by Kristian Lauszus
/// (Thanks to zenwheel and other people at the original blog post)
/// @see Panasonic https://github.com/z3t0/Arduino-IRremote
/// @see http://www.remotecentral.com/cgi-bin/mboard/rc-pronto/thread.cgi?2615
/// @see Panasonic A/C support heavily influenced by https://github.com/ToniA/ESPEasy/blob/HeatpumpIR/lib/HeatpumpIR/PanasonicHeatpumpIR.cpp
/// Panasonic A/C Clock & Timer support:
/// Reverse Engineering by MikkelTb
/// Code by crankyoldgit
#include "ir_Panasonic.h"
#include <algorithm>
#include <cstring>
#ifndef ARDUINO
#include <string>
#endif
#include "IRrecv.h"
#include "IRsend.h"
#include "IRtext.h"
#include "IRutils.h"
// Constants
/// @see http://www.remotecentral.com/cgi-bin/mboard/rc-pronto/thread.cgi?26152
const uint16_t kPanasonicHdrMark = 3456; ///< uSeconds.
const uint16_t kPanasonicHdrSpace = 1728; ///< uSeconds.
const uint16_t kPanasonicBitMark = 432; ///< uSeconds.
const uint16_t kPanasonicOneSpace = 1296; ///< uSeconds.
const uint16_t kPanasonicZeroSpace = 432; ///< uSeconds.
const uint32_t kPanasonicMinCommandLength = 163296; ///< uSeconds.
const uint16_t kPanasonicEndGap = 5000; ///< uSeconds. See #245
const uint32_t kPanasonicMinGap = 74736; ///< uSeconds.
const uint16_t kPanasonicAcSectionGap = 10000; ///< uSeconds.
const uint16_t kPanasonicAcSection1Length = 8;
const uint32_t kPanasonicAcMessageGap = kDefaultMessageGap; // Just a guess.
const uint16_t kPanasonicAc32HdrMark = 3543; ///< uSeconds.
const uint16_t kPanasonicAc32BitMark = 920; ///< uSeconds.
const uint16_t kPanasonicAc32HdrSpace = 3450; ///< uSeconds.
const uint16_t kPanasonicAc32OneSpace = 2575; ///< uSeconds.
const uint16_t kPanasonicAc32ZeroSpace = 828; ///< uSeconds.
const uint16_t kPanasonicAc32SectionGap = 13946; ///< uSeconds.
const uint8_t kPanasonicAc32Sections = 2;
const uint8_t kPanasonicAc32BlocksPerSection = 2;
using irutils::addBoolToString;
using irutils::addFanToString;
using irutils::addIntToString;
using irutils::addLabeledString;
using irutils::addModeToString;
using irutils::addModelToString;
using irutils::addSwingHToString;
using irutils::addSwingVToString;
using irutils::addTempToString;
using irutils::minsToString;
using irutils::setBit;
using irutils::setBits;
// Used by Denon as well.
#if (SEND_PANASONIC || SEND_DENON)
/// Send a Panasonic formatted message.
/// Status: STABLE / Should be working.
/// @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.
/// @note This protocol is a modified version of Kaseikyo.
/// @note Use this method if you want to send the results of `decodePanasonic`.
void IRsend::sendPanasonic64(const uint64_t data, const uint16_t nbits,
const uint16_t repeat) {
sendGeneric(kPanasonicHdrMark, kPanasonicHdrSpace, kPanasonicBitMark,
kPanasonicOneSpace, kPanasonicBitMark, kPanasonicZeroSpace,
kPanasonicBitMark, kPanasonicMinGap, kPanasonicMinCommandLength,
data, nbits, kPanasonicFreq, true, repeat, 50);
}
/// Send a Panasonic formatted message.
/// Status: STABLE, but DEPRECATED
/// @deprecated This is only for legacy use only, please use `sendPanasonic64()`
/// instead.
/// @param[in] address The 16-bit manufacturer code.
/// @param[in] data The 32-bit data portion of 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.
/// @note This protocol is a modified version of Kaseikyo.
void IRsend::sendPanasonic(const uint16_t address, const uint32_t data,
const uint16_t nbits, const uint16_t repeat) {
sendPanasonic64(((uint64_t)address << 32) | (uint64_t)data, nbits, repeat);
}
/// Calculate the raw Panasonic data based on device, subdevice, & function.
/// Status: STABLE / Should be working.
/// @param[in] manufacturer A 16-bit manufacturer code. e.g. 0x4004 is Panasonic
/// @param[in] device An 8-bit code.
/// @param[in] subdevice An 8-bit code.
/// @param[in] function An 8-bit code.
/// @return A value suitable for use with `sendPanasonic64()`.
/// @note Panasonic 48-bit protocol is a modified version of Kaseikyo.
/// @see http://www.remotecentral.com/cgi-bin/mboard/rc-pronto/thread.cgi?2615
uint64_t IRsend::encodePanasonic(const uint16_t manufacturer,
const uint8_t device,
const uint8_t subdevice,
const uint8_t function) {
uint8_t checksum = device ^ subdevice ^ function;
return (((uint64_t)manufacturer << 32) | ((uint64_t)device << 24) |
((uint64_t)subdevice << 16) | ((uint64_t)function << 8) | checksum);
}
#endif // (SEND_PANASONIC || SEND_DENON)
// Used by Denon as well.
#if (DECODE_PANASONIC || DECODE_DENON)
/// Decode the supplied Panasonic message.
/// Status: STABLE / Should be 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] manufacturer A 16-bit manufacturer code. e.g. 0x4004 is Panasonic
/// @param[in] strict Flag indicating if we should perform strict matching.
/// @return True if it can decode it, false if it can't.
/// @warning Results to be used with `sendPanasonic64()`, not `sendPanasonic()`.
/// @note Panasonic 48-bit protocol is a modified version of Kaseikyo.
/// @see http://www.remotecentral.com/cgi-bin/mboard/rc-pronto/thread.cgi?2615
/// @see http://www.hifi-remote.com/wiki/index.php?title=Panasonic
bool IRrecv::decodePanasonic(decode_results *results, uint16_t offset,
const uint16_t nbits, const bool strict,
const uint32_t manufacturer) {
if (strict) { // Compliance checks
switch (nbits) {
case kPanasonic40Bits:
case kPanasonicBits:
break;
default:
return false; // Request is out of spec.
}
}
uint64_t data = 0;
// Match Header + Data + Footer
if (!matchGeneric(results->rawbuf + offset, &data,
results->rawlen - offset, nbits,
kPanasonicHdrMark, kPanasonicHdrSpace,
kPanasonicBitMark, kPanasonicOneSpace,
kPanasonicBitMark, kPanasonicZeroSpace,
kPanasonicBitMark, kPanasonicEndGap, true)) return false;
// Compliance
uint32_t address = data >> 32;
uint32_t command = data;
if (strict) {
if (address != manufacturer) // Verify the Manufacturer code.
return false;
// Verify the checksum.
const uint8_t checksumOrig = data;
uint8_t checksumCalc = (data >> 16) ^ (data >> 8);
if (nbits != kPanasonic40Bits)
checksumCalc ^= (data >> 24);
if (checksumOrig != checksumCalc) return false;
}
// Success
results->value = data;
results->address = address;
results->command = command;
results->decode_type = decode_type_t::PANASONIC;
results->bits = nbits;
return true;
}
#endif // (DECODE_PANASONIC || DECODE_DENON)
#if SEND_PANASONIC_AC
/// Send a Panasonic A/C message.
/// Status: STABLE / Work with real device(s).
/// @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::sendPanasonicAC(const uint8_t data[], const uint16_t nbytes,
const uint16_t repeat) {
if (nbytes < kPanasonicAcSection1Length) return;
for (uint16_t r = 0; r <= repeat; r++) {
// First section. (8 bytes)
sendGeneric(kPanasonicHdrMark, kPanasonicHdrSpace, kPanasonicBitMark,
kPanasonicOneSpace, kPanasonicBitMark, kPanasonicZeroSpace,
kPanasonicBitMark, kPanasonicAcSectionGap, data,
kPanasonicAcSection1Length, kPanasonicFreq, false, 0, 50);
// First section. (The rest of the data bytes)
sendGeneric(kPanasonicHdrMark, kPanasonicHdrSpace, kPanasonicBitMark,
kPanasonicOneSpace, kPanasonicBitMark, kPanasonicZeroSpace,
kPanasonicBitMark, kPanasonicAcMessageGap,
data + kPanasonicAcSection1Length,
nbytes - kPanasonicAcSection1Length, kPanasonicFreq, false, 0,
50);
}
}
#endif // SEND_PANASONIC_AC
/// 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?
IRPanasonicAc::IRPanasonicAc(const uint16_t pin, const bool inverted,
const bool use_modulation)
: _irsend(pin, inverted, use_modulation) { stateReset(); }
/// Reset the state of the remote to a known good state/sequence.
void IRPanasonicAc::stateReset(void) {
memcpy(remote_state, kPanasonicKnownGoodState, kPanasonicAcStateLength);
_temp = 25; // An initial saved desired temp. Completely made up.
_swingh = kPanasonicAcSwingHMiddle; // A similar made up value for H Swing.
}
/// Set up hardware to be able to send a message.
void IRPanasonicAc::begin(void) { _irsend.begin(); }
/// 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 IRPanasonicAc::validChecksum(const uint8_t *state, const uint16_t length) {
if (length < 2) return false; // 1 byte of data can't have a checksum.
return (state[length - 1] ==
sumBytes(state, length - 1, kPanasonicAcChecksumInit));
}
/// 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 IRPanasonicAc::calcChecksum(const uint8_t *state,
const uint16_t length) {
return sumBytes(state, length - 1, kPanasonicAcChecksumInit);
}
/// Calculate and set the checksum values for the internal state.
/// @param[in] length The size/length of the state.
void IRPanasonicAc::fixChecksum(const uint16_t length) {
remote_state[length - 1] = calcChecksum(remote_state, length);
}
#if SEND_PANASONIC_AC
/// Send the current internal state as an IR message.
/// @param[in] repeat Nr. of times the message will be repeated.
void IRPanasonicAc::send(const uint16_t repeat) {
_irsend.sendPanasonicAC(getRaw(), kPanasonicAcStateLength, repeat);
}
#endif // SEND_PANASONIC_AC
/// Set the model of the A/C to emulate.
/// @param[in] model The enum of the appropriate model.
void IRPanasonicAc::setModel(const panasonic_ac_remote_model_t model) {
switch (model) {
case panasonic_ac_remote_model_t::kPanasonicDke:
case panasonic_ac_remote_model_t::kPanasonicJke:
case panasonic_ac_remote_model_t::kPanasonicLke:
case panasonic_ac_remote_model_t::kPanasonicNke:
case panasonic_ac_remote_model_t::kPanasonicCkp:
case panasonic_ac_remote_model_t::kPanasonicRkr: break;
// Only proceed if we know what to do.
default: return;
}
// clear & set the various bits and bytes.
remote_state[13] &= 0xF0;
remote_state[17] = 0x00;
remote_state[21] &= 0b11101111;
remote_state[23] = 0x81;
remote_state[25] = 0x00;
switch (model) {
case kPanasonicLke:
remote_state[13] |= 0x02;
remote_state[17] = 0x06;
break;
case kPanasonicDke:
remote_state[23] = 0x01;
remote_state[25] = 0x06;
// Has to be done last as setSwingHorizontal has model check built-in
setSwingHorizontal(_swingh);
break;
case kPanasonicNke:
remote_state[17] = 0x06;
break;
case kPanasonicJke:
break;
case kPanasonicCkp:
remote_state[21] |= 0x10;
remote_state[23] = 0x01;
break;
case kPanasonicRkr:
remote_state[13] |= 0x08;
remote_state[23] = 0x89;
default:
break;
}
// Reset the Ion filter.
setIon(getIon());
}
/// Get/Detect the model of the A/C.
/// @return The enum of the compatible model.
panasonic_ac_remote_model_t IRPanasonicAc::getModel(void) {
if (remote_state[23] == 0x89) return kPanasonicRkr;
if (remote_state[17] == 0x00) {
if ((remote_state[21] & 0x10) && (remote_state[23] & 0x01))
return panasonic_ac_remote_model_t::kPanasonicCkp;
if (remote_state[23] & 0x80)
return panasonic_ac_remote_model_t::kPanasonicJke;
}
if (remote_state[17] == 0x06 && (remote_state[13] & 0x0F) == 0x02)
return panasonic_ac_remote_model_t::kPanasonicLke;
if (remote_state[23] == 0x01)
return panasonic_ac_remote_model_t::kPanasonicDke;
if (remote_state[17] == 0x06)
return panasonic_ac_remote_model_t::kPanasonicNke;
return panasonic_ac_remote_model_t::kPanasonicUnknown; // Default
}
/// 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 *IRPanasonicAc::getRaw(void) {
fixChecksum();
return remote_state;
}
/// Set the internal state from a valid code for this protocol.
/// @param[in] state A valid code for this protocol.
void IRPanasonicAc::setRaw(const uint8_t state[]) {
memcpy(remote_state, state, kPanasonicAcStateLength);
}
/// Control the power state of the A/C unit.
/// @param[in] on true, the setting is on. false, the setting is off.
/// @warning For CKP models, the remote has no memory of the power state the A/C
/// unit should be in. For those models setting this on/true will toggle the
/// power state of the Panasonic A/C unit with the next message.
/// e.g. If the A/C unit is already on, setPower(true) will turn it off.
/// If the A/C unit is already off, setPower(true) will turn it on.
/// `setPower(false)` will leave the A/C power state as it was.
/// For all other models, setPower(true) should set the internal state to
/// turn it on, and setPower(false) should turn it off.
void IRPanasonicAc::setPower(const bool on) {
setBit(&remote_state[13], kPanasonicAcPowerOffset, on);
}
/// Get the A/C power state of the remote.
/// @return true, the setting is on. false, the setting is off.
/// @warning Except for CKP models, where it returns if the power state will be
/// toggled on the A/C unit when the next message is sent.
bool IRPanasonicAc::getPower(void) {
return GETBIT8(remote_state[13], kPanasonicAcPowerOffset);
}
/// Change the power setting to On.
void IRPanasonicAc::on(void) { setPower(true); }
/// Change the power setting to Off.
void IRPanasonicAc::off(void) { setPower(false); }
/// Get the operating mode setting of the A/C.
/// @return The current operating mode setting.
uint8_t IRPanasonicAc::getMode(void) {
return GETBITS8(remote_state[13], kHighNibble, kModeBitsSize);
}
/// Set the operating mode of the A/C.
/// @param[in] desired The desired operating mode.
void IRPanasonicAc::setMode(const uint8_t desired) {
uint8_t mode = kPanasonicAcAuto; // Default to Auto mode.
switch (desired) {
case kPanasonicAcFan:
// Allegedly Fan mode has a temperature of 27.
setTemp(kPanasonicAcFanModeTemp, false);
mode = desired;
break;
case kPanasonicAcAuto:
case kPanasonicAcCool:
case kPanasonicAcHeat:
case kPanasonicAcDry:
mode = desired;
// Set the temp to the saved temp, just incase our previous mode was Fan.
setTemp(_temp);
break;
}
remote_state[13] &= 0x0F; // Clear the previous mode bits.
setBits(&remote_state[13], kHighNibble, kModeBitsSize, mode);
}
/// Get the current temperature setting.
/// @return The current setting for temp. in degrees celsius.
uint8_t IRPanasonicAc::getTemp(void) {
return GETBITS8(remote_state[14], kPanasonicAcTempOffset,
kPanasonicAcTempSize);
}
/// Set the temperature.
/// @param[in] celsius The temperature in degrees celsius.
/// @param[in] remember: A flag for the class to remember the temperature.
/// @note Automatically safely limits the temp to the operating range supported.
void IRPanasonicAc::setTemp(const uint8_t celsius, const bool remember) {
uint8_t temperature;
temperature = std::max(celsius, kPanasonicAcMinTemp);
temperature = std::min(temperature, kPanasonicAcMaxTemp);
if (remember) _temp = temperature;
setBits(&remote_state[14], kPanasonicAcTempOffset, kPanasonicAcTempSize,
temperature);
}
/// Get the current vertical swing setting.
/// @return The current position it is set to.
uint8_t IRPanasonicAc::getSwingVertical(void) {
return GETBITS8(remote_state[16], kLowNibble, kNibbleSize);
}
/// Control the vertical swing setting.
/// @param[in] desired_elevation The position to set the vertical swing to.
void IRPanasonicAc::setSwingVertical(const uint8_t desired_elevation) {
uint8_t elevation = desired_elevation;
if (elevation != kPanasonicAcSwingVAuto) {
elevation = std::max(elevation, kPanasonicAcSwingVHighest);
elevation = std::min(elevation, kPanasonicAcSwingVLowest);
}
setBits(&remote_state[16], kLowNibble, kNibbleSize, elevation);
}
/// Get the current horizontal swing setting.
/// @return The current position it is set to.
uint8_t IRPanasonicAc::getSwingHorizontal(void) {
return GETBITS8(remote_state[17], kLowNibble, kNibbleSize);
}
/// Control the horizontal swing setting.
/// @param[in] desired_direction The position to set the horizontal swing to.
void IRPanasonicAc::setSwingHorizontal(const uint8_t desired_direction) {
switch (desired_direction) {
case kPanasonicAcSwingHAuto:
case kPanasonicAcSwingHMiddle:
case kPanasonicAcSwingHFullLeft:
case kPanasonicAcSwingHLeft:
case kPanasonicAcSwingHRight:
case kPanasonicAcSwingHFullRight: break;
// Ignore anything that isn't valid.
default: return;
}
_swingh = desired_direction; // Store the direction for later.
uint8_t direction = desired_direction;
switch (getModel()) {
case kPanasonicDke:
case kPanasonicRkr:
break;
case kPanasonicNke:
case kPanasonicLke:
direction = kPanasonicAcSwingHMiddle;
break;
default: // Ignore everything else.
return;
}
setBits(&remote_state[17], kLowNibble, kNibbleSize, direction);
}
/// Set the speed of the fan.
/// @param[in] speed The desired setting.
void IRPanasonicAc::setFan(const uint8_t speed) {
switch (speed) {
case kPanasonicAcFanMin:
case kPanasonicAcFanLow:
case kPanasonicAcFanMed:
case kPanasonicAcFanHigh:
case kPanasonicAcFanMax:
case kPanasonicAcFanAuto:
setBits(&remote_state[16], kHighNibble, kNibbleSize,
speed + kPanasonicAcFanDelta);
break;
default: setFan(kPanasonicAcFanAuto);
}
}
/// Get the current fan speed setting.
/// @return The current fan speed.
uint8_t IRPanasonicAc::getFan(void) {
return GETBITS8(remote_state[16], kHighNibble, kNibbleSize) -
kPanasonicAcFanDelta;
}
/// Get the Quiet setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRPanasonicAc::getQuiet(void) {
switch (getModel()) {
case kPanasonicRkr:
case kPanasonicCkp:
return GETBIT8(remote_state[21], kPanasonicAcQuietCkpOffset);
default:
return GETBIT8(remote_state[21], kPanasonicAcQuietOffset);
}
}
/// Set the Quiet setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRPanasonicAc::setQuiet(const bool on) {
uint8_t offset;
switch (getModel()) {
case kPanasonicRkr:
case kPanasonicCkp: offset = kPanasonicAcQuietCkpOffset; break;
default: offset = kPanasonicAcQuietOffset;
}
if (on) setPowerful(false); // Powerful is mutually exclusive.
setBit(&remote_state[21], offset, on);
}
/// Get the Powerful (Turbo) setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRPanasonicAc::getPowerful(void) {
switch (getModel()) {
case kPanasonicRkr:
case kPanasonicCkp:
return GETBIT8(remote_state[21], kPanasonicAcPowerfulCkpOffset);
default:
return GETBIT8(remote_state[21], kPanasonicAcPowerfulOffset);
}
}
/// Set the Powerful (Turbo) setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRPanasonicAc::setPowerful(const bool on) {
uint8_t offset;
switch (getModel()) {
case kPanasonicRkr:
case kPanasonicCkp: offset = kPanasonicAcPowerfulCkpOffset; break;
default: offset = kPanasonicAcPowerfulOffset;
}
if (on) setQuiet(false); // Quiet is mutually exclusive.
setBit(&remote_state[21], offset, on);
}
/// Convert standard (military/24hr) time to nr. of minutes since midnight.
/// @param[in] hours The hours component of the time.
/// @param[in] mins The minutes component of the time.
/// @return The nr of minutes since midnight.
uint16_t IRPanasonicAc::encodeTime(const uint8_t hours, const uint8_t mins) {
return std::min(hours, (uint8_t)23) * 60 + std::min(mins, (uint8_t)59);
}
/// Get the time from a given pointer location.
/// @param[in] ptr A pointer to a time location in a state.
/// @return The time expressed as nr. of minutes past midnight.
/// @note Internal use only.
uint16_t IRPanasonicAc::_getTime(const uint8_t ptr[]) {
uint16_t result = (GETBITS8(
ptr[1], kLowNibble, kPanasonicAcTimeOverflowSize) <<
(kPanasonicAcTimeSize - kPanasonicAcTimeOverflowSize)) + ptr[0];
if (result == kPanasonicAcTimeSpecial) return 0;
return result;
}
/// Get the current clock time value.
/// @return The time expressed as nr. of minutes past midnight.
uint16_t IRPanasonicAc::getClock(void) { return _getTime(&remote_state[24]); }
/// Set the time at a given pointer location.
/// @param[in, out] ptr A pointer to a time location in a state.
/// @param[in] mins_since_midnight The time as nr. of minutes past midnight.
/// @param[in] round_down Do we round to the nearest 10 minute mark?
/// @note Internal use only.
void IRPanasonicAc::_setTime(uint8_t * const ptr,
const uint16_t mins_since_midnight,
const bool round_down) {
uint16_t corrected = std::min(mins_since_midnight, kPanasonicAcTimeMax);
if (round_down) corrected -= corrected % 10;
if (mins_since_midnight == kPanasonicAcTimeSpecial)
corrected = kPanasonicAcTimeSpecial;
ptr[0] = corrected;
setBits(&ptr[1], kLowNibble, kPanasonicAcTimeOverflowSize,
corrected >> (kPanasonicAcTimeSize - kPanasonicAcTimeOverflowSize));
}
/// Set the current clock time value.
/// @param[in] mins_since_midnight The time as nr. of minutes past midnight.
void IRPanasonicAc::setClock(const uint16_t mins_since_midnight) {
_setTime(&remote_state[24], mins_since_midnight, false);
}
/// Get the On Timer time value.
/// @return The time expressed as nr. of minutes past midnight.
uint16_t IRPanasonicAc::getOnTimer(void) { return _getTime(&remote_state[18]); }
/// Set/Enable the On Timer.
/// @param[in] mins_since_midnight The time as nr. of minutes past midnight.
/// @param[in] enable Do we enable the timer or not?
void IRPanasonicAc::setOnTimer(const uint16_t mins_since_midnight,
const bool enable) {
// Set the timer flag.
setBit(&remote_state[13], kPanasonicAcOnTimerOffset, enable);
// Store the time.
_setTime(&remote_state[18], mins_since_midnight, true);
}
/// Cancel the On Timer.
void IRPanasonicAc::cancelOnTimer(void) { setOnTimer(0, false); }
/// Check if the On Timer is Enabled.
/// @return true, the setting is on. false, the setting is off.
bool IRPanasonicAc::isOnTimerEnabled(void) {
return GETBIT8(remote_state[13], kPanasonicAcOnTimerOffset);
}
/// Get the Off Timer time value.
/// @return The time expressed as nr. of minutes past midnight.
uint16_t IRPanasonicAc::getOffTimer(void) {
uint16_t result = (GETBITS8(remote_state[20], 0, 7) << kNibbleSize) |
GETBITS8(remote_state[19], kHighNibble, kNibbleSize);
if (result == kPanasonicAcTimeSpecial) return 0;
return result;
}
/// Set/Enable the Off Timer.
/// @param[in] mins_since_midnight The time as nr. of minutes past midnight.
/// @param[in] enable Do we enable the timer or not?
void IRPanasonicAc::setOffTimer(const uint16_t mins_since_midnight,
const bool enable) {
// Ensure its on a 10 minute boundary and no overflow.
uint16_t corrected = std::min(mins_since_midnight, kPanasonicAcTimeMax);
corrected -= corrected % 10;
if (mins_since_midnight == kPanasonicAcTimeSpecial)
corrected = kPanasonicAcTimeSpecial;
// Set the timer flag.
setBit(&remote_state[13], kPanasonicAcOffTimerOffset, enable);
// Store the time.
setBits(&remote_state[19], kHighNibble, kNibbleSize, corrected);
setBits(&remote_state[20], 0, 7, corrected >> kNibbleSize);
}
/// Cancel the Off Timer.
void IRPanasonicAc::cancelOffTimer(void) { setOffTimer(0, false); }
/// Check if the Off Timer is Enabled.
/// @return true, the setting is on. false, the setting is off.
bool IRPanasonicAc::isOffTimerEnabled(void) {
return GETBIT8(remote_state[13], kPanasonicAcOffTimerOffset);
}
/// Get the Ion (filter) setting of the A/C.
/// @return true, the setting is on. false, the setting is off.
bool IRPanasonicAc::getIon(void) {
switch (getModel()) {
case kPanasonicDke:
return GETBIT8(remote_state[kPanasonicAcIonFilterByte],
kPanasonicAcIonFilterOffset);
default:
return false;
}
}
/// Set the Ion (filter) setting of the A/C.
/// @param[in] on true, the setting is on. false, the setting is off.
void IRPanasonicAc::setIon(const bool on) {
if (getModel() == kPanasonicDke)
setBit(&remote_state[kPanasonicAcIonFilterByte],
kPanasonicAcIonFilterOffset, 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 IRPanasonicAc::convertMode(const stdAc::opmode_t mode) {
switch (mode) {
case stdAc::opmode_t::kCool: return kPanasonicAcCool;
case stdAc::opmode_t::kHeat: return kPanasonicAcHeat;
case stdAc::opmode_t::kDry: return kPanasonicAcDry;
case stdAc::opmode_t::kFan: return kPanasonicAcFan;
default: return kPanasonicAcAuto;
}
}
/// 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 IRPanasonicAc::convertFan(const stdAc::fanspeed_t speed) {
switch (speed) {
case stdAc::fanspeed_t::kMin: return kPanasonicAcFanMin;
case stdAc::fanspeed_t::kLow: return kPanasonicAcFanLow;
case stdAc::fanspeed_t::kMedium: return kPanasonicAcFanMed;
case stdAc::fanspeed_t::kHigh: return kPanasonicAcFanHigh;
case stdAc::fanspeed_t::kMax: return kPanasonicAcFanMax;
default: return kPanasonicAcFanAuto;
}
}
/// Convert a standard A/C vertical swing into its native setting.
/// @param[in] position A stdAc::swingv_t position to convert.
/// @return The equivalent native horizontal swing position.
uint8_t IRPanasonicAc::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;
default: return kPanasonicAcSwingVAuto;
}
}
/// Convert a standard A/C horizontal swing into its native setting.
/// @param[in] position A stdAc::swingh_t position to convert.
/// @return The equivalent native horizontal swing position.
uint8_t IRPanasonicAc::convertSwingH(const stdAc::swingh_t position) {
switch (position) {
case stdAc::swingh_t::kLeftMax: return kPanasonicAcSwingHFullLeft;
case stdAc::swingh_t::kLeft: return kPanasonicAcSwingHLeft;
case stdAc::swingh_t::kMiddle: return kPanasonicAcSwingHMiddle;
case stdAc::swingh_t::kRight: return kPanasonicAcSwingHRight;
case stdAc::swingh_t::kRightMax: return kPanasonicAcSwingHFullRight;
default: return kPanasonicAcSwingHAuto;
}
}
/// 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 IRPanasonicAc::toCommonMode(const uint8_t mode) {
switch (mode) {
case kPanasonicAcCool: return stdAc::opmode_t::kCool;
case kPanasonicAcHeat: return stdAc::opmode_t::kHeat;
case kPanasonicAcDry: return stdAc::opmode_t::kDry;
case kPanasonicAcFan: return stdAc::opmode_t::kFan;
default: return stdAc::opmode_t::kAuto;
}
}
/// Convert a native fan speed into its stdAc equivalent.
/// @param[in] spd The native setting to be converted.
/// @return The stdAc equivalent of the native setting.
stdAc::fanspeed_t IRPanasonicAc::toCommonFanSpeed(const uint8_t spd) {
switch (spd) {
case kPanasonicAcFanMax: return stdAc::fanspeed_t::kMax;
case kPanasonicAcFanHigh: return stdAc::fanspeed_t::kHigh;
case kPanasonicAcFanMed: return stdAc::fanspeed_t::kMedium;
case kPanasonicAcFanLow: return stdAc::fanspeed_t::kLow;
case kPanasonicAcFanMin: return stdAc::fanspeed_t::kMin;
default: return stdAc::fanspeed_t::kAuto;
}
}
/// Convert a native horizontal swing postion to it's common equivalent.
/// @param[in] pos A native position to convert.
/// @return The common horizontal swing position.
stdAc::swingh_t IRPanasonicAc::toCommonSwingH(const uint8_t pos) {
switch (pos) {
case kPanasonicAcSwingHFullLeft: return stdAc::swingh_t::kLeftMax;
case kPanasonicAcSwingHLeft: return stdAc::swingh_t::kLeft;
case kPanasonicAcSwingHMiddle: return stdAc::swingh_t::kMiddle;
case kPanasonicAcSwingHRight: return stdAc::swingh_t::kRight;
case kPanasonicAcSwingHFullRight: return stdAc::swingh_t::kRightMax;
default: return stdAc::swingh_t::kAuto;
}
}
/// Convert a native vertical swing postion to it's common equivalent.
/// @param[in] pos A native position to convert.
/// @return The common vertical swing position.
stdAc::swingv_t IRPanasonicAc::toCommonSwingV(const uint8_t pos) {
if (pos >= kPanasonicAcSwingVHighest && pos <= kPanasonicAcSwingVLowest)
return (stdAc::swingv_t)pos;
else
return stdAc::swingv_t::kAuto;
}
/// Convert the current internal state into its stdAc::state_t equivalent.
/// @return The stdAc equivalent of the native settings.
stdAc::state_t IRPanasonicAc::toCommon(void) {
stdAc::state_t result{};
result.protocol = decode_type_t::PANASONIC_AC;
result.model = getModel();
result.power = getPower();
result.mode = toCommonMode(getMode());
result.celsius = true;
result.degrees = getTemp();
result.fanspeed = toCommonFanSpeed(getFan());
result.swingv = toCommonSwingV(getSwingVertical());
result.swingh = toCommonSwingH(getSwingHorizontal());
result.quiet = getQuiet();
result.turbo = getPowerful();
result.filter = getIon();
// Not supported.
result.econo = false;
result.clean = false;
result.light = false;
result.beep = false;
result.sleep = -1;
result.clock = -1;
return result;
}
/// Convert the internal state into a human readable string.
/// @return A string containing the settings in human-readable form.
String IRPanasonicAc::toString(void) {
String result = "";
result.reserve(180); // Reserve some heap for the string to reduce fragging.
result += addModelToString(decode_type_t::PANASONIC_AC, getModel(), false);
result += addBoolToString(getPower(), kPowerStr);
result += addModeToString(getMode(), kPanasonicAcAuto, kPanasonicAcCool,
kPanasonicAcHeat, kPanasonicAcDry, kPanasonicAcFan);
result += addTempToString(getTemp());
result += addFanToString(getFan(), kPanasonicAcFanHigh, kPanasonicAcFanLow,
kPanasonicAcFanAuto, kPanasonicAcFanMin,
kPanasonicAcFanMed, kPanasonicAcFanMax);
result += addSwingVToString(getSwingVertical(), kPanasonicAcSwingVAuto,
kPanasonicAcSwingVHighest,
kPanasonicAcSwingVHigh,
kPanasonicAcSwingVAuto, // Upper Middle is unused
kPanasonicAcSwingVMiddle,
kPanasonicAcSwingVAuto, // Lower Middle is unused
kPanasonicAcSwingVLow,
kPanasonicAcSwingVLowest,
// Below are unused.
kPanasonicAcSwingVAuto,
kPanasonicAcSwingVAuto,
kPanasonicAcSwingVAuto,
kPanasonicAcSwingVAuto);
switch (getModel()) {
case kPanasonicJke:
case kPanasonicCkp:
break; // No Horizontal Swing support.
default:
result += addSwingHToString(getSwingHorizontal(), kPanasonicAcSwingHAuto,
kPanasonicAcSwingHFullLeft,
kPanasonicAcSwingHLeft,
kPanasonicAcSwingHMiddle,
kPanasonicAcSwingHRight,
kPanasonicAcSwingHFullRight,
// Below are unused.
kPanasonicAcSwingHAuto,
kPanasonicAcSwingHAuto,
kPanasonicAcSwingHAuto,
kPanasonicAcSwingHAuto,
kPanasonicAcSwingHAuto);
}
result += addBoolToString(getQuiet(), kQuietStr);
result += addBoolToString(getPowerful(), kPowerfulStr);
if (getModel() == kPanasonicDke)
result += addBoolToString(getIon(), kIonStr);
result += addLabeledString(minsToString(getClock()), kClockStr);
result += addLabeledString(
isOnTimerEnabled() ? minsToString(getOnTimer()) : kOffStr,
kOnTimerStr);
result += addLabeledString(
isOffTimerEnabled() ? minsToString(getOffTimer()) : kOffStr,
kOffTimerStr);
return result;
}
#if DECODE_PANASONIC_AC
/// Decode the supplied Panasonic AC message.
/// Status: STABLE / Works with real device(s).
/// @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.
bool IRrecv::decodePanasonicAC(decode_results *results, uint16_t offset,
const uint16_t nbits, const bool strict) {
uint8_t min_nr_of_messages = 1;
if (strict) {
if (nbits != kPanasonicAcBits && nbits != kPanasonicAcShortBits)
return false; // Not strictly a PANASONIC_AC message.
}
if (results->rawlen <=
min_nr_of_messages * (2 * nbits + kHeader + kFooter) - 1 + offset)
return false; // Can't possibly be a valid PANASONIC_AC message.
// Match Header + Data #1 + Footer
uint16_t used;
used = matchGeneric(results->rawbuf + offset, results->state,
results->rawlen - offset, kPanasonicAcSection1Length * 8,
kPanasonicHdrMark, kPanasonicHdrSpace,
kPanasonicBitMark, kPanasonicOneSpace,
kPanasonicBitMark, kPanasonicZeroSpace,
kPanasonicBitMark, kPanasonicAcSectionGap, false,
kPanasonicAcTolerance, kPanasonicAcExcess, false);
if (!used) return false;
offset += used;
// Match Header + Data #2 + Footer
if (!matchGeneric(results->rawbuf + offset,
results->state + kPanasonicAcSection1Length,
results->rawlen - offset,
nbits - kPanasonicAcSection1Length * 8,
kPanasonicHdrMark, kPanasonicHdrSpace,
kPanasonicBitMark, kPanasonicOneSpace,
kPanasonicBitMark, kPanasonicZeroSpace,
kPanasonicBitMark, kPanasonicAcMessageGap, true,
kPanasonicAcTolerance, kPanasonicAcExcess, false))
return false;
// Compliance
if (strict) {
// Check the signatures of the section blocks. They start with 0x02& 0x20.
if (results->state[0] != 0x02 || results->state[1] != 0x20 ||
results->state[8] != 0x02 || results->state[9] != 0x20)
return false;
if (!IRPanasonicAc::validChecksum(results->state, nbits / 8)) return false;
}
// Success
results->decode_type = decode_type_t::PANASONIC_AC;
results->bits = nbits;
return true;
}
#endif // DECODE_PANASONIC_AC
#if SEND_PANASONIC_AC32
/// Send a Panasonic AC 32/16bit formatted message.
/// Status: STABLE / Confirmed working.
/// @param[in] data containing the IR command.
/// @param[in] nbits Nr. of bits to send. Usually kPanasonicAc32Bits
/// @param[in] repeat Nr. of times the message is to be repeated.
/// @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1307
void IRsend::sendPanasonicAC32(const uint64_t data, const uint16_t nbits,
const uint16_t repeat) {
uint16_t section_bits;
uint16_t sections;
uint16_t blocks;
// Calculate the section, block, and bit sizes based on the requested bit size
if (nbits > kPanasonicAc32Bits / 2) { // A long message
section_bits = nbits / kPanasonicAc32Sections;
sections = kPanasonicAc32Sections;
blocks = kPanasonicAc32BlocksPerSection;
} else { // A short message
section_bits = nbits;
sections = kPanasonicAc32Sections - 1;
blocks = kPanasonicAc32BlocksPerSection + 1;
}
for (uint16_t r = 0; r <= repeat; r++) {
for (uint8_t section = 0; section < sections; section++) {
uint64_t section_data;
section_data = GETBITS64(data, section_bits * (sections - section - 1),
section_bits);
// Duplicate bytes in the data.
uint64_t expanded_data = 0;
for (uint8_t i = 0; i < sizeof(expanded_data); i++) {
const uint8_t first_byte = section_data >> 56;
for (uint8_t i = 0; i < 2; i++)
expanded_data = (expanded_data << 8) | first_byte;
section_data <<= 8;
}
// Two data blocks per section (i.e. 1 + a repeat)
sendGeneric(kPanasonicAc32HdrMark, kPanasonicAc32HdrSpace, // Header
kPanasonicAc32BitMark, kPanasonicAc32OneSpace, // Data
kPanasonicAc32BitMark, kPanasonicAc32ZeroSpace,
0, 0, // No Footer
expanded_data, section_bits * 2, kPanasonicFreq, false,
blocks - 1, // Repeat
50);
// Section Footer
sendGeneric(kPanasonicAc32HdrMark, kPanasonicAc32HdrSpace, // Header
0, 0, 0, 0, // No Data
kPanasonicAc32BitMark, kPanasonicAc32SectionGap, // Footer
data, 0, // No data (bits)
kPanasonicFreq, true, 0, 50);
}
}
}
#endif // SEND_PANASONIC_AC32
#if DECODE_PANASONIC_AC32
/// Decode the supplied Panasonic AC 32/16bit message.
/// Status: STABLE / Confirmed 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.
/// Typically: kPanasonicAc32Bits or kPanasonicAc32Bits/2
/// @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/crankyoldgit/IRremoteESP8266/issues/1307
/// @note Protocol has two known configurations:
/// (long)
/// Two sections of identical 32 bit data block pairs. ie. (32+32)+(32+32)=128
/// or
/// (short)
/// A single section of 3 x identical 32 bit data blocks i.e. (32+32+32)=96
/// Each data block also has a pair of 8 bits repeated identical bits.
/// e.g. (8+8)+(8+8)=32
///
/// So each long version really only has 32 unique bits, and the short version
/// really only has 16 unique bits.
bool IRrecv::decodePanasonicAC32(decode_results *results, uint16_t offset,
const uint16_t nbits, const bool strict) {
if (strict && (nbits != kPanasonicAc32Bits &&
nbits != kPanasonicAc32Bits / 2))
return false; // Not strictly a valid bit size.
// Determine if this is a long or a short message we are looking for.
const bool is_long = (nbits > kPanasonicAc32Bits / 2);
const uint16_t min_length = is_long ?
kPanasonicAc32Sections * kPanasonicAc32BlocksPerSection *