Adafruit AS7341 Library
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Adafruit_AS7341 Class Reference

Class that stores state and functions for interacting with the AS7341 11-Channel Spectral Sensor. More...

#include <Adafruit_AS7341.h>

Public Member Functions

 Adafruit_AS7341 ()
 Construct a new Adafruit_AS7341::Adafruit_AS7341 object.
 
 ~Adafruit_AS7341 ()
 Destroy the Adafruit_AS7341::Adafruit_AS7341 object.
 
bool begin (uint8_t i2c_addr=AS7341_I2CADDR_DEFAULT, TwoWire *wire=&Wire, int32_t sensor_id=0)
 Sets up the hardware and initializes I2C. More...
 
bool setASTEP (uint16_t astep_value)
 Sets the integration time step size. More...
 
bool setATIME (uint8_t atime_value)
 Sets the integration time step count. More...
 
bool setGain (as7341_gain_t gain_value)
 Sets the ADC gain multiplier. More...
 
uint16_t getASTEP ()
 Returns the integration time step size. More...
 
uint8_t getATIME ()
 Returns the integration time step count. More...
 
as7341_gain_t getGain ()
 Returns the ADC gain multiplier. More...
 
long getTINT ()
 Returns the integration time. More...
 
float toBasicCounts (uint16_t raw)
 Converts raw ADC values to basic counts. More...
 
bool readAllChannels (void)
 Take readings for F1-8, Clear and NIR and store them in a buffer. More...
 
bool readAllChannels (uint16_t *readings_buffer)
 fills the provided buffer with the current measurements for Spectral channels F1-8, Clear and NIR More...
 
void delayForData (int waitTime=0)
 Delay while waiting for data, with option to time out and recover. More...
 
uint16_t readChannel (as7341_adc_channel_t channel)
 Returns the ADC data for a given channel. More...
 
uint16_t getChannel (as7341_color_channel_t channel)
 Returns the reading data for the specified color channel. More...
 
bool startReading (void)
 starts the process of getting readings from all channels without using delays More...
 
bool checkReadingProgress ()
 runs the process of getting readings from all channels without using delays. Should be called regularly (ie. in loop()) Need to call startReading() to initialise the process Need to call getAllChannels() to transfer the data into an external buffer More...
 
bool getAllChannels (uint16_t *readings_buffer)
 transfer all the values from the private result buffer into one nominated More...
 
uint16_t detectFlickerHz (void)
 Detect a flickering light. More...
 
void setup_F1F4_Clear_NIR (void)
 Configure SMUX for sensors F1-4, Clear and NIR.
 
void setup_F5F8_Clear_NIR (void)
 Configure SMUX for sensors F5-8, Clear and NIR.
 
void powerEnable (bool enable_power)
 Sets the power state of the sensor. More...
 
bool enableSpectralMeasurement (bool enable_measurement)
 Enables measurement of spectral data. More...
 
bool setHighThreshold (uint16_t high_threshold)
 Sets the threshold above which spectral measurements will trigger interrupts when the APERS count is reached. More...
 
bool setLowThreshold (uint16_t low_threshold)
 Sets the threshold below which spectral measurements will trigger interrupts when the APERS count is reached. More...
 
uint16_t getHighThreshold (void)
 Returns the current high thighreshold for spectral measurements. More...
 
uint16_t getLowThreshold (void)
 Returns the current low thighreshold for spectral measurements. More...
 
bool enableSpectralInterrupt (bool enable_int)
 Enable Interrupts based on spectral measurements. More...
 
bool enableSystemInterrupt (bool enable_int)
 Enabled system interrupts. More...
 
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. More...
 
bool setSpectralThresholdChannel (as7341_adc_channel_t channel)
 Set the ADC channel to use for spectral thresholds including interrupts, automatic gain control, and persistance settings. More...
 
uint8_t getInterruptStatus (void)
 Returns the current value of the Interupt status register. More...
 
bool clearInterruptStatus (void)
 Clear the interrupt status register. More...
 
bool spectralInterruptTriggered (void)
 Returns the status of the spectral measurement threshold interrupts. More...
 
uint8_t spectralInterruptSource (void)
 The current state of the spectral measurement interrupt status register. More...
 
bool spectralLowTriggered (void)
 The status of the low threshold interrupt. More...
 
bool spectralHighTriggered (void)
 The status of the high threshold interrupt. More...
 
bool enableLED (bool enable_led)
 Enable control of an attached LED on the LDR pin. More...
 
