ESPHome  2024.11.2
as7341.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace as7341 {
9 
10 static const uint8_t AS7341_CHIP_ID = 0X09;
11 
12 static const uint8_t AS7341_CONFIG = 0x70;
13 static const uint8_t AS7341_LED = 0x74;
14 
15 static const uint8_t AS7341_ENABLE = 0x80;
16 static const uint8_t AS7341_ATIME = 0x81;
17 
18 static const uint8_t AS7341_WTIME = 0x83;
19 
20 static const uint8_t AS7341_AUXID = 0x90;
21 static const uint8_t AS7341_REVID = 0x91;
22 static const uint8_t AS7341_ID = 0x92;
23 static const uint8_t AS7341_STATUS = 0x93;
24 
25 static const uint8_t AS7341_CH0_DATA_L = 0x95;
26 static const uint8_t AS7341_CH0_DATA_H = 0x96;
27 static const uint8_t AS7341_CH1_DATA_L = 0x97;
28 static const uint8_t AS7341_CH1_DATA_H = 0x98;
29 static const uint8_t AS7341_CH2_DATA_L = 0x99;
30 static const uint8_t AS7341_CH2_DATA_H = 0x9A;
31 static const uint8_t AS7341_CH3_DATA_L = 0x9B;
32 static const uint8_t AS7341_CH3_DATA_H = 0x9C;
33 static const uint8_t AS7341_CH4_DATA_L = 0x9D;
34 static const uint8_t AS7341_CH4_DATA_H = 0x9E;
35 static const uint8_t AS7341_CH5_DATA_L = 0x9F;
36 static const uint8_t AS7341_CH5_DATA_H = 0xA0;
37 
38 static const uint8_t AS7341_STATUS2 = 0xA3;
39 
40 static const uint8_t AS7341_CFG1 = 0xAA;
41 
42 static const uint8_t AS7341_CFG6 = 0xAF; // Stores SMUX command
43 static const uint8_t AS7341_CFG9 = 0xB2; // Config for system interrupts (SMUX, Flicker detection)
44 
45 static const uint8_t AS7341_ASTEP = 0xCA; // LSB
46 static const uint8_t AS7341_ASTEP_MSB = 0xCB; // MSB
47 
55 };
56 
61 };
62 
63 enum AS7341Gain {
75 };
76 
78  public:
79  void setup() override;
80  void dump_config() override;
81  float get_setup_priority() const override;
82  void update() override;
83 
84  void set_f1_sensor(sensor::Sensor *f1_sensor) { this->f1_ = f1_sensor; }
85  void set_f2_sensor(sensor::Sensor *f2_sensor) { f2_ = f2_sensor; }
86  void set_f3_sensor(sensor::Sensor *f3_sensor) { f3_ = f3_sensor; }
87  void set_f4_sensor(sensor::Sensor *f4_sensor) { f4_ = f4_sensor; }
88  void set_f5_sensor(sensor::Sensor *f5_sensor) { f5_ = f5_sensor; }
89  void set_f6_sensor(sensor::Sensor *f6_sensor) { f6_ = f6_sensor; }
90  void set_f7_sensor(sensor::Sensor *f7_sensor) { f7_ = f7_sensor; }
91  void set_f8_sensor(sensor::Sensor *f8_sensor) { f8_ = f8_sensor; }
92  void set_clear_sensor(sensor::Sensor *clear_sensor) { clear_ = clear_sensor; }
93  void set_nir_sensor(sensor::Sensor *nir_sensor) { nir_ = nir_sensor; }
94 
96  void set_atime(uint8_t atime) { atime_ = atime; }
97  void set_astep(uint16_t astep) { astep_ = astep; }
98 
100  uint8_t get_atime();
101  uint16_t get_astep();
102  bool setup_gain(AS7341Gain gain);
103  bool setup_atime(uint8_t atime);
104  bool setup_astep(uint16_t astep);
105 
106  uint16_t read_channel(AS7341AdcChannel channel);
107  bool read_channels(uint16_t *data);
108  void set_smux_low_channels(bool enable);
109  bool set_smux_command(AS7341SmuxCommand command);
112  bool enable_smux();
113 
114  bool wait_for_data();
115  bool is_data_ready();
116  bool enable_power(bool enable);
117  bool enable_spectral_measurement(bool enable);
118 
119  bool read_register_bit(uint8_t address, uint8_t bit_position);
120  bool write_register_bit(uint8_t address, bool value, uint8_t bit_position);
121  bool set_register_bit(uint8_t address, uint8_t bit_position);
122  bool clear_register_bit(uint8_t address, uint8_t bit_position);
123  uint16_t swap_bytes(uint16_t data);
124 
125  protected:
126  sensor::Sensor *f1_{nullptr};
127  sensor::Sensor *f2_{nullptr};
128  sensor::Sensor *f3_{nullptr};
129  sensor::Sensor *f4_{nullptr};
130  sensor::Sensor *f5_{nullptr};
131  sensor::Sensor *f6_{nullptr};
132  sensor::Sensor *f7_{nullptr};
133  sensor::Sensor *f8_{nullptr};
135  sensor::Sensor *nir_{nullptr};
136 
137  uint16_t astep_;
139  uint8_t atime_;
140  uint16_t channel_readings_[12];
141 };
142 
143 } // namespace as7341
144 } // namespace esphome
void set_f8_sensor(sensor::Sensor *f8_sensor)
Definition: as7341.h:91
void set_f7_sensor(sensor::Sensor *f7_sensor)
Definition: as7341.h:90
void set_gain(AS7341Gain gain)
Definition: as7341.h:95
bool setup_gain(AS7341Gain gain)
Definition: as7341.cpp:115
bool read_channels(uint16_t *data)
Definition: as7341.cpp:121
Read SMUX configuration to RAM from SMUX chain.
Definition: as7341.h:59
bool enable_power(bool enable)
Definition: as7341.cpp:233
Write SMUX configuration from RAM to SMUX chain.
Definition: as7341.h:60
bool read_register_bit(uint8_t address, uint8_t bit_position)
Definition: as7341.cpp:239
uint16_t channel_readings_[12]
Definition: as7341.h:140
void set_f4_sensor(sensor::Sensor *f4_sensor)
Definition: as7341.h:87
void set_f5_sensor(sensor::Sensor *f5_sensor)
Definition: as7341.h:88
void set_f1_sensor(sensor::Sensor *f1_sensor)
Definition: as7341.h:84
void set_astep(uint16_t astep)
Definition: as7341.h:97
This class simplifies creating components that periodically check a state.
Definition: component.h:283
AlsGain501 gain
void set_clear_sensor(sensor::Sensor *clear_sensor)
Definition: as7341.h:92
uint16_t read_channel(AS7341AdcChannel channel)
bool enable_spectral_measurement(bool enable)
Definition: as7341.cpp:235
bool setup_atime(uint8_t atime)
Definition: as7341.cpp:117
bool write_register_bit(uint8_t address, bool value, uint8_t bit_position)
Definition: as7341.cpp:246
void set_f2_sensor(sensor::Sensor *f2_sensor)
Definition: as7341.h:85
bool setup_astep(uint16_t astep)
Definition: as7341.cpp:119
void dump_config() override
Definition: as7341.cpp:37
void set_f6_sensor(sensor::Sensor *f6_sensor)
Definition: as7341.h:89
void set_nir_sensor(sensor::Sensor *nir_sensor)
Definition: as7341.h:93
bool set_register_bit(uint8_t address, uint8_t bit_position)
Definition: as7341.cpp:254
sensor::Sensor * clear_
Definition: as7341.h:134
bool clear_register_bit(uint8_t address, uint8_t bit_position)
Definition: as7341.cpp:261
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t address
Definition: bl0906.h:211
float get_setup_priority() const override
Definition: as7341.cpp:60
void set_smux_low_channels(bool enable)
Definition: as7341.cpp:135
Base-class for all sensors.
Definition: sensor.h:57
ROM code initialization of SMUX.
Definition: as7341.h:58
This Class provides the methods to read/write bytes from/to an i2c device.
Definition: i2c.h:133
void set_f3_sensor(sensor::Sensor *f3_sensor)
Definition: as7341.h:86
uint16_t swap_bytes(uint16_t data)
Definition: as7341.cpp:268
void set_atime(uint8_t atime)
Definition: as7341.h:96
bool set_smux_command(AS7341SmuxCommand command)
Definition: as7341.cpp:148