upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
tsl2561.hpp
1 /*
2  * Author: Nandkishor Sonar <Nandkishor.Sonar@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
5  * LIGHT-TO-DIGITAL CONVERTER [TAOS-TSL2561]
6  * Inspiration and lux calculation formulas from data sheet
7  * URL: http://www.adafruit.com/datasheets/TSL2561.pdf
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 #pragma once
30 
31 #include <string>
32 #include <mraa/i2c.hpp>
33 #include <math.h>
34 
35 namespace upm {
36 
37 #define TSL2561_Address (0x29) //Device address
38 
39 // Integration time
40 #define INTEGRATION_TIME0_13MS (0x00) // 13.7ms
41 #define INTEGRATION_TIME1_101MS (0x01) // 101ms
42 #define INTEGRATION_TIME2_402MS (0x02) // 402ms
43 
44 // Integration time
45 #define GAIN_0X (0x00) // No gain - Low
46 #define GAIN_16X (0x10) // 16x gain - High
47 
48 // Power control bits
49 #define CONTROL_POWERON (0x03) // ON
50 #define CONTROL_POWEROFF (0x00) // OFF
51 
52 // TSL2561 registers
53 #define REGISTER_Control (0x80)
54 #define REGISTER_Timing (0x81)
55 #define REGISTER_Interrupt (0x86)
56 #define REGISTER_Channal0L (0x8C)
57 #define REGISTER_Channal0H (0x8D)
58 #define REGISTER_Channal1L (0x8E)
59 #define REGISTER_Channal1H (0x8F)
60 
61 // Lux calculations differ slightly for CS package
62 #define LUX_SCALE (14) // Scale by 2^14
63 #define LUX_RATIOSCALE (9) // Scale ratio by 2^9
64 #define LUX_CHSCALE (10) // Scale channel values by 2^10
65 #define LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE
66 #define LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE
67 
68 // CS package Coefficients
69 #define LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE
70 #define LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE
71 #define LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE
72 #define LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE
73 #define LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE
74 #define LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE
75 #define LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE
76 #define LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE
77 #define LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE
78 #define LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE
79 #define LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE
80 #define LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE
81 #define LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE
82 #define LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE
83 #define LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE
84 #define LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE
85 #define LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE
86 #define LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE
87 #define LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE
88 #define LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE
89 #define LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE
90 #define LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE
91 #define LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE
92 #define LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE
93 
118 class TSL2561{
119  public:
128  TSL2561(int bus=0, uint8_t devAddr=TSL2561_Address, uint8_t gain=GAIN_0X, uint8_t integrationTime=INTEGRATION_TIME1_101MS);
129 
133  ~TSL2561();
134 
140  int getLux();
141 
142  private:
150  mraa::Result i2cWriteReg(uint8_t reg, uint8_t value);
151 
159  mraa::Result i2cReadReg(uint8_t reg, uint8_t &data);
160 
161  int m_bus;
162  std::string m_name;
163  int m_controlAddr;
164  mraa::I2c m_i2ControlCtx;
165 
166  uint8_t m_gain;
167  uint8_t m_integrationTime;
168 };
169 
170 }
171 
int getLux()
Definition: tsl2561.cxx:89
~TSL2561()
Definition: tsl2561.cxx:82
API for the TSL2561 Digital Light Sensor.
Definition: tsl2561.hpp:118
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
TSL2561(int bus=0, uint8_t devAddr=TSL2561_Address, uint8_t gain=GAIN_0X, uint8_t integrationTime=INTEGRATION_TIME1_101MS)
Definition: tsl2561.cxx:38