bool setLEDCurrent (uint16_t led_current_ma)
 Set the current limit for the LED. More...
 
uint16_t getLEDCurrent (void)
 Get the current limit for the LED. More...
 
void disableAll (void)
 Disable Spectral reading, flicker detection, and power.
 
bool getIsDataReady ()
 
bool setBank (bool low)
 Sets the active register bank. More...
 
as7341_gpio_dir_t getGPIODirection (void)
 Get the GPIO pin direction setting. More...
 
bool setGPIODirection (as7341_gpio_dir_t gpio_direction)
 Set the GPIO pin to be used as an input or output. More...
 
bool getGPIOInverted (void)
 Get the output inversion setting for the GPIO pin. More...
 
bool setGPIOInverted (bool gpio_inverted)
 Invert the logic of then GPIO pin when used as an output. More...
 
bool getGPIOValue (void)
 Read the digital level of the GPIO pin, high or low. More...
 
bool setGPIOValue (bool)
 Set the digital level of the GPIO pin, high or low. More...
 

Protected Member Functions

virtual bool _init (int32_t sensor_id)
 Initializer for post i2c/spi init. More...
 

Protected Attributes

uint8_t last_spectral_int_source
 
Adafruit_I2CDevice * i2c_dev = NULL
 Pointer to I2C bus interface.
 

Detailed Description

Class that stores state and functions for interacting with the AS7341 11-Channel Spectral Sensor.

Member Function Documentation

◆ begin()

bool Adafruit_AS7341::begin ( uint8_t  i2c_address = AS7341_I2CADDR_DEFAULT,
TwoWire *  wire = &Wire,
int32_t  sensor_id = 0 
)

Sets up the hardware and initializes I2C.

Parameters
i2c_addressThe I2C address to be used.
wireThe Wire object to be used for I2C connections.
sensor_idThe unique ID to differentiate the sensors from others
Returns
True if initialization was successful, otherwise false.

◆ setASTEP()

bool Adafruit_AS7341::setASTEP ( uint16_t  astep_value)

Sets the integration time step size.

Parameters
astep_valueIntegration time step size in 2.78 microsecon increments Step size is (astep_value+1) * 2.78 uS
Returns
true: success false: failure

◆ setATIME()

bool Adafruit_AS7341::setATIME ( uint8_t  atime_value)

Sets the integration time step count.

Total integration time will be (ATIME + 1) * (ASTEP + 1) * 2.78µS

Parameters
atime_valueThe integration time step count
Returns
true: success false: failure

◆ setGain()

bool Adafruit_AS7341::setGain ( as7341_gain_t  gain_value)

Sets the ADC gain multiplier.

Parameters
gain_valueThe gain amount. must be an as7341_gain_t
Returns
true: success false: failure

◆ getASTEP()

uint16_t Adafruit_AS7341::getASTEP ( )

Returns the integration time step size.

Step size is (astep_value+1) * 2.78 uS

Returns
uint16_t The current integration time step size

◆ getATIME()

uint8_t Adafruit_AS7341::getATIME ( )

Returns the integration time step count.

Total integration time will be (ATIME + 1) * (ASTEP + 1) * 2.78µS

Returns
uint8_t The current integration time step count

◆ getGain()

as7341_gain_t Adafruit_AS7341::getGain ( )

Returns the ADC gain multiplier.

Returns
as7341_gain_t The current ADC gain multiplier

◆ getTINT()

long Adafruit_AS7341::getTINT ( )

Returns the integration time.

The integration time is (ATIME + 1) * (ASTEP + 1) * 2.78µS

Returns
long The current integration time in ms

◆ toBasicCounts()

float Adafruit_AS7341::toBasicCounts ( uint16_t  raw)

Converts raw ADC values to basic counts.

The basic counts are RAW/(GAIN * TINT)

Parameters
rawThe raw ADC values to convert
Returns
float The basic counts

◆ readAllChannels() [1/2]

bool Adafruit_AS7341::readAllChannels ( void  )

Take readings for F1-8, Clear and NIR and store them in a buffer.

Returns
true: success false: failure

◆ readAllChannels() [2/2]

bool Adafruit_AS7341::readAllChannels ( uint16_t *  readings_buffer)

fills the provided buffer with the current measurements for Spectral channels F1-8, Clear and NIR

Parameters
readings_bufferPointer to a buffer of length 10 or more to fill with sensor data
Returns
true: success false: failure

◆ delayForData()

