mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Remove arduino dependency from hm3301 (#2745)
This commit is contained in:
		| @@ -1,6 +1,5 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO |  | ||||||
| #include <cstdint> | #include <cstdint> | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| @@ -13,5 +12,3 @@ class AbstractAQICalculator { | |||||||
|  |  | ||||||
| }  // namespace hm3301 | }  // namespace hm3301 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,7 +1,5 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "abstract_aqi_calculator.h" | #include "abstract_aqi_calculator.h" | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| @@ -48,5 +46,3 @@ class AQICalculator : public AbstractAQICalculator { | |||||||
|  |  | ||||||
| }  // namespace hm3301 | }  // namespace hm3301 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,7 +1,5 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "caqi_calculator.h" | #include "caqi_calculator.h" | ||||||
| #include "aqi_calculator.h" | #include "aqi_calculator.h" | ||||||
|  |  | ||||||
| @@ -29,5 +27,3 @@ class AQICalculatorFactory { | |||||||
|  |  | ||||||
| }  // namespace hm3301 | }  // namespace hm3301 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,7 +1,5 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
| #include "abstract_aqi_calculator.h" | #include "abstract_aqi_calculator.h" | ||||||
|  |  | ||||||
| @@ -52,5 +50,3 @@ class CAQICalculator : public AbstractAQICalculator { | |||||||
|  |  | ||||||
| }  // namespace hm3301 | }  // namespace hm3301 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,5 +1,3 @@ | |||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "esphome/core/log.h" | #include "esphome/core/log.h" | ||||||
| #include "hm3301.h" | #include "hm3301.h" | ||||||
|  |  | ||||||
| @@ -14,9 +12,8 @@ static const uint8_t PM_10_0_VALUE_INDEX = 7; | |||||||
|  |  | ||||||
| void HM3301Component::setup() { | void HM3301Component::setup() { | ||||||
|   ESP_LOGCONFIG(TAG, "Setting up HM3301..."); |   ESP_LOGCONFIG(TAG, "Setting up HM3301..."); | ||||||
|   hm3301_ = make_unique<HM330X>(); |   if (i2c::ERROR_OK != this->write(&SELECT_COMM_CMD, 1)) { | ||||||
|   error_code_ = hm3301_->init(); |     error_code_ = ERROR_COMM; | ||||||
|   if (error_code_ != NO_ERROR) { |  | ||||||
|     this->mark_failed(); |     this->mark_failed(); | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
| @@ -38,7 +35,7 @@ void HM3301Component::dump_config() { | |||||||
| float HM3301Component::get_setup_priority() const { return setup_priority::DATA; } | float HM3301Component::get_setup_priority() const { return setup_priority::DATA; } | ||||||
|  |  | ||||||
| void HM3301Component::update() { | 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"); |     ESP_LOGW(TAG, "Read result failed"); | ||||||
|     this->status_set_warning(); |     this->status_set_warning(); | ||||||
|     return; |     return; | ||||||
| @@ -87,8 +84,6 @@ void HM3301Component::update() { | |||||||
|   this->status_clear_warning(); |   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) { | bool HM3301Component::validate_checksum_(const uint8_t *data) { | ||||||
|   uint8_t sum = 0; |   uint8_t sum = 0; | ||||||
|   for (int i = 0; i < 28; i++) { |   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 hm3301 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -1,17 +1,15 @@ | |||||||
| #pragma once | #pragma once | ||||||
|  |  | ||||||
| #ifdef USE_ARDUINO |  | ||||||
|  |  | ||||||
| #include "esphome/core/component.h" | #include "esphome/core/component.h" | ||||||
| #include "esphome/components/sensor/sensor.h" | #include "esphome/components/sensor/sensor.h" | ||||||
| #include "esphome/components/i2c/i2c.h" | #include "esphome/components/i2c/i2c.h" | ||||||
| #include "aqi_calculator_factory.h" | #include "aqi_calculator_factory.h" | ||||||
|  |  | ||||||
| #include <Seeed_HM330X.h> |  | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace hm3301 { | namespace hm3301 { | ||||||
|  |  | ||||||
|  | static const uint8_t SELECT_COMM_CMD = 0X88; | ||||||
|  |  | ||||||
| class HM3301Component : public PollingComponent, public i2c::I2CDevice { | class HM3301Component : public PollingComponent, public i2c::I2CDevice { | ||||||
|  public: |  public: | ||||||
|   HM3301Component() = default; |   HM3301Component() = default; | ||||||
| @@ -29,9 +27,12 @@ class HM3301Component : public PollingComponent, public i2c::I2CDevice { | |||||||
|   void update() override; |   void update() override; | ||||||
|  |  | ||||||
|  protected: |  protected: | ||||||
|   std::unique_ptr<HM330X> hm3301_; |   enum { | ||||||
|  |     NO_ERROR = 0, | ||||||
|   HM330XErrorCode error_code_{NO_ERROR}; |     ERROR_PARAM = -1, | ||||||
|  |     ERROR_COMM = -2, | ||||||
|  |     ERROR_OTHERS = -128, | ||||||
|  |   } error_code_{NO_ERROR}; | ||||||
|  |  | ||||||
|   uint8_t data_buffer_[30]; |   uint8_t data_buffer_[30]; | ||||||
|  |  | ||||||
| @@ -43,12 +44,9 @@ class HM3301Component : public PollingComponent, public i2c::I2CDevice { | |||||||
|   AQICalculatorType aqi_calc_type_; |   AQICalculatorType aqi_calc_type_; | ||||||
|   AQICalculatorFactory aqi_calculator_factory_ = AQICalculatorFactory(); |   AQICalculatorFactory aqi_calculator_factory_ = AQICalculatorFactory(); | ||||||
|  |  | ||||||
|   bool read_sensor_value_(uint8_t *); |  | ||||||
|   bool validate_checksum_(const uint8_t *); |   bool validate_checksum_(const uint8_t *); | ||||||
|   uint16_t get_sensor_value_(const uint8_t *, uint8_t); |   uint16_t get_sensor_value_(const uint8_t *, uint8_t); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| }  // namespace hm3301 | }  // namespace hm3301 | ||||||
| }  // namespace esphome | }  // namespace esphome | ||||||
|  |  | ||||||
| #endif  // USE_ARDUINO |  | ||||||
|   | |||||||
| @@ -84,7 +84,6 @@ CONFIG_SCHEMA = cv.All( | |||||||
|     .extend(cv.polling_component_schema("60s")) |     .extend(cv.polling_component_schema("60s")) | ||||||
|     .extend(i2c.i2c_device_schema(0x40)), |     .extend(i2c.i2c_device_schema(0x40)), | ||||||
|     _validate, |     _validate, | ||||||
|     cv.only_with_arduino, |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -109,6 +108,3 @@ async def to_code(config): | |||||||
|         sens = await sensor.new_sensor(config[CONF_AQI]) |         sens = await sensor.new_sensor(config[CONF_AQI]) | ||||||
|         cg.add(var.set_aqi_sensor(sens)) |         cg.add(var.set_aqi_sensor(sens)) | ||||||
|         cg.add(var.set_aqi_calculation_type(config[CONF_AQI][CONF_CALCULATION_TYPE])) |         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") |  | ||||||
|   | |||||||
| @@ -46,7 +46,6 @@ lib_deps = | |||||||
|     fastled/FastLED@3.3.2                                 ; fastled_base |     fastled/FastLED@3.3.2                                 ; fastled_base | ||||||
|     mikalhart/TinyGPSPlus@1.0.2                           ; gps |     mikalhart/TinyGPSPlus@1.0.2                           ; gps | ||||||
|     freekode/TM1651@1.0.1                                 ; tm1651 |     freekode/TM1651@1.0.1                                 ; tm1651 | ||||||
|     seeed-studio/Grove - Laser PM2.5 Sensor HM3301@1.0.3  ; hm3301 |  | ||||||
|     glmnet/Dsmr@0.5                                       ; dsmr |     glmnet/Dsmr@0.5                                       ; dsmr | ||||||
|     rweather/Crypto@0.2.0                                 ; dsmr |     rweather/Crypto@0.2.0                                 ; dsmr | ||||||
|     dudanov/MideaUART@1.1.8                               ; midea |     dudanov/MideaUART@1.1.8                               ; midea | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user