Skip to content

Commit

Permalink
Move crc16 to helpers (esphome#3780)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesserockz authored Sep 6, 2022
1 parent 614eb81 commit c317422
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 58 deletions.
24 changes: 3 additions & 21 deletions esphome/components/am2320/am2320.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 0 additions & 16 deletions esphome/components/modbus/modbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions esphome/components/modbus/modbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
20 changes: 2 additions & 18 deletions esphome/components/senseair/senseair.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "senseair.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"

namespace esphome {
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down
1 change: 0 additions & 1 deletion esphome/components/senseair/senseair.h
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
15 changes: 15 additions & 0 deletions esphome/core/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions esphome/core/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit c317422

Please sign in to comment.