-
Notifications
You must be signed in to change notification settings - Fork 852
/
Copy pathIRutils.cpp
1449 lines (1370 loc) · 54.8 KB
/
IRutils.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 2017-2021 David Conran
#include "IRutils.h"
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#define __STDC_LIMIT_MACROS
#include <math.h>
#include <stdint.h>
#include <string.h>
#include <algorithm>
#ifndef ARDUINO
#include <string>
#endif
#include "IRrecv.h"
#include "IRremoteESP8266.h"
#include "IRsend.h"
#include "IRtext.h"
// On the ESP8266 platform we need to use a set of ..._P functions
// to handle the strings stored in the flash address space.
#ifndef STRCASECMP
#if defined(ESP8266)
#define STRCASECMP(LHS, RHS) \
strcasecmp_P(LHS, reinterpret_cast<const char*>(RHS))
#else // ESP8266
#define STRCASECMP strcasecmp
#endif // ESP8266
#endif // STRCASECMP
#ifndef STRLEN
#if defined(ESP8266)
#define STRLEN(PTR) strlen_P(PTR)
#else // ESP8266
#define STRLEN(PTR) strlen(PTR)
#endif // ESP8266
#endif // STRLEN
#ifndef FPSTR
#define FPSTR(X) X
#endif // FPSTR
/// Reverse the order of the requested least significant nr. of bits.
/// @param[in] input Bit pattern/integer to reverse.
/// @param[in] nbits Nr. of bits to reverse. (LSB -> MSB)
/// @return The reversed bit pattern.
uint64_t reverseBits(uint64_t input, uint16_t nbits) {
if (nbits <= 1) return input; // Reversing <= 1 bits makes no change at all.
// Cap the nr. of bits to rotate to the max nr. of bits in the input.
nbits = std::min(nbits, static_cast<uint16_t>((sizeof(input) * 8)));
uint64_t output = 0;
for (uint16_t i = 0; i < nbits; i++) {
output <<= 1;
output |= (input & 1);
input >>= 1;
}
// Merge any remaining unreversed bits back to the top of the reversed bits.
return (input << nbits) | output;
}
/// Convert a uint64_t (unsigned long long) to a string.
/// Arduino String/toInt/Serial.print() can't handle printing 64 bit values.
/// @param[in] input The value to print
/// @param[in] base The output base.
/// @returns A String representation of the integer.
/// @note Based on Arduino's Print::printNumber()
String uint64ToString(uint64_t input, uint8_t base) {
String result = "";
// prevent issues if called with base <= 1
if (base < 2) base = 10;
// Check we have a base that we can actually print.
// i.e. [0-9A-Z] == 36
if (base > 36) base = 10;
// Reserve some string space to reduce fragmentation.
// 16 bytes should store a uint64 in hex text which is the likely worst case.
// 64 bytes would be the worst case (base 2).
result.reserve(16);
do {
char c = input % base;
input /= base;
if (c < 10)
c += '0';
else
c += 'A' - 10;
result = c + result;
} while (input);
return result;
}
/// Convert a int64_t (signed long long) to a string.
/// Arduino String/toInt/Serial.print() can't handle printing 64 bit values.
/// @param[in] input The value to print
/// @param[in] base The output base.
/// @returns A String representation of the integer.
String int64ToString(int64_t input, uint8_t base) {
if (input < 0) {
// Using String(kDashStr) to keep compatible with old arduino
// frameworks. Not needed with 3.0.2.
///> @see https://github.com/crankyoldgit/IRremoteESP8266/issues/1639#issuecomment-944906016
return String(kDashStr) + uint64ToString(-input, base);
}
return uint64ToString(input, base);
}
#ifdef ARDUINO
/// Print a uint64_t/unsigned long long to the Serial port
/// Serial.print() can't handle printing long longs. (uint64_t)
/// @param[in] input The value to print
/// @param[in] base The output base.
void serialPrintUint64(uint64_t input, uint8_t base) {
Serial.print(uint64ToString(input, base));
}
#endif
/// Convert a C-style string to a decode_type_t.
/// @param[in] str A C-style string containing a protocol name or number.
/// @return A decode_type_t enum. (decode_type_t::UNKNOWN if no match.)
decode_type_t strToDecodeType(const char * const str) {
auto *ptr = reinterpret_cast<const char*>(kAllProtocolNamesStr);
uint16_t length = STRLEN(ptr);
for (uint16_t i = 0; length; i++) {
if (!STRCASECMP(str, ptr)) return (decode_type_t)i;
ptr += length + 1;
length = STRLEN(ptr);
}
// Handle integer values of the type by converting to a string and back again.
decode_type_t result = strToDecodeType(
typeToString((decode_type_t)atoi(str)).c_str());
if (result > 0)
return result;
return decode_type_t::UNKNOWN;
}
/// Convert a protocol type (enum etc) to a human readable string.
/// @param[in] protocol Nr. (enum) of the protocol.
/// @param[in] isRepeat A flag indicating if it is a repeat message.
/// @return A String containing the protocol name. kUnknownStr if no match.
String typeToString(const decode_type_t protocol, const bool isRepeat) {
String result = "";
result.reserve(30); // Size of longest protocol name + " (Repeat)"
if (protocol > kLastDecodeType || protocol == decode_type_t::UNKNOWN) {
result = kUnknownStr;
} else {
auto *ptr = reinterpret_cast<const char*>(kAllProtocolNamesStr);
for (uint16_t i = 0; i <= protocol && STRLEN(ptr); i++) {
if (i == protocol) {
result = FPSTR(ptr);
break;
}
ptr += STRLEN(ptr) + 1;
}
}
if (isRepeat) {
result += kSpaceLBraceStr;
result += kRepeatStr;
result += ')';
}
return result;
}
/// Does the given protocol use a complex state as part of the decode?
/// @param[in] protocol The decode_type_t protocol we are enquiring about.
/// @return True if the protocol uses a state array. False if just an integer.
bool hasACState(const decode_type_t protocol) {
switch (protocol) {
// This is kept sorted by name
case AMCOR:
case ARGO:
case BLUESTARHEAVY:
case BOSCH144:
case CARRIER_AC84:
case CARRIER_AC128:
case CORONA_AC:
case DAIKIN:
case DAIKIN128:
case DAIKIN152:
case DAIKIN160:
case DAIKIN176:
case DAIKIN2:
case DAIKIN200:
case DAIKIN216:
case DAIKIN312:
case ELECTRA_AC:
case FUJITSU_AC:
case GREE:
case HAIER_AC:
case HAIER_AC_YRW02:
case HAIER_AC160:
case HAIER_AC176:
case HITACHI_AC:
case HITACHI_AC1:
case HITACHI_AC2:
case HITACHI_AC3:
case HITACHI_AC264:
case HITACHI_AC296:
case HITACHI_AC344:
case HITACHI_AC424:
case KELON168:
case KELVINATOR:
case MIRAGE:
case MITSUBISHI136:
case MITSUBISHI112:
case MITSUBISHI_AC:
case MITSUBISHI_HEAVY_88:
case MITSUBISHI_HEAVY_152:
case MWM:
case NEOCLIMA:
case PANASONIC_AC:
case RHOSS:
case SAMSUNG_AC:
case SANYO_AC:
case SANYO_AC88:
case SANYO_AC152:
case SHARP_AC:
case TCL96AC:
case TCL112AC:
case TEKNOPOINT:
case TOSHIBA_AC:
case TROTEC:
case TROTEC_3550:
case VOLTAS:
case WHIRLPOOL_AC:
case YORK:
return true;
default:
return false;
}
}
/// Return the corrected length of a 'raw' format array structure
/// after over-large values are converted into multiple entries.
/// @param[in] results A ptr to a decode_results structure.
/// @return The corrected length.
uint16_t getCorrectedRawLength(const decode_results * const results) {
uint16_t extended_length = results->rawlen - 1;
for (uint16_t i = 0; i < results->rawlen - 1; i++) {
uint32_t usecs = results->rawbuf[i] * kRawTick;
// Add two extra entries for multiple larger than UINT16_MAX it is.
extended_length += (usecs / (UINT16_MAX + 1)) * 2;
}
return extended_length;
}
/// Return a String containing the key values of a decode_results structure
/// in a C/C++ code style format.
/// @param[in] results A ptr to a decode_results structure.
/// @return A String containing the code-ified result.
String resultToSourceCode(const decode_results * const results) {
String output = "";
const uint16_t length = getCorrectedRawLength(results);
const bool hasState = hasACState(results->decode_type);
// Reserve some space for the string to reduce heap fragmentation.
// "uint16_t rawData[9999] = {}; // LONGEST_PROTOCOL\n" = ~55 chars.
// "NNNN, " = ~7 chars on average per raw entry
// Protocols with a `state`:
// "uint8_t state[NN] = {};\n" = ~25 chars
// "0xNN, " = 6 chars per byte.
// Protocols without a `state`:
// " DEADBEEFDEADBEEF\n"
// "uint32_t address = 0xDEADBEEF;\n"
// "uint32_t command = 0xDEADBEEF;\n"
// "uint64_t data = 0xDEADBEEFDEADBEEF;" = ~116 chars max.
output.reserve(55 + (length * 7) + hasState ? 25 + (results->bits / 8) * 6
: 116);
// Start declaration
output += F("uint16_t "); // variable type
output += F("rawData["); // array name
output += uint64ToString(length, 10);
// array size
output += F("] = {"); // Start declaration
// Dump data
for (uint16_t i = 1; i < results->rawlen; i++) {
uint32_t usecs;
for (usecs = results->rawbuf[i] * kRawTick; usecs > UINT16_MAX;
usecs -= UINT16_MAX) {
output += uint64ToString(UINT16_MAX);
if (i % 2)
output += F(", 0, ");
else
output += F(", 0, ");
}
output += uint64ToString(usecs, 10);
if (i < results->rawlen - 1)
output += kCommaSpaceStr; // ',' not needed on the last one
if (i % 2 == 0) output += ' '; // Extra if it was even.
}
// End declaration
output += F("};");
// Comment
output += F(" // ");
output += typeToString(results->decode_type, results->repeat);
// Only display the value if the decode type doesn't have an A/C state.
if (!hasState)
output += ' ' + uint64ToString(results->value, 16);
output += F("\n");
// Now dump "known" codes
if (results->decode_type != UNKNOWN) {
if (hasState) {
#if DECODE_AC
uint16_t nbytes = ceil(static_cast<float>(results->bits) / 8.0);
output += F("uint8_t state[");
output += uint64ToString(nbytes);
output += F("] = {");
for (uint16_t i = 0; i < nbytes; i++) {
output += F("0x");
if (results->state[i] < 0x10) output += '0';
output += uint64ToString(results->state[i], 16);
if (i < nbytes - 1) output += kCommaSpaceStr;
}
output += F("};\n");
#endif // DECODE_AC
} else {
// Simple protocols
// Some protocols have an address &/or command.
// NOTE: It will ignore the atypical case when a message has been
// decoded but the address & the command are both 0.
if (results->address > 0 || results->command > 0) {
output += F("uint32_t address = 0x");
output += uint64ToString(results->address, 16);
output += F(";\n");
output += F("uint32_t command = 0x");
output += uint64ToString(results->command, 16);
output += F(";\n");
}
// Most protocols have data
output += F("uint64_t data = 0x");
output += uint64ToString(results->value, 16);
output += F(";\n");
}
}
return output;
}
/// Dump out the decode_results structure.
/// @param[in] results A ptr to a decode_results structure.
/// @return A String containing the legacy information format.
/// @deprecated This is only for those that want this legacy format.
String resultToTimingInfo(const decode_results * const results) {
String output = "";
String value = "";
// Reserve some space for the string to reduce heap fragmentation.
// "Raw Timing[NNNN]:\n\n" = 19 chars
// " +123456, " / "-123456, " = ~12 chars on avg per raw entry.
output.reserve(19 + 12 * results->rawlen); // Should be less than this.
value.reserve(6); // Max value should be 2^17 = 131072
output += F("Raw Timing[");
output += uint64ToString(results->rawlen - 1, 10);
output += F("]:\n");
for (uint16_t i = 1; i < results->rawlen; i++) {
if (i % 2 == 0)
output += kDashStr; // even
else
output += F(" +"); // odd
value = uint64ToString(results->rawbuf[i] * kRawTick);
// Space pad the value till it is at least 6 chars long.
while (value.length() < 6) value = ' ' + value;
output += value;
if (i < results->rawlen - 1)
output += kCommaSpaceStr; // ',' not needed for last one
if (!(i % 8)) output += '\n'; // Newline every 8 entries.
}
output += '\n';
return output;
}
/// Convert the decode_results structure's value/state to simple hexadecimal.
/// @param[in] result A ptr to a decode_results structure.
/// @return A String containing the output.
String resultToHexidecimal(const decode_results * const result) {
String output = F("0x");
// Reserve some space for the string to reduce heap fragmentation.
output.reserve(2 * kStateSizeMax + 2); // Should cover worst cases.
if (hasACState(result->decode_type)) {
#if DECODE_AC
for (uint16_t i = 0; result->bits > i * 8; i++) {
if (result->state[i] < 0x10) output += '0'; // Zero pad
output += uint64ToString(result->state[i], 16);
}
#endif // DECODE_AC
} else {
output += uint64ToString(result->value, 16);
}
return output;
}
/// Dump out the decode_results structure into a human readable format.
/// @param[in] results A ptr to a decode_results structure.
/// @return A String containing the output.
String resultToHumanReadableBasic(const decode_results * const results) {
String output = "";
// Reserve some space for the string to reduce heap fragmentation.
// "Protocol : LONGEST_PROTOCOL_NAME (Repeat)\n"
// "Code : 0x (NNNN Bits)\n" = 70 chars
output.reserve(2 * kStateSizeMax + 70); // Should cover most cases.
// Show Encoding standard
output += kProtocolStr;
output += F(" : ");
output += typeToString(results->decode_type, results->repeat);
output += '\n';
// Show Code & length
output += kCodeStr;
output += F(" : ");
output += resultToHexidecimal(results);
output += kSpaceLBraceStr;
output += uint64ToString(results->bits);
output += ' ';
output += kBitsStr;
output += F(")\n");
return output;
}
/// Convert a decode_results into an array suitable for `sendRaw()`.
/// @param[in] decode A ptr to a decode_results structure that contains a mesg.
/// @return A PTR to a dynamically allocated uint16_t sendRaw compatible array.
/// @note The returned array needs to be delete[]'ed/free()'ed (deallocated)
/// after use by caller.
uint16_t* resultToRawArray(const decode_results * const decode) {
uint16_t *result = new uint16_t[getCorrectedRawLength(decode)];
if (result != NULL) { // The memory was allocated successfully.
// Convert the decode data.
uint16_t pos = 0;
for (uint16_t i = 1; i < decode->rawlen; i++) {
uint32_t usecs = decode->rawbuf[i] * kRawTick;
while (usecs > UINT16_MAX) { // Keep truncating till it fits.
result[pos++] = UINT16_MAX;
result[pos++] = 0; // A 0 in a sendRaw() array basically means skip.
usecs -= UINT16_MAX;
}
result[pos++] = usecs;
}
}
return result;
}
/// Sum all the bytes of an array and return the least significant 8-bits of
/// the result.
/// @param[in] start A ptr to the start of the byte array to calculate over.
/// @param[in] length How many bytes to use in the calculation.
/// @param[in] init Starting value of the calculation to use. (Default is 0)
/// @return The 8-bit calculated result of all the bytes and init value.
uint8_t sumBytes(const uint8_t * const start, const uint16_t length,
const uint8_t init) {
uint8_t checksum = init;
const uint8_t *ptr;
for (ptr = start; ptr - start < length; ptr++) checksum += *ptr;
return checksum;
}
/// Calculate a rolling XOR of all the bytes of an array.
/// @param[in] start A ptr to the start of the byte array to calculate over.
/// @param[in] length How many bytes to use in the calculation.
/// @param[in] init Starting value of the calculation to use. (Default is 0)
/// @return The 8-bit calculated result of all the bytes and init value.
uint8_t xorBytes(const uint8_t * const start, const uint16_t length,
const uint8_t init) {
uint8_t checksum = init;
const uint8_t *ptr;
for (ptr = start; ptr - start < length; ptr++) checksum ^= *ptr;
return checksum;
}
/// Count the number of bits of a certain type in an array.
/// @param[in] start A ptr to the start of the byte array to calculate over.
/// @param[in] length How many bytes to use in the calculation.
/// @param[in] ones Count the binary nr of `1` bits. False is count the `0`s.
/// @param[in] init Starting value of the calculation to use. (Default is 0)
/// @return The nr. of bits found of the given type found in the array.
uint16_t countBits(const uint8_t * const start, const uint16_t length,
const bool ones, const uint16_t init) {
uint16_t count = init;
for (uint16_t offset = 0; offset < length; offset++)
for (uint8_t currentbyte = *(start + offset);
currentbyte;
currentbyte >>= 1)
if (currentbyte & 1) count++;
if (ones || length == 0)
return count;
else
return (length * 8) - count;
}
/// Count the number of bits of a certain type in an Integer.
/// @param[in] data The value you want bits counted for. Starting from the LSB.
/// @param[in] length How many bits to use in the calculation? Starts at the LSB
/// @param[in] ones Count the binary nr of `1` bits. False is count the `0`s.
/// @param[in] init Starting value of the calculation to use. (Default is 0)
/// @return The nr. of bits found of the given type found in the Integer.
uint16_t countBits(const uint64_t data, const uint8_t length, const bool ones,
const uint16_t init) {
uint16_t count = init;
uint8_t bitsSoFar = length;
for (uint64_t remainder = data; remainder && bitsSoFar;
remainder >>= 1, bitsSoFar--)
if (remainder & 1) count++;
if (ones || length == 0)
return count;
else
return length - count;
}
/// Invert/Flip the bits in an Integer.
/// @param[in] data The Integer that will be inverted.
/// @param[in] nbits How many bits are to be inverted. Starting from the LSB.
/// @return An Integer with the appropriate bits inverted/flipped.
uint64_t invertBits(const uint64_t data, const uint16_t nbits) {
// No change if we are asked to invert no bits.
if (nbits == 0) return data;
uint64_t result = ~data;
// If we are asked to invert all the bits or more than we have, it's simple.
if (nbits >= sizeof(data) * 8) return result;
// Mask off any unwanted bits and return the result.
return (result & ((1ULL << nbits) - 1));
}
/// Convert degrees Celsius to degrees Fahrenheit.
float celsiusToFahrenheit(const float deg) { return (deg * 9.0) / 5.0 + 32.0; }
/// Convert degrees Fahrenheit to degrees Celsius.
float fahrenheitToCelsius(const float deg) { return (deg - 32.0) * 5.0 / 9.0; }
namespace irutils {
/// Create a String with a colon separated "label: value" pair suitable for
/// Humans.
/// @param[in] value The value to come after the label.
/// @param[in] label The label to precede the value.
/// @param[in] precomma Should the output string start with ", " or not?
/// @return The resulting String.
String addLabeledString(const String value, const String label,
const bool precomma) {
String result = "";
// ", " + ": " = 4 chars
result.reserve(4 + value.length() + label.length());
if (precomma) result += kCommaSpaceStr;
result += label;
result += kColonSpaceStr;
return result + value;
}
/// Create a String with a colon separated flag suitable for Humans.
/// e.g. "Power: On"
/// @param[in] value The value to come after the label.
/// @param[in] label The label to precede the value.
/// @param[in] precomma Should the output string start with ", " or not?
/// @return The resulting String.
String addBoolToString(const bool value, const String label,
const bool precomma) {
return addLabeledString(value ? kOnStr : kOffStr, label, precomma);
}
/// Create a String with a colon separated toggle flag suitable for Humans.
/// e.g. "Light: Toggle", "Light: -"
/// @param[in] toggle The value of the toggle to come after the label.
/// @param[in] label The label to precede the value.
/// @param[in] precomma Should the output string start with ", " or not?
/// @return The resulting String.
String addToggleToString(const bool toggle, const String label,
const bool precomma) {
return addLabeledString(toggle ? kToggleStr : kDashStr, label, precomma);
}
/// Create a String with a colon separated labeled Integer suitable for
/// Humans.
/// e.g. "Foo: 23"
/// @param[in] value The value to come after the label.
/// @param[in] label The label to precede the value.
/// @param[in] precomma Should the output string start with ", " or not?
/// @return The resulting String.
String addIntToString(const uint16_t value, const String label,
const bool precomma) {
return addLabeledString(uint64ToString(value), label, precomma);
}
/// Create a String with a colon separated labeled Integer suitable for
/// Humans.
/// e.g. "Foo: 23"
/// @param[in] value The value to come after the label.
/// @param[in] label The label to precede the value.
/// @param[in] precomma Should the output string start with ", " or not?
/// @return The resulting String.
String addSignedIntToString(const int16_t value, const String label,
const bool precomma) {
return addLabeledString(int64ToString(value), label, precomma);
}
/// Generate the model string for a given Protocol/Model pair.
/// @param[in] protocol The IR protocol.
/// @param[in] model The model number for that protocol.
/// @return The resulting String.
/// @note After adding a new model you should update IRac::strToModel() too.
String modelToStr(const decode_type_t protocol, const int16_t model) {
switch (protocol) {
case decode_type_t::FUJITSU_AC:
switch (model) {
case fujitsu_ac_remote_model_t::ARRAH2E: return kArrah2eStr;
case fujitsu_ac_remote_model_t::ARDB1: return kArdb1Str;
case fujitsu_ac_remote_model_t::ARREB1E: return kArreb1eStr;
case fujitsu_ac_remote_model_t::ARJW2: return kArjw2Str;
case fujitsu_ac_remote_model_t::ARRY4: return kArry4Str;
case fujitsu_ac_remote_model_t::ARREW4E: return kArrew4eStr;
default: return kUnknownStr;
}
break;
case decode_type_t::GREE:
switch (model) {
case gree_ac_remote_model_t::YAW1F: return kYaw1fStr;
case gree_ac_remote_model_t::YBOFB: return kYbofbStr;
case gree_ac_remote_model_t::YX1FSF: return kYx1fsfStr;
default: return kUnknownStr;
}
break;
case decode_type_t::HAIER_AC176:
switch (model) {
case haier_ac176_remote_model_t::V9014557_A:
return kV9014557AStr;
case haier_ac176_remote_model_t::V9014557_B:
return kV9014557BStr;
default:
return kUnknownStr;
}
break;
case decode_type_t::HITACHI_AC1:
switch (model) {
case hitachi_ac1_remote_model_t::R_LT0541_HTA_A:
return kRlt0541htaaStr;
case hitachi_ac1_remote_model_t::R_LT0541_HTA_B:
return kRlt0541htabStr;
default:
return kUnknownStr;
}
break;
case decode_type_t::LG:
case decode_type_t::LG2:
switch (model) {
case lg_ac_remote_model_t::GE6711AR2853M: return kGe6711ar2853mStr;
case lg_ac_remote_model_t::AKB75215403: return kAkb75215403Str;
case lg_ac_remote_model_t::AKB74955603: return kAkb74955603Str;
case lg_ac_remote_model_t::AKB73757604: return kAkb73757604Str;
case lg_ac_remote_model_t::LG6711A20083V: return kLg6711a20083vStr;
default: return kUnknownStr;
}
break;
case decode_type_t::MIRAGE:
switch (model) {
case mirage_ac_remote_model_t::KKG9AC1: return kKkg9ac1Str;
case mirage_ac_remote_model_t::KKG29AC1: return kKkg29ac1Str;
default: return kUnknownStr;
}
break;
case decode_type_t::PANASONIC_AC:
switch (model) {
case panasonic_ac_remote_model_t::kPanasonicLke: return kLkeStr;
case panasonic_ac_remote_model_t::kPanasonicNke: return kNkeStr;
case panasonic_ac_remote_model_t::kPanasonicDke: return kDkeStr;
case panasonic_ac_remote_model_t::kPanasonicJke: return kJkeStr;
case panasonic_ac_remote_model_t::kPanasonicCkp: return kCkpStr;
case panasonic_ac_remote_model_t::kPanasonicRkr: return kRkrStr;
default: return kUnknownStr;
}
break;
case decode_type_t::SHARP_AC:
switch (model) {
case sharp_ac_remote_model_t::A907: return kA907Str;
case sharp_ac_remote_model_t::A705: return kA705Str;
case sharp_ac_remote_model_t::A903: return kA903Str;
default: return kUnknownStr;
}
break;
case decode_type_t::TCL112AC:
switch (model) {
case tcl_ac_remote_model_t::TAC09CHSD: return kTac09chsdStr;
case tcl_ac_remote_model_t::GZ055BE1: return kGz055be1Str;
default: return kUnknownStr;
}
break;
case decode_type_t::VOLTAS:
switch (model) {
case voltas_ac_remote_model_t::kVoltas122LZF: return k122lzfStr;
default: return kUnknownStr;
}
break;
case decode_type_t::WHIRLPOOL_AC:
switch (model) {
case whirlpool_ac_remote_model_t::DG11J13A: return kDg11j13aStr;
case whirlpool_ac_remote_model_t::DG11J191: return kDg11j191Str;
default: return kUnknownStr;
}
break;
case decode_type_t::ARGO:
switch (model) {
case argo_ac_remote_model_t::SAC_WREM2: return kArgoWrem2Str;
case argo_ac_remote_model_t::SAC_WREM3: return kArgoWrem3Str;
default: return kUnknownStr;
}
break;
case decode_type_t::TOSHIBA_AC:
switch (model) {
case toshiba_ac_remote_model_t::kToshibaGenericRemote_A:
return kToshibaGenericRemoteAStr;
case toshiba_ac_remote_model_t::kToshibaGenericRemote_B:
return kToshibaGenericRemoteBStr;
default:
return kUnknownStr;
}
default: return kUnknownStr;
}
}
/// Create a String of human output for a given protocol model number.
/// e.g. "Model: JKE"
/// @param[in] protocol The IR protocol.
/// @param[in] model The model number for that protocol.
/// @param[in] precomma Should the output string start with ", " or not?
/// @return The resulting String.
String addModelToString(const decode_type_t protocol, const int16_t model,
const bool precomma) {
String result = "";
// ", Model: NNN (BlahBlahEtc)" = ~40 chars for longest model name.
result.reserve(40);
result += addIntToString(model, kModelStr, precomma);
result += kSpaceLBraceStr;
result += modelToStr(protocol, model);
return result + ')';
}
/// Create a String of human output for a given temperature.
/// e.g. "Temp: 25C"
/// @param[in] degrees The temperature in degrees.
/// @param[in] celsius Is the temp Celsius or Fahrenheit.
/// true is C, false is F
/// @param[in] precomma Should the output string start with ", " or not?
/// @param[in] isSensorTemp Is the value a room (ambient) temp. or target?
/// @return The resulting String.
String addTempToString(const uint16_t degrees, const bool celsius,
const bool precomma, const bool isSensorTemp) {
String result = addIntToString(degrees, (isSensorTemp)?
kSensorTempStr : kTempStr, precomma);
result += celsius ? 'C' : 'F';
return result;
}
/// Create a String of human output for a given temperature.
/// e.g. "Temp: 25.5C"
/// @param[in] degrees The temperature in degrees.
/// @param[in] celsius Is the temp Celsius or Fahrenheit.
/// true is C, false is F
/// @param[in] precomma Should the output string start with ", " or not?
/// @param[in] isSensorTemp Is the value a room (ambient) temp. or target?
/// @return The resulting String.
String addTempFloatToString(const float degrees, const bool celsius,
const bool precomma, const bool isSensorTemp) {
String result = "";
result.reserve(21); // Assuming ", Sensor Temp: XXX.5F" is the largest.
result += addIntToString(degrees, (isSensorTemp)?
kSensorTempStr : kTempStr, precomma);
// Is it a half degree?
if (static_cast<uint16_t>(2 * degrees) & 1)
result += F(".5");
result += celsius ? 'C' : 'F';
return result;
}
/// Create a String of human output for the given operating mode.
/// e.g. "Mode: 1 (Cool)"
/// @param[in] mode The operating mode to display.
/// @param[in] automatic The numeric value for Auto mode.
/// @param[in] cool The numeric value for Cool mode.
/// @param[in] heat The numeric value for Heat mode.
/// @param[in] dry The numeric value for Dry mode.
/// @param[in] fan The numeric value for Fan mode.
/// @return The resulting String.
String addModeToString(const uint8_t mode, const uint8_t automatic,
const uint8_t cool, const uint8_t heat,
const uint8_t dry, const uint8_t fan) {
String result = "";
result.reserve(22); // ", Mode: NNN (UNKNOWN)"
result += addIntToString(mode, kModeStr);
result += kSpaceLBraceStr;
if (mode == automatic) result += kAutoStr;
else if (mode == cool) result += kCoolStr;
else if (mode == heat) result += kHeatStr;
else if (mode == dry) result += kDryStr;
else if (mode == fan) result += kFanStr;
else
result += kUnknownStr;
return result + ')';
}
/// Create a String of the 3-letter day of the week from a numerical day of
/// the week. e.g. "Day: 1 (Mon)"
/// @param[in] day_of_week A numerical version of the sequential day of the
/// week. e.g. Saturday = 7 etc.
/// @param[in] offset Days to offset by.
/// e.g. For different day starting the week.
/// @param[in] precomma Should the output string start with ", " or not?
/// @return The resulting String.
String addDayToString(const uint8_t day_of_week, const int8_t offset,
const bool precomma) {
String result = "";
result.reserve(19); // ", Day: N (UNKNOWN)"
result += addIntToString(day_of_week, kDayStr, precomma);
result += kSpaceLBraceStr;
result += dayToString(day_of_week, offset);
return result + ')';
}
/// Create a String of the 3-letter day of the week from a numerical day of
/// the week. e.g. "Mon"
/// @param[in] day_of_week A numerical version of the sequential day of the
/// week. e.g. Sunday = 1, Monday = 2, ..., Saturday = 7
/// @param[in] offset Days to offset by.
/// e.g. For different day starting the week.
/// @return The resulting String.
String dayToString(const uint8_t day_of_week, const int8_t offset) {
if ((uint8_t)(day_of_week + offset) < 7)
#if UNIT_TEST
return String(kThreeLetterDayOfWeekStr).substr(
(day_of_week + offset) * 3, 3);
#else // UNIT_TEST
return String(kThreeLetterDayOfWeekStr).substring(
(day_of_week + offset) * 3, (day_of_week + offset) * 3 + 3);
#endif // UNIT_TEST
else
return kUnknownStr;
}
/// Create a String of human output for the given fan speed.
/// e.g. "Fan: 0 (Auto)"
/// @param[in] speed The numeric speed of the fan to display.
/// @param[in] high The numeric value for High speed. (second highest)
/// @param[in] low The numeric value for Low speed.
/// @param[in] automatic The numeric value for Auto speed.
/// @param[in] quiet The numeric value for Quiet speed.
/// @param[in] medium The numeric value for Medium speed.
/// @param[in] maximum The numeric value for Highest speed. (if > high)
/// @param[in] medium_high The numeric value for third-highest speed.
/// (if > medium)
/// @return The resulting String.
String addFanToString(const uint8_t speed, const uint8_t high,
const uint8_t low, const uint8_t automatic,
const uint8_t quiet, const uint8_t medium,
const uint8_t maximum, const uint8_t medium_high) {
String result = "";
result.reserve(21); // ", Fan: NNN (UNKNOWN)"
result += addIntToString(speed, kFanStr);
result += kSpaceLBraceStr;
if (speed == high) result += kHighStr;
else if (speed == low) result += kLowStr;
else if (speed == automatic) result += kAutoStr;
else if (speed == quiet) result += kQuietStr;
else if (speed == medium) result += kMediumStr;
else if (speed == maximum) result += kMaximumStr;
else if (speed == medium_high) result += kMedHighStr;
else
result += kUnknownStr;
return result + ')';
}
/// Create a String of human output for the given horizontal swing setting.
/// e.g. "Swing(H): 0 (Auto)"
/// @param[in] position The numeric position of the swing to display.
/// @param[in] automatic The numeric value for Auto position.
/// @param[in] maxleft The numeric value for most left position.
/// @param[in] left The numeric value for Left position.
/// @param[in] middle The numeric value for Middle position.
/// @param[in] right The numeric value for Right position.
/// @param[in] maxright The numeric value for most right position.
/// @param[in] off The numeric value for Off position.
/// @param[in] leftright The numeric value for "left right" position.
/// @param[in] rightleft The numeric value for "right left" position.
/// @param[in] threed The numeric value for 3D setting.
/// @param[in] wide The numeric value for Wide position.
/// @return The resulting String.
String addSwingHToString(const uint8_t position, const uint8_t automatic,
const uint8_t maxleft, const uint8_t left,
const uint8_t middle,
const uint8_t right, const uint8_t maxright,
const uint8_t off,
const uint8_t leftright, const uint8_t rightleft,
const uint8_t threed, const uint8_t wide) {
String result = "";
result.reserve(30); // ", Swing(H): NNN (Left Right)"
result += addIntToString(position, kSwingHStr);
result += kSpaceLBraceStr;
if (position == automatic) {
result += kAutoStr;
} else if (position == left) {
result += kLeftStr;
} else if (position == middle) {
result += kMiddleStr;
} else if (position == right) {
result += kRightStr;
} else if (position == maxleft) {
result += kMaxLeftStr;
} else if (position == maxright) {
result += kMaxRightStr;
} else if (position == off) {
result += kOffStr;
} else if (position == leftright) {
result += kLeftStr;
result += ' ';
result += kRightStr;
} else if (position == rightleft) {
result += kRightStr;
result += ' ';
result += kLeftStr;
} else if (position == threed) {
result += k3DStr;
} else if (position == wide) {
result += kWideStr;
} else {
result += kUnknownStr;
}
return result + ')';
}
/// Create a String of human output for the given vertical swing setting.
/// e.g. "Swing(V): 0 (Auto)"
/// @param[in] position The numeric position of the swing to display.
/// @param[in] automatic The numeric value for Auto position.
/// @param[in] highest The numeric value for Highest position.
/// @param[in] high The numeric value for High position.
/// @param[in] uppermiddle The numeric value for Upper Middle position.
/// @param[in] middle The numeric value for Middle position.
/// @param[in] lowermiddle The numeric value for Lower Middle position.
/// @param[in] low The numeric value for Low position.
/// @param[in] lowest The numeric value for Low position.
/// @param[in] off The numeric value for Off position.
/// @param[in] swing The numeric value for Swing setting.
/// @param[in] breeze The numeric value for Breeze setting.
/// @param[in] circulate The numeric value for Circulate setting.
/// @return The resulting String.
String addSwingVToString(const uint8_t position, const uint8_t automatic,
const uint8_t highest, const uint8_t high,
const uint8_t uppermiddle,
const uint8_t middle,
const uint8_t lowermiddle,
const uint8_t low, const uint8_t lowest,
const uint8_t off, const uint8_t swing,
const uint8_t breeze, const uint8_t circulate) {
String result = "";
result.reserve(31); // ", Swing(V): NNN (Upper Middle)"
result += addIntToString(position, kSwingVStr);
result += kSpaceLBraceStr;
if (position == automatic) {
result += kAutoStr;
} else if (position == highest) {
result += kHighestStr;
} else if (position == high) {
result += kHighStr;
} else if (position == middle) {
result += kMiddleStr;
} else if (position == low) {
result += kLowStr;
} else if (position == lowest) {
result += kLowestStr;
} else if (position == off) {
result += kOffStr;
} else if (position == uppermiddle) {
result += kUpperStr;
result += ' ';
result += kMiddleStr;
} else if (position == lowermiddle) {
result += kLowerStr;
result += ' ';
result += kMiddleStr;
} else if (position == swing) {
result += kSwingStr;
} else if (position == breeze) {
result += kBreezeStr;
} else if (position == circulate) {
result += kCirculateStr;
} else {
result += kUnknownStr;
}
return result + ')';
}
/// @brief Create a String of human output for the given timer setting.
/// e.g. "Timer Mode: 2 (Schedule 1)"
/// @param[in] timerMode The numeric value of the timer mode to display.
/// @param[in] noTimer The numeric value for no timer (off)
/// @param[in] delayTimer The numeric value for delay (sleep) timer
/// @param[in] schedule1 The numeric value for schedule timer #1
/// @param[in] schedule2 The numeric value for schedule timer #2
/// @param[in] schedule3 The numeric value for schedule timer #3
/// @param[in] precomma Should the output string start with ", " or not?
/// @return String representation
String addTimerModeToString(const uint8_t timerMode, const uint8_t noTimer,
const uint8_t delayTimer, const uint8_t schedule1,
const uint8_t schedule2, const uint8_t schedule3,