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