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