upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
tcs37727.hpp
1 /*
2  * Author: Norbert Wesp <nwesp@phytec.de>
3  * Copyright (c) 2017 Phytec Messtechnik GmbH.
4  *
5  * based on: RIOT-driver tcs37727 by Johann Fischer <j.fischer@phytec.de>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #pragma once
28 
29 #include <string>
30 #include <mraa/i2c.hpp>
31 #include <stdint.h>
32 #include <stdbool.h>
33 
34 #define TCS37727_NAME "TCS37727"
35 #define TCS37727_I2C_ADDRESS 0x29
36 #define TCS37727_DEVICE_ID 0x49
37 #define TCS37727_DEVICE_ID_REG 0x92
38 
39 #define TCS37727_ATIME_DEFAULT 200000 /* Default RGBC integration time */
40 #define TCS37727_AG_THRESHOLD_LOW 200
41 #define TCS37727_AG_THRESHOLD_HIGH (65535 - TCS37727_AG_THRESHOLD_LOW)
42 
43 
44 /* TCS37727 Register Map */
45 #define TCS37727_ENABLE 0x80 /* Enables states and interrupts */
46 #define TCS37727_ATIME 0x81 /* RGBC time */
47 #define TCS37727_PTIME 0x82 /* Proximity time */
48 #define TCS37727_WTIME 0x83 /* Wait time */
49 #define TCS37727_AILTL 0x04 /* Clear interrupt low threshold low byte */
50 #define TCS37727_AILTH 0x05 /* Clear interrupt low threshold high byte */
51 #define TCS37727_AIHTL 0x06 /* Clear interrupt high threshold low byte */
52 #define TCS37727_AIHTH 0x07 /* Clear interrupt high threshold high byte */
53 #define TCS37727_PILTL 0x08 /* Proximity inter. low threshold low byte */
54 #define TCS37727_PILTH 0x09 /* Proximity inter. low threshold high byte */
55 #define TCS37727_PIHTL 0x0A /* Proximity inter. high threshold low byte */
56 #define TCS37727_PIHTH 0x0B /* Proximity inter. high threshold high byte */
57 #define TCS37727_PERS 0x8C /* Interrupt persistence filters */
58 #define TCS37727_CONFIG 0x8D /* Configuration */
59 #define TCS37727_PPULSE 0x8E /* Proximity pulse count */
60 #define TCS37727_CONTROL 0x8F /* Gain control register */
61 #define TCS37727_STATUS 0x93 /* Device status */
62 #define TCS37727_CDATA 0x14 /* Clear ADC data low byte */
63 #define TCS37727_CDATAH 0x15 /* Clear ADC data high byte */
64 #define TCS37727_RDATA 0x16 /* Red ADC data low byte */
65 #define TCS37727_RDATAH 0x17 /* Red ADC data high byte */
66 #define TCS37727_GDATA 0x18 /* Green ADC data low byte */
67 #define TCS37727_GDATAH 0x19 /* Green ADC data high byte */
68 #define TCS37727_BDATA 0x1A /* Blue ADC data low byte */
69 #define TCS37727_BDATAH 0x1B /* Blue ADC data high byte */
70 #define TCS37727_PDATA 0x1C /* Proximity ADC data low byte */
71 #define TCS37727_PDATAH 0x1D /* Proximity ADC data high byte */
72 
73 /* TCS37727 Command Register */
74 #define TCS37727_BYTE_TRANS 0x80 /* Repeated byte protocol transaction */
75 #define TCS37727_INC_TRANS 0xA0 /* Auto-increment protocol transaction */
76 #define TCS37727_SF_PICLR 0xE5 /* Proximity interrupt clear */
77 #define TCS37727_SF_CICLR 0xE6 /* Clear channel interrupt clear */
78 #define TCS37727_SF_PCICLR 0xE7 /* Proximity & Clear channel inter. clear */
79 
80 /* TCS37727 Enable Register */
81 #define TCS37727_ENABLE_PIEN (1 << 5) /* Proximity interrupt enable */
82 #define TCS37727_ENABLE_AIEN (1 << 4) /* Clear channel interrupt enable */
83 #define TCS37727_ENABLE_WEN (1 << 3) /* Wait enable */
84 #define TCS37727_ENABLE_PEN (1 << 2) /* Proximity enable */
85 #define TCS37727_ENABLE_AEN (1 << 1) /* RGBC enable, actives 2-channel ADC */
86 #define TCS37727_ENABLE_PON (1 << 0) /* Power ON */
87 
88 /* TCS37727 Control Register */
89 #define TCS37727_CONTROL_PDRIVE_100 0x00 /* 100 mA LED Drive Strength */
90 #define TCS37727_CONTROL_PDRIVE_50 0x04 /* 50 mA LED Drive Strength */
91 #define TCS37727_CONTROL_PDRIVE_25 0x08 /* 25 mA LED Drive Strength */
92 #define TCS37727_CONTROL_PDRIVE_12 0x0C /* 12.5 mA LED Drive Strength */
93 #define TCS37727_CONTROL_PDRIVE_MASK 0x0C /* PDRIVE Mask */
94 #define TCS37727_CONTROL_AGAIN_1 0x00 /* 1 x gain RGBC Gain Value */
95 #define TCS37727_CONTROL_AGAIN_4 0x01 /* 4 x gain RGBC Gain Value */
96 #define TCS37727_CONTROL_AGAIN_16 0x02 /* 16 x gain RGBC Gain Value */
97 #define TCS37727_CONTROL_AGAIN_60 0x03 /* 60 x gain RGBC Gain Value */
98 #define TCS37727_CONTROL_AGAIN_MASK 0x03 /* AGAIN Mask */
99 
100 /* TCS37727 ATIME register values */
101 #define TCS37727_ATIME_MIN 2400 /* 2.4ms integr. time, max count 1024 */
102 #define TCS37727_ATIME_MAX 614000 /* 614ms integr. time, max count 0xffff */
103 
104 #define TCS37727_ATIME_TO_REG(val) (256 - (uint8_t)((val) / 2400))
105 #define TCS37727_ATIME_TO_US(reg) ((256 - (uint8_t)(reg)) * 2400)
106 
107 /* Coefficients for Lux and CT Equations (DN40) */
108 /* Coefficients in integer format, multiplied by 1000 */
109 #define DGF_IF 310
110 #define R_COEF_IF 136
111 #define G_COEF_IF 1000
112 #define B_COEF_IF -444
113 #define CT_COEF_IF 3810
114 #define CT_OFFSET_IF 1391
115 
116 namespace upm {
117 
118 typedef struct {
119  uint32_t red;
120  uint32_t green;
121  uint32_t blue;
122  uint32_t clear;
123  uint32_t lux;
124  uint32_t ct;
126 
158 class TCS37727 {
159  public:
169  TCS37727 (int bus, int atime_us=TCS37727_ATIME_DEFAULT,
170  int devAddr=TCS37727_I2C_ADDRESS);
171 
178  int checkID(void);
179 
186  int setActive(void);
187 
195  int setStandby(void);
196 
200  uint8_t trimGain(int rawc);
201 
209  int sampleData(void);
210 
218  uint32_t getChannelRed(int bSampleData = 0);
219 
227  uint32_t getChannelGreen(int bSampleData = 0);
228 
236  uint32_t getChannelBlue(int bSampleData = 0);
237 
245  uint32_t getChannelClear(int bSampleData = 0);
246 
254  uint32_t getLux(int bSampleData = 0);
255 
263  uint32_t getColorTemperature(int bSampleData = 0);
264 
273  int getData(tcs37727_data_t* data, int bSampleData = 0);
274 
275  private:
276 
277  std::string m_name;
278 
279  int m_controlAddr;
280  int m_bus;
281  mraa::I2c m_i2ControlCtx;
282 
283  int s_atime_us; /* atime value conveted to microseconds */
284  int s_again; /* amount of gain */
285 
286  tcs37727_data_t s_data[1];
287  };
288 
289 }
int setStandby(void)
Definition: tcs37727.cpp:114
uint32_t getColorTemperature(int bSampleData=0)
Definition: tcs37727.cpp:319
uint32_t ct
Definition: tcs37727.hpp:124
uint32_t blue
Definition: tcs37727.hpp:121
int getData(tcs37727_data_t *data, int bSampleData=0)
Definition: tcs37727.cpp:332
uint32_t red
Definition: tcs37727.hpp:119
uint32_t green
Definition: tcs37727.hpp:120
int setActive(void)
Definition: tcs37727.cpp:96
Definition: tcs37727.hpp:118
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
uint32_t clear
Definition: tcs37727.hpp:122
uint32_t getChannelGreen(int bSampleData=0)
Definition: tcs37727.cpp:267
uint32_t getChannelBlue(int bSampleData=0)
Definition: tcs37727.cpp:280
uint32_t lux
Definition: tcs37727.hpp:123
API for the TCS37727 Color Light-To-Digital Converter.
Definition: tcs37727.hpp:158
int checkID(void)
Definition: tcs37727.cpp:82
uint32_t getChannelRed(int bSampleData=0)
Definition: tcs37727.cpp:254
uint32_t getLux(int bSampleData=0)
Definition: tcs37727.cpp:306
TCS37727(int bus, int atime_us=TCS37727_ATIME_DEFAULT, int devAddr=TCS37727_I2C_ADDRESS)
Definition: tcs37727.cpp:39
int sampleData(void)
Definition: tcs37727.cpp:208
uint32_t getChannelClear(int bSampleData=0)
Definition: tcs37727.cpp:293