mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-01 18:48:28 +02:00
Move crc16 to helpers (#3780)
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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; }
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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};
|
||||
|
||||
Reference in New Issue
Block a user