ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
as7341.cpp
Go to the documentation of this file.
1#include "as7341.h"
2#include "esphome/core/log.h"
3#include "esphome/core/hal.h"
4
5namespace esphome::as7341 {
6
7static const char *const TAG = "as7341";
8
10 LOG_I2C_DEVICE(this);
11
12 // Verify device ID
13 uint8_t id;
14 this->read_byte(AS7341_ID, &id);
15 ESP_LOGCONFIG(TAG, " Read ID: 0x%X", id);
16 if ((id & 0xFC) != (AS7341_CHIP_ID << 2)) {
17 this->mark_failed();
18 return;
19 }
20
21 // Power on (enter IDLE state)
22 if (!this->enable_power(true)) {
23 ESP_LOGE(TAG, " Power on failed!");
24 this->mark_failed();
25 return;
26 }
27
28 // Set configuration
29 this->write_byte(AS7341_CONFIG, 0x00);
30 this->setup_atime(this->atime_);
31 this->setup_astep(this->astep_);
32 this->setup_gain(this->gain_);
33}
34
36 ESP_LOGCONFIG(TAG, "AS7341:");
37 LOG_I2C_DEVICE(this);
38 if (this->is_failed()) {
39 ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
40 }
41 LOG_UPDATE_INTERVAL(this);
42 ESP_LOGCONFIG(TAG,
43 " Gain: %u\n"
44 " ATIME: %u\n"
45 " ASTEP: %u",
47
48 LOG_SENSOR(" ", "F1", this->f1_);
49 LOG_SENSOR(" ", "F2", this->f2_);
50 LOG_SENSOR(" ", "F3", this->f3_);
51 LOG_SENSOR(" ", "F4", this->f4_);
52 LOG_SENSOR(" ", "F5", this->f5_);
53 LOG_SENSOR(" ", "F6", this->f6_);
54 LOG_SENSOR(" ", "F7", this->f7_);
55 LOG_SENSOR(" ", "F8", this->f8_);
56 LOG_SENSOR(" ", "Clear", this->clear_);
57 LOG_SENSOR(" ", "NIR", this->nir_);
58}
59
62
63 if (this->f1_ != nullptr) {
64 this->f1_->publish_state(this->channel_readings_[0]);
65 }
66 if (this->f2_ != nullptr) {
67 this->f2_->publish_state(this->channel_readings_[1]);
68 }
69 if (this->f3_ != nullptr) {
70 this->f3_->publish_state(this->channel_readings_[2]);
71 }
72 if (this->f4_ != nullptr) {
73 this->f4_->publish_state(this->channel_readings_[3]);
74 }
75 if (this->f5_ != nullptr) {
76 this->f5_->publish_state(this->channel_readings_[6]);
77 }
78 if (this->f6_ != nullptr) {
79 this->f6_->publish_state(this->channel_readings_[7]);
80 }
81 if (this->f7_ != nullptr) {
82 this->f7_->publish_state(this->channel_readings_[8]);
83 }
84 if (this->f8_ != nullptr) {
85 this->f8_->publish_state(this->channel_readings_[9]);
86 }
87 if (this->clear_ != nullptr) {
88 this->clear_->publish_state(this->channel_readings_[10]);
89 }
90 if (this->nir_ != nullptr) {
91 this->nir_->publish_state(this->channel_readings_[11]);
92 }
93}
94
96 uint8_t data;
97 this->read_byte(AS7341_CFG1, &data);
98 return (AS7341Gain) data;
99}
100
102 uint8_t data;
103 this->read_byte(AS7341_ATIME, &data);
104 return data;
105}
106
108 uint16_t data;
109 this->read_byte_16(AS7341_ASTEP, &data);
110 return this->swap_bytes(data);
111}
112
113bool AS7341Component::setup_gain(AS7341Gain gain) { return this->write_byte(AS7341_CFG1, gain); }
114
115bool AS7341Component::setup_atime(uint8_t atime) { return this->write_byte(AS7341_ATIME, atime); }
116
117bool AS7341Component::setup_astep(uint16_t astep) { return this->write_byte_16(AS7341_ASTEP, swap_bytes(astep)); }
118
119bool AS7341Component::read_channels(uint16_t *data) {
120 this->set_smux_low_channels(true);
121 this->enable_spectral_measurement(true);
122 this->wait_for_data();
123 bool low_success = this->read_bytes_16(AS7341_CH0_DATA_L, data, 6);
124
125 this->set_smux_low_channels(false);
126 this->enable_spectral_measurement(true);
127 this->wait_for_data();
128 bool high_sucess = this->read_bytes_16(AS7341_CH0_DATA_L, &data[6], 6);
129
130 return low_success && high_sucess;
131}
132
134 this->enable_spectral_measurement(false);
136
137 if (enable) {
139
140 } else {
142 }
143 this->enable_smux();
144}
145
147 uint8_t data = command << 3; // Write to bits 4:3 of the register
148 return this->write_byte(AS7341_CFG6, data);
149}
150
152 // SMUX Config for F1,F2,F3,F4,NIR,Clear
153 this->write_byte(0x00, 0x30); // F3 left set to ADC2
154 this->write_byte(0x01, 0x01); // F1 left set to ADC0
155 this->write_byte(0x02, 0x00); // Reserved or disabled
156 this->write_byte(0x03, 0x00); // F8 left disabled
157 this->write_byte(0x04, 0x00); // F6 left disabled
158 this->write_byte(0x05, 0x42); // F4 left connected to ADC3/f2 left connected to ADC1
159 this->write_byte(0x06, 0x00); // F5 left disbled
160 this->write_byte(0x07, 0x00); // F7 left disbled
161 this->write_byte(0x08, 0x50); // CLEAR connected to ADC4
162 this->write_byte(0x09, 0x00); // F5 right disabled
163 this->write_byte(0x0A, 0x00); // F7 right disabled
164 this->write_byte(0x0B, 0x00); // Reserved or disabled
165 this->write_byte(0x0C, 0x20); // F2 right connected to ADC1
166 this->write_byte(0x0D, 0x04); // F4 right connected to ADC3
167 this->write_byte(0x0E, 0x00); // F6/F8 right disabled
168 this->write_byte(0x0F, 0x30); // F3 right connected to AD2
169 this->write_byte(0x10, 0x01); // F1 right connected to AD0
170 this->write_byte(0x11, 0x50); // CLEAR right connected to AD4
171 this->write_byte(0x12, 0x00); // Reserved or disabled
172 this->write_byte(0x13, 0x06); // NIR connected to ADC5
173}
174
176 // SMUX Config for F5,F6,F7,F8,NIR,Clear
177 this->write_byte(0x00, 0x00); // F3 left disable
178 this->write_byte(0x01, 0x00); // F1 left disable
179 this->write_byte(0x02, 0x00); // reserved/disable
180 this->write_byte(0x03, 0x40); // F8 left connected to ADC3
181 this->write_byte(0x04, 0x02); // F6 left connected to ADC1
182 this->write_byte(0x05, 0x00); // F4/ F2 disabled
183 this->write_byte(0x06, 0x10); // F5 left connected to ADC0
184 this->write_byte(0x07, 0x03); // F7 left connected to ADC2
185 this->write_byte(0x08, 0x50); // CLEAR Connected to ADC4
186 this->write_byte(0x09, 0x10); // F5 right connected to ADC0
187 this->write_byte(0x0A, 0x03); // F7 right connected to ADC2
188 this->write_byte(0x0B, 0x00); // Reserved or disabled
189 this->write_byte(0x0C, 0x00); // F2 right disabled
190 this->write_byte(0x0D, 0x00); // F4 right disabled
191 this->write_byte(0x0E, 0x24); // F8 right connected to ADC2/ F6 right connected to ADC1
192 this->write_byte(0x0F, 0x00); // F3 right disabled
193 this->write_byte(0x10, 0x00); // F1 right disabled
194 this->write_byte(0x11, 0x50); // CLEAR right connected to AD4
195 this->write_byte(0x12, 0x00); // Reserved or disabled
196 this->write_byte(0x13, 0x06); // NIR connected to ADC5
197}
198
200 this->set_register_bit(AS7341_ENABLE, 4);
201
202 uint16_t timeout = 1000;
203 for (uint16_t time = 0; time < timeout; time++) {
204 // The SMUXEN bit is cleared once the SMUX operation is finished
205 bool smuxen = this->read_register_bit(AS7341_ENABLE, 4);
206 if (!smuxen) {
207 return true;
208 }
209
210 delay(1);
211 }
212
213 return false;
214}
215
217 uint16_t timeout = 1000;
218 for (uint16_t time = 0; time < timeout; time++) {
219 if (this->is_data_ready()) {
220 return true;
221 }
222
223 delay(1);
224 }
225
226 return false;
227}
228
229bool AS7341Component::is_data_ready() { return this->read_register_bit(AS7341_STATUS2, 6); }
230
231bool AS7341Component::enable_power(bool enable) { return this->write_register_bit(AS7341_ENABLE, enable, 0); }
232
234 return this->write_register_bit(AS7341_ENABLE, enable, 1);
235}
236
237bool AS7341Component::read_register_bit(uint8_t address, uint8_t bit_position) {
238 uint8_t data;
239 this->read_byte(address, &data);
240 bool bit = (data & (1 << bit_position)) > 0;
241 return bit;
242}
243
244bool AS7341Component::write_register_bit(uint8_t address, bool value, uint8_t bit_position) {
245 if (value) {
246 return this->set_register_bit(address, bit_position);
247 }
248
249 return this->clear_register_bit(address, bit_position);
250}
251
252bool AS7341Component::set_register_bit(uint8_t address, uint8_t bit_position) {
253 uint8_t data;
254 this->read_byte(address, &data);
255 data |= (1 << bit_position);
256 return this->write_byte(address, data);
257}
258
259bool AS7341Component::clear_register_bit(uint8_t address, uint8_t bit_position) {
260 uint8_t data;
261 this->read_byte(address, &data);
262 data &= ~(1 << bit_position);
263 return this->write_byte(address, data);
264}
265
266uint16_t AS7341Component::swap_bytes(uint16_t data) { return (data >> 8) | (data << 8); }
267
268} // namespace esphome::as7341
uint8_t address
Definition bl0906.h:4
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:284
bool setup_atime(uint8_t atime)
Definition as7341.cpp:115
bool setup_gain(AS7341Gain gain)
Definition as7341.cpp:113
bool read_register_bit(uint8_t address, uint8_t bit_position)
Definition as7341.cpp:237
bool clear_register_bit(uint8_t address, uint8_t bit_position)
Definition as7341.cpp:259
bool enable_power(bool enable)
Definition as7341.cpp:231
bool read_channels(uint16_t *data)
Definition as7341.cpp:119
bool set_smux_command(AS7341SmuxCommand command)
Definition as7341.cpp:146
bool setup_astep(uint16_t astep)
Definition as7341.cpp:117
void set_smux_low_channels(bool enable)
Definition as7341.cpp:133
uint16_t swap_bytes(uint16_t data)
Definition as7341.cpp:266
bool enable_spectral_measurement(bool enable)
Definition as7341.cpp:233
bool set_register_bit(uint8_t address, uint8_t bit_position)
Definition as7341.cpp:252
bool write_register_bit(uint8_t address, bool value, uint8_t bit_position)
Definition as7341.cpp:244
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:265
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:240
bool read_byte_16(uint8_t a_register, uint16_t *data)
Definition i2c.h:249
bool read_bytes_16(uint8_t a_register, uint16_t *data, uint8_t len)
Definition i2c.cpp:53
bool write_byte_16(uint8_t a_register, uint16_t data) const
Definition i2c.h:267
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t id
AlsGain501 gain
@ AS7341_SMUX_CMD_WRITE
Write SMUX configuration from RAM to SMUX chain.
Definition as7341.h:59
void HOT delay(uint32_t ms)
Definition hal.cpp:82