HAMAMATSU's I2C color sensor S11059

Dependents:   test_S11059

Committer:
Rhyme
Date:
Fri May 12 02:10:35 2017 +0000
Revision:
0:cfdb96085fb7
commit before publishing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:cfdb96085fb7 1 #ifndef _S11059_H_
Rhyme 0:cfdb96085fb7 2 #define _S11059_H_
Rhyme 0:cfdb96085fb7 3 #include "mbed.h"
Rhyme 0:cfdb96085fb7 4 /**
Rhyme 0:cfdb96085fb7 5 * S11059 HAMAMATSU
Rhyme 0:cfdb96085fb7 6 * @note RGBUr Color Sensor with I2C Interface
Rhyme 0:cfdb96085fb7 7 * @note I2C 7bit address: 0x2A
Rhyme 0:cfdb96085fb7 8 *
Rhyme 0:cfdb96085fb7 9 */
Rhyme 0:cfdb96085fb7 10 /**
Rhyme 0:cfdb96085fb7 11 @code
Rhyme 0:cfdb96085fb7 12 #include "mbed.h"
Rhyme 0:cfdb96085fb7 13 #include "S11059.h"
Rhyme 0:cfdb96085fb7 14
Rhyme 0:cfdb96085fb7 15 #define MSU_S11059_ADDRESS 0x2A
Rhyme 0:cfdb96085fb7 16
Rhyme 0:cfdb96085fb7 17 #define PIN_SCL PTE1
Rhyme 0:cfdb96085fb7 18 #define PIN_SDA PTE0
Rhyme 0:cfdb96085fb7 19
Rhyme 0:cfdb96085fb7 20 int main(void)
Rhyme 0:cfdb96085fb7 21 {
Rhyme 0:cfdb96085fb7 22 uint16_t uR, uG, uB, uIR ;
Rhyme 0:cfdb96085fb7 23 uint8_t ctrl ;
Rhyme 0:cfdb96085fb7 24 int test_loop = 10 ;
Rhyme 0:cfdb96085fb7 25 int interval = 100 ;
Rhyme 0:cfdb96085fb7 26 S11059 *s11059 = new S11059(PIN_SDA, PIN_SCL, MSU_S11059_ADDRESS) ;
Rhyme 0:cfdb96085fb7 27
Rhyme 0:cfdb96085fb7 28 for (int i = 0 ; i < test_loop ; i++ ) {
Rhyme 0:cfdb96085fb7 29 ctrl = 0x89 ; // ADC reset, High Gain, integration time 1.4ms
Rhyme 0:cfdb96085fb7 30 s11059->setControl(ctrl) ;
Rhyme 0:cfdb96085fb7 31 ctrl = 0x09 ; // Release ADC reset, High Gain, integration time 1.4ms
Rhyme 0:cfdb96085fb7 32 s11059->setControl(ctrl) ; // start measure
Rhyme 0:cfdb96085fb7 33 wait(0.02) ;
Rhyme 0:cfdb96085fb7 34 s11059->getRData(&uR) ;
Rhyme 0:cfdb96085fb7 35 s11059->getGData(&uG) ;
Rhyme 0:cfdb96085fb7 36 s11059->getBData(&uB) ;
Rhyme 0:cfdb96085fb7 37 s11059->getIRData(&uIR) ;
Rhyme 0:cfdb96085fb7 38 printf("S11059 R[%d], G[%d], B[%d], IR[%d]\n", uR, uG, uB, uIR) ;
Rhyme 0:cfdb96085fb7 39 wait_ms(interval) ;
Rhyme 0:cfdb96085fb7 40 }
Rhyme 0:cfdb96085fb7 41 delete s11059 ;
Rhyme 0:cfdb96085fb7 42 while(1) { }
Rhyme 0:cfdb96085fb7 43 }
Rhyme 0:cfdb96085fb7 44 @endcode
Rhyme 0:cfdb96085fb7 45 */
Rhyme 0:cfdb96085fb7 46
Rhyme 0:cfdb96085fb7 47 class S11059
Rhyme 0:cfdb96085fb7 48 {
Rhyme 0:cfdb96085fb7 49 public:
Rhyme 0:cfdb96085fb7 50 /**
Rhyme 0:cfdb96085fb7 51 * constructor
Rhyme 0:cfdb96085fb7 52 *
Rhyme 0:cfdb96085fb7 53 * @param sda SDA pin
Rhyme 0:cfdb96085fb7 54 * @param scl SCL pin
Rhyme 0:cfdb96085fb7 55 * @param addr address of the I2C peripheral
Rhyme 0:cfdb96085fb7 56 */
Rhyme 0:cfdb96085fb7 57 S11059(PinName sda, PinName scl, int addr) ;
Rhyme 0:cfdb96085fb7 58
Rhyme 0:cfdb96085fb7 59 ~S11059() ;
Rhyme 0:cfdb96085fb7 60
Rhyme 0:cfdb96085fb7 61 /*
Rhyme 0:cfdb96085fb7 62 * some member functions here (yet to be written)
Rhyme 0:cfdb96085fb7 63 */
Rhyme 0:cfdb96085fb7 64 /**
Rhyme 0:cfdb96085fb7 65 * Get measured Red Value as float [0..1]
Rhyme 0:cfdb96085fb7 66 * @returns the value of Red as float
Rhyme 0:cfdb96085fb7 67 */
Rhyme 0:cfdb96085fb7 68 float getR(void) ; // return float value of Red
Rhyme 0:cfdb96085fb7 69
Rhyme 0:cfdb96085fb7 70 /**
Rhyme 0:cfdb96085fb7 71 * Get measured Green Value as float [0..1]
Rhyme 0:cfdb96085fb7 72 * @returns the value of Green as float
Rhyme 0:cfdb96085fb7 73 */
Rhyme 0:cfdb96085fb7 74 float getG(void) ; // return float value of Green
Rhyme 0:cfdb96085fb7 75
Rhyme 0:cfdb96085fb7 76 /**
Rhyme 0:cfdb96085fb7 77 * Get measured Blue Value as float [0..1]
Rhyme 0:cfdb96085fb7 78 * @returns the value of Blue as float
Rhyme 0:cfdb96085fb7 79 */
Rhyme 0:cfdb96085fb7 80 float getB(void) ; // return float value of Blue
Rhyme 0:cfdb96085fb7 81
Rhyme 0:cfdb96085fb7 82 /**
Rhyme 0:cfdb96085fb7 83 * Get measured Infrared Value as float [0..1]
Rhyme 0:cfdb96085fb7 84 * @returns the value of Infrared as float
Rhyme 0:cfdb96085fb7 85 */
Rhyme 0:cfdb96085fb7 86 float getIR(void) ; // return float value of Infrared
Rhyme 0:cfdb96085fb7 87
Rhyme 0:cfdb96085fb7 88 /**
Rhyme 0:cfdb96085fb7 89 * Get measured value of Red
Rhyme 0:cfdb96085fb7 90 * @param rdata unsigned 16bit data of Red
Rhyme 0:cfdb96085fb7 91 */
Rhyme 0:cfdb96085fb7 92 void getRData(uint16_t *rdata) ;
Rhyme 0:cfdb96085fb7 93
Rhyme 0:cfdb96085fb7 94 /**
Rhyme 0:cfdb96085fb7 95 * Get measured value of Green
Rhyme 0:cfdb96085fb7 96 * @param rdata unsigned 16bit data of Green
Rhyme 0:cfdb96085fb7 97 */
Rhyme 0:cfdb96085fb7 98 void getGData(uint16_t *gdata) ;
Rhyme 0:cfdb96085fb7 99
Rhyme 0:cfdb96085fb7 100 /**
Rhyme 0:cfdb96085fb7 101 * Get measured value of Blue
Rhyme 0:cfdb96085fb7 102 * @param rdata unsigned 16bit data of Blue
Rhyme 0:cfdb96085fb7 103 */
Rhyme 0:cfdb96085fb7 104 void getBData(uint16_t *bdata) ;
Rhyme 0:cfdb96085fb7 105
Rhyme 0:cfdb96085fb7 106 /**
Rhyme 0:cfdb96085fb7 107 * Get measured value of Infrared
Rhyme 0:cfdb96085fb7 108 * @param rdata unsigned 16bit data of Infrared
Rhyme 0:cfdb96085fb7 109 */
Rhyme 0:cfdb96085fb7 110 void getIRData(uint16_t *irdata) ;
Rhyme 0:cfdb96085fb7 111
Rhyme 0:cfdb96085fb7 112 /**
Rhyme 0:cfdb96085fb7 113 * Get measured values of Red, Green, Blue and Infrared at once
Rhyme 0:cfdb96085fb7 114 * @param 4 unsigned 16bit data data[0]:Red, data[1]:Green, data[2]:Blue, data[3]:Infrared
Rhyme 0:cfdb96085fb7 115 */
Rhyme 0:cfdb96085fb7 116 void getAllData(uint16_t *data) ; /* pass uint16_t data[4] */
Rhyme 0:cfdb96085fb7 117
Rhyme 0:cfdb96085fb7 118 /**
Rhyme 0:cfdb96085fb7 119 * Get value of the Control Register
Rhyme 0:cfdb96085fb7 120 * @returns the value of Control Register at address 0x00
Rhyme 0:cfdb96085fb7 121 * @note bit[7] ADC Reset 1:reset 0:start
Rhyme 0:cfdb96085fb7 122 * @note bit[6] Sleep function 1:sleep 0:active
Rhyme 0:cfdb96085fb7 123 * @note bit[5] Sleep monitor
Rhyme 0:cfdb96085fb7 124 * @note bit[4] (reserved)
Rhyme 0:cfdb96085fb7 125 * @note bit[3] Gain 1:High Gain 0:Low Gain
Rhyme 0:cfdb96085fb7 126 * @note bit[2] Integration mode 1:manual 0:fixed
Rhyme 0:cfdb96085fb7 127 * @note bit[1:0] Integration time
Rhyme 0:cfdb96085fb7 128 * @note (00)87.5us (01)1.4ms (10)22.4ms (11)179.2ms
Rhyme 0:cfdb96085fb7 129 *
Rhyme 0:cfdb96085fb7 130 */
Rhyme 0:cfdb96085fb7 131 uint8_t getControl(void) ;
Rhyme 0:cfdb96085fb7 132
Rhyme 0:cfdb96085fb7 133 /**
Rhyme 0:cfdb96085fb7 134 * Set value to the Control Register
Rhyme 0:cfdb96085fb7 135 * @param ctrlValue value to assing to the Control Register at address 0x00
Rhyme 0:cfdb96085fb7 136 */
Rhyme 0:cfdb96085fb7 137 void setControl(uint8_t ctrlValue) ;
Rhyme 0:cfdb96085fb7 138
Rhyme 0:cfdb96085fb7 139 /**
Rhyme 0:cfdb96085fb7 140 * Get value of the Manual Timing Register at address 0x01-0x02
Rhyme 0:cfdb96085fb7 141 * @returns 16bit value of Manual Timing Register
Rhyme 0:cfdb96085fb7 142 * @note This value matters only in Manual Mode (bit[2] == 1)
Rhyme 0:cfdb96085fb7 143 * @note In manual mode the integration time is
Rhyme 0:cfdb96085fb7 144 * @note length specified by bit[1:0] of Control Register (address 0x00)
Rhyme 0:cfdb96085fb7 145 * @note multiplied by the value of address 0x01-0x02
Rhyme 0:cfdb96085fb7 146 * @note if the value of 0x01-0x02 is N
Rhyme 0:cfdb96085fb7 147 * @note and according to the bit[1:0] of Control Register
Rhyme 0:cfdb96085fb7 148 * @note Integration time length is calculated as
Rhyme 0:cfdb96085fb7 149 * @note (00) 175 * N us
Rhyme 0:cfdb96085fb7 150 * @note (01) 2.8 * N ms
Rhyme 0:cfdb96085fb7 151 * @note (10) 44.8 * N ms
Rhyme 0:cfdb96085fb7 152 * @note (11) 358.4 * N ms
Rhyme 0:cfdb96085fb7 153 */
Rhyme 0:cfdb96085fb7 154 uint16_t getTiming(void) ;
Rhyme 0:cfdb96085fb7 155
Rhyme 0:cfdb96085fb7 156 /**
Rhyme 0:cfdb96085fb7 157 * Set value to the Manual Timing Register at address 0x01-0x02
Rhyme 0:cfdb96085fb7 158 * @param timingValue 16bit timing value MSB to 0x01 LSB to 0x02
Rhyme 0:cfdb96085fb7 159 */
Rhyme 0:cfdb96085fb7 160 void setTiming(uint16_t timingValue) ;
Rhyme 0:cfdb96085fb7 161
Rhyme 0:cfdb96085fb7 162 /**
Rhyme 0:cfdb96085fb7 163 * Set/clear ADC Reset bit in the Control Register
Rhyme 0:cfdb96085fb7 164 * @param mode 1:reset 0:start
Rhyme 0:cfdb96085fb7 165 */
Rhyme 0:cfdb96085fb7 166 void setADCReset(int mode) ;
Rhyme 0:cfdb96085fb7 167
Rhyme 0:cfdb96085fb7 168 /**
Rhyme 0:cfdb96085fb7 169 * Get value of the ADC Reset bit (bit[7]) in the Control Register
Rhyme 0:cfdb96085fb7 170 * @returns the ADC Reset bit (bit[7])
Rhyme 0:cfdb96085fb7 171 */
Rhyme 0:cfdb96085fb7 172 int getADCReset(void) ;
Rhyme 0:cfdb96085fb7 173
Rhyme 0:cfdb96085fb7 174 /**
Rhyme 0:cfdb96085fb7 175 * Set value to the sleep function bit (bit[6]) in the Control Register
Rhyme 0:cfdb96085fb7 176 * @param mode bit value 1:sleep 0:active
Rhyme 0:cfdb96085fb7 177 */
Rhyme 0:cfdb96085fb7 178 void setSleepMode(int mode) ;
Rhyme 0:cfdb96085fb7 179
Rhyme 0:cfdb96085fb7 180 /**
Rhyme 0:cfdb96085fb7 181 * Get value of the sleep function bit (bit[6]) in the Control Register
Rhyme 0:cfdb96085fb7 182 * @returns bit value of sleep function bit (bit[6])
Rhyme 0:cfdb96085fb7 183 */
Rhyme 0:cfdb96085fb7 184 int getSleepMode(void) ;
Rhyme 0:cfdb96085fb7 185
Rhyme 0:cfdb96085fb7 186 /**
Rhyme 0:cfdb96085fb7 187 * Set value to the gain bit (bit[3]) in the Control Register
Rhyme 0:cfdb96085fb7 188 * @param mode bit value to assign 1:High Gain 0:Low Gain
Rhyme 0:cfdb96085fb7 189 */
Rhyme 0:cfdb96085fb7 190 void setGain(int mode) ;
Rhyme 0:cfdb96085fb7 191
Rhyme 0:cfdb96085fb7 192 /**
Rhyme 0:cfdb96085fb7 193 * Get value of the gain bit (bit[3]) in the Control Register
Rhyme 0:cfdb96085fb7 194 * @returns the bit value of gain bit (bit[3])
Rhyme 0:cfdb96085fb7 195 */
Rhyme 0:cfdb96085fb7 196 int getGain(void) ;
Rhyme 0:cfdb96085fb7 197
Rhyme 0:cfdb96085fb7 198 /**
Rhyme 0:cfdb96085fb7 199 * Set value to Integration mode bit (bit[2]) of the Control Register
Rhyme 0:cfdb96085fb7 200 * @param mode bit value to assign 1:Manual Mode 0:Fixed Time Mode
Rhyme 0:cfdb96085fb7 201 */
Rhyme 0:cfdb96085fb7 202 void setIntegralMode(int mode) ;
Rhyme 0:cfdb96085fb7 203
Rhyme 0:cfdb96085fb7 204 /**
Rhyme 0:cfdb96085fb7 205 * Get value of Integration mode bit (bit[2]) of the Control Register
Rhyme 0:cfdb96085fb7 206 * @returns the bit value of Integration mode bit (bit[2])
Rhyme 0:cfdb96085fb7 207 */
Rhyme 0:cfdb96085fb7 208 int getIntegralMode(void) ;
Rhyme 0:cfdb96085fb7 209
Rhyme 0:cfdb96085fb7 210 private:
Rhyme 0:cfdb96085fb7 211 I2C m_i2c;
Rhyme 0:cfdb96085fb7 212 int m_addr;
Rhyme 0:cfdb96085fb7 213 void readRegs(int addr, uint8_t * data, int len);
Rhyme 0:cfdb96085fb7 214 void writeRegs(uint8_t * data, int len);
Rhyme 0:cfdb96085fb7 215 } ;
Rhyme 0:cfdb96085fb7 216
Rhyme 0:cfdb96085fb7 217 #endif /* _S11059_H_ */