mirror of
https://github.com/esphome/esphome.git
synced 2024-12-25 15:04:54 +01:00
Remove arduino dependency from hm3301 (#2745)
This commit is contained in:
parent
6f9439e1bc
commit
8267f01ccd
8 changed files with 11 additions and 40 deletions
|
@ -1,6 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#include <cstdint>
|
||||
|
||||
namespace esphome {
|
||||
|
@ -13,5 +12,3 @@ class AbstractAQICalculator {
|
|||
|
||||
} // namespace hm3301
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ARDUINO
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<HM330X>();
|
||||
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
|
||||
|
|
|
@ -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 <Seeed_HM330X.h>
|
||||
|
||||
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<HM330X> 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
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue