From 50fd1d747833c5303f9f27ff140b2de8f8de282c Mon Sep 17 00:00:00 2001 From: Kiveisha Yevgeniy Date: Wed, 27 Aug 2014 12:40:33 +0000 Subject: [PATCH] tcs3414cs:: added new color sensor Signed-off-by: Kiveisha Yevgeniy --- examples/CMakeLists.txt | 3 + examples/tcs3414cs-example.cxx | 62 ++++++++++++++++ src/tcs3414cs/.DS_Store | Bin 0 -> 6148 bytes src/tcs3414cs/._.DS_Store | Bin 0 -> 4096 bytes src/tcs3414cs/CMakeLists.txt | 5 ++ src/tcs3414cs/jsupm_tcs3414cs.i | 8 +++ src/tcs3414cs/pyupm_tcs3414cs.i | 9 +++ src/tcs3414cs/tcs3414cs.cxx | 156 ++++++++++++++++++++++++++++++++++++++++ src/tcs3414cs/tcs3414cs.h | 155 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 398 insertions(+) create mode 100644 examples/tcs3414cs-example.cxx create mode 100644 src/tcs3414cs/.DS_Store create mode 100644 src/tcs3414cs/._.DS_Store create mode 100644 src/tcs3414cs/CMakeLists.txt create mode 100644 src/tcs3414cs/jsupm_tcs3414cs.i create mode 100644 src/tcs3414cs/pyupm_tcs3414cs.i create mode 100644 src/tcs3414cs/tcs3414cs.cxx create mode 100644 src/tcs3414cs/tcs3414cs.h diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 97ac0a6..a002c81 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -33,6 +33,7 @@ add_executable (mq2-example mq2-example.cxx) add_executable (mq3-example mq3-example.cxx) add_executable (mq5-example mq5-example.cxx) add_executable (mq9-example mq9-example.cxx) +add_executable (tcs3414cs-example tcs3414cs-example.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -60,6 +61,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806) include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614) include_directories (${PROJECT_SOURCE_DIR}/src/ecs1030) include_directories (${PROJECT_SOURCE_DIR}/src/gas) +include_directories (${PROJECT_SOURCE_DIR}/src/tcs3414cs) target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT}) @@ -96,3 +98,4 @@ target_link_libraries (mq2-example gas ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mq3-example gas ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mq5-example gas ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/tcs3414cs-example.cxx b/examples/tcs3414cs-example.cxx new file mode 100644 index 0000000..c3bcd64 --- /dev/null +++ b/examples/tcs3414cs-example.cxx @@ -0,0 +1,62 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include "tcs3414cs.h" +#include + +int doWork = 0; +upm::TCS3414CS *sensor = NULL; + +void +sig_handler(int signo) +{ + printf("got signal\n"); + if (signo == SIGINT) { + printf("exiting application\n"); + doWork = 1; + } +} + +int +main(int argc, char **argv) +{ + //! [Interesting] + upm::tcs3414sc_rgb_t rgb; + sensor = new upm::TCS3414CS (); + + while (!doWork) { + sensor->readRGB (&rgb); + std::cout << (int)rgb.r << ", " << (int)rgb.g << ", " << (int)rgb.b << ", " << rgb.clr << std::endl; + usleep (500000); + } + //! [Interesting] + + std::cout << "exiting application" << std::endl; + + delete sensor; + + return 0; +} diff --git a/src/tcs3414cs/.DS_Store b/src/tcs3414cs/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b152328eb65e4bf2d39f7070fac45405aa5aaff1 GIT binary patch literal 6148 zcmeHK%}T>S5dNlCs3@iM6!U7!V^L07CDF#KS8U46@Hmf0stnBxK0nBol!y4x-& zpnKQEIc_k-GdB0H!(CiqaW$KdN3;2e`7!FAzdUOR`fuaI?vD?!6e^Gt@ zFOt0H3b+FQN&%_&@A^G1$!hD?;{shDQtvN%&}UEkLkgfPqaZy4YP>M Q(ELXr$l$>h_)`VG0UsAp>i_@% literal 0 HcmV?d00001 diff --git a/src/tcs3414cs/._.DS_Store b/src/tcs3414cs/._.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d4698e3f153d04a0480aefc0ac1c59ef5a5ad816 GIT binary patch literal 4096 zcmZQz6=P>$Vqox1Ojhs@R)|o50+1L3ClDJkFz{^v(m+1nBL)UWIk*Y|peR=07!nc$ zlxT$LV5q>VXjE`C1V%$(Gz3ONU^E0qLtr!nMnhmU1V%$(Gz3ONU^E1%90H6$^FSC3 z + * Copyright (c) 2014 Intel Corporation. + * + * Credits to Seeed Studeo. + * Based on Seeed Studeo code example, + * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_I2C_Color_Sensor_v0.9b. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include + +#include "tcs3414cs.h" + +using namespace upm; + +struct TCS3414CSException : public std::exception { + std::string message; + TCS3414CSException (std::string msg) : message (msg) { } + ~TCS3414CSException () throw () { } + const char* what() const throw () { return message.c_str(); } +}; + +TCS3414CS::TCS3414CS () { + m_name = "TCS3414CS"; + m_i2Ctx = mraa_i2c_init(0); + + mraa_result_t ret = mraa_i2c_address(m_i2Ctx, ADDR); + if (ret != MRAA_SUCCESS) { + throw TCS3414CSException ("Couldn't initilize I2C."); + } + + // Set timing register + i2cWriteReg (REG_TIMING, INTEG_MODE_FREE); + usleep (100000); + + // Set interrupt source register + i2cWriteReg (REG_INT_SOURCE, INT_SOURCE_GREEN); + usleep (100000); + + // Set interrupt control register + i2cWriteReg (REG_INT, INTR_LEVEL | INTR_PERSIST_EVERY); + usleep (100000); + + // Set gain + i2cWriteReg (REG_GAIN, GAIN_1 | PRESCALER_4); + usleep (100000); + + // Enable ADC + i2cWriteReg (REG_CTL, CTL_DAT_INIITIATE); + usleep (100000); +} + +TCS3414CS::~TCS3414CS () { + mraa_i2c_stop(m_i2Ctx); +} + +void +TCS3414CS::readRGB (tcs3414sc_rgb_t * rgb) { + uint8_t buffer[8]; + + // We need 7 bytes of data. + if (i2cReadReg_N (REG_BLOCK_READ, 8, buffer) > 7) { + rgb->g = buffer[1] * 256 + buffer[0]; + rgb->r = buffer[3] * 256 + buffer[2]; + rgb->b = buffer[5] * 256 + buffer[4]; + rgb->clr = buffer[7] * 256 + buffer[6]; + } +} + +void +TCS3414CS::clearInterrupt () { + mraa_result_t error = MRAA_SUCCESS; + + if (m_i2Ctx == NULL) { + throw TCS3414CSException ("Couldn't find initilized I2C."); + } + + error = mraa_i2c_address (m_i2Ctx, ADDR); + error = mraa_i2c_write_byte (m_i2Ctx, CLR_INT); + + if (error != MRAA_SUCCESS) { + throw TCS3414CSException ("Couldn't clear interrupt."); + } +} + +/* + * ************** + * private area + * ************** + */ +uint16_t +TCS3414CS::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) { + int readByte = 0; + + if (m_i2Ctx == NULL) { + throw TCS3414CSException ("Couldn't find initilized I2C."); + } + + mraa_i2c_address(m_i2Ctx, ADDR); + mraa_i2c_write_byte(m_i2Ctx, reg); + + mraa_i2c_address(m_i2Ctx, ADDR); + readByte = mraa_i2c_read(m_i2Ctx, buffer, len); + return readByte; +} + +mraa_result_t +TCS3414CS::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) { + mraa_result_t error = MRAA_SUCCESS; + + if (m_i2Ctx == NULL) { + throw TCS3414CSException ("Couldn't find initilized I2C."); + } + + error = mraa_i2c_address (m_i2Ctx, ADDR); + error = mraa_i2c_write_byte (m_i2Ctx, reg); + error = mraa_i2c_write (m_i2Ctx, buffer, len); + + return error; +} + +mraa_result_t +TCS3414CS::i2cWriteReg (uint8_t reg, uint8_t data) { + mraa_result_t error = MRAA_SUCCESS; + + if (m_i2Ctx == NULL) { + throw TCS3414CSException ("Couldn't find initilized I2C."); + } + + error = mraa_i2c_address (m_i2Ctx, ADDR); + error = mraa_i2c_write_byte (m_i2Ctx, reg); + error = mraa_i2c_write_byte (m_i2Ctx, data); + + return error; +} diff --git a/src/tcs3414cs/tcs3414cs.h b/src/tcs3414cs/tcs3414cs.h new file mode 100644 index 0000000..933a9ff --- /dev/null +++ b/src/tcs3414cs/tcs3414cs.h @@ -0,0 +1,155 @@ +/* + * Author: Yevgeniy Kiveisha + * Copyright (c) 2014 Intel Corporation. + * + * Credits to Seeed Studeo. + * Based on Seeed Studeo code example, + * http://www.seeedstudio.com/wiki/index.php?title=Twig_-_I2C_Color_Sensor_v0.9b. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +#pragma once + +#include +#include + +#define ADDR 0x39 // device address + +#define REG_CTL 0x80 +#define REG_TIMING 0x81 +#define REG_INT 0x82 +#define REG_INT_SOURCE 0x83 +#define REG_ID 0x84 +#define REG_GAIN 0x87 +#define REG_LOW_THRESH_LOW_BYTE 0x88 +#define REG_LOW_THRESH_HIGH_BYTE 0x89 +#define REG_HIGH_THRESH_LOW_BYTE 0x8A +#define REG_HIGH_THRESH_HIGH_BYTE 0x8B +#define REG_BLOCK_READ 0xCF +#define REG_GREEN_LOW 0xD0 +#define REG_GREEN_HIGH 0xD1 +#define REG_RED_LOW 0xD2 +#define REG_RED_HIGH 0xD3 +#define REG_BLUE_LOW 0xD4 +#define REG_BLUE_HIGH 0xD5 +#define REG_CLEAR_LOW 0xD6 +#define REG_CLEAR_HIGH 0xD7 +#define CTL_DAT_INIITIATE 0x03 +#define CLR_INT 0xE0 + +/* Timing Register */ +#define SYNC_EDGE 0x40 +#define INTEG_MODE_FREE 0x00 +#define INTEG_MODE_MANUAL 0x10 +#define INTEG_MODE_SYN_SINGLE 0x20 +#define INTEG_MODE_SYN_MULTI 0x30 + +#define INTEG_PARAM_PULSE_COUNT1 0x00 +#define INTEG_PARAM_PULSE_COUNT2 0x01 +#define INTEG_PARAM_PULSE_COUNT4 0x02 +#define INTEG_PARAM_PULSE_COUNT8 0x03 + +/* Interrupt Control Register */ +#define INTR_STOP 40 +#define INTR_DISABLE 0x00 +#define INTR_LEVEL 0x10 +#define INTR_PERSIST_EVERY 0x00 +#define INTR_PERSIST_SINGLE 0x01 + +/* Interrupt Souce Register */ +#define INT_SOURCE_GREEN 0x00 +#define INT_SOURCE_RED 0x01 +#define INT_SOURCE_BLUE 0x10 +#define INT_SOURCE_CLEAR 0x03 + +/* Gain Register */ +#define GAIN_1 0x00 +#define GAIN_4 0x10 +#define GAIN_16 0x20 +#define GANI_64 0x30 +#define PRESCALER_1 0x00 +#define PRESCALER_2 0x01 +#define PRESCALER_4 0x02 +#define PRESCALER_8 0x03 +#define PRESCALER_16 0x04 +#define PRESCALER_32 0x05 +#define PRESCALER_64 0x06 + +#define HIGH 1 +#define LOW 0 + +namespace upm { + +typedef struct { + uint16_t r; + uint16_t g; + uint16_t b; + uint16_t clr; +} tcs3414sc_rgb_t; + +/** + * @brief C++ API for TCS3414CS chip (Color sensor) + * + * This file defines the TCS3414CS C++ interface for libtcs3414cs + * + */ +class TCS3414CS { + public: + /** + * Instanciates a TCS3414CS object + * + * @param bus number of used bus + */ + TCS3414CS (); + + /** + * TCS3414CS object destructor, basicaly it close i2c connection. + */ + ~TCS3414CS (); + + /** + * Get the RGB value from sensor. + * + * @param rgb color values + */ + void readRGB (tcs3414sc_rgb_t * rgb); + + /** + * Clear interrupts. + */ + void clearInterrupt (); + + /** + * Return name of the component + */ + std::string name() + { + return m_name; + } + private: + std::string m_name; + mraa_i2c_context m_i2Ctx; + + uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer); + mraa_result_t i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer); + mraa_result_t i2cWriteReg (uint8_t reg, uint8_t data); +}; + +} -- 2.7.4