1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-14 01:02:19 +01:00

Introduce byteswap helpers (#2661)

* Backport std::byteswap() in helpers.h

* Introduce convert_big_endian() function

* Use convert_big_endian() in i2c byte swap functions
This commit is contained in:
Oxan van Leeuwen
2021-11-10 19:40:18 +01:00
committed by GitHub
parent 8aa72f4c1e
commit c422b2fb0b
2 changed files with 28 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include "i2c_bus.h"
#include "esphome/core/helpers.h"
#include "esphome/core/optional.h"
#include <array>
#include <vector>
@@ -32,16 +33,8 @@ class I2CRegister {
// like ntohs/htons but without including networking headers.
// ("i2c" byte order is big-endian)
inline uint16_t i2ctohs(uint16_t i2cshort) {
union {
uint16_t x;
uint8_t y[2];
} conv;
conv.x = i2cshort;
return ((uint16_t) conv.y[0] << 8) | ((uint16_t) conv.y[1] << 0);
}
inline uint16_t htoi2cs(uint16_t hostshort) { return i2ctohs(hostshort); }
inline uint16_t i2ctohs(uint16_t i2cshort) { return convert_big_endian(i2cshort); }
inline uint16_t htoi2cs(uint16_t hostshort) { return convert_big_endian(hostshort); }
class I2CDevice {
public: