ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
dfrobot_sen0465_base.h
Go to the documentation of this file.
1#pragma once
2
7
9
10static constexpr uint8_t FRAME_SIZE = 9;
11static constexpr uint8_t FRAME_HEAD = 0xFF;
12
13// Scheduler ids, shared with the bus variants
14static constexpr uint32_t STEP_TIMEOUT_ID = 0;
15static constexpr uint32_t RESPONSE_TIMEOUT_ID = 1;
16
17enum class AlarmType : uint8_t {
21};
22
24enum class SetupStep : uint8_t {
28};
29
33 SUB_SENSOR(concentration)
34 SUB_SENSOR(temperature)
35
36 public:
39 DFRobotSEN0465Component(uint8_t gas_type, const char *gas_name) : gas_name_(gas_name), gas_type_(gas_type) {}
40
41 void setup() override;
42 void update() override;
43 void dump_config() override;
44
47 void set_alarm(AlarmType type, uint16_t threshold, uint8_t decimals) {
48 this->alarm_type_ = type;
49 this->alarm_threshold_ = threshold;
50 this->alarm_decimals_ = decimals;
51 }
52
53#ifdef USE_DFROBOT_SEN0465_TEMPERATURE_COMPENSATION
55 void add_compensation_range(int8_t low, int8_t high, float a, float b, float c, float d) {
56 this->compensation_.push_back({low, high, a, b, c, d});
57 }
58#endif
59
60 protected:
62 virtual bool write_frame(const uint8_t *frame) = 0;
64 virtual void read_response() = 0;
65
66 void handle_response_(const uint8_t *frame);
69 void fail_reading_();
70
72 bool send_step_request_();
75 void step_failed_(const LogString *reason);
76 void next_step_();
77 void handle_data_(const uint8_t *frame);
79 bool send_command_(uint8_t command, uint8_t arg0 = 0, uint8_t arg1 = 0, uint8_t arg2 = 0, uint8_t arg3 = 0);
80
81 const char *gas_name_;
82 uint16_t alarm_threshold_{0};
83 uint8_t gas_type_;
85 uint8_t alarm_decimals_{0};
87 uint8_t attempts_{0};
89 uint8_t pending_command_{0};
91 bool setup_incomplete_{false};
92
93#ifdef USE_DFROBOT_SEN0465_TEMPERATURE_COMPENSATION
95 int8_t low;
96 int8_t high;
97 float a;
98 float b;
99 float c;
100 float d;
101 };
102
104 float compensate_(float concentration, float temperature) const;
105
107#endif
108};
109
110} // namespace esphome::dfrobot_sen0465_base
This class simplifies creating components that periodically check a state.
Definition component.h:519
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:303
DFRobot "Factory Calibrated Electrochemical Gas Sensor" series (SEN0465 to SEN0476).
StaticVector< CompensationRange, DFROBOT_SEN0465_COMPENSATION_RANGES > compensation_
bool send_step_request_()
Sends the request of the current step.
virtual void read_response()=0
Waits for the answer to a request, then calls either handle_response_() or handle_no_response_(),...
void retry_step_request_()
Sends the request again from a scheduled callback; a failed write counts as a failed attempt.
bool setup_incomplete_
A setup step gave up, so the alarm or the mode may not be what was configured.
virtual bool write_frame(const uint8_t *frame)=0
Sends one complete request frame.
uint8_t pending_command_
Command whose answer is awaited, 0 when no request is outstanding.
bool send_command_(uint8_t command, uint8_t arg0=0, uint8_t arg1=0, uint8_t arg2=0, uint8_t arg3=0)
Sends a request and waits for its answer; returns false when the request could not be sent.
float compensate_(float concentration, float temperature) const
Returns NAN when the temperature is outside every range.
void fail_reading_()
Publish unknown on both sensors and set the warning when a reading cannot be used.
uint8_t type
SetupStep
Settings sent once after boot, before the first reading.
static void uint32_t
uint16_t temperature
Definition sun_gtil2.cpp:12