use esphome::crc16
This commit is contained in:
parent
d10ec6546d
commit
98e5dd0a5c
1 changed files with 12 additions and 29 deletions
|
|
@ -65,29 +65,15 @@ uint8_t crc(const std::span<uint8_t, size> &bytes, uint8_t init = 0x00) {
|
|||
return init;
|
||||
}
|
||||
|
||||
uint16_t crc16(uint8_t byte, uint16_t crc) {
|
||||
crc ^= static_cast<uint16_t>(byte) << 8;
|
||||
for (int j=0; j<4; ++j) {
|
||||
uint16_t tmp = crc << 1;
|
||||
if (crc & 0x8000)
|
||||
tmp ^= 0x1021;
|
||||
crc = tmp << 1;
|
||||
if (tmp & 0x8000)
|
||||
crc ^= 0x1021;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint16_t crc16(const std::array<uint8_t, 3> &addr, const std::span<uint8_t> &data, const uint16_t seed = 0xFFFF) {
|
||||
auto crc = seed;
|
||||
|
||||
for (auto it = addr.rbegin(); it != addr.rend(); ++it)
|
||||
crc = crc16(*it, crc);
|
||||
|
||||
for (const auto &byte:data)
|
||||
crc = crc16(reverse_bits(byte), crc);
|
||||
|
||||
return ~reverse_bits(crc);
|
||||
uint16_t crc16(const std::span<const uint8_t> &data, const uint16_t seed = 0xFFFF) {
|
||||
return ~esphome::crc16(
|
||||
data.data(),
|
||||
data.size(),
|
||||
seed,
|
||||
0x1021,
|
||||
true, // refin
|
||||
true // refout
|
||||
);
|
||||
}
|
||||
|
||||
std::span<uint8_t> inner(const std::span<uint8_t> &buffer, Light::Id id, const std::span<uint8_t> &light_data) {
|
||||
|
|
@ -158,7 +144,7 @@ std::span<uint8_t> command(const std::span<uint8_t> &buffer, const Key &key, Lig
|
|||
}
|
||||
|
||||
std::span<uint8_t> payload(const std::span<uint8_t> &buffer, const Key &key, Light::Id id, uint8_t seq, const std::span<uint8_t> &light_data) {
|
||||
constexpr std::array<uint8_t, 3> ADDRESS = {0xC1, 0xC2, 0xC3};
|
||||
constexpr std::array<uint8_t, 3> ADDRESS = { 0xC3, 0x43, 0x83 };
|
||||
constexpr std::array<uint8_t, 3> PREFIX = { 0x71, 0x0F, 0x55 };
|
||||
|
||||
const auto prefix = subspan(buffer, 0, PREFIX.size());
|
||||
|
|
@ -188,10 +174,9 @@ std::span<uint8_t> payload(const std::span<uint8_t> &buffer, const Key &key, Lig
|
|||
std::copy(PREFIX.cbegin(), PREFIX.cend(), prefix.begin());
|
||||
|
||||
// copy
|
||||
std::reverse_copy(ADDRESS.cbegin(), ADDRESS.cend(), address.begin());
|
||||
std::copy(ADDRESS.cbegin(), ADDRESS.cend(), address.begin());
|
||||
|
||||
|
||||
uint16_t crc_value = crc16(ADDRESS, command);
|
||||
uint16_t crc_value = crc16(buffer.subspan(prefix.size(), address.size() + command.size()));
|
||||
crc[0] = crc_value;
|
||||
crc[1] = crc_value >> 8;
|
||||
|
||||
|
|
@ -200,8 +185,6 @@ std::span<uint8_t> payload(const std::span<uint8_t> &buffer, const Key &key, Lig
|
|||
|
||||
for (auto &byte:prefix)
|
||||
byte = reverse_bits(byte);
|
||||
for (auto &byte:address)
|
||||
byte = reverse_bits(byte);
|
||||
|
||||
//ESP_LOGV(TAG, "%c%02hhX: reverse: %s", id.kind(), id.value(),
|
||||
// format_hex_pretty(&result[0], result.size()).c_str());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue