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;
|
return init;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t crc16(uint8_t byte, uint16_t crc) {
|
uint16_t crc16(const std::span<const uint8_t> &data, const uint16_t seed = 0xFFFF) {
|
||||||
crc ^= static_cast<uint16_t>(byte) << 8;
|
return ~esphome::crc16(
|
||||||
for (int j=0; j<4; ++j) {
|
data.data(),
|
||||||
uint16_t tmp = crc << 1;
|
data.size(),
|
||||||
if (crc & 0x8000)
|
seed,
|
||||||
tmp ^= 0x1021;
|
0x1021,
|
||||||
crc = tmp << 1;
|
true, // refin
|
||||||
if (tmp & 0x8000)
|
true // refout
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::span<uint8_t> inner(const std::span<uint8_t> &buffer, Light::Id id, const std::span<uint8_t> &light_data) {
|
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) {
|
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 };
|
constexpr std::array<uint8_t, 3> PREFIX = { 0x71, 0x0F, 0x55 };
|
||||||
|
|
||||||
const auto prefix = subspan(buffer, 0, PREFIX.size());
|
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());
|
std::copy(PREFIX.cbegin(), PREFIX.cend(), prefix.begin());
|
||||||
|
|
||||||
// copy
|
// copy
|
||||||
std::reverse_copy(ADDRESS.cbegin(), ADDRESS.cend(), address.begin());
|
std::copy(ADDRESS.cbegin(), ADDRESS.cend(), address.begin());
|
||||||
|
|
||||||
|
uint16_t crc_value = crc16(buffer.subspan(prefix.size(), address.size() + command.size()));
|
||||||
uint16_t crc_value = crc16(ADDRESS, command);
|
|
||||||
crc[0] = crc_value;
|
crc[0] = crc_value;
|
||||||
crc[1] = crc_value >> 8;
|
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)
|
for (auto &byte:prefix)
|
||||||
byte = reverse_bits(byte);
|
byte = reverse_bits(byte);
|
||||||
for (auto &byte:address)
|
|
||||||
byte = reverse_bits(byte);
|
|
||||||
|
|
||||||
//ESP_LOGV(TAG, "%c%02hhX: reverse: %s", id.kind(), id.value(),
|
//ESP_LOGV(TAG, "%c%02hhX: reverse: %s", id.kind(), id.value(),
|
||||||
// format_hex_pretty(&result[0], result.size()).c_str());
|
// format_hex_pretty(&result[0], result.size()).c_str());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue