From 8267f01ccdcb4d9564d262f3ec0a02fb0650c107 Mon Sep 17 00:00:00 2001 From: Martin <25747549+martgras@users.noreply.github.com> Date: Wed, 17 Nov 2021 20:03:46 +0100 Subject: [PATCH] Remove arduino dependency from hm3301 (#2745) --- .../hm3301/abstract_aqi_calculator.h | 3 --- esphome/components/hm3301/aqi_calculator.h | 4 ---- .../components/hm3301/aqi_calculator_factory.h | 4 ---- esphome/components/hm3301/caqi_calculator.h | 4 ---- esphome/components/hm3301/hm3301.cpp | 13 +++---------- esphome/components/hm3301/hm3301.h | 18 ++++++++---------- esphome/components/hm3301/sensor.py | 4 ---- platformio.ini | 1 - 8 files changed, 11 insertions(+), 40 deletions(-) diff --git a/esphome/components/hm3301/abstract_aqi_calculator.h b/esphome/components/hm3301/abstract_aqi_calculator.h index fb41b921d9..42d900a262 100644 --- a/esphome/components/hm3301/abstract_aqi_calculator.h +++ b/esphome/components/hm3301/abstract_aqi_calculator.h @@ -1,6 +1,5 @@ #pragma once -#ifdef USE_ARDUINO #include namespace esphome { @@ -13,5 +12,3 @@ class AbstractAQICalculator { } // namespace hm3301 } // namespace esphome - -#endif // USE_ARDUINO diff --git a/esphome/components/hm3301/aqi_calculator.h b/esphome/components/hm3301/aqi_calculator.h index a3839b643c..08d1dc2921 100644 --- a/esphome/components/hm3301/aqi_calculator.h +++ b/esphome/components/hm3301/aqi_calculator.h @@ -1,7 +1,5 @@ #pragma once -#ifdef USE_ARDUINO - #include "abstract_aqi_calculator.h" namespace esphome { @@ -48,5 +46,3 @@ class AQICalculator : public AbstractAQICalculator { } // namespace hm3301 } // namespace esphome - -#endif // USE_ARDUINO diff --git a/esphome/components/hm3301/aqi_calculator_factory.h b/esphome/components/hm3301/aqi_calculator_factory.h index 3c6f9709b6..55608b6e51 100644 --- a/esphome/components/hm3301/aqi_calculator_factory.h +++ b/esphome/components/hm3301/aqi_calculator_factory.h @@ -1,7 +1,5 @@ #pragma once -#ifdef USE_ARDUINO - #include "caqi_calculator.h" #include "aqi_calculator.h" @@ -29,5 +27,3 @@ class AQICalculatorFactory { } // namespace hm3301 } // namespace esphome - -#endif // USE_ARDUINO diff --git a/esphome/components/hm3301/caqi_calculator.h b/esphome/components/hm3301/caqi_calculator.h index a7f5460e0a..1ec61f2416 100644 --- a/esphome/components/hm3301/caqi_calculator.h +++ b/esphome/components/hm3301/caqi_calculator.h @@ -1,7 +1,5 @@ #pragma once -#ifdef USE_ARDUINO - #include "esphome/core/log.h" #include "abstract_aqi_calculator.h" @@ -52,5 +50,3 @@ class CAQICalculator : public AbstractAQICalculator { } // namespace hm3301 } // namespace esphome - -#endif // USE_ARDUINO diff --git a/esphome/components/hm3301/hm3301.cpp b/esphome/components/hm3301/hm3301.cpp index 759157f330..a2bef2a01d 100644 --- a/esphome/components/hm3301/hm3301.cpp +++ b/esphome/components/hm3301/hm3301.cpp @@ -1,5 +1,3 @@ -#ifdef USE_ARDUINO - #include "esphome/core/log.h" #include "hm3301.h" @@ -14,9 +12,8 @@ static const uint8_t PM_10_0_VALUE_INDEX = 7; void HM3301Component::setup() { ESP_LOGCONFIG(TAG, "Setting up HM3301..."); - hm3301_ = make_unique(); - error_code_ = hm3301_->init(); - if (error_code_ != NO_ERROR) { + if (i2c::ERROR_OK != this->write(&SELECT_COMM_CMD, 1)) { + error_code_ = ERROR_COMM; this->mark_failed(); return; } @@ -38,7 +35,7 @@ void HM3301Component::dump_config() { float HM3301Component::get_setup_priority() const { return setup_priority::DATA; } void HM3301Component::update() { - if (!this->read_sensor_value_(data_buffer_)) { + if (this->read(data_buffer_, 29) != i2c::ERROR_OK) { ESP_LOGW(TAG, "Read result failed"); this->status_set_warning(); return; @@ -87,8 +84,6 @@ void HM3301Component::update() { this->status_clear_warning(); } -bool HM3301Component::read_sensor_value_(uint8_t *data) { return !hm3301_->read_sensor_value(data, 29); } - bool HM3301Component::validate_checksum_(const uint8_t *data) { uint8_t sum = 0; for (int i = 0; i < 28; i++) { @@ -104,5 +99,3 @@ uint16_t HM3301Component::get_sensor_value_(const uint8_t *data, uint8_t i) { } // namespace hm3301 } // namespace esphome - -#endif // USE_ARDUINO diff --git a/esphome/components/hm3301/hm3301.h b/esphome/components/hm3301/hm3301.h index 61bbf7e4ab..e13ffa466e 100644 --- a/esphome/components/hm3301/hm3301.h +++ b/esphome/components/hm3301/hm3301.h @@ -1,17 +1,15 @@ #pragma once -#ifdef USE_ARDUINO - #include "esphome/core/component.h" #include "esphome/components/sensor/sensor.h" #include "esphome/components/i2c/i2c.h" #include "aqi_calculator_factory.h" -#include - namespace esphome { namespace hm3301 { +static const uint8_t SELECT_COMM_CMD = 0X88; + class HM3301Component : public PollingComponent, public i2c::I2CDevice { public: HM3301Component() = default; @@ -29,9 +27,12 @@ class HM3301Component : public PollingComponent, public i2c::I2CDevice { void update() override; protected: - std::unique_ptr hm3301_; - - HM330XErrorCode error_code_{NO_ERROR}; + enum { + NO_ERROR = 0, + ERROR_PARAM = -1, + ERROR_COMM = -2, + ERROR_OTHERS = -128, + } error_code_{NO_ERROR}; uint8_t data_buffer_[30]; @@ -43,12 +44,9 @@ class HM3301Component : public PollingComponent, public i2c::I2CDevice { AQICalculatorType aqi_calc_type_; AQICalculatorFactory aqi_calculator_factory_ = AQICalculatorFactory(); - bool read_sensor_value_(uint8_t *); bool validate_checksum_(const uint8_t *); uint16_t get_sensor_value_(const uint8_t *, uint8_t); }; } // namespace hm3301 } // namespace esphome - -#endif // USE_ARDUINO diff --git a/esphome/components/hm3301/sensor.py b/esphome/components/hm3301/sensor.py index 976a0488e1..8e9ee4c6fb 100644 --- a/esphome/components/hm3301/sensor.py +++ b/esphome/components/hm3301/sensor.py @@ -84,7 +84,6 @@ CONFIG_SCHEMA = cv.All( .extend(cv.polling_component_schema("60s")) .extend(i2c.i2c_device_schema(0x40)), _validate, - cv.only_with_arduino, ) @@ -109,6 +108,3 @@ async def to_code(config): sens = await sensor.new_sensor(config[CONF_AQI]) cg.add(var.set_aqi_sensor(sens)) cg.add(var.set_aqi_calculation_type(config[CONF_AQI][CONF_CALCULATION_TYPE])) - - # https://platformio.org/lib/show/6306/Grove%20-%20Laser%20PM2.5%20Sensor%20HM3301 - cg.add_library("seeed-studio/Grove - Laser PM2.5 Sensor HM3301", "1.0.3") diff --git a/platformio.ini b/platformio.ini index b49438b097..0f80d6d8d3 100644 --- a/platformio.ini +++ b/platformio.ini @@ -46,7 +46,6 @@ lib_deps = fastled/FastLED@3.3.2 ; fastled_base mikalhart/TinyGPSPlus@1.0.2 ; gps freekode/TM1651@1.0.1 ; tm1651 - seeed-studio/Grove - Laser PM2.5 Sensor HM3301@1.0.3 ; hm3301 glmnet/Dsmr@0.5 ; dsmr rweather/Crypto@0.2.0 ; dsmr dudanov/MideaUART@1.1.8 ; midea