diff --git a/docs/images/grovetsl2561.jpeg b/docs/images/grovetsl2561.jpeg new file mode 100755 index 00000000..04891f30 Binary files /dev/null and b/docs/images/grovetsl2561.jpeg differ diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 4ea12c3e..185c6e3e 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -39,6 +39,7 @@ add_executable (lsm303-example lsm303.cxx) add_executable (joystick12-example joystick12-example.cxx) add_executable (lol-example lol-example.cxx) add_executable (nrf_ble_broadcast-example ble_broadcast.cxx) +add_executable (tsl2561-example tsl2561.cxx) include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l) include_directories (${PROJECT_SOURCE_DIR}/src/grove) @@ -71,6 +72,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/th02) include_directories (${PROJECT_SOURCE_DIR}/src/lsm303) include_directories (${PROJECT_SOURCE_DIR}/src/joystick12) include_directories (${PROJECT_SOURCE_DIR}/src/lol) +include_directories (${PROJECT_SOURCE_DIR}/src/tsl2561) target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT}) @@ -113,3 +115,4 @@ target_link_libraries (lsm303-example lsm303 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (joystick12-example joystick12 ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (lol-example lol ${CMAKE_THREAD_LIBS_INIT}) target_link_libraries (nrf_ble_broadcast-example nrf24l01 ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries (tsl2561-example tsl2561 ${CMAKE_THREAD_LIBS_INIT}) diff --git a/examples/tsl2561.cxx b/examples/tsl2561.cxx new file mode 100755 index 00000000..c2403762 --- /dev/null +++ b/examples/tsl2561.cxx @@ -0,0 +1,65 @@ +/* + * Author: Nandkishor Sonar + * Copyright (c) 2014 Intel Corporation. + * + * LIGHT-TO-DIGITAL CONVERTER [TAOS-TSL2561] + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +#include +#include "tsl2561.h" + +int main (int argc, char **argv) +{ + mraa_result_t error = MRAA_SUCCESS; + upm::TSL2561 *sensor = NULL; + int calculatedLux = 0; + int loopCount = 100; + +//! [Interesting] + if (argc < 2) { + printf("Provide loop count \n"); + } else { + loopCount = atoi(argv[1]); + } + sensor = new upm::TSL2561(); + //sensor = new upm::TSL2561(0, TSL2561_Address, GAIN_16X, INTEGRATION_TIME2_402MS); + //sensor = new upm::TSL2561(0, TSL2561_Address, GAIN_16X, INTEGRATION_TIME1_101MS); + //sensor = new upm::TSL2561(0, TSL2561_Address, GAIN_16X, INTEGRATION_TIME0_13MS); + //sensor = new upm::TSL2561(0, TSL2561_Address, GAIN_0X, INTEGRATION_TIME2_402MS); + + for(int i=0; i< loopCount; i++){ + error = sensor->getLux(calculatedLux); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: on i2c bus address setup in i2cReadReg()\n"); + return error; + } + + fprintf(stdout, "Lux = %d\n", calculatedLux); + } + +//! [Interesting] + + delete(sensor); + + return (0); +} diff --git a/src/tsl2561/CMakeLists.txt b/src/tsl2561/CMakeLists.txt new file mode 100644 index 00000000..9434da3a --- /dev/null +++ b/src/tsl2561/CMakeLists.txt @@ -0,0 +1,5 @@ +set (libname "tsl2561") +set (libdescription "upm tsl2561") +set (module_src ${libname}.cxx) +set (module_h ${libname}.h) +upm_module_init() diff --git a/src/tsl2561/jsupm_tsl2561.i b/src/tsl2561/jsupm_tsl2561.i new file mode 100644 index 00000000..e5403d13 --- /dev/null +++ b/src/tsl2561/jsupm_tsl2561.i @@ -0,0 +1,8 @@ +%module jsupm_tsl2561 +%include "../upm.i" + +%{ + #include "tsl2561.h" +%} + +%include "tsl2561.h" diff --git a/src/tsl2561/pyupm_tsl2561.i b/src/tsl2561/pyupm_tsl2561.i new file mode 100644 index 00000000..c9404914 --- /dev/null +++ b/src/tsl2561/pyupm_tsl2561.i @@ -0,0 +1,13 @@ +%module pyupm_tsl2561 +%include "../upm.i" + +%feature("autodoc", "3"); + +#ifdef DOXYGEN +%include "tsl2561_doc.i" +#endif + +%include "tsl2561.h" +%{ + #include "tsl2561.h" +%} diff --git a/src/tsl2561/tsl2561.cxx b/src/tsl2561/tsl2561.cxx new file mode 100755 index 00000000..46e6dbf8 --- /dev/null +++ b/src/tsl2561/tsl2561.cxx @@ -0,0 +1,242 @@ +/* + * Author: Nandkishor Sonar + * Copyright (c) 2014 Intel Corporation. + * + * LIGHT-TO-DIGITAL CONVERTER [TAOS-TSL2561] + * Inspiration and lux calculation formulas from data sheet + * URL: http://www.adafruit.com/datasheets/TSL2561.pdf + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + + +#include +#include "tsl2561.h" + +using namespace upm; + + +TSL2561::TSL2561(int bus, uint8_t devAddr, uint8_t gain, uint8_t integrationTime) { + m_controlAddr = devAddr; + m_bus = bus; + m_gain = gain ; + m_integrationTime = integrationTime; + + m_name = "TSL2561- Digital Light Sensor"; + + m_i2ControlCtx = mraa_i2c_init(m_bus); + + mraa_result_t error = mraa_i2c_address(m_i2ControlCtx, m_controlAddr); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Messed up i2c bus in TSL2561()\n"); + return; + } + + // POWER UP. + error = i2cWriteReg(REGISTER_Control,CONTROL_POWERON); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: Unable to power up - Ensure TSL2561 is connected to I2C\n"); + return; + } + // Power on Settling time + usleep(1000); + + // Gain & Integration time . + error = i2cWriteReg(REGISTER_Timing, m_gain | m_integrationTime); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: Unable to set gain/time - Ensure TSL2561 is connected to I2C\n"); + return; + } + + // Set interrupt threshold to default. + error = i2cWriteReg(REGISTER_Interrupt,0x00); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: Unable to interrupt threshold - Ensure TSL2561 is connected to I2C\n"); + return; + } +} + +TSL2561::~TSL2561() { + // POWER DOWN + i2cWriteReg(REGISTER_Control,CONTROL_POWEROFF); + + // Stop I2C bus + mraa_i2c_stop(m_i2ControlCtx); +} + +mraa_result_t +TSL2561::getLux(int &lux) +{ + mraa_result_t error = MRAA_SUCCESS; + uint16_t rawLuxCh0; + uint16_t rawLuxCh1; + uint8_t ch0_low, ch0_high, ch1_low, ch1_high; + + error = i2cReadReg(REGISTER_Channal0L, ch0_low); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: Unable to read channel0L in getRawLux()\n"); + return error; + } + + error = i2cReadReg(REGISTER_Channal0H, ch0_high); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: Unable to read channel0H in getRawLux()\n"); + return error; + } + + rawLuxCh0 = ch0_high*256+ch0_low; + + error= i2cReadReg(REGISTER_Channal1L, ch1_low); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: Unable to read channel1L in getRawLux()\n"); + return error; + } + + error = i2cReadReg(REGISTER_Channal1H, ch1_high); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: Unable to read channel1H in getRawLux()\n"); + return error; + } + + rawLuxCh1 = ch1_high*256+ch1_low; + + uint64_t scale = 0; + + switch (m_integrationTime) + { + case 0: // 13.7 msec + scale = LUX_CHSCALE_TINT0; + break; + case 1: // 101 msec + scale = LUX_CHSCALE_TINT1; + break; + default: // assume no scaling + scale = (1 << LUX_CHSCALE); + break; + } + + // scale if gain is NOT 16X + if (!m_gain) scale = scale << 4; + + uint64_t channel1 = 0; + uint64_t channel0 = 0; + // scale the channel values + channel0 = (rawLuxCh0 * scale) >> LUX_CHSCALE; + channel1 = (rawLuxCh1 * scale) >> LUX_CHSCALE; + + // find the ratio of the channel values (Channel1/Channel0) + // protect against divide by zero + unsigned long ratio1 = 0; + if (channel0 != 0) ratio1 = (channel1 << (LUX_RATIOSCALE+1)) / channel0; + + // round the ratio value + unsigned long ratio = (ratio1 + 1) >> 1; + + unsigned int b, m; + + // CS package + // Check if ratio <= eachBreak ? + if ((ratio >= 0) && (ratio <= LUX_K1C)) + {b=LUX_B1C; m=LUX_M1C;} + else if (ratio <= LUX_K2C) + {b=LUX_B2C; m=LUX_M2C;} + else if (ratio <= LUX_K3C) + {b=LUX_B3C; m=LUX_M3C;} + else if (ratio <= LUX_K4C) + {b=LUX_B4C; m=LUX_M4C;} + else if (ratio <= LUX_K5C) + {b=LUX_B5C; m=LUX_M5C;} + else if (ratio <= LUX_K6C) + {b=LUX_B6C; m=LUX_M6C;} + else if (ratio <= LUX_K7C) + {b=LUX_B7C; m=LUX_M7C;} + else if (ratio > LUX_K8C) + {b=LUX_B8C; m=LUX_M8C;} + + uint64_t tempLux = 0; + tempLux = ((channel0 * b) - (channel1 * m)); + // do not allow negative lux value + if (tempLux < 0) tempLux = 0; + + // round lsb (2^(LUX_SCALE-1)) + tempLux += (1 << (LUX_SCALE-1)); + + // strip off fractional portion + lux = tempLux >> LUX_SCALE; + + return error; +} + + +mraa_result_t +TSL2561::i2cWriteReg (uint8_t reg, uint8_t value) { + mraa_result_t error = MRAA_SUCCESS; + + // Start transmission to device + error = mraa_i2c_address (m_i2ControlCtx, m_controlAddr); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: on i2c bus address setup in i2cWriteReg()\n"); + return error; + } + // Write register to I2C + error = mraa_i2c_write_byte (m_i2ControlCtx, reg); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: on i2c bus write reg in i2cWriteReg()\n"); + return error; + } + + // Write value to I2C + error = mraa_i2c_write_byte (m_i2ControlCtx, value); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: on i2c bus write value in i2cWriteReg()\n"); + return error; + } + + usleep(100000); + + return error; +} + +mraa_result_t +TSL2561::i2cReadReg(uint8_t reg, uint8_t &data) { + mraa_result_t error = MRAA_SUCCESS; + + // Start transmission to device + error = mraa_i2c_address(m_i2ControlCtx, m_controlAddr); + + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: on i2c bus address setup in i2cReadReg()\n"); + return error; + } + + // Send address of register to be read. + error = mraa_i2c_write_byte(m_i2ControlCtx, reg); + if (error != MRAA_SUCCESS) { + fprintf(stderr, "Error: on i2c bus write in i2cReadReg()\n"); + return error; + } + + // Read byte. + data = mraa_i2c_read_byte(m_i2ControlCtx); + + usleep(10000); + + return error; +} diff --git a/src/tsl2561/tsl2561.h b/src/tsl2561/tsl2561.h new file mode 100755 index 00000000..274a81e3 --- /dev/null +++ b/src/tsl2561/tsl2561.h @@ -0,0 +1,167 @@ +/* + * Author: Nandkishor Sonar + * Copyright (c) 2014 Intel Corporation. + * + * * LIGHT-TO-DIGITAL CONVERTER [TAOS-TSL2561] + * Inspiration and lux calculation formulas from data sheet + * URL: http://www.adafruit.com/datasheets/TSL2561.pdf + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#pragma once + +#include +#include +#include + +#define TSL2561_Address (0x29) //Device address + +// Integration time +#define INTEGRATION_TIME0_13MS (0x00) // 13.7ms +#define INTEGRATION_TIME1_101MS (0x01) // 101ms +#define INTEGRATION_TIME2_402MS (0x02) // 402ms + +// Integration time +#define GAIN_0X (0x00) // No gain - Low +#define GAIN_16X (0x10) // 16x gain - High + +// Power control bits +#define CONTROL_POWERON (0x03) // ON +#define CONTROL_POWEROFF (0x00) // OFF + +// TSL2561 registers +#define REGISTER_Control (0x80) +#define REGISTER_Timing (0x81) +#define REGISTER_Interrupt (0x86) +#define REGISTER_Channal0L (0x8C) +#define REGISTER_Channal0H (0x8D) +#define REGISTER_Channal1L (0x8E) +#define REGISTER_Channal1H (0x8F) + +// Lux calculations differ slightly for CS package +#define LUX_SCALE (14) // Scale by 2^14 +#define LUX_RATIOSCALE (9) // Scale ratio by 2^9 +#define LUX_CHSCALE (10) // Scale channel values by 2^10 +#define LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE +#define LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE + +// CS package Coefficients +#define LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE +#define LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE +#define LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE +#define LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE +#define LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE +#define LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE +#define LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE +#define LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE +#define LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE +#define LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE +#define LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE +#define LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE +#define LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE +#define LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE +#define LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE +#define LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE +#define LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE +#define LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE +#define LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE +#define LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE +#define LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE +#define LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE +#define LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE +#define LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE + + +namespace upm { + +/** + * @brief TSL2561 Digital Light Sensor library + * @defgroup tsl2561 libupm-tsl2561 + */ + +/** + * @brief C++ API for TSL2561 chip (Digital Light Sensor library) + * + * The LIGHT-TO-DIGITAL CONVERTER [TAOS-TSL2561] + * (http://www.adafruit.com/datasheets/TSL2561.pdf) + * The TSL2560 and TSL2561 are light-to-digital converters that transform light + * intensity to a digital signal output capable of direct I2C (TSL2561). + * + * @ingroup tsl2561 + * @snippet tsl2561.cxx Interesting + * @image html grovetsl2561.jpeg + */ +class TSL2561{ + public: + /** + * Instanciates a TSL2561 object + * + * @param bus number of used bus + * @param devAddr address of used i2c device + */ + TSL2561(int bus=0, uint8_t devAddr=TSL2561_Address, uint8_t gain=GAIN_0X, uint8_t integrationTime=INTEGRATION_TIME1_101MS); + + /** + * GY65 object destructor to power down TSL2561 and close i2c connection. + */ + ~TSL2561(); + + /** + * Get calculated lux reading from TSL2561 + * + * @param lux - place holder to receive calculated lux value from TSL2561 + * + * Return mraa_result_t + */ + mraa_result_t getLux(int &lux); + + private: + /** + * Write to TSL2561 register + * + * @param reg addess to write + * @param value to write + * + * Return mraa_result_t + */ + mraa_result_t i2cWriteReg(uint8_t reg, uint8_t value); + + /** + * Read from TSL2561 register + * + * @param reg addess to read + * @param data byte read from the register + * + * Return mraa_result_t + */ + mraa_result_t i2cReadReg(uint8_t reg, uint8_t &data); + + int m_bus; + std::string m_name; + int m_controlAddr; + mraa_i2c_context m_i2ControlCtx; + + uint8_t m_gain; + uint8_t m_integrationTime; +}; + +} +