|
@@ -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
|
+}
|