Move crc16 to helpers (#3780)

This commit is contained in:
Jesse Hills 2022-09-06 12:57:21 +12:00 committed by GitHub
parent 614eb81ad7
commit c317422ed7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 23 additions and 58 deletions

View file

@ -4,33 +4,15 @@
// - Arduino - AM2320: https://github.com/EngDial/AM2320/blob/master/src/AM2320.cpp
#include "am2320.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace am2320 {
static const char *const TAG = "am2320";
// ---=== Calc CRC16 ===---
uint16_t crc_16(uint8_t *ptr, uint8_t length) {
uint16_t crc = 0xFFFF;
uint8_t i;
//------------------------------
while (length--) {
crc ^= *ptr++;
for (i = 0; i < 8; i++) {
if ((crc & 0x01) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
void AM2320Component::update() {
uint8_t data[8];
data[0] = 0;
@ -98,7 +80,7 @@ bool AM2320Component::read_data_(uint8_t *data) {
checksum = data[7] << 8;
checksum += data[6];
if (crc_16(data, 6) != checksum) {
if (crc16(data, 6) != checksum) {
ESP_LOGW(TAG, "AM2320 Checksum invalid!");
return false;
}

View file

@ -35,22 +35,6 @@ void Modbus::loop() {
}
}
uint16_t crc16(const uint8_t *data, uint8_t len) {
uint16_t crc = 0xFFFF;
while (len--) {
crc ^= *data++;
for (uint8_t i = 0; i < 8; i++) {
if ((crc & 0x01) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
bool Modbus::parse_modbus_byte_(uint8_t byte) {
size_t at = this->rx_buffer_.size();
this->rx_buffer_.push_back(byte);

View file

@ -40,8 +40,6 @@ class Modbus : public uart::UARTDevice, public Component {
std::vector<ModbusDevice *> devices_;
};
uint16_t crc16(const uint8_t *data, uint8_t len);
class ModbusDevice {
public:
void set_parent(Modbus *parent) { parent_ = parent; }

View file

@ -1,4 +1,5 @@
#include "senseair.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
@ -42,7 +43,7 @@ void SenseAirComponent::update() {
return;
}
uint16_t calc_checksum = this->senseair_checksum_(response, 11);
uint16_t calc_checksum = crc16(response, 11);
uint16_t resp_checksum = (uint16_t(response[12]) << 8) | response[11];
if (resp_checksum != calc_checksum) {
ESP_LOGW(TAG, "SenseAir checksum doesn't match: 0x%02X!=0x%02X", resp_checksum, calc_checksum);
@ -60,23 +61,6 @@ void SenseAirComponent::update() {
this->co2_sensor_->publish_state(ppm);
}
uint16_t SenseAirComponent::senseair_checksum_(uint8_t *ptr, uint8_t length) {
uint16_t crc = 0xFFFF;
uint8_t i;
while (length--) {
crc ^= *ptr++;
for (i = 0; i < 8; i++) {
if ((crc & 0x01) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
void SenseAirComponent::background_calibration() {
// Responses are just echoes but must be read to clear the buffer
ESP_LOGD(TAG, "SenseAir Starting background calibration");

View file

@ -23,7 +23,6 @@ class SenseAirComponent : public PollingComponent, public uart::UARTDevice {
void abc_disable();
protected:
uint16_t senseair_checksum_(uint8_t *ptr, uint8_t length);
bool senseair_write_command_(const uint8_t *command, uint8_t *response, uint8_t response_length);
sensor::Sensor *co2_sensor_{nullptr};

View file

@ -62,6 +62,21 @@ uint8_t crc8(uint8_t *data, uint8_t len) {
}
return crc;
}
uint16_t crc16(const uint8_t *data, uint8_t len) {
uint16_t crc = 0xFFFF;
while (len--) {
crc ^= *data++;
for (uint8_t i = 0; i < 8; i++) {
if ((crc & 0x01) != 0) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
uint32_t fnv1_hash(const std::string &str) {
uint32_t hash = 2166136261UL;
for (char c : str) {

View file

@ -149,6 +149,9 @@ template<typename T, typename U> T remap(U value, U min, U max, T min_out, T max
/// Calculate a CRC-8 checksum of \p data with size \p len.
uint8_t crc8(uint8_t *data, uint8_t len);
/// Calculate a CRC-16 checksum of \p data with size \p len.
uint16_t crc16(const uint8_t *data, uint8_t len);
/// Calculate a FNV-1 hash of \p str.
uint32_t fnv1_hash(const std::string &str);