Adafruit AS7341 Library
Adafruit_AS7341.h
Go to the documentation of this file.
1 
33 /*
34  This library is adapted from an example with the following Copyright and
35  Warranty:
36 
37  This is a Hello world example code written for the AS7241 XWing Spectral
38  Sensor I2C interface with Arduino µC. The main idea is to get fimilar with the
39  register configuration. This code helps to learn basic settings and procedure
40  to read out raw values with different SMUX configuration. Also defined the
41  procedure to set the default flicker detection for 100 and 120 Hz.
42 
43  Written by Sijo John @ ams AG, Application Support in October, 2018
44 
45  Development environment specifics: Arduino IDE 1.8.5
46 
47  This program is distributed in the hope that it will be useful,
48  but WITHOUT ANY WARRANTY; without even the implied warranty of
49  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
50 
51 */
52 
53 #ifndef _ADAFRUIT_AS7341_H
54 #define _ADAFRUIT_AS7341_H
55 
56 #include "Arduino.h"
57 #include <Adafruit_BusIO_Register.h>
58 #include <Adafruit_I2CDevice.h>
59 #include <Wire.h>
60 #define AS7341_I2CADDR_DEFAULT 0x39
61 #define AS7341_CHIP_ID 0x09
62 
63 #define AS7341_WHOAMI 0x92
64 
65 #define AS7341_ASTATUS 0x60
66 #define AS7341_CH0_DATA_L_ 0x61
67 #define AS7341_CH0_DATA_H_ 0x62
68 #define AS7341_ITIME_L 0x63
69 #define AS7341_ITIME_M 0x64
70 #define AS7341_ITIME_H 0x65
71 #define AS7341_CONFIG 0x70
72 #define AS7341_STAT 0x71
73 #define AS7341_EDGE 0x72
74 #define AS7341_GPIO 0x73
75 #define AS7341_LED 0x74
76 #define AS7341_ENABLE \
77  0x80
78 #define AS7341_ATIME 0x81
80 #define AS7341_WTIME 0x83
81 #define AS7341_SP_LOW_TH_L 0x84
82 #define AS7341_SP_LOW_TH_H \
83  0x85
84 #define AS7341_SP_HIGH_TH_L \
85  0x86
86 #define AS7341_SP_HIGH_TH_H \
87  0x87
88 #define AS7341_AUXID 0x90
89 #define AS7341_REVID 0x91
90 #define AS7341_ID 0x92
91 #define AS7341_STATUS \
92  0x93
93 #define AS7341_ASTATUS_ 0x94
94 #define AS7341_CH0_DATA_L 0x95
95 #define AS7341_CH0_DATA_H 0x96
96 #define AS7341_CH1_DATA_L 0x97
97 #define AS7341_CH1_DATA_H 0x98
98 #define AS7341_CH2_DATA_L 0x99
99 #define AS7341_CH2_DATA_H 0x9A
100 #define AS7341_CH3_DATA_L 0x9B
101 #define AS7341_CH3_DATA_H 0x9C
102 #define AS7341_CH4_DATA_L 0x9D
103 #define AS7341_CH4_DATA_H 0x9E
104 #define AS7341_CH5_DATA_L 0x9F
105 #define AS7341_CH5_DATA_H 0xA0
106 #define AS7341_STATUS2 0xA3
107 #define AS7341_STATUS3 \
108  0xA4
109 #define AS7341_STATUS5 0xA6
110 #define AS7341_STATUS6 0xA7
111 #define AS7341_CFG0 \
112  0xA9
113 #define AS7341_CFG1 0xAA
114 #define AS7341_CFG3 0xAC
115 #define AS7341_CFG6 0xAF
116 #define AS7341_CFG8 0xB1
117 #define AS7341_CFG9 \
118  0xB2
119 #define AS7341_CFG10 0xB3
121 #define AS7341_CFG12 \
122  0xB5
123 #define AS7341_PERS \
124  0xBD
125 #define AS7341_GPIO2 \
127  0xBE
128 #define AS7341_ASTEP_L 0xCA
130 #define AS7341_ASTEP_H 0xCB
131 #define AS7341_AGC_GAIN_MAX 0xCF
132 #define AS7341_AZ_CONFIG 0xD6
133 #define AS7341_FD_TIME1 0xD8
134 #define AS7341_FD_TIME2 0xDA
135 #define AS7341_FD_CFG0 0xD7
136 #define AS7341_FD_STATUS \
137  0xDB
138 #define AS7341_INTENAB 0xF9
140 #define AS7341_CONTROL 0xFA
141 #define AS7341_FIFO_MAP 0xFC
142 #define AS7341_FIFO_LVL 0xFD
143 #define AS7341_FDATA_L 0xFE
144 #define AS7341_FDATA_H 0xFF
145 
146 #define AS7341_SPECTRAL_INT_HIGH_MSK \
147  0b00100000
148 #define AS7341_SPECTRAL_INT_LOW_MSK \
149  0b00010000
150 
151 
155 typedef enum {
156  AS7341_GAIN_0_5X,
157  AS7341_GAIN_1X,
158  AS7341_GAIN_2X,
159  AS7341_GAIN_4X,
160  AS7341_GAIN_8X,
161  AS7341_GAIN_16X,
162  AS7341_GAIN_32X,
163  AS7341_GAIN_64X,
164  AS7341_GAIN_128X,
165  AS7341_GAIN_256X,
166  AS7341_GAIN_512X,
167 } as7341_gain_t;
168 
173 typedef enum {
182 typedef enum {
183  AS7341_ADC_CHANNEL_0,
184  AS7341_ADC_CHANNEL_1,
185  AS7341_ADC_CHANNEL_2,
186  AS7341_ADC_CHANNEL_3,
187  AS7341_ADC_CHANNEL_4,
188  AS7341_ADC_CHANNEL_5,
194 typedef enum {
195  AS7341_CHANNEL_415nm_F1,
196  AS7341_CHANNEL_445nm_F2,
197  AS7341_CHANNEL_480nm_F3,
198  AS7341_CHANNEL_515nm_F4,
199  AS7341_CHANNEL_CLEAR_0,
200  AS7341_CHANNEL_NIR_0,
201  AS7341_CHANNEL_555nm_F5,
202  AS7341_CHANNEL_590nm_F6,
203  AS7341_CHANNEL_630nm_F7,
204  AS7341_CHANNEL_680nm_F8,
205  AS7341_CHANNEL_CLEAR,
206  AS7341_CHANNEL_NIR,
208 
214 typedef enum {
232 
237 typedef enum {
241 
245 typedef enum {
246  AS7341_WAITING_START, //
247  AS7341_WAITING_LOW, //
248  AS7341_WAITING_HIGH, //
249  AS7341_WAITING_DONE, //
251 
252 class Adafruit_AS7341;
253 
259 public:
260  Adafruit_AS7341();
262 
263  bool begin(uint8_t i2c_addr = AS7341_I2CADDR_DEFAULT, TwoWire *wire = &Wire,
264  int32_t sensor_id = 0);
265 
266  bool setASTEP(uint16_t astep_value);
267  bool setATIME(uint8_t atime_value);
268  bool setGain(as7341_gain_t gain_value);
269 
270  uint16_t getASTEP();
271  uint8_t getATIME();
273 
274  long getTINT();
275  float toBasicCounts(uint16_t raw);
276 
277  bool readAllChannels(void);
278  bool readAllChannels(uint16_t *readings_buffer);
279  void delayForData(int waitTime = 0);
280  uint16_t readChannel(as7341_adc_channel_t channel);
281  uint16_t getChannel(as7341_color_channel_t channel);
282 
283  bool startReading(void);
284  bool checkReadingProgress();
285  bool getAllChannels(uint16_t *readings_buffer);
286 
287  uint16_t detectFlickerHz(void);
288 
289  void setup_F1F4_Clear_NIR(void);
290  void setup_F5F8_Clear_NIR(void);
291 
292  void powerEnable(bool enable_power);
293  bool enableSpectralMeasurement(bool enable_measurement);
294 
295  bool setHighThreshold(uint16_t high_threshold);
296  bool setLowThreshold(uint16_t low_threshold);
297 
298  uint16_t getHighThreshold(void);
299  uint16_t getLowThreshold(void);
300 
301  bool enableSpectralInterrupt(bool enable_int);
302  bool enableSystemInterrupt(bool enable_int);
303 
304  bool setAPERS(as7341_int_cycle_count_t cycle_count);
306 
307  uint8_t getInterruptStatus(void);
308  bool clearInterruptStatus(void);
309 
310  bool spectralInterruptTriggered(void);
311  uint8_t spectralInterruptSource(void);
312  bool spectralLowTriggered(void);
313  bool spectralHighTriggered(void);
314 
315  bool enableLED(bool enable_led);
316  bool setLEDCurrent(uint16_t led_current_ma);
317  uint16_t getLEDCurrent(void);
318 
319  void disableAll(void);
320 
321  bool getIsDataReady();
322  bool setBank(bool low); // low true gives access to 0x60 to 0x74
323 
325  bool setGPIODirection(as7341_gpio_dir_t gpio_direction);
326  bool getGPIOInverted(void);
327  bool setGPIOInverted(bool gpio_inverted);
328  bool getGPIOValue(void);
329  bool setGPIOValue(bool);
330 
331 protected:
332  virtual bool _init(int32_t sensor_id);
334  0;
335 
337  Adafruit_I2CDevice *i2c_dev = NULL;
338 
339 private:
340  bool enableSMUX(void);
341  bool enableFlickerDetection(bool enable_fd);
342  void FDConfig(void);
343  int8_t getFlickerDetectStatus(void);
344  bool setSMUXCommand(as7341_smux_cmd_t command);
345  void writeRegister(byte addr, byte val);
346  void setSMUXLowChannels(bool f1_f4);
347  uint16_t _channel_readings[12];
348  as7341_waiting_t _readingState;
349 };
350 
351 #endif
uint8_t getInterruptStatus(void)
Returns the current value of the Interupt status register.
Definition: Adafruit_AS7341.cpp:678
bool checkReadingProgress()
runs the process of getting readings from all channels without using delays. Should be called regular...
Definition: Adafruit_AS7341.cpp:172
as7341_color_channel_t
Spectral Channel specifiers for configuration and reading.
Definition: Adafruit_AS7341.h:194
uint16_t getHighThreshold(void)
Returns the current high thighreshold for spectral measurements.
Definition: Adafruit_AS7341.cpp:595
as7341_waiting_t
Wait states for async reading.
Definition: Adafruit_AS7341.h:245
bool enableSystemInterrupt(bool enable_int)
Enabled system interrupts.
Definition: Adafruit_AS7341.cpp:621
12
Definition: Adafruit_AS7341.h:227
13
Definition: Adafruit_AS7341.h:228
Adafruit_AS7341()
Construct a new Adafruit_AS7341::Adafruit_AS7341 object.
Definition: Adafruit_AS7341.cpp:27
as7341_adc_channel_t
ADC Channel specifiers for configuration.
Definition: Adafruit_AS7341.h:182
bool setASTEP(uint16_t astep_value)
Sets the integration time step size.
Definition: Adafruit_AS7341.cpp:882
The GPIO Pin is set as a high-impedence input.
Definition: Adafruit_AS7341.h:239
bool setLEDCurrent(uint16_t led_current_ma)
Set the current limit for the LED.
Definition: Adafruit_AS7341.cpp:483
8
Definition: Adafruit_AS7341.h:223
uint8_t getATIME()
Returns the integration time step count.
Definition: Adafruit_AS7341.cpp:869
0
Definition: Adafruit_AS7341.h:215
4
Definition: Adafruit_AS7341.h:219
virtual bool _init(int32_t sensor_id)
Initializer for post i2c/spi init.
Definition: Adafruit_AS7341.cpp:69
Read SMUX configuration to RAM from SMUX chain.
Definition: Adafruit_AS7341.h:175
uint16_t detectFlickerHz(void)
Detect a flickering light.
Definition: Adafruit_AS7341.cpp:994
bool setLowThreshold(uint16_t low_threshold)
Sets the threshold below which spectral measurements will trigger interrupts when the APERS count is ...
Definition: Adafruit_AS7341.cpp:560
bool getGPIOValue(void)
Read the digital level of the GPIO pin, high or low.
Definition: Adafruit_AS7341.cpp:413
long getTINT()
Returns the integration time.
Definition: Adafruit_AS7341.cpp:932
bool readAllChannels(void)
Take readings for F1-8, Clear and NIR and store them in a buffer.
Definition: Adafruit_AS7341.cpp:263
bool setAPERS(as7341_int_cycle_count_t cycle_count)
Sets the number of times an interrupt threshold must be exceeded before an interrupt is triggered...
Definition: Adafruit_AS7341.cpp:645
9
Definition: Adafruit_AS7341.h:224
bool enableSpectralInterrupt(bool enable_int)
Enable Interrupts based on spectral measurements.
Definition: Adafruit_AS7341.cpp:607
14
Definition: Adafruit_AS7341.h:229
bool setBank(bool low)
Sets the active register bank.
Definition: Adafruit_AS7341.cpp:542
5
Definition: Adafruit_AS7341.h:220
as7341_gpio_dir_t getGPIODirection(void)
Get the GPIO pin direction setting.
Definition: Adafruit_AS7341.cpp:353
bool getGPIOInverted(void)
Get the output inversion setting for the GPIO pin.
Definition: Adafruit_AS7341.cpp:382
float toBasicCounts(uint16_t raw)
Converts raw ADC values to basic counts.
Definition: Adafruit_AS7341.cpp:948
uint8_t spectralInterruptSource(void)
The current state of the spectral measurement interrupt status register.
Definition: Adafruit_AS7341.cpp:716
7
Definition: Adafruit_AS7341.h:222
6
Definition: Adafruit_AS7341.h:221
bool setATIME(uint8_t atime_value)
Sets the integration time step count.
Definition: Adafruit_AS7341.cpp:856
Write SMUX configuration from RAM to SMUX chain.
Definition: Adafruit_AS7341.h:176
as7341_gain_t getGain()
Returns the ADC gain multiplier.
Definition: Adafruit_AS7341.cpp:919
bool getIsDataReady()
Definition: Adafruit_AS7341.cpp:748
1
Definition: Adafruit_AS7341.h:216
uint16_t getLowThreshold(void)
Returns the current low thighreshold for spectral measurements.
Definition: Adafruit_AS7341.cpp:571
bool setSpectralThresholdChannel(as7341_adc_channel_t channel)
Set the ADC channel to use for spectral thresholds including interrupts, automatic gain control...
Definition: Adafruit_AS7341.cpp:661
uint16_t getASTEP()
Returns the integration time step size.
Definition: Adafruit_AS7341.cpp:895
bool enableSpectralMeasurement(bool enable_measurement)
Enables measurement of spectral data.
Definition: Adafruit_AS7341.cpp:308
void disableAll(void)
Disable Spectral reading, flicker detection, and power.
Definition: Adafruit_AS7341.cpp:295
~Adafruit_AS7341()
Destroy the Adafruit_AS7341::Adafruit_AS7341 object.
Definition: Adafruit_AS7341.cpp:33
2
Definition: Adafruit_AS7341.h:217
uint16_t readChannel(as7341_adc_channel_t channel)
Returns the ADC data for a given channel.
Definition: Adafruit_AS7341.cpp:105
bool setGPIOInverted(bool gpio_inverted)
Invert the logic of then GPIO pin when used as an output.
Definition: Adafruit_AS7341.cpp:399
bool setGain(as7341_gain_t gain_value)
Sets the ADC gain multiplier.
Definition: Adafruit_AS7341.cpp:907
Class that stores state and functions for interacting with the AS7341 11-Channel Spectral Sensor...
Definition: Adafruit_AS7341.h:258
bool startReading(void)
starts the process of getting readings from all channels without using delays
Definition: Adafruit_AS7341.cpp:158
bool spectralHighTriggered(void)
The status of the high threshold interrupt.
Definition: Adafruit_AS7341.cpp:739
void powerEnable(bool enable_power)
Sets the power state of the sensor.
Definition: Adafruit_AS7341.cpp:283
void delayForData(int waitTime=0)
Delay while waiting for data, with option to time out and recover.
Definition: Adafruit_AS7341.cpp:235
bool setGPIODirection(as7341_gpio_dir_t gpio_direction)
Set the GPIO pin to be used as an input or output.
Definition: Adafruit_AS7341.cpp:368
as7341_int_cycle_count_t
The number of measurement cycles with spectral data outside of a threshold required to trigger an int...
Definition: Adafruit_AS7341.h:214
uint16_t getLEDCurrent(void)
Get the current limit for the LED.
Definition: Adafruit_AS7341.cpp:511
as7341_gpio_dir_t
Pin directions to set how the GPIO pin is to be used.
Definition: Adafruit_AS7341.h:237
bool setHighThreshold(uint16_t high_threshold)
Sets the threshold above which spectral measurements will trigger interrupts when the APERS count is ...
Definition: Adafruit_AS7341.cpp:584
bool spectralLowTriggered(void)
The status of the low threshold interrupt.
Definition: Adafruit_AS7341.cpp:730
THhe GPIO pin is configured as an open drain output.
Definition: Adafruit_AS7341.h:238
ROM code initialization of SMUX.
Definition: Adafruit_AS7341.h:174
3
Definition: Adafruit_AS7341.h:218
#define AS7341_I2CADDR_DEFAULT
AS7341 default i2c address.
Definition: Adafruit_AS7341.h:60
uint16_t getChannel(as7341_color_channel_t channel)
Returns the reading data for the specified color channel.
Definition: Adafruit_AS7341.cpp:121
Adafruit_I2CDevice * i2c_dev
Pointer to I2C bus interface.
Definition: Adafruit_AS7341.h:337
void setup_F1F4_Clear_NIR(void)
Configure SMUX for sensors F1-4, Clear and NIR.
Definition: Adafruit_AS7341.cpp:761
bool spectralInterruptTriggered(void)
Returns the status of the spectral measurement threshold interrupts.
Definition: Adafruit_AS7341.cpp:689
10
Definition: Adafruit_AS7341.h:225
bool getAllChannels(uint16_t *readings_buffer)
transfer all the values from the private result buffer into one nominated
Definition: Adafruit_AS7341.cpp:223
void setup_F5F8_Clear_NIR(void)
Configure SMUX for sensors F5-8, Clear and NIR.
Definition: Adafruit_AS7341.cpp:791
as7341_smux_cmd_t
Available SMUX configuration commands.
Definition: Adafruit_AS7341.h:173
11
Definition: Adafruit_AS7341.h:226
uint8_t last_spectral_int_source
Definition: Adafruit_AS7341.h:333
as7341_gain_t
Allowable gain multipliers for setGain
Definition: Adafruit_AS7341.h:155
bool enableLED(bool enable_led)
Enable control of an attached LED on the LDR pin.
Definition: Adafruit_AS7341.cpp:454
15
Definition: Adafruit_AS7341.h:230
bool clearInterruptStatus(void)
Clear the interrupt status register.
Definition: Adafruit_AS7341.cpp:703
bool begin(uint8_t i2c_addr=AS7341_I2CADDR_DEFAULT, TwoWire *wire=&Wire, int32_t sensor_id=0)
Sets up the hardware and initializes I2C.
Definition: Adafruit_AS7341.cpp:50
bool setGPIOValue(bool)
Set the digital level of the GPIO pin, high or low.
Definition: Adafruit_AS7341.cpp:430