esphome/esphome/components/t6615/t6615.h
Jas Strong 63a186bdf9
t6615: tolerate sensor dropping commands (#2255)
The Amphenol T6615 has a built-in calibration system which means that
the sensor could go away for a couple of seconds to figure itself out.
While this is happening, commands are silently dropped.

This caused the previous version of this code to lock up completely,
since there was no way for the command_ state machine to tick back to
the NONE state.

Instead of just breaking the state machine, which might be harmful on
a multi-core or multi-threaded device, add a timestamp and only break
the lock if it's been more than a second since the command was issued.

The command usually doesn't take more than a few milliseconds to
complete, so this should not affect things unduly.

While we're at it, rewrite the rx side to be more robust against
bytes going missing.

Instead of reading in the data essentially inline, read into a buffer
and process it when enough has been read to make progress.

If data stops coming when we expect it to, or the data is malformed,
have a timeout that sends a new command.

Co-authored-by: jas <jas@asspa.in>
2021-09-13 09:54:48 +02:00

44 lines
913 B
C++

#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/uart/uart.h"
namespace esphome {
namespace t6615 {
enum class T6615Command : uint8_t {
NONE = 0,
GET_PPM,
GET_SERIAL,
GET_VERSION,
GET_ELEVATION,
GET_ABC,
ENABLE_ABC,
DISABLE_ABC,
SET_ELEVATION,
};
class T6615Component : public PollingComponent, public uart::UARTDevice {
public:
float get_setup_priority() const override;
void loop() override;
void update() override;
void dump_config() override;
void set_co2_sensor(sensor::Sensor *co2_sensor) { this->co2_sensor_ = co2_sensor; }
protected:
void query_ppm_();
void send_ppm_command_();
T6615Command command_ = T6615Command::NONE;
unsigned long command_time_ = 0;
sensor::Sensor *co2_sensor_{nullptr};
};
} // namespace t6615
} // namespace esphome