Browse Source

tcs3414cs:: added new color sensor

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
50fd1d7478

+ 3
- 0
examples/CMakeLists.txt View File

@@ -33,6 +33,7 @@ add_executable (mq2-example mq2-example.cxx)
33 33
 add_executable (mq3-example mq3-example.cxx)
34 34
 add_executable (mq5-example mq5-example.cxx)
35 35
 add_executable (mq9-example mq9-example.cxx)
36
+add_executable (tcs3414cs-example tcs3414cs-example.cxx)
36 37
 
37 38
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
38 39
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -60,6 +61,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806)
60 61
 include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614)
61 62
 include_directories (${PROJECT_SOURCE_DIR}/src/ecs1030)
62 63
 include_directories (${PROJECT_SOURCE_DIR}/src/gas)
64
+include_directories (${PROJECT_SOURCE_DIR}/src/tcs3414cs)
63 65
 
64 66
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
65 67
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -96,3 +98,4 @@ target_link_libraries (mq2-example gas ${CMAKE_THREAD_LIBS_INIT})
96 98
 target_link_libraries (mq3-example gas ${CMAKE_THREAD_LIBS_INIT})
97 99
 target_link_libraries (mq5-example gas ${CMAKE_THREAD_LIBS_INIT})
98 100
 target_link_libraries (mq9-example gas ${CMAKE_THREAD_LIBS_INIT})
101
+target_link_libraries (tcs3414cs-example tcs3414cs ${CMAKE_THREAD_LIBS_INIT})

+ 62
- 0
examples/tcs3414cs-example.cxx View File

@@ -0,0 +1,62 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include <unistd.h>
26
+#include <iostream>
27
+#include "tcs3414cs.h"
28
+#include <signal.h>
29
+
30
+int doWork = 0;
31
+upm::TCS3414CS *sensor = NULL;
32
+
33
+void
34
+sig_handler(int signo)
35
+{
36
+    printf("got signal\n");
37
+    if (signo == SIGINT) {
38
+        printf("exiting application\n");
39
+        doWork = 1;
40
+    }
41
+}
42
+
43
+int
44
+main(int argc, char **argv)
45
+{
46
+    //! [Interesting]
47
+    upm::tcs3414sc_rgb_t rgb;
48
+    sensor = new upm::TCS3414CS ();
49
+
50
+    while (!doWork) {
51
+        sensor->readRGB (&rgb);
52
+        std::cout << (int)rgb.r << ", " << (int)rgb.g << ", " << (int)rgb.b << ", " << rgb.clr << std::endl;
53
+        usleep (500000);
54
+    }
55
+    //! [Interesting]
56
+
57
+    std::cout << "exiting application" << std::endl;
58
+
59
+    delete sensor;
60
+
61
+    return 0;
62
+}

BIN
src/tcs3414cs/.DS_Store View File


BIN
src/tcs3414cs/._.DS_Store View File


+ 5
- 0
src/tcs3414cs/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+set (libname "tcs3414cs")
2
+set (libdescription "I2C Color sensor")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/tcs3414cs/jsupm_tcs3414cs.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_tcs3414cs
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "tcs3414cs.h"
6
+%}
7
+
8
+%include "tcs3414cs.h"

+ 9
- 0
src/tcs3414cs/pyupm_tcs3414cs.i View File

@@ -0,0 +1,9 @@
1
+%module pyupm_tcs3414cs
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "tcs3414cs.h"
7
+%{
8
+    #include "tcs3414cs.h"
9
+%}

+ 156
- 0
src/tcs3414cs/tcs3414cs.cxx View File

