ESPHome 2026.3.0
Loading...
Searching...
No Matches
ltr501.h
Go to the documentation of this file.
1#pragma once
2
8
10
11namespace esphome {
12namespace ltr501 {
13
15
22
24 public:
25 //
26 // EspHome framework functions
27 //
28 void setup() override;
29 void dump_config() override;
30 void update() override;
31 void loop() override;
32
33 // Configuration setters : General
34 //
36
37 // Configuration setters : ALS
38 //
39 void set_als_auto_mode(bool enable) { this->automatic_mode_enabled_ = enable; }
43 void set_als_glass_attenuation_factor(float factor) { this->glass_attenuation_factor_ = factor; }
44
45 // Configuration setters : PS
46 //
47 void set_ps_high_threshold(uint16_t threshold) { this->ps_threshold_high_ = threshold; }
48 void set_ps_low_threshold(uint16_t threshold) { this->ps_threshold_low_ = threshold; }
49 void set_ps_cooldown_time_s(uint16_t time) { this->ps_cooldown_time_s_ = time; }
51
52 // Sensors setters
53 //
60
61 protected:
62 //
63 // Internal state machine, used to split all the actions into
64 // small steps in loop() to make sure we are not blocking execution
65 //
77 uint8_t tries_{0};
78
80
81 //
82 // Current measurements data
83 //
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_();
110 LtrDataAvail is_als_data_ready_(AlsReadings &data);
111 void read_sensor_data_(AlsReadings &data);
112 bool are_adjustments_required_(AlsReadings &data);
113 void apply_lux_calculation_(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_();
120
121 //
122 // Component configuration
123 //
129
132 uint16_t ps_threshold_high_{0xffff};
133 uint16_t ps_threshold_low_{0x0000};
136
137 //
138 // Sensors for publishing data
139 //
140 sensor::Sensor *infrared_counts_sensor_{nullptr}; // direct reading CH1, infrared only
141 sensor::Sensor *full_spectrum_counts_sensor_{nullptr}; // direct reading CH0, infrared + visible light
142 sensor::Sensor *ambient_light_sensor_{nullptr}; // calculated lux
143 sensor::Sensor *actual_gain_sensor_{nullptr}; // actual gain of reading
144 sensor::Sensor *actual_integration_time_sensor_{nullptr}; // actual integration time
145 sensor::Sensor *proximity_counts_sensor_{nullptr}; // proximity sensor
146
148 return this->ambient_light_sensor_ != nullptr || this->full_spectrum_counts_sensor_ != nullptr ||
149 this->infrared_counts_sensor_ != nullptr || this->actual_gain_sensor_ != nullptr ||
150 this->actual_integration_time_sensor_ != nullptr;
151 }
152 bool is_any_ps_sensor_enabled_() const { return this->proximity_counts_sensor_ != nullptr; }
153
154 //
155 // Trigger section for the automations
156 //
157 friend class LTRPsHighTrigger;
158 friend class LTRPsLowTrigger;
159
162
163 void add_on_ps_high_trigger_callback_(std::function<void()> callback) {
164 this->on_ps_high_trigger_callback_.add(std::move(callback));
165 }
166
167 void add_on_ps_low_trigger_callback_(std::function<void()> callback) {
168 this->on_ps_low_trigger_callback_.add(std::move(callback));
169 }
170};
171
172class LTRPsHighTrigger : public Trigger<> {
173 public:
175 parent->add_on_ps_high_trigger_callback_([this]() { this->trigger(); });
176 }
177};
178
179class LTRPsLowTrigger : public Trigger<> {
180 public:
182 parent->add_on_ps_low_trigger_callback_([this]() { this->trigger(); });
183 }
184};
185} // namespace ltr501
186} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:538
void trigger(const Ts &...x)
Definition automation.h:325
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:132
LtrDataAvail is_als_data_ready_(AlsReadings &data)
Definition ltr501.cpp:381
void set_ps_high_threshold(uint16_t threshold)
Definition ltr501.h:47
void configure_gain_(AlsGain501 gain)
Definition ltr501.cpp:349
void set_ps_cooldown_time_s(uint16_t time)
Definition ltr501.h:49
void publish_data_part_1_(AlsReadings &data)
Definition ltr501.cpp:522
void publish_data_part_2_(AlsReadings &data)
Definition ltr501.cpp:537
void set_infrared_counts_sensor(sensor::Sensor *sensor)
Definition ltr501.h:56
void add_on_ps_high_trigger_callback_(std::function< void()> callback)
Definition ltr501.h:163
void configure_integration_time_(IntegrationTime501 time)
Definition ltr501.cpp:365
sensor::Sensor * actual_integration_time_sensor_
Definition ltr501.h:144
CallbackManager< void()> on_ps_high_trigger_callback_
Definition ltr501.h:160
void set_full_spectrum_counts_sensor(sensor::Sensor *sensor)
Definition ltr501.h:55
void set_ps_low_threshold(uint16_t threshold)
Definition ltr501.h:48
IntegrationTime501 integration_time_
Definition ltr501.h:126
struct esphome::ltr501::LTRAlsPs501Component::AlsReadings als_readings_
void read_sensor_data_(AlsReadings &data)
Definition ltr501.cpp:395
MeasurementRepeatRate repeat_rate_
Definition ltr501.h:127
void set_als_integration_time(IntegrationTime501 time)
Definition ltr501.h:41
enum esphome::ltr501::LTRAlsPs501Component::State NOT_INITIALIZED
void set_actual_gain_sensor(sensor::Sensor *sensor)
Definition ltr501.h:57
void set_proximity_counts_sensor(sensor::Sensor *sensor)
Definition ltr501.h:59
void set_ltr_type(LtrType type)
Definition ltr501.h:35
CallbackManager< void()> on_ps_low_trigger_callback_
Definition ltr501.h:161
void apply_lux_calculation_(AlsReadings &data)
Definition ltr501.cpp:480
void set_als_auto_mode(bool enable)
Definition ltr501.h:39
void set_ps_gain(PsGain501 gain)
Definition ltr501.h:50
sensor::Sensor * proximity_counts_sensor_
Definition ltr501.h:145
void set_als_glass_attenuation_factor(float factor)
Definition ltr501.h:43
void set_ambient_light_sensor(sensor::Sensor *sensor)
Definition ltr501.h:54
sensor::Sensor * infrared_counts_sensor_
Definition ltr501.h:140
bool are_adjustments_required_(AlsReadings &data)
Definition ltr501.cpp:408
void set_als_gain(AlsGain501 gain)
Definition ltr501.h:40
void set_actual_integration_time_sensor(sensor::Sensor *sensor)
Definition ltr501.h:58
sensor::Sensor * full_spectrum_counts_sensor_
Definition ltr501.h:141
sensor::Sensor * actual_gain_sensor_
Definition ltr501.h:143
sensor::Sensor * ambient_light_sensor_
Definition ltr501.h:142
void add_on_ps_low_trigger_callback_(std::function< void()> callback)
Definition ltr501.h:167
void set_als_meas_repeat_rate(MeasurementRepeatRate rate)
Definition ltr501.h:42
LTRPsHighTrigger(LTRAlsPs501Component *parent)
Definition ltr501.h:174
LTRPsLowTrigger(LTRAlsPs501Component *parent)
Definition ltr501.h:181
Base-class for all sensors.
Definition sensor.h:47
uint16_t type
AlsGain501 gain
@ LTR_TYPE_ALS_AND_PS
Definition ltr501.h:20
@ LTR_TYPE_ALS_ONLY
Definition ltr501.h:18
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t