IRremoteESP8266
IRutils.h
Go to the documentation of this file.
1 #ifndef IRUTILS_H_
2 #define IRUTILS_H_
3 
4 // Copyright 2017 David Conran
5 
6 #ifndef UNIT_TEST
7 #include <Arduino.h>
8 #endif
9 #define __STDC_LIMIT_MACROS
10 #include <stdint.h>
11 #ifndef ARDUINO
12 #include <string>
13 #endif
14 #include "IRremoteESP8266.h"
15 #include "IRrecv.h"
16 
17 const uint8_t kNibbleSize = 4;
18 const uint8_t kLowNibble = 0;
19 const uint8_t kHighNibble = 4;
20 const uint8_t kModeBitsSize = 3;
21 uint64_t reverseBits(uint64_t input, uint16_t nbits);
22 String uint64ToString(uint64_t input, uint8_t base = 10);
23 String int64ToString(int64_t input, uint8_t base = 10);
24 String typeToString(const decode_type_t protocol,
25  const bool isRepeat = false);
26 void serialPrintUint64(uint64_t input, uint8_t base = 10);
27 String resultToSourceCode(const decode_results * const results);
28 String resultToTimingInfo(const decode_results * const results);
29 String resultToHumanReadableBasic(const decode_results * const results);
30 String resultToHexidecimal(const decode_results * const result);
31 bool hasACState(const decode_type_t protocol);
32 uint16_t getCorrectedRawLength(const decode_results * const results);
33 uint16_t *resultToRawArray(const decode_results * const decode);
34 uint8_t sumBytes(const uint8_t * const start, const uint16_t length,
35  const uint8_t init = 0);
36 uint8_t xorBytes(const uint8_t * const start, const uint16_t length,
37  const uint8_t init = 0);
38 uint16_t countBits(const uint8_t * const start, const uint16_t length,
39  const bool ones = true, const uint16_t init = 0);
40 uint16_t countBits(const uint64_t data, const uint8_t length,
41  const bool ones = true, const uint16_t init = 0);
42 uint64_t invertBits(const uint64_t data, const uint16_t nbits);
43 decode_type_t strToDecodeType(const char *str);
44 float celsiusToFahrenheit(const float deg);
45 float fahrenheitToCelsius(const float deg);
48 namespace irutils {
49  String addBoolToString(const bool value, const String label,
50  const bool precomma = true);
51  String addToggleToString(const bool toggle, const String label,
52  const bool precomma = true);
53  String addIntToString(const uint16_t value, const String label,
54  const bool precomma = true);
55  String addSignedIntToString(const int16_t value, const String label,
56  const bool precomma = true);
57  String modelToStr(const decode_type_t protocol, const int16_t model);
58  String addModelToString(const decode_type_t protocol, const int16_t model,
59  const bool precomma = true);
60  String addLabeledString(const String value, const String label,
61  const bool precomma = true);
62  String addTempToString(const uint16_t degrees, const bool celsius = true,
63  const bool precomma = true,
64  const bool isSensorTemp = false);
65  String addTempFloatToString(const float degrees, const bool celsius = true,
66  const bool precomma = true,
67  const bool isSensorTemp = false);
68  String addModeToString(const uint8_t mode, const uint8_t automatic,
69  const uint8_t cool, const uint8_t heat,
70  const uint8_t dry, const uint8_t fan);
71  String addFanToString(const uint8_t speed, const uint8_t high,
72  const uint8_t low, const uint8_t automatic,
73  const uint8_t quiet, const uint8_t medium,
74  const uint8_t maximum = 0xFF,
75  const uint8_t medium_high = 0xFF);
76  String addSwingHToString(const uint8_t position, const uint8_t automatic,
77  const uint8_t maxleft, const uint8_t left,
78  const uint8_t middle,
79  const uint8_t right, const uint8_t maxright,
80  const uint8_t off,
81  const uint8_t leftright, const uint8_t rightleft,
82  const uint8_t threed, const uint8_t wide);
83  String addSwingVToString(const uint8_t position, const uint8_t automatic,
84  const uint8_t highest, const uint8_t high,
85  const uint8_t uppermiddle,
86  const uint8_t middle,
87  const uint8_t lowermiddle,
88  const uint8_t low, const uint8_t lowest,
89  const uint8_t off, const uint8_t swing,
90  const uint8_t breeze, const uint8_t circulate);
91  String addDayToString(const uint8_t day_of_week, const int8_t offset = 0,
92  const bool precomma = true);
93  String addTimerModeToString(const uint8_t timerType, const uint8_t noTimer,
94  const uint8_t delayTimer,
95  const uint8_t schedule1 = 0xFF,
96  const uint8_t schedule2 = 0xFF,
97  const uint8_t schedule3 = 0xFF,
98  const bool precomma = true);
99  String irCommandTypeToString(uint8_t commandType, uint8_t acControlCmd,
100  uint8_t iFeelReportCmd = 0xFF,
101  uint8_t timerCmd = 0xFF,
102  uint8_t configCmd = 0xFF);
103  String dayToString(const uint8_t day_of_week, const int8_t offset = 0);
104  String daysBitmaskToString(uint8_t daysBitmap, uint8_t offset = 0);
105  String channelToString(const uint8_t channel);
106  String htmlEscape(const String unescaped);
107  String msToString(uint32_t const msecs);
108  String minsToString(const uint16_t mins);
109  uint8_t sumNibbles(const uint8_t * const start, const uint16_t length,
110  const uint8_t init = 0);
111  uint8_t sumNibbles(const uint64_t data, const uint8_t count = 16,
112  const uint8_t init = 0, const bool nibbleonly = true);
113  uint16_t sumBytes(const uint64_t data, const uint8_t count = 8,
114  const uint8_t init = 0, const bool byteonly = true);
115  uint8_t bcdToUint8(const uint8_t bcd);
116  uint8_t uint8ToBcd(const uint8_t integer);
117  bool getBit(const uint64_t data, const uint8_t position,
118  const uint8_t size = 64);
119  bool getBit(const uint8_t data, const uint8_t position);
120 #define GETBIT8(a, b) ((a) & ((uint8_t)1 << (b)))
121 #define GETBIT16(a, b) ((a) & ((uint16_t)1 << (b)))
122 #define GETBIT32(a, b) ((a) & ((uint32_t)1 << (b)))
123 #define GETBIT64(a, b) ((a) & ((uint64_t)1 << (b)))
124 #define GETBITS8(data, offset, size) \
125  (((data) & (((uint8_t)UINT8_MAX >> (8 - (size))) << (offset))) >> (offset))
126 #define GETBITS16(data, offset, size) \
127  (((data) & (((uint16_t)UINT16_MAX >> (16 - (size))) << (offset))) >> \
128  (offset))
129 #define GETBITS32(data, offset, size) \
130  (((data) & (((uint32_t)UINT32_MAX >> (32 - (size))) << (offset))) >> \
131  (offset))
132 #define GETBITS64(data, offset, size) \
133  (((data) & (((uint64_t)UINT64_MAX >> (64 - (size))) << (offset))) >> \
134  (offset))
135  uint64_t setBit(const uint64_t data, const uint8_t position,
136  const bool on = true, const uint8_t size = 64);
137  uint8_t setBit(const uint8_t data, const uint8_t position,
138  const bool on = true);
139  void setBit(uint8_t * const data, const uint8_t position,
140  const bool on = true);
141  void setBit(uint32_t * const data, const uint8_t position,
142  const bool on = true);
143  void setBit(uint64_t * const data, const uint8_t position,
144  const bool on = true);
145  void setBits(uint8_t * const dst, const uint8_t offset, const uint8_t nbits,
146  const uint8_t data);
147  void setBits(uint32_t * const dst, const uint8_t offset, const uint8_t nbits,
148  const uint32_t data);
149  void setBits(uint64_t * const dst, const uint8_t offset, const uint8_t nbits,
150  const uint64_t data);
151  uint8_t * invertBytePairs(uint8_t *ptr, const uint16_t length);
152  bool checkInvertedBytePairs(const uint8_t * const ptr, const uint16_t length);
153  uint8_t lowLevelSanityCheck(void);
154 } // namespace irutils
155 #endif // IRUTILS_H_
decode_type_t
decode_type_t
Enumerator for defining and numbering of supported IR protocol.
Definition: IRremoteESP8266.h:1011
irutils::addDayToString
String addDayToString(const uint8_t day_of_week, const int8_t offset, const bool precomma)
Create a String of the 3-letter day of the week from a numerical day of the week. e....
Definition: IRutils.cpp:795
resultToRawArray
uint16_t * resultToRawArray(const decode_results *const decode)
Convert a decode_results into an array suitable for sendRaw().
Definition: IRutils.cpp:425
irutils::setBits
void setBits(uint8_t *const dst, const uint8_t offset, const uint8_t nbits, const uint8_t data)
Alter an uint8_t value by overwriting an arbitrary given number of bits.
Definition: IRutils.cpp:1309
resultToSourceCode
String resultToSourceCode(const decode_results *const results)
Return a String containing the key values of a decode_results structure in a C/C++ code style format.
Definition: IRutils.cpp:250
decode_results
Results returned from the decoder.
Definition: IRrecv.h:99
getCorrectedRawLength
uint16_t getCorrectedRawLength(const decode_results *const results)
Return the corrected length of a 'raw' format array structure after over-large values are converted i...
Definition: IRutils.cpp:236
irutils::addModelToString
String addModelToString(const decode_type_t protocol, const int16_t model, const bool precomma)
Create a String of human output for a given protocol model number. e.g. "Model: JKE".
Definition: IRutils.cpp:714
irutils::addSignedIntToString
String addSignedIntToString(const int16_t value, const String label, const bool precomma)
Create a String with a colon separated labeled Integer suitable for Humans. e.g. "Foo: 23".
Definition: IRutils.cpp:588
countBits
uint16_t countBits(const uint8_t *const start, const uint16_t length, const bool ones=true, const uint16_t init=0)
Count the number of bits of a certain type in an array.
Definition: IRutils.cpp:476
irutils::msToString
String msToString(uint32_t const msecs)
Convert a nr. of milliSeconds into a Human-readable string. e.g. "1 Day 6 Hours 34 Minutes 17 Seconds...
Definition: IRutils.cpp:1112
irutils::lowLevelSanityCheck
uint8_t lowLevelSanityCheck(void)
Perform a low level bit manipulation sanity check for the given cpu architecture and the compiler ope...
Definition: IRutils.cpp:1398
irutils::addModeToString
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)
Create a String of human output for the given operating mode. e.g. "Mode: 1 (Cool)".
Definition: IRutils.cpp:770
resultToHumanReadableBasic
String resultToHumanReadableBasic(const decode_results *const results)
Dump out the decode_results structure into a human readable format.
Definition: IRutils.cpp:396
resultToTimingInfo
String resultToTimingInfo(const decode_results *const results)
Dump out the decode_results structure.
Definition: IRutils.cpp:344
String
std::string String
Definition: IRremoteESP8266.h:1521
kNibbleSize
const uint8_t kNibbleSize
Definition: IRutils.h:17
irutils::dayToString
String dayToString(const uint8_t day_of_week, const int8_t offset)
Create a String of the 3-letter day of the week from a numerical day of the week. e....
Definition: IRutils.cpp:812
irutils::modelToStr
String modelToStr(const decode_type_t protocol, const int16_t model)
Generate the model string for a given Protocol/Model pair.
Definition: IRutils.cpp:599
kLowNibble
const uint8_t kLowNibble
Definition: IRutils.h:18
IRremoteESP8266.h
irutils::uint8ToBcd
uint8_t uint8ToBcd(const uint8_t integer)
Convert an Integer into a byte of Binary Coded Decimal(BCD).
Definition: IRutils.cpp:1214
irutils::addTimerModeToString
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, const bool precomma)
Create a String of human output for the given timer setting. e.g. "Timer Mode: 2 (Schedule 1)".
Definition: IRutils.cpp:987
strToDecodeType
decode_type_t strToDecodeType(const char *str)
Convert a C-style string to a decode_type_t.
Definition: IRutils.cpp:120
irutils::checkInvertedBytePairs
bool checkInvertedBytePairs(const uint8_t *const ptr, const uint16_t length)
Check an array to see if every second byte of a pair is a bit inverted/flipped copy of the first/prev...
Definition: IRutils.cpp:1376
kHighNibble
const uint8_t kHighNibble
Definition: IRutils.h:19
irutils::addTempToString
String addTempToString(const uint16_t degrees, const bool celsius, const bool precomma, const bool isSensorTemp)
Create a String of human output for a given temperature. e.g. "Temp: 25C".
Definition: IRutils.cpp:733
irutils::sumNibbles
uint8_t sumNibbles(const uint8_t *const start, const uint16_t length, const uint8_t init)
Sum all the nibbles together in a series of bytes.
Definition: IRutils.cpp:1164
uint64ToString
String uint64ToString(uint64_t input, uint8_t base=10)
Convert a uint64_t (unsigned long long) to a string. Arduino String/toInt/Serial.print() can't handle...
Definition: IRutils.cpp:66
celsiusToFahrenheit
float celsiusToFahrenheit(const float deg)
Convert degrees Celsius to degrees Fahrenheit.
Definition: IRutils.cpp:524
irutils::addIntToString
String addIntToString(const uint16_t value, const String label, const bool precomma)
Create a String with a colon separated labeled Integer suitable for Humans. e.g. "Foo: 23".
Definition: IRutils.cpp:576
xorBytes
uint8_t xorBytes(const uint8_t *const start, const uint16_t length, const uint8_t init=0)
Calculate a rolling XOR of all the bytes of an array.
Definition: IRutils.cpp:462
irutils::addSwingVToString
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)
Create a String of human output for the given vertical swing setting. e.g. "Swing(V): 0 (Auto)".
Definition: IRutils.cpp:931
kModeBitsSize
const uint8_t kModeBitsSize
Definition: IRutils.h:20
irutils::getBit
bool getBit(const uint64_t data, const uint8_t position, const uint8_t size)
Return the value of positionth bit of an Integer.
Definition: IRutils.cpp:1224
irutils::sumBytes
uint16_t sumBytes(const uint64_t data, const uint8_t count, const uint8_t init, const bool byteonly)
Sum all the bytes together in an integer.
Definition: IRutils.cpp:1194
fahrenheitToCelsius
float fahrenheitToCelsius(const float deg)
Convert degrees Fahrenheit to degrees Celsius.
Definition: IRutils.cpp:527
IRrecv.h
sumBytes
uint8_t sumBytes(const uint8_t *const start, const uint16_t length, const uint8_t init=0)
Sum all the bytes of an array and return the least significant 8-bits of the result.
Definition: IRutils.cpp:449
irutils::addSwingHToString
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)
Create a String of human output for the given horizontal swing setting. e.g. "Swing(H): 0 (Auto)".
Definition: IRutils.cpp:872
typeToString
String typeToString(const decode_type_t protocol, const bool isRepeat=false)
Convert a protocol type (enum etc) to a human readable string.
Definition: IRutils.cpp:141
irutils::addToggleToString
String addToggleToString(const bool toggle, const String label, const bool precomma)
Create a String with a colon separated toggle flag suitable for Humans. e.g. "Light: Toggle",...
Definition: IRutils.cpp:564
irutils::irCommandTypeToString
String irCommandTypeToString(uint8_t irCommandType, uint8_t acControlCmd, uint8_t iFeelReportCmd, uint8_t timerCmd, uint8_t configCmd)
Create a String of human output for the given command type e.g. "IFeel Report".
Definition: IRutils.cpp:1036
resultToHexidecimal
String resultToHexidecimal(const decode_results *const result)
Convert the decode_results structure's value/state to simple hexadecimal.
Definition: IRutils.cpp:376
irutils::addBoolToString
String addBoolToString(const bool value, const String label, const bool precomma)
Create a String with a colon separated flag suitable for Humans. e.g. "Power: On".
Definition: IRutils.cpp:553
irutils::addFanToString
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)
Create a String of human output for the given fan speed. e.g. "Fan: 0 (Auto)".
Definition: IRutils.cpp:837
irutils::minsToString
String minsToString(const uint16_t mins)
Convert a nr. of minutes into a 24h clock format Human-readable string. e.g. "23:59".
Definition: IRutils.cpp:1149
irutils::addTempFloatToString
String addTempFloatToString(const float degrees, const bool celsius, const bool precomma, const bool isSensorTemp)
Create a String of human output for a given temperature. e.g. "Temp: 25.5C".
Definition: IRutils.cpp:749
irutils::invertBytePairs
uint8_t * invertBytePairs(uint8_t *ptr, const uint16_t length)
Create byte pairs where the second byte of the pair is a bit inverted/flipped copy of the first/previ...
Definition: IRutils.cpp:1361
irutils::bcdToUint8
uint8_t bcdToUint8(const uint8_t bcd)
Convert a byte of Binary Coded Decimal(BCD) into an Integer.
Definition: IRutils.cpp:1206
irutils
Namespace for covering common functions & procedures for advancd protocol handlers.
Definition: IRutils.cpp:529
reverseBits
uint64_t reverseBits(uint64_t input, uint16_t nbits)
Reverse the order of the requested least significant nr. of bits.
Definition: IRutils.cpp:46
IRXmpUtils::isRepeat
bool isRepeat(const uint64_t data, const uint16_t nbits)
Test if an XMP message code is a repeat or not.
Definition: ir_Xmp.cpp:85
irutils::htmlEscape
String htmlEscape(const String unescaped)
Escape any special HTML (unsafe) characters in a string. e.g. anti-XSS.
Definition: IRutils.cpp:1080
irutils::channelToString
String channelToString(const uint8_t channel)
Create a String of human output for the given channel e.g. "[CH#0]".
Definition: IRutils.cpp:1018
hasACState
bool hasACState(const decode_type_t protocol)
Does the given protocol use a complex state as part of the decode?
Definition: IRutils.cpp:167
irutils::setBit
uint64_t setBit(const uint64_t data, const uint8_t position, const bool on, const uint8_t size)
Return the value of an Integer with the positionth bit changed.
Definition: IRutils.cpp:1244
serialPrintUint64
void serialPrintUint64(uint64_t input, uint8_t base=10)
Print a uint64_t/unsigned long long to the Serial port Serial.print() can't handle printing long long...
Definition: IRutils.cpp:112
int64ToString
String int64ToString(int64_t input, uint8_t base=10)
Convert a int64_t (signed long long) to a string. Arduino String/toInt/Serial.print() can't handle pr...
Definition: IRutils.cpp:97
irutils::daysBitmaskToString
String daysBitmaskToString(uint8_t daysBitmap, uint8_t offset)
Create a String of the 3-letter day of the week bitmap.
Definition: IRutils.cpp:1062
irutils::addLabeledString
String addLabeledString(const String value, const String label, const bool precomma)
Create a String with a colon separated "label: value" pair suitable for Humans.
Definition: IRutils.cpp:536
invertBits
uint64_t invertBits(const uint64_t data, const uint16_t nbits)
Invert/Flip the bits in an Integer.
Definition: IRutils.cpp:513