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.hpp>
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
98namespace upm {
99
100typedef 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 TCS3414CS Color Sensor library
109 * @defgroup tcs3414cs libupm-tcs3414cs
110 * @ingroup seeed i2c color
111 */
112/**
113 * @library tcs3414cs
114 * @sensor tcs3414cs
115 * @comname TCS3414CS Color Sensor
116 * @altname Grove Color Sensor
117 * @type color
118 * @man seeed
119 * @web http://www.seeedstudio.com/wiki/Grove_-_I2C_Color_Sensor
120 * @con i2c
121 *
122 * @brief API for the TCS3414CS Color Sensor
123 *
124 * This module defines the TCS3414CS interface for the color sensor
125 *
126 * @image html tcs3414cs.jpg
127 * @snippet tcs3414cs.cxx Interesting
128 */
129class TCS3414CS {
130    public:
131        /**
132         * Instantiates a TCS3414CS object
133         *
134         * @param bus Number of the used bus
135         */
136        TCS3414CS ();
137
138        /**
139         * Gets the RGB value from the sensor.
140         *
141         * @param rgb Color values
142         */
143        void readRGB (tcs3414sc_rgb_t * rgb);
144
145        /**
146         * Clears interrupts.
147         */
148        void clearInterrupt ();
149
150        /**
151         * Returns the name of the component
152         */
153        std::string name()
154        {
155            return m_name;
156        }
157    private:
158        std::string m_name;
159        mraa::I2c m_i2Ctx;
160
161        uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
162        mraa::Result i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer);
163        mraa::Result i2cWriteReg (uint8_t reg, uint8_t data);
164};
165
166}
167