upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
tsl2561.h
Go to the documentation of this file.
1 /*
2  * Author: Nandkishor Sonar <Nandkishor.Sonar@intel.com>,
3  * Abhishek Malik <abhishek.malik@intel.com>
4  * Copyright (c) 2014 Intel Corporation.
5  *
6  * LIGHT-TO-DIGITAL CONVERTER [TAOS-TSL2561]
7  * Inspiration and lux calculation formulas from data sheet
8  * URL: http://www.adafruit.com/datasheets/TSL2561.pdf
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #ifndef TSL2561_H_
31 #define TSL2561_H_
32 
33 #include <stdint.h>
34 #include <upm.h>
35 #include <mraa/i2c.h>
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 
105 typedef struct _tsl2561_context {
106  mraa_i2c_context i2c;
107  int bus;
108  uint8_t address;
109  uint8_t gain;
110  uint8_t integration_time;
111 } *tsl2561_context;
112 
122 tsl2561_context tsl2561_init(int bus, uint8_t dev_address, uint8_t gain,
123  uint8_t integration_time);
124 
131 
139 upm_result_t tsl2561_get_lux(const tsl2561_context, float* lux);
140 
149 upm_result_t tsl2561_i2c_write_reg(tsl2561_context dev, uint8_t reg,
150  uint8_t value);
151 
160 upm_result_t tsl2561_i2c_read_reg(tsl2561_context dev, uint8_t reg,
161  uint8_t* data);
162 
163 
164 #endif /* TSL2561_H_ */
tsl2561_context tsl2561_init(int bus, uint8_t dev_address, uint8_t gain, uint8_t integration_time)
Definition: tsl2561.c:36
void tsl2561_close(tsl2561_context dev)
Definition: tsl2561.c:97
upm_result_t tsl2561_get_lux(const tsl2561_context, float *lux)
Definition: tsl2561.c:107
Definition: tsl2561.h:105
upm_result_t tsl2561_i2c_write_reg(tsl2561_context dev, uint8_t reg, uint8_t value)
Definition: tsl2561.c:116
struct _tsl2561_context * tsl2561_context
upm_result_t tsl2561_i2c_read_reg(tsl2561_context dev, uint8_t reg, uint8_t *data)
Definition: tsl2561.c:132