A library for the Avago ADJD-S311-CR999 Color Light Sensor

Dependents:   ADJD-S311_HelloWorld

Committer:
CheeseW
Date:
Mon Mar 24 04:46:51 2014 +0000
Revision:
0:d5bb69f92ea3
Child:
1:fc17a6ccc6f0
Come out for publication

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CheeseW 0:d5bb69f92ea3 1 #ifndef ADJDs311_h
CheeseW 0:d5bb69f92ea3 2 #define ADJDs311_h
CheeseW 0:d5bb69f92ea3 3
CheeseW 0:d5bb69f92ea3 4 /**
CheeseW 0:d5bb69f92ea3 5 * Includes
CheeseW 0:d5bb69f92ea3 6 */
CheeseW 0:d5bb69f92ea3 7 #include "mbed.h"
CheeseW 0:d5bb69f92ea3 8
CheeseW 0:d5bb69f92ea3 9 // ADJD-S311's I2C address, don't change
CheeseW 0:d5bb69f92ea3 10 #define WRITE_ADDRESS 0xE8 // Address for write
CheeseW 0:d5bb69f92ea3 11 #define READ_ADDRESS 0xE9 // Address for read
CheeseW 0:d5bb69f92ea3 12
CheeseW 0:d5bb69f92ea3 13
CheeseW 0:d5bb69f92ea3 14 //#define RED 0
CheeseW 0:d5bb69f92ea3 15 //#define GREEN 1
CheeseW 0:d5bb69f92ea3 16 //#define BLUE 2
CheeseW 0:d5bb69f92ea3 17 //#define CLEAR 3
CheeseW 0:d5bb69f92ea3 18
CheeseW 0:d5bb69f92ea3 19 // ADJD-S311's register list
CheeseW 0:d5bb69f92ea3 20 #define CTRL 0x00
CheeseW 0:d5bb69f92ea3 21 #define CONFIG 0x01
CheeseW 0:d5bb69f92ea3 22 #define CAP_RED 0x06
CheeseW 0:d5bb69f92ea3 23 #define CAP_GREEN 0x07
CheeseW 0:d5bb69f92ea3 24 #define CAP_BLUE 0x08
CheeseW 0:d5bb69f92ea3 25 #define CAP_CLEAR 0x09
CheeseW 0:d5bb69f92ea3 26 #define INT_RED_LO 0xA
CheeseW 0:d5bb69f92ea3 27 #define INT_RED_HI 0xB
CheeseW 0:d5bb69f92ea3 28 #define INT_GREEN_LO 0xC
CheeseW 0:d5bb69f92ea3 29 #define INT_GREEN_HI 0xD
CheeseW 0:d5bb69f92ea3 30 #define INT_BLUE_LO 0xE
CheeseW 0:d5bb69f92ea3 31 #define INT_BLUE_HI 0xF
CheeseW 0:d5bb69f92ea3 32 #define INT_CLEAR_LO 0x10
CheeseW 0:d5bb69f92ea3 33 #define INT_CLEAR_HI 0x11
CheeseW 0:d5bb69f92ea3 34 #define DATA_RED_LO 0x40
CheeseW 0:d5bb69f92ea3 35 #define DATA_RED_HI 0x41
CheeseW 0:d5bb69f92ea3 36 #define DATA_GREEN_LO 0x42
CheeseW 0:d5bb69f92ea3 37 #define DATA_GREEN_HI 0x43
CheeseW 0:d5bb69f92ea3 38 #define DATA_BLUE_LO 0x44
CheeseW 0:d5bb69f92ea3 39 #define DATA_BLUE_HI 0x45
CheeseW 0:d5bb69f92ea3 40 #define DATA_CLEAR_LO 0x46
CheeseW 0:d5bb69f92ea3 41 #define DATA_CLEAR_HI 0x47
CheeseW 0:d5bb69f92ea3 42 #define OFFSET_RED 0x48
CheeseW 0:d5bb69f92ea3 43 #define OFFSET_GREEN 0x49
CheeseW 0:d5bb69f92ea3 44 #define OFFSET_BLUE 0x4A
CheeseW 0:d5bb69f92ea3 45 #define OFFSET_CLEAR 0x4B
CheeseW 0:d5bb69f92ea3 46
CheeseW 0:d5bb69f92ea3 47 struct RGBC{
CheeseW 0:d5bb69f92ea3 48 int red;
CheeseW 0:d5bb69f92ea3 49 int blue;
CheeseW 0:d5bb69f92ea3 50 int green;
CheeseW 0:d5bb69f92ea3 51 int clear;
CheeseW 0:d5bb69f92ea3 52 };
CheeseW 0:d5bb69f92ea3 53
CheeseW 0:d5bb69f92ea3 54
CheeseW 0:d5bb69f92ea3 55 class ADJDs311{
CheeseW 0:d5bb69f92ea3 56 public:
CheeseW 0:d5bb69f92ea3 57
CheeseW 0:d5bb69f92ea3 58 /**
CheeseW 0:d5bb69f92ea3 59 * Create a color sensor interface
CheeseW 0:d5bb69f92ea3 60 *
CheeseW 0:d5bb69f92ea3 61 * @param sda pin connected to sda of color sensor
CheeseW 0:d5bb69f92ea3 62 * @param scl pin connected to scl of color sensor
CheeseW 0:d5bb69f92ea3 63 * @param led pin connected to on board led of the sensor
CheeseW 0:d5bb69f92ea3 64 */
CheeseW 0:d5bb69f92ea3 65 ADJDs311(PinName sda, PinName scl, PinName led);
CheeseW 0:d5bb69f92ea3 66
CheeseW 0:d5bb69f92ea3 67
CheeseW 0:d5bb69f92ea3 68 /**
CheeseW 0:d5bb69f92ea3 69 * Calibrate the capacitance and integration time slot so that the current
CheeseW 0:d5bb69f92ea3 70 * readings are as close to 1000 as possible and the difference between RGB
CheeseW 0:d5bb69f92ea3 71 * readings are as small as possible
CheeseW 0:d5bb69f92ea3 72 */
CheeseW 0:d5bb69f92ea3 73 void calibrate();
CheeseW 0:d5bb69f92ea3 74
CheeseW 0:d5bb69f92ea3 75 /**
CheeseW 0:d5bb69f92ea3 76 * Turn the on board LED on/off.
CheeseW 0:d5bb69f92ea3 77 *
CheeseW 0:d5bb69f92ea3 78 * @param ledOn Whether to turn the LED on.
CheeseW 0:d5bb69f92ea3 79 */
CheeseW 0:d5bb69f92ea3 80 void ledMode(bool ledOn);
CheeseW 0:d5bb69f92ea3 81
CheeseW 0:d5bb69f92ea3 82 /**
CheeseW 0:d5bb69f92ea3 83 * Get the current offset stored in offset registers
CheeseW 0:d5bb69f92ea3 84 *
CheeseW 0:d5bb69f92ea3 85 * @return Current offset stored in offset registers
CheeseW 0:d5bb69f92ea3 86 */
CheeseW 0:d5bb69f92ea3 87 RGBC getOffset();
CheeseW 0:d5bb69f92ea3 88
CheeseW 0:d5bb69f92ea3 89 /**
CheeseW 0:d5bb69f92ea3 90 * Use the current light condition to set the offset
CheeseW 0:d5bb69f92ea3 91 *
CheeseW 0:d5bb69f92ea3 92 * @param useOffset Wether to use the offset
CheeseW 0:d5bb69f92ea3 93 * @return The offset set
CheeseW 0:d5bb69f92ea3 94 */
CheeseW 0:d5bb69f92ea3 95 RGBC setOffset(bool useOffset = true);
CheeseW 0:d5bb69f92ea3 96
CheeseW 0:d5bb69f92ea3 97
CheeseW 0:d5bb69f92ea3 98 /**
CheeseW 0:d5bb69f92ea3 99 * Use the offset registers to automatically subtract offset from the readings
CheeseW 0:d5bb69f92ea3 100 *
CheeseW 0:d5bb69f92ea3 101 * @param useOffset Wether to use the offset
CheeseW 0:d5bb69f92ea3 102 */
CheeseW 0:d5bb69f92ea3 103 void offsetMode(bool useOffset);
CheeseW 0:d5bb69f92ea3 104
CheeseW 0:d5bb69f92ea3 105 /**
CheeseW 0:d5bb69f92ea3 106 * Read in the color value from the sensor
CheeseW 0:d5bb69f92ea3 107 *
CheeseW 0:d5bb69f92ea3 108 * @return Structure containing the value of red, green, blue and clear
CheeseW 0:d5bb69f92ea3 109 */
CheeseW 0:d5bb69f92ea3 110 RGBC read();
CheeseW 0:d5bb69f92ea3 111
CheeseW 0:d5bb69f92ea3 112 /**
CheeseW 0:d5bb69f92ea3 113 * Get the gain of number of capacitor for each channel, in the range of 0 to
CheeseW 0:d5bb69f92ea3 114 * 15. Less capacitor will give higher sensitivity.
CheeseW 0:d5bb69f92ea3 115 *
CheeseW 0:d5bb69f92ea3 116 * @return Structure containing the gain of number of capacitor for each
CheeseW 0:d5bb69f92ea3 117 * channel.
CheeseW 0:d5bb69f92ea3 118 */
CheeseW 0:d5bb69f92ea3 119 RGBC getColorCap();
CheeseW 0:d5bb69f92ea3 120
CheeseW 0:d5bb69f92ea3 121 /**
CheeseW 0:d5bb69f92ea3 122 * Get the gain of number of integration time slot, in the range of 0 to 4095.
CheeseW 0:d5bb69f92ea3 123 * More integration time slot will give higher sensitivity.
CheeseW 0:d5bb69f92ea3 124 *
CheeseW 0:d5bb69f92ea3 125 * @return Structure containing the gain of number of integration time
CheeseW 0:d5bb69f92ea3 126 * slot for each channel.
CheeseW 0:d5bb69f92ea3 127 */
CheeseW 0:d5bb69f92ea3 128 RGBC getColorInt();
CheeseW 0:d5bb69f92ea3 129
CheeseW 0:d5bb69f92ea3 130 /**
CheeseW 0:d5bb69f92ea3 131 * Set the gain of number of capacitor for each channel, in the range of 0 to
CheeseW 0:d5bb69f92ea3 132 * 15. Less capacitor will give higher sensitivity.
CheeseW 0:d5bb69f92ea3 133 *
CheeseW 0:d5bb69f92ea3 134 * @param red gain value for red
CheeseW 0:d5bb69f92ea3 135 * @param green gain value for green
CheeseW 0:d5bb69f92ea3 136 * @param blue gain value for blue
CheeseW 0:d5bb69f92ea3 137 * @param clear gain value for clear
CheeseW 0:d5bb69f92ea3 138 */
CheeseW 0:d5bb69f92ea3 139 void setColorCap(int red, int green, int blue, int clear);
CheeseW 0:d5bb69f92ea3 140
CheeseW 0:d5bb69f92ea3 141 /**
CheeseW 0:d5bb69f92ea3 142 * Set the gain of number of integration time slot, in the range of 0 to 4095.
CheeseW 0:d5bb69f92ea3 143 * More integration time slot will give higher sensitivity.
CheeseW 0:d5bb69f92ea3 144 *
CheeseW 0:d5bb69f92ea3 145 * @param red gain value for red
CheeseW 0:d5bb69f92ea3 146 * @param green gain value for green
CheeseW 0:d5bb69f92ea3 147 * @param blue gain value for blue
CheeseW 0:d5bb69f92ea3 148 * @param clear gain value for clear
CheeseW 0:d5bb69f92ea3 149 */
CheeseW 0:d5bb69f92ea3 150 void setColorInt(int red, int green, int blue, int clear);
CheeseW 0:d5bb69f92ea3 151
CheeseW 0:d5bb69f92ea3 152 private:
CheeseW 0:d5bb69f92ea3 153
CheeseW 0:d5bb69f92ea3 154 // fields
CheeseW 0:d5bb69f92ea3 155 I2C _i2c;
CheeseW 0:d5bb69f92ea3 156 DigitalOut _led;
CheeseW 0:d5bb69f92ea3 157
CheeseW 0:d5bb69f92ea3 158 RGBC colorCap;
CheeseW 0:d5bb69f92ea3 159 RGBC colorInt;
CheeseW 0:d5bb69f92ea3 160 RGBC colorOffset;
CheeseW 0:d5bb69f92ea3 161
CheeseW 0:d5bb69f92ea3 162 // private memeber functions
CheeseW 0:d5bb69f92ea3 163
CheeseW 0:d5bb69f92ea3 164 /** Write a byte of data to ADJD-S311 register
CheeseW 0:d5bb69f92ea3 165 *
CheeseW 0:d5bb69f92ea3 166 * @param data byte data to write to the register
CheeseW 0:d5bb69f92ea3 167 * @param regAddr address of the register to write
CheeseW 0:d5bb69f92ea3 168 */
CheeseW 0:d5bb69f92ea3 169 void writeRegister(char data, char regAddr);
CheeseW 0:d5bb69f92ea3 170
CheeseW 0:d5bb69f92ea3 171 /** Read a byte of data from ADJD-S311 register
CheeseW 0:d5bb69f92ea3 172 *
CheeseW 0:d5bb69f92ea3 173 * @param regAddr address of the register to write
CheeseW 0:d5bb69f92ea3 174 * @retrun byte data read from the register
CheeseW 0:d5bb69f92ea3 175 */
CheeseW 0:d5bb69f92ea3 176 char readRegister(char regAddr);
CheeseW 0:d5bb69f92ea3 177
CheeseW 0:d5bb69f92ea3 178 /** Read 2 bytes of data from ADJD-S311 registers and return as an integer
CheeseW 0:d5bb69f92ea3 179 *
CheeseW 0:d5bb69f92ea3 180 * @param loRegAddr low register address
CheeseW 0:d5bb69f92ea3 181 * @return value from registers as an int
CheeseW 0:d5bb69f92ea3 182 */
CheeseW 0:d5bb69f92ea3 183 int readInt(char loRegAddr);
CheeseW 0:d5bb69f92ea3 184
CheeseW 0:d5bb69f92ea3 185 /** Write an integer data of 2 bytes to ADJD-S311 registers
CheeseW 0:d5bb69f92ea3 186 *
CheeseW 0:d5bb69f92ea3 187 * @param loRegAddr low register address
CheeseW 0:d5bb69f92ea3 188 * @param data integer value to write
CheeseW 0:d5bb69f92ea3 189 */
CheeseW 0:d5bb69f92ea3 190 void writeInt(int data, char loRegAddr);
CheeseW 0:d5bb69f92ea3 191
CheeseW 0:d5bb69f92ea3 192 /**
CheeseW 0:d5bb69f92ea3 193 * Tell the color sensor to perform measurement and store data to the color and
CheeseW 0:d5bb69f92ea3 194 * clear registers. Must be called before reading
CheeseW 0:d5bb69f92ea3 195 * color values
CheeseW 0:d5bb69f92ea3 196 */
CheeseW 0:d5bb69f92ea3 197 void performMeasurement();
CheeseW 0:d5bb69f92ea3 198
CheeseW 0:d5bb69f92ea3 199 /**
CheeseW 0:d5bb69f92ea3 200 * Calibrate the clear integration time slot so that current reading of the
CheeseW 0:d5bb69f92ea3 201 * clear channel will be as close to 1000 as possible
CheeseW 0:d5bb69f92ea3 202 */
CheeseW 0:d5bb69f92ea3 203 void calibrateClearInt();
CheeseW 0:d5bb69f92ea3 204
CheeseW 0:d5bb69f92ea3 205 /**
CheeseW 0:d5bb69f92ea3 206 * Calibrate the color integration time slots so that the max current reading
CheeseW 0:d5bb69f92ea3 207 * of the RGB channels will be as close to 1000 as possible
CheeseW 0:d5bb69f92ea3 208 */
CheeseW 0:d5bb69f92ea3 209 void calibrateColorInt();
CheeseW 0:d5bb69f92ea3 210
CheeseW 0:d5bb69f92ea3 211 /**
CheeseW 0:d5bb69f92ea3 212 * Calibrate the color capacitors so the difference of reading of different
CheeseW 0:d5bb69f92ea3 213 * channels are as small as possible
CheeseW 0:d5bb69f92ea3 214 */
CheeseW 0:d5bb69f92ea3 215 void calibrateCapacitors();
CheeseW 0:d5bb69f92ea3 216 };
CheeseW 0:d5bb69f92ea3 217
CheeseW 0:d5bb69f92ea3 218 #endif