From c756bb3b3e294f2ba1d967c05358dbcf7a920bd1 Mon Sep 17 00:00:00 2001 From: Thomas Rupprecht Date: Tue, 29 Apr 2025 21:29:04 +0200 Subject: [PATCH] [pmsa003i] code improvements (#8485) --- esphome/components/pmsa003i/pmsa003i.cpp | 59 +++++++++++++++++------- esphome/components/pmsa003i/pmsa003i.h | 50 ++++++++++---------- 2 files changed, 68 insertions(+), 41 deletions(-) diff --git a/esphome/components/pmsa003i/pmsa003i.cpp b/esphome/components/pmsa003i/pmsa003i.cpp index a9665c6a5a..36f9c9a132 100644 --- a/esphome/components/pmsa003i/pmsa003i.cpp +++ b/esphome/components/pmsa003i/pmsa003i.cpp @@ -1,5 +1,6 @@ #include "pmsa003i.h" #include "esphome/core/log.h" +#include "esphome/core/helpers.h" #include namespace esphome { @@ -7,6 +8,16 @@ namespace pmsa003i { static const char *const TAG = "pmsa003i"; +static const uint8_t COUNT_PAYLOAD_BYTES = 28; +static const uint8_t COUNT_PAYLOAD_LENGTH_BYTES = 2; +static const uint8_t COUNT_START_CHARACTER_BYTES = 2; +static const uint8_t COUNT_DATA_BYTES = COUNT_START_CHARACTER_BYTES + COUNT_PAYLOAD_LENGTH_BYTES + COUNT_PAYLOAD_BYTES; +static const uint8_t CHECKSUM_START_INDEX = COUNT_DATA_BYTES - 2; +static const uint8_t COUNT_16_BIT_VALUES = (COUNT_PAYLOAD_LENGTH_BYTES + COUNT_PAYLOAD_BYTES) / 2; +static const uint8_t START_CHARACTER_1 = 0x42; +static const uint8_t START_CHARACTER_2 = 0x4D; +static const uint8_t READ_DATA_RETRY_COUNT = 3; + void PMSA003IComponent::setup() { ESP_LOGCONFIG(TAG, "Setting up pmsa003i..."); @@ -14,7 +25,7 @@ void PMSA003IComponent::setup() { bool successful_read = this->read_data_(&data); if (!successful_read) { - for (int i = 0; i < 3; i++) { + for (uint8_t i = 0; i < READ_DATA_RETRY_COUNT; i++) { successful_read = this->read_data_(&data); if (successful_read) { break; @@ -28,7 +39,10 @@ void PMSA003IComponent::setup() { } } -void PMSA003IComponent::dump_config() { LOG_I2C_DEVICE(this); } +void PMSA003IComponent::dump_config() { + ESP_LOGCONFIG(TAG, "PMSA003I:"); + LOG_I2C_DEVICE(this); +} void PMSA003IComponent::update() { PM25AQIData data; @@ -75,35 +89,48 @@ void PMSA003IComponent::update() { } bool PMSA003IComponent::read_data_(PM25AQIData *data) { - const uint8_t num_bytes = 32; - uint8_t buffer[num_bytes]; + uint8_t buffer[COUNT_DATA_BYTES]; - this->read_bytes_raw(buffer, num_bytes); + this->read_bytes_raw(buffer, COUNT_DATA_BYTES); // https://github.com/adafruit/Adafruit_PM25AQI // Check that start byte is correct! - if (buffer[0] != 0x42) { + if (buffer[0] != START_CHARACTER_1 || buffer[1] != START_CHARACTER_2) { + ESP_LOGW(TAG, "Start character mismatch: %02X %02X != %02X %02X", buffer[0], buffer[1], START_CHARACTER_1, + START_CHARACTER_2); return false; } - // get checksum ready - int16_t sum = 0; - for (uint8_t i = 0; i < 30; i++) { - sum += buffer[i]; + const uint16_t payload_length = encode_uint16(buffer[2], buffer[3]); + if (payload_length != COUNT_PAYLOAD_BYTES) { + ESP_LOGW(TAG, "Payload length mismatch: %u != %u", payload_length, COUNT_PAYLOAD_BYTES); + return false; + } + + // Calculate checksum + uint16_t checksum = 0; + for (uint8_t i = 0; i < CHECKSUM_START_INDEX; i++) { + checksum += buffer[i]; + } + + const uint16_t check = encode_uint16(buffer[CHECKSUM_START_INDEX], buffer[CHECKSUM_START_INDEX + 1]); + if (checksum != check) { + ESP_LOGW(TAG, "Checksum mismatch: %u != %u", checksum, check); + return false; } // The data comes in endian'd, this solves it so it works on all platforms - uint16_t buffer_u16[15]; - for (uint8_t i = 0; i < 15; i++) { - buffer_u16[i] = buffer[2 + i * 2 + 1]; - buffer_u16[i] += (buffer[2 + i * 2] << 8); + uint16_t buffer_u16[COUNT_16_BIT_VALUES]; + for (uint8_t i = 0; i < COUNT_16_BIT_VALUES; i++) { + const uint8_t buffer_index = COUNT_START_CHARACTER_BYTES + i * 2; + buffer_u16[i] = encode_uint16(buffer[buffer_index], buffer[buffer_index + 1]); } // put it into a nice struct :) - memcpy((void *) data, (void *) buffer_u16, 30); + memcpy((void *) data, (void *) buffer_u16, COUNT_16_BIT_VALUES * 2); - return (sum == data->checksum); + return true; } } // namespace pmsa003i diff --git a/esphome/components/pmsa003i/pmsa003i.h b/esphome/components/pmsa003i/pmsa003i.h index 1fe4139951..59f39a7314 100644 --- a/esphome/components/pmsa003i/pmsa003i.h +++ b/esphome/components/pmsa003i/pmsa003i.h @@ -10,21 +10,21 @@ namespace pmsa003i { /**! Structure holding Plantower's standard packet **/ // From https://github.com/adafruit/Adafruit_PM25AQI struct PM25AQIData { - uint16_t framelen; ///< How long this data chunk is - uint16_t pm10_standard, ///< Standard PM1.0 - pm25_standard, ///< Standard PM2.5 - pm100_standard; ///< Standard PM10.0 - uint16_t pm10_env, ///< Environmental PM1.0 - pm25_env, ///< Environmental PM2.5 - pm100_env; ///< Environmental PM10.0 - uint16_t particles_03um, ///> 0.3um Particle Count - particles_05um, ///> 0.5um Particle Count - particles_10um, ///> 1.0um Particle Count - particles_25um, ///> 2.5um Particle Count - particles_50um, ///> 5.0um Particle Count - particles_100um; ///> 10.0um Particle Count - uint16_t unused; ///< Unused - uint16_t checksum; ///< Packet checksum + uint16_t framelen; ///< How long this data chunk is + uint16_t pm10_standard; ///< Standard PM1.0 + uint16_t pm25_standard; ///< Standard PM2.5 + uint16_t pm100_standard; ///< Standard PM10.0 + uint16_t pm10_env; ///< Environmental PM1.0 + uint16_t pm25_env; ///< Environmental PM2.5 + uint16_t pm100_env; ///< Environmental PM10.0 + uint16_t particles_03um; ///< 0.3um Particle Count + uint16_t particles_05um; ///< 0.5um Particle Count + uint16_t particles_10um; ///< 1.0um Particle Count + uint16_t particles_25um; ///< 2.5um Particle Count + uint16_t particles_50um; ///< 5.0um Particle Count + uint16_t particles_100um; ///< 10.0um Particle Count + uint16_t unused; ///< Unused + uint16_t checksum; ///< Packet checksum }; class PMSA003IComponent : public PollingComponent, public i2c::I2CDevice { @@ -34,18 +34,18 @@ class PMSA003IComponent : public PollingComponent, public i2c::I2CDevice { void update() override; float get_setup_priority() const override { return setup_priority::DATA; } - void set_standard_units(bool standard_units) { standard_units_ = standard_units; } + void set_standard_units(bool standard_units) { this->standard_units_ = standard_units; } - void set_pm_1_0_sensor(sensor::Sensor *pm_1_0) { pm_1_0_sensor_ = pm_1_0; } - void set_pm_2_5_sensor(sensor::Sensor *pm_2_5) { pm_2_5_sensor_ = pm_2_5; } - void set_pm_10_0_sensor(sensor::Sensor *pm_10_0) { pm_10_0_sensor_ = pm_10_0; } + void set_pm_1_0_sensor(sensor::Sensor *pm_1_0) { this->pm_1_0_sensor_ = pm_1_0; } + void set_pm_2_5_sensor(sensor::Sensor *pm_2_5) { this->pm_2_5_sensor_ = pm_2_5; } + void set_pm_10_0_sensor(sensor::Sensor *pm_10_0) { this->pm_10_0_sensor_ = pm_10_0; } - void set_pmc_0_3_sensor(sensor::Sensor *pmc_0_3) { pmc_0_3_sensor_ = pmc_0_3; } - void set_pmc_0_5_sensor(sensor::Sensor *pmc_0_5) { pmc_0_5_sensor_ = pmc_0_5; } - void set_pmc_1_0_sensor(sensor::Sensor *pmc_1_0) { pmc_1_0_sensor_ = pmc_1_0; } - void set_pmc_2_5_sensor(sensor::Sensor *pmc_2_5) { pmc_2_5_sensor_ = pmc_2_5; } - void set_pmc_5_0_sensor(sensor::Sensor *pmc_5_0) { pmc_5_0_sensor_ = pmc_5_0; } - void set_pmc_10_0_sensor(sensor::Sensor *pmc_10_0) { pmc_10_0_sensor_ = pmc_10_0; } + void set_pmc_0_3_sensor(sensor::Sensor *pmc_0_3) { this->pmc_0_3_sensor_ = pmc_0_3; } + void set_pmc_0_5_sensor(sensor::Sensor *pmc_0_5) { this->pmc_0_5_sensor_ = pmc_0_5; } + void set_pmc_1_0_sensor(sensor::Sensor *pmc_1_0) { this->pmc_1_0_sensor_ = pmc_1_0; } + void set_pmc_2_5_sensor(sensor::Sensor *pmc_2_5) { this->pmc_2_5_sensor_ = pmc_2_5; } + void set_pmc_5_0_sensor(sensor::Sensor *pmc_5_0) { this->pmc_5_0_sensor_ = pmc_5_0; } + void set_pmc_10_0_sensor(sensor::Sensor *pmc_10_0) { this->pmc_10_0_sensor_ = pmc_10_0; } protected: bool read_data_(PM25AQIData *data);