2 * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Credits to Seeed Studeo.
6 * Based on Seeed Studeo code example,
7 * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_I2C_Color_Sensor_v0.9b.
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #include "tcs3414cs.h"
37 struct TCS3414CSException : public std::exception {
39 TCS3414CSException (std::string msg) : message (msg) { }
40 ~TCS3414CSException () throw () { }
41 const char* what() const throw () { return message.c_str(); }
44 TCS3414CS::TCS3414CS () {
46 m_i2Ctx = mraa_i2c_init(0);
48 mraa_result_t ret = mraa_i2c_address(m_i2Ctx, ADDR);
49 if (ret != MRAA_SUCCESS) {
50 throw TCS3414CSException ("Couldn't initilize I2C.");
53 // Set timing register
54 i2cWriteReg (REG_TIMING, INTEG_MODE_FREE);
57 // Set interrupt source register
58 i2cWriteReg (REG_INT_SOURCE, INT_SOURCE_GREEN);
61 // Set interrupt control register
62 i2cWriteReg (REG_INT, INTR_LEVEL | INTR_PERSIST_EVERY);
66 i2cWriteReg (REG_GAIN, GAIN_1 | PRESCALER_4);
70 i2cWriteReg (REG_CTL, CTL_DAT_INIITIATE);
74 TCS3414CS::~TCS3414CS () {
75 mraa_i2c_stop(m_i2Ctx);
79 TCS3414CS::readRGB (tcs3414sc_rgb_t * rgb) {
82 // We need 7 bytes of data.
83 if (i2cReadReg_N (REG_BLOCK_READ, 8, buffer) > 7) {
84 rgb->g = buffer[1] * 256 + buffer[0];
85 rgb->r = buffer[3] * 256 + buffer[2];
86 rgb->b = buffer[5] * 256 + buffer[4];
87 rgb->clr = buffer[7] * 256 + buffer[6];
92 TCS3414CS::clearInterrupt () {
93 mraa_result_t error = MRAA_SUCCESS;
95 if (m_i2Ctx == NULL) {
96 throw TCS3414CSException ("Couldn't find initilized I2C.");
99 error = mraa_i2c_address (m_i2Ctx, ADDR);
100 error = mraa_i2c_write_byte (m_i2Ctx, CLR_INT);
102 if (error != MRAA_SUCCESS) {
103 throw TCS3414CSException ("Couldn't clear interrupt.");
113 TCS3414CS::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) {
116 if (m_i2Ctx == NULL) {
117 throw TCS3414CSException ("Couldn't find initilized I2C.");
120 mraa_i2c_address(m_i2Ctx, ADDR);
121 mraa_i2c_write_byte(m_i2Ctx, reg);
123 mraa_i2c_address(m_i2Ctx, ADDR);
124 readByte = mraa_i2c_read(m_i2Ctx, buffer, len);
129 TCS3414CS::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) {
130 mraa_result_t error = MRAA_SUCCESS;
132 if (m_i2Ctx == NULL) {
133 throw TCS3414CSException ("Couldn't find initilized I2C.");
136 error = mraa_i2c_address (m_i2Ctx, ADDR);
137 error = mraa_i2c_write_byte (m_i2Ctx, reg);
138 error = mraa_i2c_write (m_i2Ctx, buffer, len);
144 TCS3414CS::i2cWriteReg (uint8_t reg, uint8_t data) {
145 mraa_result_t error = MRAA_SUCCESS;
147 if (m_i2Ctx == NULL) {
148 throw TCS3414CSException ("Couldn't find initilized I2C.");
151 error = mraa_i2c_address (m_i2Ctx, ADDR);
152 error = mraa_i2c_write_byte (m_i2Ctx, reg);
153 error = mraa_i2c_write_byte (m_i2Ctx, data);