ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
veml7700.h
Go to the documentation of this file.
1#pragma once
11//
12// Datasheet: https://www.vishay.com/docs/84286/veml7700.pdf
13//
14
15enum class CommandRegisters : uint8_t {
16 ALS_CONF_0 = 0x00, // W: ALS gain, integration time, interrupt, and shutdown
17 ALS_WH = 0x01, // W: ALS high threshold window setting
18 ALS_WL = 0x02, // W: ALS low threshold window setting
19 PWR_SAVING = 0x03, // W: Set (15 : 3) 0000 0000 0000 0b
20 ALS = 0x04, // R: MSB, LSB data of whole ALS 16 bits
21 WHITE = 0x05, // R: MSB, LSB data of whole WHITE 16 bits
22 ALS_INT = 0x06 // R: ALS INT trigger event
23};
24
25enum Gain : uint16_t {
26 X_1 = 0,
27 X_2 = 1,
28 X_1_8 = 2,
29 X_1_4 = 3,
30};
31const uint8_t GAINS_COUNT = 4;
32
41const uint8_t INTEGRATION_TIMES_COUNT = 6;
42
49
50enum PSMMode : uint16_t {
55};
56
57// The following section with bit-fields brings GCC compilation 'notes' about padding bytes due to bug in older GCC back
58// in 2009 "Packed bit-fields of type char were not properly bit-packed on many targets prior to GCC 4.4" Even more to
59// this - this message can't be disabled with "#pragma GCC diagnostic ignored" due to another bug which was only fixed
60// in GCC 13 in 2022 :) No actions required, it is just a note. The code is correct.
61
62//
63// VEML7700_CR_ALS_CONF_0 Register (0x00)
64//
66 uint16_t raw;
67 uint8_t raw_bytes[2];
68 struct {
69 bool ALS_SD : 1; // ALS shut down setting: 0 = ALS power on, 1 = ALS shut
70 // down
71 bool ALS_INT_EN : 1; // ALS interrupt enable setting: 0 = ALS INT disable, 1
72 // = ALS INT enable
73 bool reserved_2 : 1; // 0
74 bool reserved_3 : 1; // 0
75 Persistence ALS_PERS : 2; // 00 - 1, 01- 2, 10 - 4, 11 - 8
76 IntegrationTime ALS_IT : 4; // ALS integration time setting
77 bool reserved_10 : 1; // 0
78 Gain ALS_GAIN : 2; // Gain selection
79 bool reserved_13 : 1; // 0
80 bool reserved_14 : 1; // 0
81 bool reserved_15 : 1; // 0
82 } __attribute__((packed));
83};
84
85//
86// Power Saving Mode: PSM Register (0x03)
87//
89 uint16_t raw;
90 uint8_t raw_bytes[2];
91 struct {
92 bool PSM_EN : 1;
94 uint16_t reserved : 13;
95 } __attribute__((packed));
96};
97
99 public:
100 //
101 // EspHome framework functions
102 //
103 void setup() override;
104 void dump_config() override;
105 void update() override;
106 void loop() override;
107
108 //
109 // Configuration setters
110 //
111 void set_gain(Gain gain) { this->gain_ = gain; }
113 void set_enable_automatic_mode(bool enable) { this->automatic_mode_enabled_ = enable; }
114 void set_enable_lux_compensation(bool enable) { this->lux_compensation_enabled_ = enable; }
115 void set_glass_attenuation_factor(float factor) { this->glass_attenuation_factor_ = factor; }
116
117 SUB_SENSOR(ambient_light) // Human eye range 500-600 nm, lx
118 SUB_SENSOR(ambient_light_counts) // Raw counts
119 SUB_SENSOR(white) // Wide range 450-950 nm, lx
120 SUB_SENSOR(white_counts) // Raw counts
121 SUB_SENSOR(infrared) // Artificial. = WHITE lx - ALS lx.
122 SUB_SENSOR(actual_gain) // Actual gain multiplier for the measurement
123 SUB_SENSOR(actual_integration_time) // Actual integration time for the measurement
124
125 protected:
126 //
127 // Internal state machine, used to split all the actions into
128 // small steps in loop() to make sure we are not blocking execution
129 //
130 enum class State : uint8_t {
131 NOT_INITIALIZED,
132 INITIAL_SETUP_COMPLETED,
133 IDLE,
134 COLLECTING_DATA,
135 COLLECTING_DATA_AUTO,
136 DATA_COLLECTED,
137 ADJUSTMENT_NEEDED,
138 ADJUSTMENT_IN_PROGRESS,
139 READY_TO_APPLY_ADJUSTMENTS,
140 READY_TO_PUBLISH_PART_1,
141 READY_TO_PUBLISH_PART_2,
142 READY_TO_PUBLISH_PART_3
143 } state_{State::NOT_INITIALIZED};
144
145 //
146 // Current measurements data
147 //
158
159 //
160 // Device interaction
161 //
164 ErrorCode read_sensor_output_(Readings &data);
165
166 //
167 // Working with the data
168 //
169 bool are_adjustments_required_(Readings &data);
170 void apply_lux_calculation_(Readings &data);
171 void apply_lux_compensation_(Readings &data);
172 void apply_glass_attenuation_(Readings &data);
173 void publish_data_part_1_(Readings &data);
174 void publish_data_part_2_(Readings &data);
175 void publish_data_part_3_(Readings &data);
176
177 //
178 // Component configuration
179 //
185};
186
187} // namespace esphome::veml7700
This class simplifies creating components that periodically check a state.
Definition component.h:532
This Class provides the methods to read/write bytes from/to an i2c device.
Definition i2c.h:144
void publish_data_part_1_(Readings &data)
Definition veml7700.cpp:407
void set_enable_automatic_mode(bool enable)
Definition veml7700.h:113
void set_enable_lux_compensation(bool enable)
Definition veml7700.h:114
void publish_data_part_2_(Readings &data)
Definition veml7700.cpp:412
void publish_data_part_3_(Readings &data)
Definition veml7700.cpp:418
void apply_glass_attenuation_(Readings &data)
Definition veml7700.cpp:399
bool are_adjustments_required_(Readings &data)
Definition veml7700.cpp:305
SUB_SENSOR(ambient_light) SUB_SENSOR(ambient_light_counts) SUB_SENSOR(white) SUB_SENSOR(white_counts) SUB_SENSOR(infrared) SUB_SENSOR(actual_gain) SUB_SENSOR(actual_integration_time) protected struct esphome::veml7700::VEML7700Component::Readings readings_
ErrorCode reconfigure_time_and_gain_(IntegrationTime time, Gain gain, bool shutdown)
Definition veml7700.cpp:260
void set_integration_time(IntegrationTime time)
Definition veml7700.h:112
void apply_lux_calculation_(Readings &data)
Definition veml7700.cpp:352
void set_glass_attenuation_factor(float factor)
Definition veml7700.h:115
void apply_lux_compensation_(Readings &data)
Definition veml7700.cpp:370
ErrorCode read_sensor_output_(Readings &data)
Definition veml7700.cpp:281
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
AlsGain501 gain
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition i2c_bus.h:13
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
const uint8_t INTEGRATION_TIMES_COUNT
Definition veml7700.h:41
const uint8_t GAINS_COUNT
Definition veml7700.h:31