ESPHome  2024.10.2
veml7700.h
Go to the documentation of this file.
1 #pragma once
2 
7 
8 namespace esphome {
9 namespace veml7700 {
10 
12 
13 //
14 // Datasheet: https://www.vishay.com/docs/84286/veml7700.pdf
15 //
16 
17 enum class CommandRegisters : uint8_t {
18  ALS_CONF_0 = 0x00, // W: ALS gain, integration time, interrupt, and shutdown
19  ALS_WH = 0x01, // W: ALS high threshold window setting
20  ALS_WL = 0x02, // W: ALS low threshold window setting
21  PWR_SAVING = 0x03, // W: Set (15 : 3) 0000 0000 0000 0b
22  ALS = 0x04, // R: MSB, LSB data of whole ALS 16 bits
23  WHITE = 0x05, // R: MSB, LSB data of whole WHITE 16 bits
24  ALS_INT = 0x06 // R: ALS INT trigger event
25 };
26 
27 enum Gain : uint16_t {
28  X_1 = 0,
29  X_2 = 1,
30  X_1_8 = 2,
31  X_1_4 = 3,
32 };
33 const uint8_t GAINS_COUNT = 4;
34 
35 enum IntegrationTime : uint16_t {
42 };
43 const uint8_t INTEGRATION_TIMES_COUNT = 6;
44 
45 enum Persistence : uint16_t {
50 };
51 
52 enum PSMMode : uint16_t {
57 };
58 
59 // The following section with bit-fields brings GCC compilation 'notes' about padding bytes due to bug in older GCC back
60 // in 2009 "Packed bit-fields of type char were not properly bit-packed on many targets prior to GCC 4.4" Even more to
61 // this - this message can't be disabled with "#pragma GCC diagnostic ignored" due to another bug which was only fixed
62 // in GCC 13 in 2022 :) No actions required, it is just a note. The code is correct.
63 
64 //
65 // VEML7700_CR_ALS_CONF_0 Register (0x00)
66 //
68  uint16_t raw;
69  uint8_t raw_bytes[2];
70  struct {
71  bool ALS_SD : 1; // ALS shut down setting: 0 = ALS power on, 1 = ALS shut
72  // down
73  bool ALS_INT_EN : 1; // ALS interrupt enable setting: 0 = ALS INT disable, 1
74  // = ALS INT enable
75  bool reserved_2 : 1; // 0
76  bool reserved_3 : 1; // 0
77  Persistence ALS_PERS : 2; // 00 - 1, 01- 2, 10 - 4, 11 - 8
78  IntegrationTime ALS_IT : 4; // ALS integration time setting
79  bool reserved_10 : 1; // 0
80  Gain ALS_GAIN : 2; // Gain selection
81  bool reserved_13 : 1; // 0
82  bool reserved_14 : 1; // 0
83  bool reserved_15 : 1; // 0
84  } __attribute__((packed));
85 };
86 
87 //
88 // Power Saving Mode: PSM Register (0x03)
89 //
90 union PSMRegister {
91  uint16_t raw;
92  uint8_t raw_bytes[2];
93  struct {
94  bool PSM_EN : 1;
95  PSMMode PSM : 2;
96  uint16_t reserved : 13;
97  } __attribute__((packed));
98 };
99 
101  public:
102  //
103  // EspHome framework functions
104  //
105  float get_setup_priority() const override { return setup_priority::DATA; }
106  void setup() override;
107  void dump_config() override;
108  void update() override;
109  void loop() override;
110 
111  //
112  // Configuration setters
113  //
114  void set_gain(Gain gain) { this->gain_ = gain; }
115  void set_integration_time(IntegrationTime time) { this->integration_time_ = time; }
116  void set_enable_automatic_mode(bool enable) { this->automatic_mode_enabled_ = enable; }
117  void set_enable_lux_compensation(bool enable) { this->lux_compensation_enabled_ = enable; }
118  void set_glass_attenuation_factor(float factor) { this->glass_attenuation_factor_ = factor; }
119 
120  void set_ambient_light_sensor(sensor::Sensor *sensor) { this->ambient_light_sensor_ = sensor; }
121  void set_ambient_light_counts_sensor(sensor::Sensor *sensor) { this->ambient_light_counts_sensor_ = sensor; }
122  void set_white_sensor(sensor::Sensor *sensor) { this->white_sensor_ = sensor; }
123  void set_white_counts_sensor(sensor::Sensor *sensor) { this->white_counts_sensor_ = sensor; }
124  void set_infrared_sensor(sensor::Sensor *sensor) { this->fake_infrared_sensor_ = sensor; }
125  void set_actual_gain_sensor(sensor::Sensor *sensor) { this->actual_gain_sensor_ = sensor; }
126  void set_actual_integration_time_sensor(sensor::Sensor *sensor) { this->actual_integration_time_sensor_ = sensor; }
127 
128  protected:
129  //
130  // Internal state machine, used to split all the actions into
131  // small steps in loop() to make sure we are not blocking execution
132  //
133  enum class State : uint8_t {
134  NOT_INITIALIZED,
135  INITIAL_SETUP_COMPLETED,
136  IDLE,
137  COLLECTING_DATA,
138  COLLECTING_DATA_AUTO,
139  DATA_COLLECTED,
140  ADJUSTMENT_NEEDED,
141  ADJUSTMENT_IN_PROGRESS,
142  READY_TO_APPLY_ADJUSTMENTS,
143  READY_TO_PUBLISH_PART_1,
144  READY_TO_PUBLISH_PART_2,
145  READY_TO_PUBLISH_PART_3
146  } state_{State::NOT_INITIALIZED};
147 
148  //
149  // Current measurements data
150  //
151  struct Readings {
152  uint16_t als_counts{0};
153  uint16_t white_counts{0};
155  Gain actual_gain{X_1_8};
156  float als_lux{0};
157  float white_lux{0};
158  float fake_infrared_lux{0};
160  } readings_;
161 
162  //
163  // Device interaction
164  //
165  ErrorCode configure_();
166  ErrorCode reconfigure_time_and_gain_(IntegrationTime time, Gain gain, bool shutdown);
167  ErrorCode read_sensor_output_(Readings &data);
168 
169  //
170  // Working with the data
171  //
172  bool are_adjustments_required_(Readings &data);
173  void apply_lux_calculation_(Readings &data);
174  void apply_lux_compensation_(Readings &data);
175  void apply_glass_attenuation_(Readings &data);
176  void publish_data_part_1_(Readings &data);
177  void publish_data_part_2_(Readings &data);
178  void publish_data_part_3_(Readings &data);
179 
180  //
181  // Component configuration
182  //
183  bool automatic_mode_enabled_{true};
184  bool lux_compensation_enabled_{true};
185  float glass_attenuation_factor_{1.0};
187  Gain gain_{X_1};
188 
189  //
190  // Sensors for publishing data
191  //
192  sensor::Sensor *ambient_light_sensor_{nullptr}; // Human eye range 500-600 nm, lx
193  sensor::Sensor *ambient_light_counts_sensor_{nullptr}; // Raw counts
194  sensor::Sensor *white_sensor_{nullptr}; // Wide range 450-950 nm, lx
195  sensor::Sensor *white_counts_sensor_{nullptr}; // Raw counts
196  sensor::Sensor *fake_infrared_sensor_{nullptr}; // Artificial. = WHITE lx - ALS lx.
197  sensor::Sensor *actual_gain_sensor_{nullptr}; // Actual gain multiplier for the measurement
198  sensor::Sensor *actual_integration_time_sensor_{nullptr}; // Actual integration time for the measurement
199 };
200 
201 } // namespace veml7700
202 } // namespace esphome
void setup()
void loop()
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void set_glass_attenuation_factor(float factor)
Definition: veml7700.h:118
float get_setup_priority() const override
Definition: veml7700.h:105
const uint8_t INTEGRATION_TIMES_COUNT
Definition: veml7700.h:43
bool reserved_2
Definition: veml7700.h:74
This class simplifies creating components that periodically check a state.
Definition: component.h:283
AlsGain501 gain
PSMMode PSM
Definition: veml7700.h:94
IntegrationTime ALS_IT
Definition: veml7700.h:77
void set_white_counts_sensor(sensor::Sensor *sensor)
Definition: veml7700.h:123
bool reserved_3
Definition: veml7700.h:75
void set_integration_time(IntegrationTime time)
Definition: veml7700.h:115
void set_enable_automatic_mode(bool enable)
Definition: veml7700.h:116
void set_ambient_light_sensor(sensor::Sensor *sensor)
Definition: veml7700.h:120
No error found during execution of method.
Definition: i2c_bus.h:13
void set_infrared_sensor(sensor::Sensor *sensor)
Definition: veml7700.h:124
bool reserved_13
Definition: veml7700.h:80
void set_enable_lux_compensation(bool enable)
Definition: veml7700.h:117
void set_ambient_light_counts_sensor(sensor::Sensor *sensor)
Definition: veml7700.h:121
const uint8_t GAINS_COUNT
Definition: veml7700.h:33
enum esphome::EntityCategory __attribute__
void set_white_sensor(sensor::Sensor *sensor)
Definition: veml7700.h:122
void set_actual_gain_sensor(sensor::Sensor *sensor)
Definition: veml7700.h:125
void set_actual_integration_time_sensor(sensor::Sensor *sensor)
Definition: veml7700.h:126
bool PSM_EN
Definition: veml7700.h:93
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
bool reserved_10
Definition: veml7700.h:78
ErrorCode
Error codes returned by I2CBus and I2CDevice methods.
Definition: i2c_bus.h:11
Base-class for all sensors.
Definition: sensor.h:57
bool reserved_14
Definition: veml7700.h:81
Gain ALS_GAIN
Definition: veml7700.h:79
Persistence ALS_PERS
Definition: veml7700.h:76
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