From 57ff465d39236ae7982849611ac9b68b1db03f06 Mon Sep 17 00:00:00 2001 From: Igor Mammedov Date: Thu, 23 May 2019 21:14:40 +0200 Subject: [PATCH] mhz19: use the same length for request and reply per spec request and response are of the same length, simplify code by using the same length buffers. It should help later when more code that uses buffers are added. Signed-off-by: Igor Mammedov --- esphome/components/mhz19/mhz19.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/esphome/components/mhz19/mhz19.cpp b/esphome/components/mhz19/mhz19.cpp index 581d010aaf..198ac01139 100644 --- a/esphome/components/mhz19/mhz19.cpp +++ b/esphome/components/mhz19/mhz19.cpp @@ -5,20 +5,19 @@ namespace esphome { namespace mhz19 { static const char *TAG = "mhz19"; -static const uint8_t MHZ19_REQUEST_LENGTH = 8; -static const uint8_t MHZ19_RESPONSE_LENGTH = 9; -static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00}; +static const uint8_t MHZ19_PDU_LENGTH = 9; +static const uint8_t MHZ19_COMMAND_GET_PPM[] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; uint8_t mhz19_checksum(const uint8_t *command) { uint8_t sum = 0; - for (uint8_t i = 1; i < MHZ19_REQUEST_LENGTH; i++) { + for (uint8_t i = 1; i < MHZ19_PDU_LENGTH - 1; i++) { sum += command[i]; } return 0xFF - sum + 0x01; } void MHZ19Component::update() { - uint8_t response[MHZ19_RESPONSE_LENGTH]; + uint8_t response[MHZ19_PDU_LENGTH]; if (!this->mhz19_write_command_(MHZ19_COMMAND_GET_PPM, response)) { ESP_LOGW(TAG, "Reading data from MHZ19 failed!"); this->status_set_warning(); @@ -51,14 +50,13 @@ void MHZ19Component::update() { } bool MHZ19Component::mhz19_write_command_(const uint8_t *command, uint8_t *response) { - this->write_array(command, MHZ19_REQUEST_LENGTH); - this->write_byte(mhz19_checksum(command)); + this->write_array(command, MHZ19_PDU_LENGTH); this->flush(); if (response == nullptr) return true; - bool ret = this->read_array(response, MHZ19_RESPONSE_LENGTH); + bool ret = this->read_array(response, MHZ19_PDU_LENGTH); return ret; } float MHZ19Component::get_setup_priority() const { return setup_priority::DATA; }