void Adafruit_AS7341::delayForData ( int  waitTime = 0)

Delay while waiting for data, with option to time out and recover.

Parameters
waitTimethe maximum amount of time to wait
Returns
none

◆ readChannel()

uint16_t Adafruit_AS7341::readChannel ( as7341_adc_channel_t  channel)

Returns the ADC data for a given channel.

Parameters
channelThe ADC channel to read
Returns
uint16_t The measured data for the currently configured sensor

◆ getChannel()

uint16_t Adafruit_AS7341::getChannel ( as7341_color_channel_t  channel)

Returns the reading data for the specified color channel.

call readAllChannels before reading to update the stored readings

Parameters
channelThe color sensor channel to read
Returns
uint16_t The measured data for the selected sensor channel

◆ startReading()

bool Adafruit_AS7341::startReading ( void  )

starts the process of getting readings from all channels without using delays

Returns
true: success false: failure (a bit arbitrary)

◆ checkReadingProgress()

bool Adafruit_AS7341::checkReadingProgress ( )

runs the process of getting readings from all channels without using delays. Should be called regularly (ie. in loop()) Need to call startReading() to initialise the process Need to call getAllChannels() to transfer the data into an external buffer

Returns
true: reading is complete false: reading is incomplete (or failed)

◆ getAllChannels()

bool Adafruit_AS7341::getAllChannels ( uint16_t *  readings_buffer)

transfer all the values from the private result buffer into one nominated

Parameters
readings_bufferPointer to a buffer of length 12 (THERE IS NO ERROR CHECKING, YE BE WARNED!)
Returns
true: success false: failure

◆ detectFlickerHz()

uint16_t Adafruit_AS7341::detectFlickerHz ( void  )

Detect a flickering light.

Returns
The frequency of a detected flicker or 1 if a flicker of unknown frequency is detected

◆ powerEnable()

void Adafruit_AS7341::powerEnable ( bool  enable_power)

Sets the power state of the sensor.

Parameters
enable_powertrue: on false: off

◆ enableSpectralMeasurement()

bool Adafruit_AS7341::enableSpectralMeasurement ( bool  enable_measurement)

Enables measurement of spectral data.

Parameters
enable_measurementtrue: enabled false: disabled
Returns
true: success false: failure

◆ setHighThreshold()

bool Adafruit_AS7341::setHighThreshold ( uint16_t  high_threshold)

Sets the threshold above which spectral measurements will trigger interrupts when the APERS count is reached.

Parameters
high_threshold
Returns
true: success false: failure

◆ setLowThreshold()

bool Adafruit_AS7341::setLowThreshold ( uint16_t  low_threshold)

Sets the threshold below which spectral measurements will trigger interrupts when the APERS count is reached.

Parameters
low_thresholdthe new threshold
Returns
true: success false: failure

◆ getHighThreshold()

uint16_t Adafruit_AS7341::getHighThreshold ( void  )

Returns the current high thighreshold for spectral measurements.

Returns
int16_t The current high threshold

◆ getLowThreshold()

uint16_t Adafruit_AS7341::getLowThreshold ( void  )

Returns the current low thighreshold for spectral measurements.

Returns
int16_t The current low threshold

◆ enableSpectralInterrupt()

bool Adafruit_AS7341::enableSpectralInterrupt ( bool  enable_int)

Enable Interrupts based on spectral measurements.

Parameters
enable_inttrue: enable false: disable
Returns
true: success false: falure

◆ enableSystemInterrupt()

bool Adafruit_AS7341::enableSystemInterrupt ( bool  enable_int)

Enabled system interrupts.

Parameters
enable_intSet to true to enable system interrupts
Returns
true: success false: failure

◆ setAPERS()

bool Adafruit_AS7341::setAPERS ( as7341_int_cycle_count_t  cycle_count)

Sets the number of times an interrupt threshold must be exceeded before an interrupt is triggered.

Parameters
cycle_countThe number of cycles to trigger an interrupt
Returns
true: success false: failure

◆ setSpectralThresholdChannel()

bool Adafruit_AS7341::setSpectralThresholdChannel ( as7341_adc_channel_t  channel)

Set the ADC channel to use for spectral thresholds including interrupts, automatic gain control, and persistance settings.

Parameters
channelThe channel to use for spectral thresholds. Must be a as7341_adc_channel_t except for AS7341_ADC_CHANNEL_5
Returns
true: success false: failure

◆ getInterruptStatus()

uint8_t Adafruit_AS7341::getInterruptStatus ( void  )

