Skip to content

Commit fdb7230

Browse files
Cleaned library
1 parent 1ae90d8 commit fdb7230

File tree

4 files changed

+160
-16
lines changed

4 files changed

+160
-16
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
This is a library written for the AS726X Spectral Sensor (Visible or IR) with I2C firmware
3+
specially loaded. SparkFun sells these at its website: www.sparkfun.com
4+
5+
Written by Nathan Seidle & Andrew England @ SparkFun Electronics, July 12th, 2017
6+
7+
https://github.com/sparkfun/Qwiic_Spectral_Sensor_AS726X
8+
9+
Do you like this library? Help support SparkFun. Buy a board!
10+
11+
Development environment specifics:
12+
Arduino IDE 1.8.1
13+
14+
This program is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
GNU General Public License for more details.
18+
19+
You should have received a copy of the GNU General Public License
20+
along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
22+
23+
---AVAILABLE FUNCTIONS---
24+
AS726X(TwoWire &wirePort = Wire, byte gain = 3, byte measurementMode = 3);
25+
void takeMeasurements();
26+
void takeMeasurementsWithBulb();
27+
void printMeasurements();
28+
byte getTemperature();
29+
float getTemperatureF();
30+
void setMeasurementMode(byte mode);
31+
boolean dataAvailable();
32+
void enableIndicator();
33+
void disableIndicator();
34+
void setIndicatorCurrent(byte current);
35+
void enableBulb();
36+
void disableBulb();
37+
void setBulbCurrent(byte current);
38+
void softReset();
39+
void setGain(byte gain);
40+
void setIntegrationTime(byte integrationValue);
41+
void enableInterrupt();
42+
void disableInterrupt();
43+
44+
//Get the various color readings
45+
int getViolet();
46+
int getBlue();
47+
int getGreen();
48+
int getYellow();
49+
int getOrange();
50+
int getRed();
51+
52+
//Get the various NIR readings
53+
int getR();
54+
int getS();
55+
int getT();
56+
int getU();
57+
int getV();
58+
int getW();
59+
60+
//Returns the various calibration data
61+
float getCalibratedViolet();
62+
float getCalibratedBlue();
63+
float getCalibratedGreen();
64+
float getCalibratedYellow();
65+
float getCalibratedOrange();
66+
float getCalibratedRed();
67+
68+
float getCalibratedR();
69+
float getCalibratedS();
70+
float getCalibratedT();
71+
float getCalibratedU();
72+
float getCalibratedV();
73+
float getCalibratedW();
74+
*/
75+
76+
#include "AS726X.h"
77+
78+
AS726X sensor;
79+
80+
void setup() {
81+
sensor.begin();
82+
83+
}
84+
85+
void loop() {
86+
sensor.takeMeasurements();
87+
sensor.printMeasurements();//Prints out all measurements (calibrated)
88+
}

examples/AS726X_Example/AS726X_Example.ino renamed to examples/Example2_SensorSettings/Example2_SensorSettings.ino

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/*
22
This is a library written for the AS726X Spectral Sensor (Visible or IR) with I2C firmware
33
specially loaded. SparkFun sells these at its website: www.sparkfun.com
44
@@ -19,12 +19,13 @@
1919
You should have received a copy of the GNU General Public License
2020
along with this program. If not, see <http://www.gnu.org/licenses/>.
2121
22-
22+
2323
---AVAILABLE FUNCTIONS---
2424
AS726X(TwoWire &wirePort = Wire, byte gain = 3, byte measurementMode = 3);
2525
void takeMeasurements();
2626
void takeMeasurementsWithBulb();
2727
void printMeasurements();
28+
void printUncalibratedMeasurements();
2829
byte getTemperature();
2930
float getTemperatureF();
3031
void setMeasurementMode(byte mode);
@@ -41,7 +42,7 @@
4142
void enableInterrupt();
4243
void disableInterrupt();
4344
44-
45+
4546
//Get the various color readings
4647
int getViolet();
4748
int getBlue();
@@ -73,14 +74,15 @@
7374
float getCalibratedV();
7475
float getCalibratedW();
7576
*/
76-
7777
#include "AS726X.h"
78+
AS726X sensor;
79+
byte GAIN = 0;
80+
byte MEASUREMENT_MODE = 0;
7881

