mirror of
				https://github.com/eclipse/upm.git
				synced 2025-10-29 14:15:33 +03:00 
			
		
		
		
	tcs3414cs:: added new color sensor
Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								src/tcs3414cs/.DS_Store
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/tcs3414cs/.DS_Store
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/tcs3414cs/._.DS_Store
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/tcs3414cs/._.DS_Store
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										5
									
								
								src/tcs3414cs/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/tcs3414cs/CMakeLists.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,5 @@ | ||||
| set (libname "tcs3414cs") | ||||
| set (libdescription "I2C Color sensor") | ||||
| set (module_src ${libname}.cxx) | ||||
| set (module_h ${libname}.h) | ||||
| upm_module_init() | ||||
							
								
								
									
										8
									
								
								src/tcs3414cs/jsupm_tcs3414cs.i
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								src/tcs3414cs/jsupm_tcs3414cs.i
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| %module jsupm_tcs3414cs | ||||
| %include "../upm.i" | ||||
|  | ||||
| %{ | ||||
|     #include "tcs3414cs.h" | ||||
| %} | ||||
|  | ||||
| %include "tcs3414cs.h" | ||||
							
								
								
									
										9
									
								
								src/tcs3414cs/pyupm_tcs3414cs.i
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/tcs3414cs/pyupm_tcs3414cs.i
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| %module pyupm_tcs3414cs | ||||
| %include "../upm.i" | ||||
|  | ||||
| %feature("autodoc", "3"); | ||||
|  | ||||
| %include "tcs3414cs.h" | ||||
| %{ | ||||
|     #include "tcs3414cs.h" | ||||
| %} | ||||
							
								
								
									
										156
									
								
								src/tcs3414cs/tcs3414cs.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										156
									
								
								src/tcs3414cs/tcs3414cs.cxx
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,156 @@ | ||||
| /* | ||||
|  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> | ||||
|  * 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 <iostream> | ||||
| #include <unistd.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #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; | ||||
| } | ||||
							
								
								
									
										155
									
								
								src/tcs3414cs/tcs3414cs.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										155
									
								
								src/tcs3414cs/tcs3414cs.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,155 @@ | ||||
| /* | ||||
|  * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com> | ||||
|  * 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 <string> | ||||
| #include <mraa/i2c.h> | ||||
|  | ||||
| #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); | ||||
| }; | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 Kiveisha Yevgeniy
					Kiveisha Yevgeniy