# IRremoteESP8266: src/IRutils.cpp File Reference

IRutils.cpp File Reference

## Namespaces

| irutils
| Namespace for covering common functions & procedures for advancd protocol handlers.

## Functions

uint64_t | reverseBits (uint64_t input, uint16_t nbits)
| Reverse the order of the requested least significant nr. of bits. More...
String | uint64ToString (uint64_t input, uint8_t base)
| Convert a uint64_t (unsigned long long) to a string. Arduino String/toInt/Serial.print() can't handle printing 64 bit values. More...
void | serialPrintUint64 (uint64_t input, uint8_t base)
| Print a uint64_t/unsigned long long to the Serial port Serial.print() can't handle printing long longs. (uint64_t) More...
decode_type_t | strToDecodeType (const char *const str)
| Convert a C-style string to a decode_type_t. More...
String | typeToString (const decode_type_t protocol, const bool isRepeat)
| Convert a protocol type (enum etc) to a human readable string. More...
bool | hasACState (const decode_type_t protocol)
| Does the given protocol use a complex state as part of the decode? More...
uint16_t | getCorrectedRawLength (const decode_results *const results)
| Return the corrected length of a 'raw' format array structure after over-large values are converted into multiple entries. More...
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. More...
String | resultToTimingInfo (const decode_results *const results)
| Dump out the decode_results structure. More...
String | resultToHexidecimal (const decode_results *const result)
| Convert the decode_results structure's value/state to simple hexadecimal. More...
String | resultToHumanReadableBasic (const decode_results *const results)
| Dump out the decode_results structure into a human readable format. More...
uint16_t * | resultToRawArray (const decode_results *const decode)
| Convert a decode_results into an array suitable for `sendRaw()`. More...
uint8_t | sumBytes (const uint8_t *const start, const uint16_t length, const uint8_t init)
| Sum all the bytes of an array and return the least significant 8-bits of the result. More...
uint8_t | xorBytes (const uint8_t *const start, const uint16_t length, const uint8_t init)
| Calculate a rolling XOR of all the bytes of an array. More...
uint16_t | countBits (const uint8_t *const start, const uint16_t length, const bool ones, const uint16_t init)
| Count the number of bits of a certain type in an array. More...
uint16_t | countBits (const uint64_t data, const uint8_t length, const bool ones, const uint16_t init)
| Count the number of bits of a certain type in an Integer. More...
uint64_t | invertBits (const uint64_t data, const uint16_t nbits)
| Invert/Flip the bits in an Integer. More...
float | celsiusToFahrenheit (const float deg)
| Convert degrees Celsius to degrees Fahrenheit. More...
float | fahrenheitToCelsius (const float deg)
| Convert degrees Fahrenheit to degrees Celsius. More...
String | irutils::addLabeledString (const String value, const String label, const bool precomma)
| Create a String with a colon separated "label: value" pair suitable for Humans. More...
String | irutils::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". More...
String | irutils::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". More...
String | irutils::modelToStr (const decode_type_t protocol, const int16_t model)
| Generate the model string for a given Protocol/Model pair. More...
String | irutils::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". More...
String | irutils::addTempToString (const uint16_t degrees, const bool celsius, const bool precomma)
| Create a String of human output for a given temperature. e.g. "Temp: 25C". More...
String | irutils::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)". More...
String | irutils::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.g. "Day: 1 (Mon)". More...
String | irutils::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)
| Create a String of human output for the given fan speed. e.g. "Fan: 0 (Auto)". More...
String | irutils::htmlEscape (const String unescaped)
| Escape any special HTML (unsafe) characters in a string. e.g. anti-XSS. More...
String | irutils::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". More...
String | irutils::minsToString (const uint16_t mins)
| Convert a nr. of minutes into a 24h clock format Human-readable string. e.g. "23:59". More...
uint8_t | irutils::sumNibbles (const uint8_t *const start, const uint16_t length, const uint8_t init)
| Sum all the nibbles together in a series of bytes. More...
uint8_t | irutils::sumNibbles (const uint64_t data, const uint8_t count, const uint8_t init, const bool nibbleonly)
| Sum all the nibbles together in an integer. More...
uint8_t | irutils::bcdToUint8 (const uint8_t bcd)
| Convert a byte of Binary Coded Decimal(BCD) into an Integer. More...
uint8_t | irutils::uint8ToBcd (const uint8_t integer)
| Convert an Integer into a byte of Binary Coded Decimal(BCD). More...
bool | irutils::getBit (const uint64_t data, const uint8_t position, const uint8_t size)
| Return the value of `position`th bit of an Integer. More...
bool | irutils::getBit (const uint8_t data, const uint8_t position)
| Return the value of `position`th bit of an Integer. More...
uint64_t | irutils::setBit (const uint64_t data, const uint8_t position, const bool on, const uint8_t size)
| Return the value of an Integer with the `position`th bit changed. More...
uint8_t | irutils::setBit (const uint8_t data, const uint8_t position, const bool on)
| Return the value of an Integer with the `position`th bit changed. More...
void | irutils::setBit (uint8_t *const data, const uint8_t position, const bool on)
| Alter the value of an Integer with the `position`th bit changed. More...
void | irutils::setBit (uint32_t *const data, const uint8_t position, const bool on)
| Alter the value of an Integer with the `position`th bit changed. More...
void | irutils::setBit (uint64_t *const data, const uint8_t position, const bool on)
| Alter the value of an Integer with the `position`th bit changed. More...
void | irutils::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. More...
void | irutils::setBits (uint32_t *const dst, const uint8_t offset, const uint8_t nbits, const uint32_t data)
| Alter an uint32_t value by overwriting an arbitrary given number of bits. More...
void | irutils::setBits (uint64_t *const dst, const uint8_t offset, const uint8_t nbits, const uint64_t data)
| Alter an uint64_t value by overwriting an arbitrary given number of bits. More...
uint8_t * | irutils::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/previous byte of the pair. More...
bool | irutils::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/previous byte of the pair. More...
uint8_t | irutils::lowLevelSanityCheck (void)
| Perform a low level bit manipulation sanity check for the given cpu architecture and the compiler operation. Calls to this should return 0 if everything is as expected, anything else means the library won't work as expected. More...