7982
void setup() {
80-
}
81-
AS726X sensor;
82-
/*//You can also create an instance that runs on a different port (For instance, if you are using a teensy). The Gain and Measurement mode can also be changed.
83-
//Uncomment the below code and comment the above declaration of 'sensor' to customize these settings.
83+
sensor.begin(Wire, GAIN, MEASUREMENT_MODE);
84+
/*//You can also create an instance that runs on a different port (For instance, if you are using a teensy). The Gain and Measurement mode can also be changed.
85+
//Uncomment the below code and comment the above call of "sensor.begin()" to customize these settings.
8486
byte GAIN = 3;
8587
//Sets the gain
8688
//Sets the gain value
@@ -94,12 +96,12 @@ AS726X sensor;
9496
//Mode 1: Continuous reading of GYOR (7262) / RTUX (7263)
9597
//Mode 2: Continuous reading of all channels (power-on default)
9698
//Mode 3: One-shot reading of all channels
97-
AS726X sensor(Wire, GAIN, MEASUREMENT_MODE);
99+
sensor.begin(Wire, GAIN, MEASUREMENT_MODE);
98100
//New declaration of sensor with different values
99-
*/
100-
101+
*/
102+
}
103+
101104
void loop() {
102105
sensor.takeMeasurements();
103-
//sensor.takeMeasurementsWithBulb(); //Uncomment to illuminate the onboard Bulb for taking measurements
104-
sensor.printMeasurements();//Prints out all measurements
106+
sensor.printMeasurements();//Prints out all measurements (calibrated)
105107
}

src/AS726X.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#include "AS726X.h"
2+
#include "Arduino.h"
23
//Sets up the sensor for constant read
34
//Returns the sensor version (AS7262 or AS7263)
45

5-
AS726X::AS726X(TwoWire &wirePort, byte gain, byte measurementMode)
6+
AS726X::AS726X()
7+
{
8+
9+
}
10+
11+
void AS726X::begin(TwoWire &wirePort, byte gain, byte measurementMode)
612
{
713
_i2cPort = &wirePort;
814
_i2cPort->begin();
@@ -139,6 +145,50 @@ void AS726X::printMeasurements()
139145
Serial.println();
140146
}
141147

148+
void AS726X::printUncalibratedMeasurements()
149+
{
150+
float tempF = getTemperatureF();
151+
152+
if (_sensorVersion == SENSORTYPE_AS7262)
153+
{
154+
//Visible readings
155+
Serial.print(" Reading: V[");
156+
Serial.print(getViolet(), 2);
157+
Serial.print("] B[");
158+
Serial.print(getBlue(), 2);
159+
Serial.print("] G[");
160+
Serial.print(getGreen(), 2);
161+
Serial.print("] Y[");
162+
Serial.print(getYellow(), 2);
163+
Serial.print("] O[");
164+
Serial.print(getOrange(), 2);
165+
Serial.print("] R[");
166+
Serial.print(getRed(), 2);
167+
}
168+
else if (_sensorVersion == SENSORTYPE_AS7263)
169+
{
170+
//Near IR readings
171+
Serial.print(" Reading: R[");
172+
Serial.print(getR(), 2);
173+
Serial.print("] S[");
174+
Serial.print(getS(), 2);
175+
Serial.print("] T[");
176+
Serial.print(getT(), 2);
177+
Serial.print("] U[");
178+
Serial.print(getU(), 2);
179+
Serial.print("] V[");
180+
Serial.print(getV(), 2);
181+
Serial.print("] W[");
182+
Serial.print(getW(), 2);
183+
}
184+
185+
Serial.print("] tempF[");
186+
Serial.print(tempF, 1);
187+
Serial.print("]");
188+
189+
Serial.println();
190+
}
191+
142192
//Tells IC to take measurements and polls for data ready flag
143193
void AS726X::takeMeasurements()
144194
{

src/AS726X.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
#include "Wire.h"
1212
class AS726X {
1313
public:
14-
AS726X(TwoWire &wirePort = Wire, byte gain = 3, byte measurementMode = 3);
14+
AS726X();
15+
void begin(TwoWire &wirePort = Wire, byte gain = 3, byte measurementMode = 3);
1516
void takeMeasurements();
1617
void takeMeasurementsWithBulb();
1718
void printMeasurements();
19+
void printUncalibratedMeasurements();
1820
byte getTemperature();
1921
float getTemperatureF();
2022
void setMeasurementMode(byte mode);
@@ -119,10 +121,12 @@ class AS726X {
119121
#define AS72XX_SLAVE_TX_VALID 0x02
120122
#define AS72XX_SLAVE_RX_VALID 0x01
121123

124+
#define SENSORTYPE_AS7262 0x3E
125+
#define SENSORTYPE_AS7263 0x3F
122126

123127
#define POLLING_DELAY 5 //Amount of ms to wait between checking for virtual register changes
124128

125129
byte _sensorVersion = 0;
126130
};
127131

128-
#endif
132+
#endif

0 commit comments

Comments
 (0)