@@ -0,0 +1,156 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
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.
8
+ *
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:
16
+ *
17
+ * The above copyright notice and this permission notice shall be
18
+ * included in all copies or substantial portions of the Software.
19
+ *
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.
27
+ */
28
+
29
+#include <iostream>
30
+#include <unistd.h>
31
+#include <stdlib.h>
32
+
33
+#include "tcs3414cs.h"
34
+
35
+using namespace upm;
36
+
37
+struct TCS3414CSException : public std::exception {
38
+    std::string message;
39
+    TCS3414CSException (std::string msg) : message (msg) { }
40
+    ~TCS3414CSException () throw () { }
41
+    const char* what() const throw () { return message.c_str(); }
42
+};
43
+
44
+TCS3414CS::TCS3414CS () {
45
+    m_name = "TCS3414CS";
46
+    m_i2Ctx = mraa_i2c_init(0);
47
+
48
+    mraa_result_t ret = mraa_i2c_address(m_i2Ctx, ADDR);
49
+    if (ret != MRAA_SUCCESS) {
50
+        throw TCS3414CSException ("Couldn't initilize I2C.");
51
+    }
52
+
53
+    // Set timing register
54
+    i2cWriteReg (REG_TIMING, INTEG_MODE_FREE);
55
+    usleep (100000);
56
+
57
+    // Set interrupt source register
58
+    i2cWriteReg (REG_INT_SOURCE, INT_SOURCE_GREEN);
59
+    usleep (100000);
60
+
61
+    // Set interrupt control register
62
+    i2cWriteReg (REG_INT, INTR_LEVEL | INTR_PERSIST_EVERY);
63
+    usleep (100000);
64
+
65
+    // Set gain
66
+    i2cWriteReg (REG_GAIN, GAIN_1 | PRESCALER_4);
67
+    usleep (100000);
68
+
69
+    // Enable ADC
70
+    i2cWriteReg (REG_CTL, CTL_DAT_INIITIATE);
71
+    usleep (100000);
72
+}
73
+
74
+TCS3414CS::~TCS3414CS () {
75
+    mraa_i2c_stop(m_i2Ctx);
76
+}
77
+
78
+void
79
+TCS3414CS::readRGB (tcs3414sc_rgb_t * rgb) {
80
+    uint8_t buffer[8];
81
+
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];
88
+    }
89
+}
90
+
91
+void
92
+TCS3414CS::clearInterrupt () {
93
+    mraa_result_t error = MRAA_SUCCESS;
94
+
95
+    if (m_i2Ctx == NULL) {
96
+        throw TCS3414CSException ("Couldn't find initilized I2C.");
97
+    }
98
+
99
+    error = mraa_i2c_address (m_i2Ctx, ADDR);
100
+    error = mraa_i2c_write_byte (m_i2Ctx, CLR_INT);
101
+
102
+    if (error != MRAA_SUCCESS) {
103
+        throw TCS3414CSException ("Couldn't clear interrupt.");
104
+    }
105
+}
106
+
107
+/*
108
+ * **************
109
+ *  private area
110
+ * **************
111
+ */
112
+uint16_t
113
+TCS3414CS::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) {
114
+    int readByte = 0;
115
+
116
+    if (m_i2Ctx == NULL) {
117
+        throw TCS3414CSException ("Couldn't find initilized I2C.");
118
+    }
119
+
120
+    mraa_i2c_address(m_i2Ctx, ADDR);
121
+    mraa_i2c_write_byte(m_i2Ctx, reg);
122
+
123
+    mraa_i2c_address(m_i2Ctx, ADDR);
124
+    readByte = mraa_i2c_read(m_i2Ctx, buffer, len);
125
+    return readByte;
126
+}
127
+
128
+mraa_result_t
129
+TCS3414CS::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) {
130
+    mraa_result_t error = MRAA_SUCCESS;
131
+
132
+    if (m_i2Ctx == NULL) {
133
+        throw TCS3414CSException ("Couldn't find initilized I2C.");
134
+    }
135
+
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);
139
+
140
+    return error;
141
+}
142
+
143
+mraa_result_t
144
+TCS3414CS::i2cWriteReg (uint8_t reg, uint8_t data) {
145
+    mraa_result_t error = MRAA_SUCCESS;
146
+
147
+    if (m_i2Ctx == NULL) {
148
+        throw TCS3414CSException ("Couldn't find initilized I2C.");
149
+    }
150
+
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);
154
+
155
+    return error;
156
+}

+ 155
- 0
src/tcs3414cs/tcs3414cs.h View File