## Function Documentation

## celsiusToFahrenheit()

float celsiusToFahrenheit | ( | const float | deg | ) |

Convert degrees Celsius to degrees Fahrenheit.

## countBits() [1/2]

uint16_t countBits | ( | const uint64_t | data,
|  | const uint8_t | length,
|  | const bool | ones,
|  | const uint16_t | init
| ) |  |

Count the number of bits of a certain type in an Integer.

Parameters

  [in] | data | The value you want bits counted for. Starting from the LSB.
  [in] | length | How many bits to use in the calculation? Starts at the LSB
  [in] | ones | Count the binary nr of `1` bits. False is count the `0`s.
  [in] | init | Starting value of the calculation to use. (Default is 0)

Returns
  The nr. of bits found of the given type found in the Integer.

## countBits() [2/2]

uint16_t countBits | ( | const uint8_t *const | start,
|  | const uint16_t | length,
|  | const bool | ones,
|  | const uint16_t | init
| ) |  |

Count the number of bits of a certain type in an array.

Parameters

  [in] | start | A ptr to the start of the byte array to calculate over.
  [in] | length | How many bytes to use in the calculation.
  [in] | ones | Count the binary nr of `1` bits. False is count the `0`s.
  [in] | init | Starting value of the calculation to use. (Default is 0)

Returns
  The nr. of bits found of the given type found in the array.

## fahrenheitToCelsius()

float fahrenheitToCelsius | ( | const float | deg | ) |

Convert degrees Fahrenheit to degrees Celsius.

## 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 into multiple entries.

Parameters

  [in] | results | A ptr to a decode_results structure.

Returns
  The corrected length.

## hasACState()

bool hasACState | ( | const decode_type_t | protocol | ) |

Does the given protocol use a complex state as part of the decode?

Parameters

  [in] | protocol | The decode_type_t protocol we are enquiring about.

Returns
  True if the protocol uses a state array. False if just an integer.

## invertBits()

uint64_t invertBits | ( | const uint64_t | data,
|  | const uint16_t | nbits
| ) |  |

Invert/Flip the bits in an Integer.

