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