ESPHome  2024.10.3
ltr501.h
Go to the documentation of this file.
1 #pragma once
2 
8 
9 #include "ltr_definitions_501.h"
10 
11 namespace esphome {
12 namespace ltr501 {
13 
14 enum DataAvail : uint8_t { NO_DATA, BAD_DATA, DATA_OK };
15 
16 enum LtrType : uint8_t {
21 };
22 
24  public:
25  //
26  // EspHome framework functions
27  //
28  float get_setup_priority() const override { return setup_priority::DATA; }
29  void setup() override;
30  void dump_config() override;
31  void update() override;
32  void loop() override;
33 
34  // Configuration setters : General
35  //
36  void set_ltr_type(LtrType type) { this->ltr_type_ = type; }
37 
38  // Configuration setters : ALS
39  //
40  void set_als_auto_mode(bool enable) { this->automatic_mode_enabled_ = enable; }
41  void set_als_gain(AlsGain501 gain) { this->gain_ = gain; }
44  void set_als_glass_attenuation_factor(float factor) { this->glass_attenuation_factor_ = factor; }
45 
46  // Configuration setters : PS
47  //
48  void set_ps_high_threshold(uint16_t threshold) { this->ps_threshold_high_ = threshold; }
49  void set_ps_low_threshold(uint16_t threshold) { this->ps_threshold_low_ = threshold; }
50  void set_ps_cooldown_time_s(uint16_t time) { this->ps_cooldown_time_s_ = time; }
51  void set_ps_gain(PsGain501 gain) { this->ps_gain_ = gain; }
52 
53  // Sensors setters
54  //
61 
62  protected:
63  //
64  // Internal state machine, used to split all the actions into
65  // small steps in loop() to make sure we are not blocking execution
66  //
67  enum class State : uint8_t {
70  IDLE,
77  } state_{State::NOT_INITIALIZED};
78 
80 
81  //
82  // Current measurements data
83  //
84  struct AlsReadings {
85  uint16_t ch0{0};
86  uint16_t ch1{0};
89  float lux{0.0f};
90  uint8_t number_of_adjustments{0};
91  } als_readings_;
92  uint16_t ps_readings_{0xfffe};
93 
94  inline bool is_als_() const {
96  }
97  inline bool is_ps_() const {
99  }
100 
101  //
102  // Device interaction and data manipulation
103  //
104  bool check_part_number_();
105 
106  void configure_reset_();
107  void configure_als_();
111  void read_sensor_data_(AlsReadings &data);
114  void publish_data_part_1_(AlsReadings &data);
115  void publish_data_part_2_(AlsReadings &data);
116 
117  void configure_ps_();
118  uint16_t read_ps_data_();
119  void check_and_trigger_ps_();
120 
121  //
122  // Component configuration
123  //
129 
130  uint16_t ps_cooldown_time_s_{5};
132  uint16_t ps_threshold_high_{0xffff};
133  uint16_t ps_threshold_low_{0x0000};
134 
135  //
136  // Sensors for publishing data
137  //
138  sensor::Sensor *infrared_counts_sensor_{nullptr}; // direct reading CH1, infrared only
139  sensor::Sensor *full_spectrum_counts_sensor_{nullptr}; // direct reading CH0, infrared + visible light
140  sensor::Sensor *ambient_light_sensor_{nullptr}; // calculated lux
141  sensor::Sensor *actual_gain_sensor_{nullptr}; // actual gain of reading
142  sensor::Sensor *actual_integration_time_sensor_{nullptr}; // actual integration time
143  sensor::Sensor *proximity_counts_sensor_{nullptr}; // proximity sensor
144 
146  return this->ambient_light_sensor_ != nullptr || this->full_spectrum_counts_sensor_ != nullptr ||
147  this->infrared_counts_sensor_ != nullptr || this->actual_gain_sensor_ != nullptr ||
148  this->actual_integration_time_sensor_ != nullptr;
149  }
150  bool is_any_ps_sensor_enabled_() const { return this->proximity_counts_sensor_ != nullptr; }
151 
152  //
153  // Trigger section for the automations
154  //
155  friend class LTRPsHighTrigger;
156  friend class LTRPsLowTrigger;
157 
160 
161  void add_on_ps_high_trigger_callback_(std::function<void()> callback) {
162  this->on_ps_high_trigger_callback_.add(std::move(callback));
163  }
164 
165  void add_on_ps_low_trigger_callback_(std::function<void()> callback) {
166  this->on_ps_low_trigger_callback_.add(std::move(callback));
167  }
168 };
169 
170 class LTRPsHighTrigger : public Trigger<> {
171  public:
173  parent->add_on_ps_high_trigger_callback_([this]() { this->trigger(); });
174  }
175 };
176 
177 class LTRPsLowTrigger : public Trigger<> {
178  public:
180  parent->add_on_ps_low_trigger_callback_([this]() { this->trigger(); });
181  }
182 };
183 } // namespace ltr501
184 } // namespace esphome
void apply_lux_calculation_(AlsReadings &data)
Definition: ltr501.cpp:476
void set_actual_integration_time_sensor(sensor::Sensor *sensor)
Definition: ltr501.h:59
void set_ltr_type(LtrType type)
Definition: ltr501.h:36
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
bool are_adjustments_required_(AlsReadings &data)
Definition: ltr501.cpp:404
void publish_data_part_2_(AlsReadings &data)
Definition: ltr501.cpp:533
void set_als_auto_mode(bool enable)
Definition: ltr501.h:40
sensor::Sensor * infrared_counts_sensor_
Definition: ltr501.h:138
float get_setup_priority() const override
Definition: ltr501.h:28
void set_als_meas_repeat_rate(MeasurementRepeatRate rate)
Definition: ltr501.h:43
void set_als_gain(AlsGain501 gain)
Definition: ltr501.h:41
void add_on_ps_low_trigger_callback_(std::function< void()> callback)
Definition: ltr501.h:165
void set_ambient_light_sensor(sensor::Sensor *sensor)
Definition: ltr501.h:55
sensor::Sensor * ambient_light_sensor_
Definition: ltr501.h:140
void set_als_integration_time(IntegrationTime501 time)
Definition: ltr501.h:42
struct esphome::ltr501::LTRAlsPs501Component::AlsReadings als_readings_
void read_sensor_data_(AlsReadings &data)
Definition: ltr501.cpp:391
This class simplifies creating components that periodically check a state.
Definition: component.h:283
IntegrationTime501 integration_time_
Definition: ltr501.h:126
CallbackManager< void()> on_ps_low_trigger_callback_
Definition: ltr501.h:159
AlsGain501 gain
sensor::Sensor * actual_integration_time_sensor_
Definition: ltr501.h:142
void set_ps_low_threshold(uint16_t threshold)
Definition: ltr501.h:49
void set_actual_gain_sensor(sensor::Sensor *sensor)
Definition: ltr501.h:58
void set_ps_high_threshold(uint16_t threshold)
Definition: ltr501.h:48
void set_ps_gain(PsGain501 gain)
Definition: ltr501.h:51
DataAvail is_als_data_ready_(AlsReadings &data)
Definition: ltr501.cpp:377
void set_infrared_counts_sensor(sensor::Sensor *sensor)
Definition: ltr501.h:57
LTRPsLowTrigger(LTRAlsPs501Component *parent)
Definition: ltr501.h:179
uint8_t type
sensor::Sensor * proximity_counts_sensor_
Definition: ltr501.h:143
void set_proximity_counts_sensor(sensor::Sensor *sensor)
Definition: ltr501.h:60
void set_ps_cooldown_time_s(uint16_t time)
Definition: ltr501.h:50
IntegrationTime501 integration_time
void configure_integration_time_(IntegrationTime501 time)
Definition: ltr501.cpp:361
void publish_data_part_1_(AlsReadings &data)
Definition: ltr501.cpp:518
MeasurementRepeatRate repeat_rate_
Definition: ltr501.h:127
sensor::Sensor * full_spectrum_counts_sensor_
Definition: ltr501.h:139
void set_als_glass_attenuation_factor(float factor)
Definition: ltr501.h:44
LTRPsHighTrigger(LTRAlsPs501Component *parent)
Definition: ltr501.h:172
CallbackManager< void()> on_ps_high_trigger_callback_
Definition: ltr501.h:158
void add_on_ps_high_trigger_callback_(std::function< void()> callback)
Definition: ltr501.h:161
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
sensor::Sensor * actual_gain_sensor_
Definition: ltr501.h:141
Base-class for all sensors.
Definition: sensor.h:57
void set_full_spectrum_counts_sensor(sensor::Sensor *sensor)
Definition: ltr501.h:56
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
esphome::sensor::Sensor * sensor
Definition: statsd.h:37
void configure_gain_(AlsGain501 gain)
Definition: ltr501.cpp:345