Parameters

  [in] | data | The Integer that will be inverted.
  [in] | nbits | How many bits are to be inverted. Starting from the LSB.

Returns
  An Integer with the appropriate bits inverted/flipped.

## resultToHexidecimal()

String resultToHexidecimal | ( | const decode_results *const | result | ) |

Convert the decode_results structure's value/state to simple hexadecimal.

Parameters

  [in] | result | A ptr to a decode_results structure.

Returns
  A String containing the output.

## resultToHumanReadableBasic()

String resultToHumanReadableBasic | ( | const decode_results *const | results | ) |

Dump out the decode_results structure into a human readable format.

Parameters

  [in] | results | A ptr to a decode_results structure.

Returns
  A String containing the output.

## resultToRawArray()

uint16_t* resultToRawArray | ( | const decode_results *const | decode | ) |

Convert a decode_results into an array suitable for `sendRaw()`.

Parameters

  [in] | decode | A ptr to a decode_results structure that contains a mesg.

Returns
  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.

## 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.

Parameters

  [in] | results | A ptr to a decode_results structure.

Returns
  A String containing the code-ified result.

## resultToTimingInfo()

String resultToTimingInfo | ( | const decode_results *const | results | ) |

Dump out the decode_results structure.

Parameters

  [in] | results | A ptr to a decode_results structure.

Returns
  A String containing the legacy information format.

Deprecated:
  This is only for those that want this legacy format.

## reverseBits()

uint64_t reverseBits | ( | uint64_t | input,
|  | uint16_t | nbits
| ) |  |

Reverse the order of the requested least significant nr. of bits.

Parameters

  [in] | input | Bit pattern/integer to reverse.
  [in] | nbits | Nr. of bits to reverse. (LSB -> MSB)

Returns
  The reversed bit pattern.

## serialPrintUint64()

void serialPrintUint64 | ( | uint64_t | input,
|  | uint8_t | base
| ) |  |

Print a uint64_t/unsigned long long to the Serial port Serial.print() can't handle printing long longs. (uint64_t)

Parameters

  [in] | input | The value to print
  [in] | base | The output base.

## strToDecodeType()

decode_type_t strToDecodeType | ( | const char *const | str | ) |

Convert a C-style string to a decode_type_t.

Parameters

  [in] | str | A C-style string containing a protocol name or number.

Returns
  A decode_type_t enum. (decode_type_t::UNKNOWN if no match.)

## sumBytes()

uint8_t sumBytes | ( | const uint8_t *const | start,
|  | const uint16_t | length,
|  | const uint8_t | init
| ) |  |

Sum all the bytes of an array and return the least significant 8-bits of the result.

Parameters

  [in] | start | A ptr to the start of the byte array to calculate over.
  [in] | length | How many bytes to use in the calculation.
  [in] | init | Starting value of the calculation to use. (Default is 0)

Returns
  The 8-bit calculated result of all the bytes and init value.

## typeToString()

String typeToString | ( | const decode_type_t | protocol,
|  | const bool | isRepeat
| ) |  |

Convert a protocol type (enum etc) to a human readable string.

Parameters

  [in] | protocol | Nr. (enum) of the protocol.
  [in] | isRepeat | A flag indicating if it is a repeat message.

Returns
  A String containing the protocol name. kUnknownStr if no match.

## uint64ToString()

String uint64ToString | ( | uint64_t | input,
|  | uint8_t | base
| ) |  |

Convert a uint64_t (unsigned long long) to a string. Arduino String/toInt/Serial.print() can't handle printing 64 bit values.

Parameters

  [in] | input | The value to print
  [in] | base | The output base.

Returns
  A String representation of the integer.

Note
  Based on Arduino's Print::printNumber()

## xorBytes()

uint8_t xorBytes | ( | const uint8_t *const | start,
|  | const uint16_t | length,
|  | const uint8_t | init
| ) |  |

Calculate a rolling XOR of all the bytes of an array.

Parameters

  [in] | start | A ptr to the start of the byte array to calculate over.
  [in] | length | How many bytes to use in the calculation.
  [in] | init | Starting value of the calculation to use. (Default is 0)

Returns
  The 8-bit calculated result of all the bytes and init value.