@@ -0,0 +1,155 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
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.
8
+ *
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:
16
+ *
17
+ * The above copyright notice and this permission notice shall be
18
+ * included in all copies or substantial portions of the Software.
19
+ *
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.
27
+ */
28
+#pragma once
29
+
30
+#include <string>
31
+#include <mraa/i2c.h>
32
+
33
+#define ADDR                        0x39 // device address
34
+
35
+#define REG_CTL                     0x80
36
+#define REG_TIMING                  0x81
37
+#define REG_INT                     0x82
38
+#define REG_INT_SOURCE              0x83
39
+#define REG_ID                      0x84
40
+#define REG_GAIN                    0x87
41
+#define REG_LOW_THRESH_LOW_BYTE     0x88
42
+#define REG_LOW_THRESH_HIGH_BYTE    0x89
43
+#define REG_HIGH_THRESH_LOW_BYTE    0x8A
44
+#define REG_HIGH_THRESH_HIGH_BYTE   0x8B
45
+#define REG_BLOCK_READ              0xCF
46
+#define REG_GREEN_LOW               0xD0
47
+#define REG_GREEN_HIGH              0xD1
48
+#define REG_RED_LOW                 0xD2
49
+#define REG_RED_HIGH                0xD3
50
+#define REG_BLUE_LOW                0xD4
51
+#define REG_BLUE_HIGH               0xD5
52
+#define REG_CLEAR_LOW               0xD6
53
+#define REG_CLEAR_HIGH              0xD7
54
+#define CTL_DAT_INIITIATE           0x03
55
+#define CLR_INT                     0xE0
56
+
57
+/* Timing Register */
58
+#define SYNC_EDGE                   0x40
59
+#define INTEG_MODE_FREE             0x00
60
+#define INTEG_MODE_MANUAL           0x10
61
+#define INTEG_MODE_SYN_SINGLE       0x20
62
+#define INTEG_MODE_SYN_MULTI        0x30
63
+
64
+#define INTEG_PARAM_PULSE_COUNT1    0x00
65
+#define INTEG_PARAM_PULSE_COUNT2    0x01
66
+#define INTEG_PARAM_PULSE_COUNT4    0x02
67
+#define INTEG_PARAM_PULSE_COUNT8    0x03
68
+
69
+/* Interrupt Control Register */
70
+#define INTR_STOP                   40
71
+#define INTR_DISABLE                0x00
72
+#define INTR_LEVEL                  0x10
73
+#define INTR_PERSIST_EVERY          0x00
74
+#define INTR_PERSIST_SINGLE         0x01
75
+
76
+/* Interrupt Souce Register */
77
+#define INT_SOURCE_GREEN            0x00
78
+#define INT_SOURCE_RED              0x01
79
+#define INT_SOURCE_BLUE             0x10
80
+#define INT_SOURCE_CLEAR            0x03
81
+
82
+/* Gain Register */
83
+#define GAIN_1                      0x00
84
+#define GAIN_4                      0x10
85
+#define GAIN_16                     0x20
86
+#define GANI_64                     0x30
87
+#define PRESCALER_1                 0x00
88
+#define PRESCALER_2                 0x01
89
+#define PRESCALER_4                 0x02
90
+#define PRESCALER_8                 0x03
91
+#define PRESCALER_16                0x04
92
+#define PRESCALER_32                0x05
93
+#define PRESCALER_64                0x06
94
+
95
+#define HIGH                        1
96
+#define LOW                         0
97
+
98
+namespace upm {
99
+
100
+typedef struct {
101
+    uint16_t r;
102
+    uint16_t g;
103
+    uint16_t b;
104
+    uint16_t clr;
105
+} tcs3414sc_rgb_t;
106
+
107
+/**
108
+ * @brief C++ API for TCS3414CS chip (Color sensor)
109
+ *
110
+ * This file defines the TCS3414CS C++ interface for libtcs3414cs
111
+ *
112
+ */
113
+class TCS3414CS {
114
+    public:
115
+        /**
116
+         * Instanciates a TCS3414CS object
117
+         *
118
+         * @param bus number of used bus
119
+         */
120
+        TCS3414CS ();
121
+
122
+        /**
123
+         * TCS3414CS object destructor, basicaly it close i2c connection.
124
+         */
125
+        ~TCS3414CS ();
126
+
127
+        /**
128
+         * Get the RGB value from sensor.
129
+         *
130
+         * @param rgb color values
131
+         */
132
+        void readRGB (tcs3414sc_rgb_t * rgb);
133
+
134
+        /**
135
+         * Clear interrupts.
136
+         */
137
+        void clearInterrupt ();
138
+
139
+        /**
140
+         * Return name of the component
141
+         */
142
+        std::string name()
143
+        {
144
+            return m_name;
145
+        }
146
+    private:
147
+        std::string m_name;
148
+        mraa_i2c_context m_i2Ctx;
149
+
150
+        uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
151
+        mraa_result_t i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer);
152
+        mraa_result_t i2cWriteReg (uint8_t reg, uint8_t data);
153
+};
154
+
155
+}