From b1bb9b614629762b05ccb97f0f47629bdc4caa31 Mon Sep 17 00:00:00 2001 From: "Ho Yun \"Bobby" Date: Thu, 12 Mar 2020 10:14:21 -0600 Subject: [PATCH 01/13] Update README.md Update images and description for anyone viewing this repo in a browser. No need to update library version number since nothing in the library has been modified. --- README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6fdb39c..7828ef7 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,18 @@ SparkFun AS726X Arduino Library ======================================== -![AS726X](https://github.com/sparkfun/Sparkfun_AS726X_Arduino_Library/blob/master/extras/14351-01.jpg) - -[*AS7262 (14347)*](https://www.sparkfun.com/products/14347) - -[*AS7263 (14351)*](https://www.sparkfun.com/products/14351) - -This is an Arduino IDE library to control the AS726X Spectral Sensor, which can be configured via I2C or SPI. + + + + + + + + + +
+ +This is an Arduino IDE library to control the AS726X Spectral Sensor, which can be configured via I2C or SPI. The AS7262 version detects wavelengths in the visible range while the AS7263 version detects wavelengths just below the visible range, in the Near Infrared (NIR) range. Repository Contents ------------------- @@ -41,7 +46,7 @@ This product is _**open source**_! Please review the LICENSE.md file for license information. -If you have any questions or concerns on licensing, please contact techsupport@sparkfun.com. +If you have any questions or concerns on licensing, please contact technical support on our [SparkFun forums](https://forum.sparkfun.com/viewforum.php?f=152). Distributed as-is; no warranty is given. From cddf89eec09f52d87775a4e6f6adf9d65512bef5 Mon Sep 17 00:00:00 2001 From: "Ho Yun \"Bobby" Date: Thu, 12 Mar 2020 10:16:27 -0600 Subject: [PATCH 02/13] Update README.md Link product repo. No need to create new version number since nothing in the library was modified. --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7828ef7..f6d5418 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Documentation -------------- * **[Library](https://github.com/sparkfun/Qwiic_Spectral_Sensor_AS726X)** - C library for the AS726X. * **[Hookup Guide](https://learn.sparkfun.com/tutorials/as726x-nirvi)** - Basic hookup guide for the AS726X. +* **[Product Repository](https://github.com/sparkfun/Qwiic_Spectral_Sensor_AS726X)** - Main repository (including hardware files) for the AS726X Spectral Sensor. Product Versions ---------------- From 2bb1c1838255b7e0a2183c15ef29385a948a8f06 Mon Sep 17 00:00:00 2001 From: "Ho Yun \"Bobby" Date: Thu, 12 Mar 2020 10:43:18 -0600 Subject: [PATCH 03/13] Update README.md Remove mislabeled redundant link. No need to update version number since there were no changes to the library. --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index f6d5418..72a5212 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ Repository Contents Documentation -------------- -* **[Library](https://github.com/sparkfun/Qwiic_Spectral_Sensor_AS726X)** - C library for the AS726X. * **[Hookup Guide](https://learn.sparkfun.com/tutorials/as726x-nirvi)** - Basic hookup guide for the AS726X. * **[Product Repository](https://github.com/sparkfun/Qwiic_Spectral_Sensor_AS726X)** - Main repository (including hardware files) for the AS726X Spectral Sensor. From 71cfb8ffbd240623d0593b7d364cdbcdbd911b5f Mon Sep 17 00:00:00 2001 From: Anders Nelson Date: Sat, 25 May 2024 14:51:58 -0400 Subject: [PATCH 04/13] - Added support for the AS7261 CIE sensor. - Added get fcns for gain and integration time. - Minor cleanup. --- src/AS726X.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++------ src/AS726X.h | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/AS726X.cpp b/src/AS726X.cpp index 5fd580c..1c10245 100644 --- a/src/AS726X.cpp +++ b/src/AS726X.cpp @@ -12,7 +12,9 @@ bool AS726X::begin(TwoWire &wirePort, uint8_t gain, uint8_t measurementMode) { _i2cPort = &wirePort; _sensorVersion = virtualReadRegister(AS726x_HW_VERSION); - if (_sensorVersion != 0x3E && _sensorVersion != 0x3F) //HW version for AS7262 and AS7263 + + //HW version for AS7262, AS7263 and AS7261 + if (_sensorVersion != 0x3E && _sensorVersion != 0x3F && _sensorVersion != 0x40) { return false; } @@ -28,12 +30,8 @@ bool AS726X::begin(TwoWire &wirePort, uint8_t gain, uint8_t measurementMode) setGain(gain); //Set gain to 64x - setMeasurementMode(measurementMode); //One-shot reading of VBGYOR + setMeasurementMode(measurementMode); //One-shot mode - if (_sensorVersion == 0) - { - return false; - } return true; } @@ -58,6 +56,12 @@ void AS726X::setMeasurementMode(uint8_t mode) virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write } +uint8_t AS726X::getMeasurementMode() +{ + uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); //Read + return (value & 0b00001100); //Isolate BANK bits. +} + //Sets the gain value //Gain 0: 1x (power-on default) //Gain 1: 3.7x @@ -74,6 +78,12 @@ void AS726X::setGain(uint8_t gain) virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write } +uint8_t AS726X::getGain() +{ + uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); //Read + return (value & 0b00110000); //Isolate GAIN bits. +} + //Sets the integration value //Give this function a uint8_t from 0 to 255. //Time will be 2.8ms * [integration value] @@ -82,6 +92,12 @@ void AS726X::setIntegrationTime(uint8_t integrationValue) virtualWriteRegister(AS726x_INT_T, integrationValue); //Write } +uint8_t AS726X::getIntegrationTime() +{ + uint8_t value = virtualReadRegister(AS726x_INT_T); //Read + return value; +} + void AS726X::enableInterrupt() { //Read, mask/set, write @@ -143,6 +159,14 @@ int AS726X::getU() { return(getChannel(AS7263_U)); } int AS726X::getV() { return(getChannel(AS7263_V)); } int AS726X::getW() { return(getChannel(AS7263_W)); } +//Get the various CIE readings +int AS726X::getX() { return(getChannel(AS7261_X)); } +int AS726X::getY() { return(getChannel(AS7261_Y)); } +int AS726X::getZ() { return(getChannel(AS7261_Z)); } +int AS726X::getNir() { return(getChannel(AS7261_NIR)); } +int AS726X::getDark() { return(getChannel(AS7261_DARK)); } +int AS726X::getClear() { return(getChannel(AS7261_CLEAR)); } + //A the 16-bit value stored in a given channel registerReturns int AS726X::getChannel(uint8_t channelRegister) { @@ -166,6 +190,19 @@ float AS726X::getCalibratedU() { return(getCalibratedValue(AS7263_U_CAL)); } float AS726X::getCalibratedV() { return(getCalibratedValue(AS7263_V_CAL)); } float AS726X::getCalibratedW() { return(getCalibratedValue(AS7263_W_CAL)); } +float AS726X::getCalibratedX() { return(getCalibratedValue(AS7261_X_CAL)); } +float AS726X::getCalibratedY() { return(getCalibratedValue(AS7261_Y_CAL)); } +float AS726X::getCalibratedZ() { return(getCalibratedValue(AS7261_Z_CAL)); } +float AS726X::getCalibratedX1931() { return(getCalibratedValue(AS7261_X1931_CAL)); } +float AS726X::getCalibratedY1931() { return(getCalibratedValue(AS7261_Y1931_CAL)); } +float AS726X::getCalibratedUPri1976() { return(getCalibratedValue(AS7261_UPRI_CAL)); } +float AS726X::getCalibratedVPri1976() { return(getCalibratedValue(AS7261_VPRI_CAL)); } +float AS726X::getCalibratedU1976() { return(getCalibratedValue(AS7261_U_CAL)); } +float AS726X::getCalibratedV1976() { return(getCalibratedValue(AS7261_V_CAL)); } +float AS726X::getCalibratedDUV1976() { return(getCalibratedValue(AS7261_DUV_CAL)); } +int AS726X::getCalibratedLux() { return(getChannel(AS7261_LUX_CAL)); } +int AS726X::getCalibratedCCT() { return(getChannel(AS7261_CCT_CAL)); } + //Given an address, read four uint8_ts and return the floating point calibrated value float AS726X::getCalibratedValue(uint8_t calAddress) { diff --git a/src/AS726X.h b/src/AS726X.h index abfe9af..fb5ac4a 100644 --- a/src/AS726X.h +++ b/src/AS726X.h @@ -19,6 +19,7 @@ class AS726X { uint8_t getTemperature(); float getTemperatureF(); void setMeasurementMode(uint8_t mode); + uint8_t getMeasurementMode(); bool dataAvailable(); void enableIndicator(); void disableIndicator(); @@ -28,9 +29,12 @@ class AS726X { void setBulbCurrent(uint8_t current); void softReset(); void setGain(uint8_t gain); + uint8_t getGain(); void setIntegrationTime(uint8_t integrationValue); + uint8_t getIntegrationTime(); void enableInterrupt(); void disableInterrupt(); + //Get the various color readings int getViolet(); int getBlue(); @@ -47,6 +51,14 @@ class AS726X { int getV(); int getW(); + //get X, Y, Z, NIR, Dark, Clear readings. + int getX(); + int getY(); + int getZ(); + int getNir(); + int getDark(); + int getClear(); + //Returns the various calibration data float getCalibratedViolet(); float getCalibratedBlue(); @@ -62,6 +74,19 @@ class AS726X { float getCalibratedV(); float getCalibratedW(); + float getCalibratedX(); + float getCalibratedY(); + float getCalibratedZ(); + float getCalibratedX1931(); + float getCalibratedY1931(); + float getCalibratedUPri1976(); + float getCalibratedVPri1976(); + float getCalibratedU1976(); + float getCalibratedV1976(); + float getCalibratedDUV1976(); + int getCalibratedLux(); + int getCalibratedCCT(); + private: TwoWire *_i2cPort; int getChannel(uint8_t channelRegister); @@ -72,9 +97,8 @@ class AS726X { void virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite); void writeRegister(uint8_t addr, uint8_t val); uint8_t readRegister(uint8_t addr); + #define AS726X_ADDR 0x49 //7-bit unshifted default I2C Address -#define SENSORTYPE_AS7262 0x3E -#define SENSORTYPE_AS7263 0x3F //Register addresses #define AS726x_DEVICE_TYPE 0x00 @@ -117,11 +141,32 @@ class AS726X { #define AS7263_V_CAL 0x24 #define AS7263_W_CAL 0x28 + //AS7261 Registers +#define AS7261_X 0x08 //16b +#define AS7261_Y 0x0A //16b +#define AS7261_Z 0x0C //16b +#define AS7261_NIR 0x0E //16b +#define AS7261_DARK 0x10 //16b +#define AS7261_CLEAR 0x12 //16b +#define AS7261_X_CAL 0x14 +#define AS7261_Y_CAL 0x18 +#define AS7261_Z_CAL 0x1C +#define AS7261_X1931_CAL 0x20 +#define AS7261_Y1931_CAL 0x24 +#define AS7261_UPRI_CAL 0x28 +#define AS7261_VPRI_CAL 0x2C +#define AS7261_U_CAL 0x30 +#define AS7261_V_CAL 0x34 +#define AS7261_DUV_CAL 0x38 +#define AS7261_LUX_CAL 0x3C //16b +#define AS7261_CCT_CAL 0x3E //16b + #define AS72XX_SLAVE_TX_VALID 0x02 #define AS72XX_SLAVE_RX_VALID 0x01 #define SENSORTYPE_AS7262 0x3E #define SENSORTYPE_AS7263 0x3F +#define SENSORTYPE_AS7261 0x40 #define POLLING_DELAY 5 //Amount of ms to wait between checking for virtual register changes From 8d0a0fbe2b2d97e5ed94c9177219144c439addab Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Thu, 6 Jun 2024 12:31:51 -0700 Subject: [PATCH 05/13] Update library.properties --- library.properties | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library.properties b/library.properties index bc417a3..3b5c067 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ name=SparkFun AS726X -version=1.0.4 +version=1.0.5 author=Andrew England maintainer=SparkFun Electronics sentence=A library to drive the AMS AS726X NIR/VIS Spectrum Sensor paragraph=The SparkFun AS726X Visible Spectral Sensor Breakout brings spectroscopy to the palm of your hand, making it easier than ever to measure and characterize how different materials absorb and reflect different wavelengths of light. The AS726X Breakout is unique in its ability to communicate by both an I2C interface and serial interface using AT commands. Hookup is easy, thanks to the Qwiic connectors attached to the board --- simply plug one end of the Qwiic cable into the breakout and the other into one of the Qwiic Shields, then stack the board on a development board. You’ll be ready to upload a sketch to start taking spectroscopy measurements in no time. category=Sensors url=https://github.com/sparkfun/SparkFun_AS726X_Arduino_Library -architectures=* \ No newline at end of file +architectures=* From 0b7337b074f94af1b23170d1a6c7c34d5194a6dc Mon Sep 17 00:00:00 2001 From: Anders Nelson Date: Thu, 25 Jul 2024 23:45:26 -0400 Subject: [PATCH 06/13] - Fixed AS7261 hardware ID. - Changed protos for all functions with Wire failure potential. - Added fcn exits for Wire failures. - Added local timeout for measurement fcn. - Added retry limit. --- src/AS726X.cpp | 152 +++++++++++++++++++++++++++++++------------------ src/AS726X.h | 38 +++++++------ 2 files changed, 117 insertions(+), 73 deletions(-) diff --git a/src/AS726X.cpp b/src/AS726X.cpp index 1c10245..a19dd0c 100644 --- a/src/AS726X.cpp +++ b/src/AS726X.cpp @@ -14,23 +14,31 @@ bool AS726X::begin(TwoWire &wirePort, uint8_t gain, uint8_t measurementMode) _sensorVersion = virtualReadRegister(AS726x_HW_VERSION); //HW version for AS7262, AS7263 and AS7261 - if (_sensorVersion != 0x3E && _sensorVersion != 0x3F && _sensorVersion != 0x40) + if (_sensorVersion != SENSORTYPE_AS7261 && + _sensorVersion != SENSORTYPE_AS7262 && + _sensorVersion != SENSORTYPE_AS7263) { + Serial.print("_sensorVersion: "); Serial.println(_sensorVersion); return false; } - setBulbCurrent(0b00); //Set to 12.5mA (minimum) - disableBulb(); //Turn off to avoid heating the sensor + //Set to 12.5mA (minimum) + if(setBulbCurrent(0b00)) return false; - setIndicatorCurrent(0b11); //Set to 8mA (maximum) - disableIndicator(); //Turn off lights to save power + if(disableBulb()) return false; //Turn off to avoid heating the sensor - setIntegrationTime(50); //50 * 2.8ms = 140ms. 0 to 255 is valid. - //If you use Mode 2 or 3 (all the colors) then integration time is double. 140*2 = 280ms between readings. + if(setIndicatorCurrent(0b11)) return false; //Set to 8mA (maximum) - setGain(gain); //Set gain to 64x + if(disableIndicator()) return false; //Turn off lights to save power - setMeasurementMode(measurementMode); //One-shot mode + if(setIntegrationTime(50)) return false; //50 * 2.8ms = 140ms. 0 to 255 is valid. + + //If you use Mode 2 or 3 (all the colors) then integration time is double. + //140*2 = 280ms between readings. + + if(setGain(gain)) return false; //Set gain to 64x + + if(setMeasurementMode(measurementMode)) return false; //One-shot mode return true; } @@ -45,7 +53,7 @@ uint8_t AS726X::getVersion() //Mode 1: Continuous reading of GYOR (7262) / RTUX (7263) //Mode 2: Continuous reading of all channels (power-on default) //Mode 3: One-shot reading of all channels -void AS726X::setMeasurementMode(uint8_t mode) +int AS726X::setMeasurementMode(uint8_t mode) { if (mode > 0b11) mode = 0b11; @@ -53,7 +61,7 @@ void AS726X::setMeasurementMode(uint8_t mode) uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); //Read value &= 0b11110011; //Clear BANK bits value |= (mode << 2); //Set BANK bits with user's choice - virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write + return virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write } uint8_t AS726X::getMeasurementMode() @@ -67,7 +75,7 @@ uint8_t AS726X::getMeasurementMode() //Gain 1: 3.7x //Gain 2: 16x //Gain 3: 64x -void AS726X::setGain(uint8_t gain) +int AS726X::setGain(uint8_t gain) { if (gain > 0b11) gain = 0b11; @@ -75,7 +83,7 @@ void AS726X::setGain(uint8_t gain) uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); //Read value &= 0b11001111; //Clear GAIN bits value |= (gain << 4); //Set GAIN bits with user's choice - virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write + return virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write } uint8_t AS726X::getGain() @@ -87,9 +95,9 @@ uint8_t AS726X::getGain() //Sets the integration value //Give this function a uint8_t from 0 to 255. //Time will be 2.8ms * [integration value] -void AS726X::setIntegrationTime(uint8_t integrationValue) +int AS726X::setIntegrationTime(uint8_t integrationValue) { - virtualWriteRegister(AS726x_INT_T, integrationValue); //Write + return virtualWriteRegister(AS726x_INT_T, integrationValue); //Write } uint8_t AS726X::getIntegrationTime() @@ -98,49 +106,59 @@ uint8_t AS726X::getIntegrationTime() return value; } -void AS726X::enableInterrupt() +int AS726X::enableInterrupt() { //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); //Read value |= 0b01000000; //Set INT bit - virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write + return virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write } //Disables the interrupt pin -void AS726X::disableInterrupt() +int AS726X::disableInterrupt() { //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); //Read value &= 0b10111111; //Clear INT bit - virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write + return virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write } //Tells IC to take measurements and polls for data ready flag -void AS726X::takeMeasurements() +int AS726X::takeMeasurements() { - clearDataAvailable(); //Clear DATA_RDY flag when using Mode 3 + //Clear DATA_RDY flag when using Mode 3 + if(clearDataAvailable()) return -1; + + //Goto mode 3 for one shot measurement of all channels + if(setMeasurementMode(3)) return -1; - //Goto mode 3 for one shot measurement of all channels - setMeasurementMode(3); + uint32_t timeout = millis() + TIMEOUT; //Wait for data to be ready - while (dataAvailable() == false) delay(POLLING_DELAY); + while (dataAvailable() == false) + { + delay(POLLING_DELAY); + if(millis() > timeout) return -1; + } //Readings can now be accessed via getViolet(), getBlue(), etc + return 0; } //Turns on bulb, takes measurements, turns off bulb -void AS726X::takeMeasurementsWithBulb() +int AS726X::takeMeasurementsWithBulb() { //enableIndicator(); //Tell the world we are taking a reading. //The indicator LED is red and may corrupt the readings - enableBulb(); //Turn on bulb to take measurement + if(enableBulb()) return -1; //Turn on bulb to take measurement + + if(takeMeasurements()) return -1; - takeMeasurements(); + if(disableBulb()) return -1; //Turn off bulb to avoid heating sensor + //disableIndicator(); - disableBulb(); //Turn off bulb to avoid heating sensor - //disableIndicator(); + return 0; } //Get the various color readings @@ -239,58 +257,58 @@ bool AS726X::dataAvailable() //Clears the DRDY flag //Normally this should clear when data registers are read -void AS726X::clearDataAvailable() +int AS726X::clearDataAvailable() { uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); value &= ~(1 << 1); //Set the DATA_RDY bit - virtualWriteRegister(AS726x_CONTROL_SETUP, value); + return virtualWriteRegister(AS726x_CONTROL_SETUP, value); } //Enable the onboard indicator LED -void AS726X::enableIndicator() +int AS726X::enableIndicator() { //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_LED_CONTROL); value |= (1 << 0); //Set the bit - virtualWriteRegister(AS726x_LED_CONTROL, value); + return virtualWriteRegister(AS726x_LED_CONTROL, value); } //Disable the onboard indicator LED -void AS726X::disableIndicator() +int AS726X::disableIndicator() { //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_LED_CONTROL); value &= ~(1 << 0); //Clear the bit - virtualWriteRegister(AS726x_LED_CONTROL, value); + return virtualWriteRegister(AS726x_LED_CONTROL, value); } //Set the current limit of onboard LED. Default is max 8mA = 0b11. -void AS726X::setIndicatorCurrent(uint8_t current) +int AS726X::setIndicatorCurrent(uint8_t current) { if (current > 0b11) current = 0b11; //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_LED_CONTROL); //Read value &= 0b11111001; //Clear ICL_IND bits value |= (current << 1); //Set ICL_IND bits with user's choice - virtualWriteRegister(AS726x_LED_CONTROL, value); //Write + return virtualWriteRegister(AS726x_LED_CONTROL, value); //Write } //Enable the onboard 5700k or external incandescent bulb -void AS726X::enableBulb() +int AS726X::enableBulb() { //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_LED_CONTROL); value |= (1 << 3); //Set the bit - virtualWriteRegister(AS726x_LED_CONTROL, value); + return virtualWriteRegister(AS726x_LED_CONTROL, value); } //Disable the onboard 5700k or external incandescent bulb -void AS726X::disableBulb() +int AS726X::disableBulb() { //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_LED_CONTROL); value &= ~(1 << 3); //Clear the bit - virtualWriteRegister(AS726x_LED_CONTROL, value); + return virtualWriteRegister(AS726x_LED_CONTROL, value); } //Set the current limit of bulb/LED. @@ -298,7 +316,7 @@ void AS726X::disableBulb() //Current 1: 25mA //Current 2: 50mA //Current 3: 100mA -void AS726X::setBulbCurrent(uint8_t current) +int AS726X::setBulbCurrent(uint8_t current) { if (current > 0b11) current = 0b11; //Limit to two bits @@ -306,7 +324,7 @@ void AS726X::setBulbCurrent(uint8_t current) uint8_t value = virtualReadRegister(AS726x_LED_CONTROL); //Read value &= 0b11001111; //Clear ICL_DRV bits value |= (current << 4); //Set ICL_DRV bits with user's choice - virtualWriteRegister(AS726x_LED_CONTROL, value); //Write + return virtualWriteRegister(AS726x_LED_CONTROL, value); //Write } //Returns the temperature in C @@ -326,18 +344,19 @@ float AS726X::getTemperatureF() //Does a soft reset //Give sensor at least 1000ms to reset -void AS726X::softReset() +int AS726X::softReset() { //Read, mask/set, write uint8_t value = virtualReadRegister(AS726x_CONTROL_SETUP); //Read value |= (1 << 7); //Set RST bit - virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write + return virtualWriteRegister(AS726x_CONTROL_SETUP, value); //Write } //Read a virtual register from the AS726x uint8_t AS726X::virtualReadRegister(uint8_t virtualAddr) { uint8_t status; + uint8_t retries = 0; //Do a prelim check of the read register status = readRegister(AS72XX_SLAVE_STATUS_REG); @@ -351,19 +370,25 @@ uint8_t AS726X::virtualReadRegister(uint8_t virtualAddr) while (1) { status = readRegister(AS72XX_SLAVE_STATUS_REG); + if (status == 0xFF) return status; if ((status & AS72XX_SLAVE_TX_VALID) == 0) break; // If TX bit is clear, it is ok to write delay(POLLING_DELAY); + if(retries++ > retries) return 0xFF; } // Send the virtual register address (bit 7 should be 0 to indicate we are reading a register). - writeRegister(AS72XX_SLAVE_WRITE_REG, virtualAddr); + if(writeRegister(AS72XX_SLAVE_WRITE_REG, virtualAddr)) return 0xFF; + + retries = 0; //Wait for READ flag to be set while (1) { status = readRegister(AS72XX_SLAVE_STATUS_REG); + if (status == 0xFF) return status; if ((status & AS72XX_SLAVE_RX_VALID) != 0) break; // Read data is ready. delay(POLLING_DELAY); + if(retries++ > retries) return 0xFF; } uint8_t incoming = readRegister(AS72XX_SLAVE_READ_REG); @@ -371,55 +396,72 @@ uint8_t AS726X::virtualReadRegister(uint8_t virtualAddr) } //Write to a virtual register in the AS726x -void AS726X::virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite) +int AS726X::virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite) { uint8_t status; + uint8_t retries = 0; //Wait for WRITE register to be empty while (1) { status = readRegister(AS72XX_SLAVE_STATUS_REG); + if (status == 0xFF) return -1; if ((status & AS72XX_SLAVE_TX_VALID) == 0) break; // No inbound TX pending at slave. Okay to write now. delay(POLLING_DELAY); + if(retries++ > retries) return -1; } // Send the virtual register address (setting bit 7 to indicate we are writing to a register). writeRegister(AS72XX_SLAVE_WRITE_REG, (virtualAddr | 0x80)); + retries = 0; + //Wait for WRITE register to be empty while (1) { status = readRegister(AS72XX_SLAVE_STATUS_REG); + if (status == 0xFF) return -1; if ((status & AS72XX_SLAVE_TX_VALID) == 0) break; // No inbound TX pending at slave. Okay to write now. delay(POLLING_DELAY); + if(retries++ > retries) return -1; } // Send the data to complete the operation. writeRegister(AS72XX_SLAVE_WRITE_REG, dataToWrite); + + return 0; } //Reads from a give location from the AS726x uint8_t AS726X::readRegister(uint8_t addr) { + uint8_t err = 0xFF; + _i2cPort->beginTransmission(AS726X_ADDR); - _i2cPort->write(addr); - _i2cPort->endTransmission(); + if(_i2cPort->write(addr) == 0) return err; + if(_i2cPort->endTransmission()) return err; - _i2cPort->requestFrom(AS726X_ADDR, 1); + if(_i2cPort->requestFrom(AS726X_ADDR, 1) == 0) return err; if (_i2cPort->available()) { return (_i2cPort->read()); } else { Serial.println("I2C Error"); - return (0xFF); //Error + return err; //Error } + + return 0; } //Write a value to a spot in the AS726x -void AS726X::writeRegister(uint8_t addr, uint8_t val) +int AS726X::writeRegister(uint8_t addr, uint8_t val) { + uint8_t err = 0xFF; + _i2cPort->beginTransmission(AS726X_ADDR); - _i2cPort->write(addr); - _i2cPort->write(val); - _i2cPort->endTransmission(); + if(_i2cPort->write(addr) == 0) return (int)err; + if(_i2cPort->write(val) == 0) return (int)err; + if(_i2cPort->endTransmission()) return (int)err; + + return 0; } \ No newline at end of file diff --git a/src/AS726X.h b/src/AS726X.h index fb5ac4a..00698ec 100644 --- a/src/AS726X.h +++ b/src/AS726X.h @@ -13,27 +13,27 @@ class AS726X { public: AS726X(); bool begin(TwoWire &wirePort = Wire, uint8_t gain = 3, uint8_t measurementMode = 3); - void takeMeasurements(); + int takeMeasurements(); uint8_t getVersion(); - void takeMeasurementsWithBulb(); + int takeMeasurementsWithBulb(); uint8_t getTemperature(); float getTemperatureF(); - void setMeasurementMode(uint8_t mode); + int setMeasurementMode(uint8_t mode); uint8_t getMeasurementMode(); bool dataAvailable(); - void enableIndicator(); - void disableIndicator(); - void setIndicatorCurrent(uint8_t current); - void enableBulb(); - void disableBulb(); - void setBulbCurrent(uint8_t current); - void softReset(); - void setGain(uint8_t gain); + int enableIndicator(); + int disableIndicator(); + int setIndicatorCurrent(uint8_t current); + int enableBulb(); + int disableBulb(); + int setBulbCurrent(uint8_t current); + int softReset(); + int setGain(uint8_t gain); uint8_t getGain(); - void setIntegrationTime(uint8_t integrationValue); + int setIntegrationTime(uint8_t integrationValue); uint8_t getIntegrationTime(); - void enableInterrupt(); - void disableInterrupt(); + int enableInterrupt(); + int disableInterrupt(); //Get the various color readings int getViolet(); @@ -92,10 +92,10 @@ class AS726X { int getChannel(uint8_t channelRegister); float getCalibratedValue(uint8_t calAddress); float convertBytesToFloat(uint32_t myLong); - void clearDataAvailable(); + int clearDataAvailable(); uint8_t virtualReadRegister(uint8_t virtualAddr); - void virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite); - void writeRegister(uint8_t addr, uint8_t val); + int virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite); + int writeRegister(uint8_t addr, uint8_t val); uint8_t readRegister(uint8_t addr); #define AS726X_ADDR 0x49 //7-bit unshifted default I2C Address @@ -164,11 +164,13 @@ class AS726X { #define AS72XX_SLAVE_TX_VALID 0x02 #define AS72XX_SLAVE_RX_VALID 0x01 +#define SENSORTYPE_AS7261 0x3D #define SENSORTYPE_AS7262 0x3E #define SENSORTYPE_AS7263 0x3F -#define SENSORTYPE_AS7261 0x40 #define POLLING_DELAY 5 //Amount of ms to wait between checking for virtual register changes +#define RETRIES 3 +#define TIMEOUT 3000 uint8_t _sensorVersion = 0; }; From 73faeffb5e676890bc0c5f96f2181ea08db9fdff Mon Sep 17 00:00:00 2001 From: Anders Nelson Date: Thu, 25 Jul 2024 23:56:41 -0400 Subject: [PATCH 07/13] - Removed debug output. --- src/AS726X.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/AS726X.cpp b/src/AS726X.cpp index a19dd0c..f96c936 100644 --- a/src/AS726X.cpp +++ b/src/AS726X.cpp @@ -18,7 +18,6 @@ bool AS726X::begin(TwoWire &wirePort, uint8_t gain, uint8_t measurementMode) _sensorVersion != SENSORTYPE_AS7262 && _sensorVersion != SENSORTYPE_AS7263) { - Serial.print("_sensorVersion: "); Serial.println(_sensorVersion); return false; } From 61a2648cb44b742db53079f69a9a9a514f2fcc60 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Thu, 22 Aug 2024 13:07:37 -0700 Subject: [PATCH 08/13] Update library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 3b5c067..4f49a8e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun AS726X -version=1.0.5 +version=2.0.0 author=Andrew England maintainer=SparkFun Electronics sentence=A library to drive the AMS AS726X NIR/VIS Spectrum Sensor From c2fc49123c8e6aa2ea6e6676495181ed7f9806fa Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Thu, 22 Aug 2024 13:09:49 -0700 Subject: [PATCH 09/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 72a5212..d54f2c9 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Version History --------------- * [V1.0.0](https://github.com/sparkfun/Sparkfun_AS726X_Arduino_Library/releases/tag/V1.0.0) - Initial Release * [V1.0.1](https://github.com/sparkfun/Sparkfun_AS726X_Arduino_Library/releases/tag/V1.0.1) - Updating printUncalibratedMeasurements() - +* [v2.0.0](https://github.com/sparkfun/SparkFun_AS726X_Arduino_Library/releases/tag/v2.0.0) - Add error handling. Thanks @ufanders! License Information ------------------- From 04536a991af775bd47a15bda08422fdbc4b666be Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Mon, 26 Aug 2024 14:02:46 -0600 Subject: [PATCH 10/13] Properly use max retries. --- src/AS726X.cpp | 8 ++++---- src/AS726X.h | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/AS726X.cpp b/src/AS726X.cpp index f96c936..dfe342e 100644 --- a/src/AS726X.cpp +++ b/src/AS726X.cpp @@ -372,7 +372,7 @@ uint8_t AS726X::virtualReadRegister(uint8_t virtualAddr) if (status == 0xFF) return status; if ((status & AS72XX_SLAVE_TX_VALID) == 0) break; // If TX bit is clear, it is ok to write delay(POLLING_DELAY); - if(retries++ > retries) return 0xFF; + if(retries++ > MAX_RETRIES) return 0xFF; } // Send the virtual register address (bit 7 should be 0 to indicate we are reading a register). @@ -387,7 +387,7 @@ uint8_t AS726X::virtualReadRegister(uint8_t virtualAddr) if (status == 0xFF) return status; if ((status & AS72XX_SLAVE_RX_VALID) != 0) break; // Read data is ready. delay(POLLING_DELAY); - if(retries++ > retries) return 0xFF; + if(retries++ > MAX_RETRIES) return 0xFF; } uint8_t incoming = readRegister(AS72XX_SLAVE_READ_REG); @@ -407,7 +407,7 @@ int AS726X::virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite) if (status == 0xFF) return -1; if ((status & AS72XX_SLAVE_TX_VALID) == 0) break; // No inbound TX pending at slave. Okay to write now. delay(POLLING_DELAY); - if(retries++ > retries) return -1; + if(retries++ > MAX_RETRIES) return -1; } // Send the virtual register address (setting bit 7 to indicate we are writing to a register). @@ -422,7 +422,7 @@ int AS726X::virtualWriteRegister(uint8_t virtualAddr, uint8_t dataToWrite) if (status == 0xFF) return -1; if ((status & AS72XX_SLAVE_TX_VALID) == 0) break; // No inbound TX pending at slave. Okay to write now. delay(POLLING_DELAY); - if(retries++ > retries) return -1; + if(retries++ > MAX_RETRIES) return -1; } // Send the data to complete the operation. diff --git a/src/AS726X.h b/src/AS726X.h index 00698ec..81fa338 100644 --- a/src/AS726X.h +++ b/src/AS726X.h @@ -169,7 +169,7 @@ class AS726X { #define SENSORTYPE_AS7263 0x3F #define POLLING_DELAY 5 //Amount of ms to wait between checking for virtual register changes -#define RETRIES 3 +#define MAX_RETRIES 3 #define TIMEOUT 3000 uint8_t _sensorVersion = 0; From 55ecffb8dbcadac8b3d2cb2376eb4cc1c6f33fd7 Mon Sep 17 00:00:00 2001 From: Nathan Seidle Date: Mon, 26 Aug 2024 13:03:24 -0700 Subject: [PATCH 11/13] Update library.properties --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 4f49a8e..c72c061 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SparkFun AS726X -version=2.0.0 +version=2.0.1 author=Andrew England maintainer=SparkFun Electronics sentence=A library to drive the AMS AS726X NIR/VIS Spectrum Sensor From 583c3983ca6fb77816c84eb3696d46e740e4348d Mon Sep 17 00:00:00 2001 From: Algaras <145165373+Algaras-dev@users.noreply.github.com> Date: Thu, 7 Nov 2024 14:29:19 +0100 Subject: [PATCH 12/13] Move Wire and Serial initialization to setup() --- examples/Example3_BulbReadings/Example3_BulbReadings.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/Example3_BulbReadings/Example3_BulbReadings.ino b/examples/Example3_BulbReadings/Example3_BulbReadings.ino index aa79ecb..9754dc7 100644 --- a/examples/Example3_BulbReadings/Example3_BulbReadings.ino +++ b/examples/Example3_BulbReadings/Example3_BulbReadings.ino @@ -27,13 +27,13 @@ byte GAIN = 2; byte MEASUREMENT_MODE = 0; void setup() { + Wire.begin(); + Serial.begin(115200); + sensor.begin(Wire, GAIN, MEASUREMENT_MODE); } void loop() { - Wire.begin(); - Serial.begin(115200); - sensor.takeMeasurementsWithBulb(); if (sensor.getVersion() == SENSORTYPE_AS7262) From 5185314f9432f570232b02ae31ff8833fc4f026e Mon Sep 17 00:00:00 2001 From: Algaras <145165373+Algaras-dev@users.noreply.github.com> Date: Thu, 7 Nov 2024 14:32:53 +0100 Subject: [PATCH 13/13] Add missing Serial.println() --- examples/Example3_BulbReadings/Example3_BulbReadings.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/Example3_BulbReadings/Example3_BulbReadings.ino b/examples/Example3_BulbReadings/Example3_BulbReadings.ino index 9754dc7..4228753 100644 --- a/examples/Example3_BulbReadings/Example3_BulbReadings.ino +++ b/examples/Example3_BulbReadings/Example3_BulbReadings.ino @@ -72,4 +72,6 @@ void loop() { Serial.print("] tempF["); Serial.print(sensor.getTemperatureF(), 1); Serial.print("]"); + + Serial.println(); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy