Introduce encode_value/decode_value() template functions (#2662)

This commit is contained in:
Oxan van Leeuwen 2021-11-10 19:44:01 +01:00 committed by GitHub
parent 4d43396835
commit 99c775d8cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 19 deletions

View File

@ -52,7 +52,7 @@ void CCS811Component::setup() {
if (this->baseline_.has_value()) { if (this->baseline_.has_value()) {
// baseline available, write to sensor // baseline available, write to sensor
this->write_bytes(0x11, decode_uint16(*this->baseline_)); this->write_bytes(0x11, decode_value(*this->baseline_));
} }
auto hardware_version_data = this->read_bytes<1>(0x21); auto hardware_version_data = this->read_bytes<1>(0x21);

View File

@ -350,17 +350,6 @@ std::string str_sprintf(const char *fmt, ...) {
return str; return str;
} }
uint16_t encode_uint16(uint8_t msb, uint8_t lsb) { return (uint16_t(msb) << 8) | uint16_t(lsb); }
std::array<uint8_t, 2> decode_uint16(uint16_t value) {
uint8_t msb = (value >> 8) & 0xFF;
uint8_t lsb = (value >> 0) & 0xFF;
return {msb, lsb};
}
uint32_t encode_uint32(uint8_t msb, uint8_t byte2, uint8_t byte3, uint8_t lsb) {
return (uint32_t(msb) << 24) | (uint32_t(byte2) << 16) | (uint32_t(byte3) << 8) | uint32_t(lsb);
}
std::string hexencode(const uint8_t *data, uint32_t len) { std::string hexencode(const uint8_t *data, uint32_t len) {
char buf[20]; char buf[20];
std::string res; std::string res;

View File

@ -138,13 +138,6 @@ uint8_t reverse_bits_8(uint8_t x);
uint16_t reverse_bits_16(uint16_t x); uint16_t reverse_bits_16(uint16_t x);
uint32_t reverse_bits_32(uint32_t x); uint32_t reverse_bits_32(uint32_t x);
/// Encode a 16-bit unsigned integer given a most and least-significant byte.
uint16_t encode_uint16(uint8_t msb, uint8_t lsb);
/// Decode a 16-bit unsigned integer into an array of two values: most significant byte, least significant byte.
std::array<uint8_t, 2> decode_uint16(uint16_t value);
/// Encode a 32-bit unsigned integer given four bytes in MSB -> LSB order
uint32_t encode_uint32(uint8_t msb, uint8_t byte2, uint8_t byte3, uint8_t lsb);
/// Convert RGB floats (0-1) to hue (0-360) & saturation/value percentage (0-1) /// Convert RGB floats (0-1) to hue (0-360) & saturation/value percentage (0-1)
void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value); void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value);
/// Convert hue (0-360) & saturation/value percentage (0-1) to RGB floats (0-1) /// Convert hue (0-360) & saturation/value percentage (0-1) to RGB floats (0-1)
@ -306,6 +299,41 @@ constexpr uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); }
/// @name Bit manipulation /// @name Bit manipulation
///@{ ///@{
/// Encode a 16-bit value given the most and least significant byte.
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb) {
return (static_cast<uint16_t>(msb) << 8) | (static_cast<uint16_t>(lsb));
}
/// Encode a 32-bit value given four bytes in most to least significant byte order.
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4) {
return (static_cast<uint32_t>(byte1) << 24) | (static_cast<uint32_t>(byte2) << 16) |
(static_cast<uint32_t>(byte3) << 8) | (static_cast<uint32_t>(byte4));
}
/// Encode a value from its constituent bytes (from most to least significant) in an array with length sizeof(T).
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> inline T encode_value(const uint8_t *bytes) {
T val = 0;
for (size_t i = 0; i < sizeof(T); i++) {
val <<= 8;
val |= bytes[i];
}
return val;
}
/// Encode a value from its constituent bytes (from most to least significant) in an std::array with length sizeof(T).
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
inline T encode_value(const std::array<uint8_t, sizeof(T)> bytes) {
return encode_value<T>(bytes.data());
}
/// Decode a value into its constituent bytes (from most to least significant).
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
inline std::array<uint8_t, sizeof(T)> decode_value(T val) {
std::array<uint8_t, sizeof(T)> ret{};
for (size_t i = sizeof(T); i > 0; i--) {
ret[i - 1] = val & 0xFF;
val >>= 8;
}
return ret;
}
/// Convert a value between host byte order and big endian (most significant byte first) order. /// Convert a value between host byte order and big endian (most significant byte first) order.
template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr T convert_big_endian(T val) { template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr T convert_big_endian(T val) {
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__