Returns the current value of the Interupt status register.

Returns
uint8_t

◆ clearInterruptStatus()

bool Adafruit_AS7341::clearInterruptStatus ( void  )

Clear the interrupt status register.

Returns
true: success false: failure

◆ spectralInterruptTriggered()

bool Adafruit_AS7341::spectralInterruptTriggered ( void  )

Returns the status of the spectral measurement threshold interrupts.

Returns
true: interrupt triggered false: interrupt not triggered

◆ spectralInterruptSource()

uint8_t Adafruit_AS7341::spectralInterruptSource ( void  )

The current state of the spectral measurement interrupt status register.

Returns
uint8_t The current status register

◆ spectralLowTriggered()

bool Adafruit_AS7341::spectralLowTriggered ( void  )

The status of the low threshold interrupt.

Returns
true: low interrupt triggered false: interrupt not triggered

◆ spectralHighTriggered()

bool Adafruit_AS7341::spectralHighTriggered ( void  )

The status of the high threshold interrupt.

Returns
true: high interrupt triggered false: interrupt not triggered

◆ enableLED()

bool Adafruit_AS7341::enableLED ( bool  enable_led)

Enable control of an attached LED on the LDR pin.

Parameters
enable_ledtrue: LED enabled false: LED disabled
Returns
true: success false: failure

◆ setLEDCurrent()

bool Adafruit_AS7341::setLEDCurrent ( uint16_t  led_current_ma)

Set the current limit for the LED.

Parameters
led_current_mathe value to set in milliamps. With a minimum of 4. Any amount under 4 will be rounded up to 4

Range is 4mA to 258mA

Returns
true: success false: failure

◆ getLEDCurrent()

uint16_t Adafruit_AS7341::getLEDCurrent ( void  )

Get the current limit for the LED.

Range is 4mA to 258mA

Returns
current limit in mA

◆ getIsDataReady()

bool Adafruit_AS7341::getIsDataReady ( )
Returns
true: success false: failure

◆ setBank()

bool Adafruit_AS7341::setBank ( bool  low)

Sets the active register bank.

The AS7341 uses banks to organize the register making it nescessary to set the correct bank to access a register.

Parameters
lowtrue: false: Set the current bank to allow access to registers with addresses of 0x80 and above
Returns
true: success false: failure

◆ getGPIODirection()

as7341_gpio_dir_t Adafruit_AS7341::getGPIODirection ( void  )

Get the GPIO pin direction setting.

Returns
AS7341_OUTPUT or AS7341_INPUT

◆ setGPIODirection()

bool Adafruit_AS7341::setGPIODirection ( as7341_gpio_dir_t  gpio_direction)

Set the GPIO pin to be used as an input or output.

Parameters
gpio_directionThe IO direction to set
Returns
true: success false: failure

◆ getGPIOInverted()

bool Adafruit_AS7341::getGPIOInverted ( void  )

Get the output inversion setting for the GPIO pin.

Returns
true: GPIO output inverted false: GPIO output normal

◆ setGPIOInverted()

bool Adafruit_AS7341::setGPIOInverted ( bool  gpio_inverted)

Invert the logic of then GPIO pin when used as an output.

Parameters
gpio_invertedWhen true setting the gpio value to true will connect the GPIO pin to ground. When set to false, setting the GPIO pin value to true will disconnect the GPIO pin from ground
Returns
true: success false: failure

◆ getGPIOValue()

bool Adafruit_AS7341::getGPIOValue ( void  )

Read the digital level of the GPIO pin, high or low.

Returns
true: GPIO pin level is high false: GPIO pin level is low

◆ setGPIOValue()

bool Adafruit_AS7341::setGPIOValue ( bool  gpio_high)

Set the digital level of the GPIO pin, high or low.

Parameters
gpio_highThe GPIO level to set. Set to true to disconnect the pin from ground. Set to false to connect the gpio pin to ground. This can be used to connect the cathode of an LED to ground to turn it on.
Returns
true: success false: failure

◆ _init()

bool Adafruit_AS7341::_init ( int32_t  sensor_id)
protectedvirtual

Initializer for post i2c/spi init.

Parameters
sensor_idOptional unique ID for the sensor set
Returns
True if chip identified and initialized

Member Data Documentation

◆ last_spectral_int_source

uint8_t Adafruit_AS7341::last_spectral_int_source
protected
Initial value:
=
0

The value of the last reading of the spectral interrupt source register


The documentation for this class was generated from the following files: