1 /* 2 * Author: Nandkishor Sonar <Nandkishor.Sonar (at) 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 94 /** 95 * @brief TSL2561 Digital Light Sensor library 96 * @defgroup tsl2561 libupm-tsl2561 97 * @ingroup seeed i2c light eak 98 */ 99 /** 100 * @library tsl2561 101 * @sensor tsl2561 102 * @comname TSL2561 Light Sensor 103 * @altname Grove Digital Light Sensor 104 * @type light 105 * @man seeed 106 * @web http://www.seeedstudio.com/wiki/Grove_-_Digital_Light_Sensor 107 * @con i2c 108 * @kit eak 109 * 110 * @brief API for the TSL2561 Digital Light Sensor 111 * 112 * TSL2560 and TSL2561 are light-to-digital converters that transform 113 * light intensity to a digital signal output capable of a direct I2C (TSL2561) interface 114 * 115 * @image html tsl2561.jpg 116 * @snippet tsl2561.cxx Interesting 117 */ 118 class TSL2561{ 119 public: 120 /** 121 * Instantiates a TSL2561 object 122 * 123 * @param bus Number of the used bus 124 * @param devAddr Address of the used I2C device 125 * @param gain Correct gain to use 126 * @param integration Time to use 127 */ 128 TSL2561(int bus=0, uint8_t devAddr=TSL2561_Address, uint8_t gain=GAIN_0X, uint8_t integrationTime=INTEGRATION_TIME1_101MS); 129 130 /** 131 * GY65 object destructor; powers down TSL2561 and closes the I2C connection. 132 */ 133 ~TSL2561(); 134 135 /** 136 * Gets the calculated lux reading from TSL2561 137 * 138 * @return Calculated lux value from the sensor 139 */ 140 int getLux(); 141 142 private: 143 /** 144 * Writes to a TSL2561 register 145 * 146 * @param reg Addess to write 147 * @param Value to write 148 * @return mraa::Result 149 */ 150 mraa::Result i2cWriteReg(uint8_t reg, uint8_t value); 151 152 /** 153 * Reads from a TSL2561 register 154 * 155 * @param reg Addess to read 156 * @param data Byte read from the register 157 * @return mraa::Result 158 */ 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 172