ESPHome 2025.10.0-dev
Loading...
Searching...
No Matches
veml7700.h
Go to the documentation of this file.
1#pragma once
7namespace esphome {
8namespace veml7700 {
12//
13// Datasheet: https://www.vishay.com/docs/84286/veml7700.pdf
14//
15
16enum class CommandRegisters : uint8_t {
17 ALS_CONF_0 = 0x00, // W: ALS gain, integration time, interrupt, and shutdown
18 ALS_WH = 0x01, // W: ALS high threshold window setting
19 ALS_WL = 0x02, // W: ALS low threshold window setting
20 PWR_SAVING = 0x03, // W: Set (15 : 3) 0000 0000 0000 0b
21 ALS = 0x04, // R: MSB, LSB data of whole ALS 16 bits
22 WHITE = 0x05, // R: MSB, LSB data of whole WHITE 16 bits
23 ALS_INT = 0x06 // R: ALS INT trigger event
24};
25
26enum Gain : uint16_t {
27 X_1 = 0,
28 X_2 = 1,
29 X_1_8 = 2,
30 X_1_4 = 3,
31};
32const uint8_t GAINS_COUNT = 4;
33
42const uint8_t INTEGRATION_TIMES_COUNT = 6;
43
50
51enum PSMMode : uint16_t {
56};
57
58// The following section with bit-fields brings GCC compilation 'notes' about padding bytes due to bug in older GCC back
59// in 2009 "Packed bit-fields of type char were not properly bit-packed on many targets prior to GCC 4.4" Even more to
60// this - this message can't be disabled with "#pragma GCC diagnostic ignored" due to another bug which was only fixed
61// in GCC 13 in 2022 :) No actions required, it is just a note. The code is correct.
62
63//
64// VEML7700_CR_ALS_CONF_0 Register (0x00)
65//
67 uint16_t raw;
68 uint8_t raw_bytes[2];
69 struct {
70 bool ALS_SD : 1; // ALS shut down setting: 0 = ALS power on, 1 = ALS shut
71 // down
72 bool ALS_INT_EN : 1; // ALS interrupt enable setting: 0 = ALS INT disable, 1
73 // = ALS INT enable
74 bool reserved_2 : 1; // 0
75 bool reserved_3 : 1; // 0
76 Persistence ALS_PERS : 2; // 00 - 1, 01- 2, 10 - 4, 11 - 8
77 IntegrationTime ALS_IT : 4; // ALS integration time setting
78 bool reserved_10 : 1; // 0
79 Gain ALS_GAIN : 2; // Gain selection
80 bool reserved_13 : 1; // 0
81 bool reserved_14 : 1; // 0
82 bool reserved_15 : 1; // 0
83 } __attribute__((packed));
84};
85
86//
87// Power Saving Mode: PSM Register (0x03)
88//
90 uint16_t raw;
91 uint8_t raw_bytes[2];
92 struct {
93 bool PSM_EN : 1;
95 uint16_t reserved : 13;
96 } __attribute__((packed));
97};
98
100 public:
101 //
102 // EspHome framework functions
103 //
104 void setup() override;
105 void dump_config() override;
106 void update() override;
107 void loop() override;
108
109 //
110 // Configuration setters
111 //
112 void set_gain(Gain gain) { this->gain_ = gain; }
114 void set_enable_automatic_mode(bool enable) { this->automatic_mode_enabled_ = enable; }
115 void set_enable_lux_compensation(bool enable) { this->lux_compensation_enabled_ = enable; }
116 void set_glass_attenuation_factor(float factor) { this->glass_attenuation_factor_ = factor; }
117
120 void set_white_sensor(sensor::Sensor *sensor) { this->white_sensor_ = sensor; }
125
126 protected:
127 //
128 // Internal state machine, used to split all the actions into
129 // small steps in loop() to make sure we are not blocking execution
130 //
145
146 //
147 // Current measurements data
148 //
159
160 //
161 // Device interaction
162 //
166
167 //
168 // Working with the data
169 //
170 bool are_adjustments_required_(Readings &data);
171 void apply_lux_calculation_(Readings &data);
172 void apply_lux_compensation_(Readings &data);
173 void apply_glass_attenuation_(Readings &data);
174 void publish_data_part_1_(Readings &data);
175 void publish_data_part_2_(Readings &data);
176 void publish_data_part_3_(Readings &data);
177
178 //
179 // Component configuration
180 //
186
187 //
188 // Sensors for publishing data
189 //
190 sensor::Sensor *ambient_light_sensor_{nullptr}; // Human eye range 500-600 nm, lx
192 sensor::Sensor *white_sensor_{nullptr}; // Wide range 450-950 nm, lx
193 sensor::Sensor *white_counts_sensor_{nullptr}; // Raw counts
194 sensor::Sensor *fake_infrared_sensor_{nullptr}; // Artificial. = WHITE lx - ALS lx.
195 sensor::Sensor *actual_gain_sensor_{nullptr}; // Actual gain multiplier for the measurement
196 sensor::Sensor *actual_integration_time_sensor_{nullptr}; // Actual integration time for the measurement
197};
198
199} // namespace veml7700
200} // namespace esphome
This class simplifies creating components that periodically check a state.
Definition component.h:429
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:133
Base-class for all sensors.
Definition sensor.h:42
void publish_data_part_1_(Readings &data)
Definition veml7700.cpp:407
void set_enable_automatic_mode(bool enable)
Definition veml7700.h:114
void set_enable_lux_compensation(bool enable)
Definition veml7700.h:115
void publish_data_part_2_(Readings &data)
Definition veml7700.cpp:416
void publish_data_part_3_(Readings &data)
Definition veml7700.cpp:428
sensor::Sensor * white_counts_sensor_
Definition veml7700.h:193
enum esphome::veml7700::VEML7700Component::State NOT_INITIALIZED
void apply_glass_attenuation_(Readings &data)
Definition veml7700.cpp:399
sensor::Sensor * ambient_light_counts_sensor_
Definition veml7700.h:191
void set_ambient_light_counts_sensor(sensor::Sensor *sensor)
Definition veml7700.h:119
void set_actual_integration_time_sensor(sensor::Sensor *sensor)
Definition veml7700.h:124
bool are_adjustments_required_(Readings &data)
Definition veml7700.cpp:305
void set_actual_gain_sensor(sensor::Sensor *sensor)
Definition veml7700.h:123
ErrorCode reconfigure_time_and_gain_(IntegrationTime time, Gain gain, bool shutdown)
Definition veml7700.cpp:260
sensor::Sensor * actual_integration_time_sensor_
Definition veml7700.h:196
void set_white_counts_sensor(sensor::Sensor *sensor)
Definition veml7700.h:121
void set_white_sensor(sensor::Sensor *sensor)
Definition veml7700.h:120
void set_integration_time(IntegrationTime time)
Definition veml7700.h:113
void apply_lux_calculation_(Readings &data)
Definition veml7700.cpp:352
void set_glass_attenuation_factor(float factor)
Definition veml7700.h:116
struct esphome::veml7700::VEML7700Component::Readings readings_
sensor::Sensor * fake_infrared_sensor_
Definition veml7700.h:194
void set_ambient_light_sensor(sensor::Sensor *sensor)
Definition veml7700.h:118
void apply_lux_compensation_(Readings &data)
Definition veml7700.cpp:370
sensor::Sensor * ambient_light_sensor_
Definition veml7700.h:190
ErrorCode read_sensor_output_(Readings &data)
Definition veml7700.cpp:281
void set_infrared_sensor(sensor::Sensor *sensor)
Definition veml7700.h:122
struct @63::@64 __attribute__
AlsGain501 gain
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:31
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:33
const uint8_t INTEGRATION_TIMES_COUNT
Definition veml7700.h:42
const uint8_t GAINS_COUNT
Definition veml7700.h:32
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7