HAMAMATSU's I2C color sensor S11059
Revision 0:cfdb96085fb7, committed 2017-05-12
- Comitter:
- Rhyme
- Date:
- Fri May 12 02:10:35 2017 +0000
- Commit message:
- commit before publishing
Changed in this revision
S11059.cpp | Show annotated file Show diff for this revision Revisions of this file |
S11059.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r cfdb96085fb7 S11059.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/S11059.cpp Fri May 12 02:10:35 2017 +0000 @@ -0,0 +1,263 @@ +/* + * HAMAMATSU Color Senosr S11059 + * + */ + #include "mbed.h" + #include "S11059.h" + + /* S11059 Register Address */ + #define REG_CONTROL 0x00 + #define REG_TIMING_DATA 0x01 + #define REG_TIMING_MSB 0x01 + #define REG_TIMING_LSB 0x02 + #define REG_R_DATA 0x03 + #define REG_R_MSB 0x03 + #define REG_R_LSB 0x04 + #define REG_G_DATA 0x05 + #define REG_G_MSB 0x05 + #define REG_G_LSB 0x06 + #define REG_B_DATA 0x07 + #define REG_B_MSB 0x07 + #define REG_B_LSB 0x08 + #define REG_IR_DATA 0x09 + #define REG_IR_MSB 0x09 + #define REG_IR_LSB 0x0A + + /* register bits */ + /* REG_CONTROL (00) */ + // bit[7] ADC Reset 0:normal 1:reset + #define BIT_ADC_RESET 0x80 + // bit[6] Sleep function 0:normal 1:standby + #define BIT_SLEEP_MODE 0x40 + // bit[5] Sleeep function monitor bit + #define BIT_SLEEP_MONITOR 0x20 + // bit[4] (reserved) + // bit[3] Gain select 0:Low Gain 1:High Gain + #define BIT_HIGH_GAIN 0x08 + // bit[2] Ingegration mode 0:Fixed 1:Manual + #define BIT_INT_MANUAL 0x02 + // bit[1:0] Integration Time + #define BIT_INT_LEN0 0x00 /* 87.5 us */ + #define BIT_INT_LEN1 0x01 /* 1.4 ms */ + #define BIT_INT_LEN2 0x02 /* 22.4 ms */ + #define BIT_INT_LEN3 0x03 /* 179.2 ms */ + +/* constructor and destructor */ + + S11059::S11059(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) { + // activate the peripheral +} + +S11059::~S11059() { } + +float S11059::getR(void) // return float value of Red +{ + uint16_t r_data ; + float data ; + getRData(&r_data) ; + data = (float)r_data / 65535.0 ; + return(data) ; +} + +float S11059::getG(void) // return float value of Green +{ + uint16_t g_data ; + float data ; + getGData(&g_data) ; + data = (float)g_data / 65535.0 ; + return(data) ; +} + +float S11059::getB(void) // return float value of Blue +{ + uint16_t b_data ; + float data ; + getBData(&b_data) ; + data = (float)b_data / 65535.0 ; + return(data) ; +} + +float S11059::getIR(void) // return float value of Infrared +{ + uint16_t ir_data ; + float data ; + getIRData(&ir_data) ; + data = (float)ir_data / 65535.0 ; + return(data) ; +} + +void S11059::getRData(uint16_t *rdata) +{ + uint8_t data[2] ; + readRegs(REG_R_DATA, data, 2) ; + *rdata = (data[0]<<8) | data[1] ; +} + +void S11059::getGData(uint16_t *gdata) +{ + uint8_t data[2] ; + readRegs(REG_G_DATA, data, 2) ; + *gdata = (data[0]<<8) | data[1] ; +} + +void S11059::getBData(uint16_t *bdata) +{ + uint8_t data[2] ; + readRegs(REG_B_DATA, data, 2) ; + *bdata = (data[0]<<8) | data[1] ; +} + +void S11059::getIRData(uint16_t *irdata) +{ + uint8_t data[2] ; + readRegs(REG_IR_DATA, data, 2) ; + *irdata = (data[0]<<8) | data[1] ; +} + +void S11059::getAllData(uint16_t *data) +{ + uint8_t raw_data[8] ; + readRegs(REG_R_DATA, raw_data, 8) ; + data[0] = (raw_data[0] << 8) | raw_data[1] ; + data[1] = (raw_data[2] << 8) | raw_data[3] ; + data[2] = (raw_data[4] << 8) | raw_data[5] ; + data[3] = (raw_data[6] << 8) | raw_data[7] ; +} + +uint8_t S11059::getControl(void) +{ + uint8_t data[1] ; + readRegs(REG_CONTROL, data, 1) ; + return(data[0]) ; +} + +void S11059::setControl(uint8_t ctrlValue) +{ + uint8_t data[2] ; + data[0] = REG_CONTROL ; + data[1] = ctrlValue ; + writeRegs(data, 2) ; +} + +uint16_t S11059::getTiming(void) +{ + uint8_t data[2] ; + uint16_t timing ; + readRegs(REG_TIMING_DATA, data, 2) ; + timing = (data[0] << 8) | data[1] ; + return(timing) ; +} + +void S11059::setTiming(uint16_t timingValue) +{ + uint8_t data[3] ; + data[0] = REG_TIMING_DATA ; + data[1] = (timingValue >> 8) & 0xFF ; + data[2] = timingValue & 0xFF ; + writeRegs(data, 3) ; +} + +void S11059::setADCReset(int mode) +{ + uint8_t data[2] ; + data[0] = REG_CONTROL ; + readRegs(data[0], &data[1], 1) ; + if (mode == 0) { + data[1] ^= BIT_ADC_RESET ; + } else { + data[1] |= BIT_ADC_RESET ; /* 0x80 */ + } + writeRegs(data, 2) ; +} + +int S11059::getADCReset(void) +{ + uint8_t data[1] ; + int result = 0 ; + readRegs(REG_CONTROL, data, 1) ; + if (data[0] & BIT_ADC_RESET) { + result = 1 ; + } + return(result) ; +} + +void S11059::setSleepMode(int mode) +{ + uint8_t data[2] ; + data[0] = REG_CONTROL ; + readRegs(data[0], &data[1], 1) ; + if (mode == 0) { + data[1] ^= BIT_SLEEP_MODE ; + } else { + data[1] |= BIT_SLEEP_MODE ; + } + writeRegs(data, 2) ; +} + +int S11059::getSleepMode(void) +{ + uint8_t data[1] ; + int result = 0 ; + readRegs(REG_CONTROL, data, 1) ; + if (data[0] & BIT_SLEEP_MODE) { + result = 1 ; + } + return(result) ; +} + +void S11059::setGain(int mode) +{ + uint8_t data[2] ; + data[0] = REG_CONTROL ; + readRegs(data[0], &data[1], 1) ; + if (mode == 0) { + data[1] ^= BIT_HIGH_GAIN ; + } else { + data[1] |= BIT_HIGH_GAIN ; + } +} + +int S11059::getGain(void) +{ + uint8_t data[1] ; + int result = 0 ; + readRegs(REG_CONTROL, data, 1) ; + if (data[0] & BIT_HIGH_GAIN) { + result = 1 ; + } + return(result) ; +} + +void S11059::setIntegralMode(int mode) +{ + uint8_t data[2] ; + data[0] = REG_CONTROL ; + readRegs(data[0], &data[1], 1) ; + if (mode == 0) { + data[1] ^= BIT_INT_MANUAL ; + } else { + data[1] |= BIT_INT_MANUAL ; + } +} + +int S11059::getIntegralMode(void) +{ + uint8_t data[1] ; + int result = 0 ; + readRegs(REG_CONTROL, data, 1) ; + if (data[0] & BIT_INT_MANUAL) { + result = 1 ; + } + return(result) ; +} + +void S11059::readRegs(int addr, uint8_t * data, int len) { + char t[1] = {addr}; + m_i2c.write(m_addr, t, 1, true); + m_i2c.read(m_addr, (char *)data, len); +} + +void S11059::writeRegs(uint8_t * data, int len) { + m_i2c.write(m_addr, (char *)data, len); +} +
diff -r 000000000000 -r cfdb96085fb7 S11059.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/S11059.h Fri May 12 02:10:35 2017 +0000 @@ -0,0 +1,217 @@ +#ifndef _S11059_H_ +#define _S11059_H_ +#include "mbed.h" +/** + * S11059 HAMAMATSU + * @note RGBUr Color Sensor with I2C Interface + * @note I2C 7bit address: 0x2A + * + */ + /** +@code +#include "mbed.h" +#include "S11059.h" + +#define MSU_S11059_ADDRESS 0x2A + +#define PIN_SCL PTE1 +#define PIN_SDA PTE0 + +int main(void) +{ + uint16_t uR, uG, uB, uIR ; + uint8_t ctrl ; + int test_loop = 10 ; + int interval = 100 ; + S11059 *s11059 = new S11059(PIN_SDA, PIN_SCL, MSU_S11059_ADDRESS) ; + + for (int i = 0 ; i < test_loop ; i++ ) { + ctrl = 0x89 ; // ADC reset, High Gain, integration time 1.4ms + s11059->setControl(ctrl) ; + ctrl = 0x09 ; // Release ADC reset, High Gain, integration time 1.4ms + s11059->setControl(ctrl) ; // start measure + wait(0.02) ; + s11059->getRData(&uR) ; + s11059->getGData(&uG) ; + s11059->getBData(&uB) ; + s11059->getIRData(&uIR) ; + printf("S11059 R[%d], G[%d], B[%d], IR[%d]\n", uR, uG, uB, uIR) ; + wait_ms(interval) ; + } + delete s11059 ; + while(1) { } +} +@endcode + */ + +class S11059 +{ +public: + /** + * constructor + * + * @param sda SDA pin + * @param scl SCL pin + * @param addr address of the I2C peripheral + */ + S11059(PinName sda, PinName scl, int addr) ; + + ~S11059() ; + + /* + * some member functions here (yet to be written) + */ +/** + * Get measured Red Value as float [0..1] + * @returns the value of Red as float + */ +float getR(void) ; // return float value of Red + +/** + * Get measured Green Value as float [0..1] + * @returns the value of Green as float + */ +float getG(void) ; // return float value of Green + +/** + * Get measured Blue Value as float [0..1] + * @returns the value of Blue as float + */ +float getB(void) ; // return float value of Blue + +/** + * Get measured Infrared Value as float [0..1] + * @returns the value of Infrared as float + */ +float getIR(void) ; // return float value of Infrared + +/** + * Get measured value of Red + * @param rdata unsigned 16bit data of Red + */ +void getRData(uint16_t *rdata) ; + +/** + * Get measured value of Green + * @param rdata unsigned 16bit data of Green + */ +void getGData(uint16_t *gdata) ; + +/** + * Get measured value of Blue + * @param rdata unsigned 16bit data of Blue + */ +void getBData(uint16_t *bdata) ; + +/** + * Get measured value of Infrared + * @param rdata unsigned 16bit data of Infrared + */ +void getIRData(uint16_t *irdata) ; + +/** + * Get measured values of Red, Green, Blue and Infrared at once + * @param 4 unsigned 16bit data data[0]:Red, data[1]:Green, data[2]:Blue, data[3]:Infrared + */ +void getAllData(uint16_t *data) ; /* pass uint16_t data[4] */ + + /** + * Get value of the Control Register + * @returns the value of Control Register at address 0x00 + * @note bit[7] ADC Reset 1:reset 0:start + * @note bit[6] Sleep function 1:sleep 0:active + * @note bit[5] Sleep monitor + * @note bit[4] (reserved) + * @note bit[3] Gain 1:High Gain 0:Low Gain + * @note bit[2] Integration mode 1:manual 0:fixed + * @note bit[1:0] Integration time + * @note (00)87.5us (01)1.4ms (10)22.4ms (11)179.2ms + * + */ +uint8_t getControl(void) ; + +/** + * Set value to the Control Register + * @param ctrlValue value to assing to the Control Register at address 0x00 + */ +void setControl(uint8_t ctrlValue) ; + +/** + * Get value of the Manual Timing Register at address 0x01-0x02 + * @returns 16bit value of Manual Timing Register + * @note This value matters only in Manual Mode (bit[2] == 1) + * @note In manual mode the integration time is + * @note length specified by bit[1:0] of Control Register (address 0x00) + * @note multiplied by the value of address 0x01-0x02 + * @note if the value of 0x01-0x02 is N + * @note and according to the bit[1:0] of Control Register + * @note Integration time length is calculated as + * @note (00) 175 * N us + * @note (01) 2.8 * N ms + * @note (10) 44.8 * N ms + * @note (11) 358.4 * N ms + */ +uint16_t getTiming(void) ; + +/** + * Set value to the Manual Timing Register at address 0x01-0x02 + * @param timingValue 16bit timing value MSB to 0x01 LSB to 0x02 + */ +void setTiming(uint16_t timingValue) ; + +/** + * Set/clear ADC Reset bit in the Control Register + * @param mode 1:reset 0:start + */ +void setADCReset(int mode) ; + +/** + * Get value of the ADC Reset bit (bit[7]) in the Control Register + * @returns the ADC Reset bit (bit[7]) + */ +int getADCReset(void) ; + +/** + * Set value to the sleep function bit (bit[6]) in the Control Register + * @param mode bit value 1:sleep 0:active + */ +void setSleepMode(int mode) ; + +/** + * Get value of the sleep function bit (bit[6]) in the Control Register + * @returns bit value of sleep function bit (bit[6]) + */ +int getSleepMode(void) ; + +/** + * Set value to the gain bit (bit[3]) in the Control Register + * @param mode bit value to assign 1:High Gain 0:Low Gain + */ +void setGain(int mode) ; + +/** + * Get value of the gain bit (bit[3]) in the Control Register + * @returns the bit value of gain bit (bit[3]) + */ +int getGain(void) ; + +/** + * Set value to Integration mode bit (bit[2]) of the Control Register + * @param mode bit value to assign 1:Manual Mode 0:Fixed Time Mode + */ +void setIntegralMode(int mode) ; + +/** + * Get value of Integration mode bit (bit[2]) of the Control Register + * @returns the bit value of Integration mode bit (bit[2]) + */ +int getIntegralMode(void) ; + +private: + I2C m_i2c; + int m_addr; + void readRegs(int addr, uint8_t * data, int len); + void writeRegs(uint8_t * data, int len); +} ; + +#endif /* _S11059_H_ */ \ No newline at end of file