5.9.1f * Upgrade library ArduinoJson to 5.11.2 * Upgrade library IRRemoteEsp8266 to 2.2.1 + 2 commits but tweaked some protocols to keep code usage small * Upgrade library NeoPixelBus to 2.2.9 * Upgrade library OneWire to 2.3.3 + 6 commits * Formalize library PubSubClient to 2.6 + 9 commits and additional delay * Add optional ADS1115 driver as alternative for unsupported I2Cdevlib in esp8266-core 2.4.0-rc2 * Fix wrong response name for command HlwISet (#1214)
83 lines
2.1 KiB
C++
83 lines
2.1 KiB
C++
// Copyright 2017 David Conran
|
|
|
|
#include "IRutils.h"
|
|
#ifndef UNIT_TEST
|
|
#include <Arduino.h>
|
|
#endif
|
|
|
|
#define __STDC_LIMIT_MACROS
|
|
#include <stdint.h>
|
|
#include <algorithm>
|
|
#ifndef ARDUINO
|
|
#include <string>
|
|
#endif
|
|
#include "IRrecv.h"
|
|
|
|
// Reverse the order of the requested least significant nr. of bits.
|
|
// Args:
|
|
// input: Bit pattern/integer to reverse.
|
|
// nbits: Nr. of bits to reverse.
|
|
// Returns:
|
|
// 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, (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.
|
|
//
|
|
// Args:
|
|
// input: The value to print
|
|
// base: The output base.
|
|
// Returns:
|
|
// A string representation of the integer.
|
|
// Note: Based on Arduino's Print::printNumber()
|
|
#ifdef ARDUINO // Arduino's & C++'s string implementations can't co-exist.
|
|
String uint64ToString(uint64_t input, uint8_t base) {
|
|
String result = "";
|
|
#else
|
|
std::string uint64ToString(uint64_t input, uint8_t base) {
|
|
std::string result = "";
|
|
#endif
|
|
// 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;
|
|
|
|
do {
|
|
char c = input % base;
|
|
input /= base;
|
|
|
|
if (c < 10)
|
|
c +='0';
|
|
else
|
|
c += 'A' - 10;
|
|
result = c + result;
|
|
} while (input);
|
|
return result;
|
|
}
|
|
|
|
#ifdef ARDUINO
|
|
// Print a uint64_t/unsigned long long to the Serial port
|
|
// Serial.print() can't handle printing long longs. (uint64_t)
|
|
//
|
|
// Args:
|
|
// input: The value to print
|
|
// base: The output base.
|
|
void serialPrintUint64(uint64_t input, uint8_t base) {
|
|
Serial.print(uint64ToString(input, base));
|
|
}
|
|
#endif
|