diff --git a/src/tsl2561/CMakeLists.txt b/src/tsl2561/CMakeLists.txt index 1a396399..24c0ea1c 100644 --- a/src/tsl2561/CMakeLists.txt +++ b/src/tsl2561/CMakeLists.txt @@ -1,5 +1,9 @@ -set (libname "tsl2561") -set (libdescription "upm tsl2561") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init() +upm_mixed_module_init (NAME tsl2561 + DESCRIPTION "upm tsl2561 Digital Light Sensor" + C_HDR tsl2561.h + C_SRC tsl2561.c + CPP_HDR tsl2561.hpp + CPP_SRC tsl2561.cxx + FTI_SRC tsl2561_fti.c + CPP_WRAPS_C + REQUIRES libmraa) diff --git a/src/tsl2561/tsl2561.c b/src/tsl2561/tsl2561.c new file mode 100644 index 00000000..75e48494 --- /dev/null +++ b/src/tsl2561/tsl2561.c @@ -0,0 +1,246 @@ +/* + * Author: Nandkishor Sonar , + * Abhishek Malik + * 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 "tsl2561.h" + +// forward declaration +upm_result_t tsl2561_compute_lux(const tsl2561_context dev, int *int_data); + +tsl2561_context tsl2561_init(int bus, uint8_t dev_address, uint8_t gain, + uint8_t integration_time){ + tsl2561_context dev = + (tsl2561_context)malloc(sizeof(struct _tsl2561_context)); + + if (!dev) + return NULL; + + dev->bus = bus; + dev->address = dev_address; + dev->gain = gain; + dev->integration_time = integration_time; + + dev->i2c = mraa_i2c_init(dev->bus); + if(dev->i2c == NULL){ + free(dev); + return NULL; + } + + if (mraa_i2c_address(dev->i2c, dev->address) != MRAA_SUCCESS) + { + mraa_i2c_stop(dev->i2c); + free(dev); + return NULL; + } + + // POWER UP. + if(mraa_i2c_write_byte_data(dev->i2c, CONTROL_POWERON, REGISTER_Control) != MRAA_SUCCESS){ + mraa_i2c_stop(dev->i2c); + free(dev); + return NULL; + } + + // Power on Settling time + upm_delay_us(1000); + + // Gain & Integration time. + if(mraa_i2c_write_byte_data(dev->i2c, (dev->gain | dev->integration_time), REGISTER_Timing) != MRAA_SUCCESS){ + mraa_i2c_stop(dev->i2c); + free(dev); + return NULL; + } + + // Set interrupt threshold to default. + if(mraa_i2c_write_byte_data(dev->i2c, 0x00, REGISTER_Interrupt) != MRAA_SUCCESS){ + mraa_i2c_stop(dev->i2c); + free(dev); + return NULL; + } + + return dev; +} + +void tsl2561_close(tsl2561_context dev){ + if (mraa_i2c_write_byte_data(dev->i2c, CONTROL_POWEROFF, + REGISTER_Control) != MRAA_SUCCESS){ + printf("Unable turn off device\n"); + } + + mraa_i2c_stop(dev->i2c); + free(dev); +} + +upm_result_t tsl2561_get_lux(const tsl2561_context dev, float* lux){ + int lux_val=0; + + tsl2561_compute_lux(dev, &lux_val); + + *lux = (float) lux_val; + return UPM_SUCCESS; +} + +upm_result_t tsl2561_i2c_write_reg(tsl2561_context dev, uint8_t reg, + uint8_t value){ + // Start transmission to device + if(mraa_i2c_address(dev->i2c, dev->address) != MRAA_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + + // Write register to I2C + if(mraa_i2c_write_byte(dev->i2c, reg) != MRAA_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + + // Write value to I2C + if(mraa_i2c_write_byte(dev->i2c, value) != MRAA_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + + upm_delay_ms(100); + return UPM_SUCCESS; +} + +upm_result_t tsl2561_i2c_read_reg(tsl2561_context dev, uint8_t reg, + uint8_t* data){ + // Start transmission to dev + if(mraa_i2c_address(dev->i2c, dev->address) != MRAA_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + + // Send address of register to be read. + if(mraa_i2c_write_byte(dev->i2c, reg) != MRAA_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + + // Read byte. + *data = mraa_i2c_read_byte(dev->i2c); + + //upm_delay(1); + return UPM_SUCCESS; +} + +upm_result_t tsl2561_compute_lux(const tsl2561_context dev, int *int_data) { + int lux; + uint16_t raw_lux_ch_0; + uint16_t raw_lux_ch_1; + uint8_t ch0_low, ch0_high, ch1_low, ch1_high; + + if (tsl2561_i2c_read_reg(dev, REGISTER_Channal0L, &ch0_low) != UPM_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + + if(tsl2561_i2c_read_reg(dev, REGISTER_Channal0H, &ch0_high) != UPM_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + raw_lux_ch_0 = ch0_high*256 + ch0_low; + + if(tsl2561_i2c_read_reg(dev, REGISTER_Channal1L, &ch1_low) != UPM_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + + if(tsl2561_i2c_read_reg(dev, REGISTER_Channal1H, &ch1_high) != UPM_SUCCESS){ + return UPM_ERROR_OPERATION_FAILED; + } + raw_lux_ch_1 = ch1_high*256 + ch1_low; + + uint64_t scale = 0; + + switch(dev->integration_time){ + case 0: // 13.7 msec + scale = LUX_CHSCALE_TINT0; + break; + case 1: // 101 msec + scale = LUX_CHSCALE_TINT1; + break; + case 2: // assume no scaling + scale = (1 << LUX_CHSCALE); + break; + } + + // scale if gain is NOT 16X + if(!dev->gain) + scale = scale << 4; + + uint64_t channel1 = 0; + uint64_t channel0 = 0; + + // scale the channel values + channel0 = (raw_lux_ch_0 * scale) >> LUX_CHSCALE; + channel1 = (raw_lux_ch_1 * scale) >> LUX_CHSCALE; + + // find the ratio of the channel values (Channel1/Channel0) + // protect against divide by zero + uint64_t ratio_1 = 0; + if (channel0 != 0) + ratio_1 = (channel1 << (LUX_RATIOSCALE+1)) / channel0; + + // round the ratio value + unsigned long ratio = (ratio_1 + 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 temp_lux = 0; + temp_lux = ((channel0 * b) - (channel1 * m)); + // do not allow negative lux value + if (temp_lux < 0) temp_lux = 0; + + // round lsb (2^(LUX_SCALE-1)) + temp_lux += (1 << (LUX_SCALE-1)); + + // strip off fractional portion + lux = temp_lux >> LUX_SCALE; + + *int_data = lux; + + return UPM_SUCCESS; +} diff --git a/src/tsl2561/tsl2561.h b/src/tsl2561/tsl2561.h new file mode 100644 index 00000000..7b9f34d1 --- /dev/null +++ b/src/tsl2561/tsl2561.h @@ -0,0 +1,156 @@ +/* + * Author: Nandkishor Sonar , + * Abhishek Malik + * 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. + */ + +#ifndef TSL2561_H_ +#define TSL2561_H_ + +#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 + +/** + * device context + */ +typedef struct _tsl2561_context { + mraa_i2c_context i2c; + int bus; + uint8_t address; + uint8_t gain; + uint8_t integration_time; +} *tsl2561_context; + +/** + * Sensor Init Function + * + * @param bus I2C bus + * @param dev_address I2C address + * @param gain Gain associated with the driver + * @param integration_time + * @return void* pointer to the sensor struct + */ +tsl2561_context tsl2561_init(int bus, uint8_t dev_address, uint8_t gain, + uint8_t integration_time); + +/** + * Closes the sensor module + * + * @param dev pointer to the sensor struct + */ +void tsl2561_close(tsl2561_context dev); + +/** + * Gets the Lux value + * + * @param dev pointer to the sensor struct + * @param lux pointer to store the lux value + * @return upm_result_t UPM success/error code + */ +upm_result_t tsl2561_get_lux(const tsl2561_context, float* lux); + +/** + * Write I2C register on the device + * + * @param dev pointer to the sensor struct + * @param reg register to write value to + * @param value the value to be written + * @return upm_result_t UPM success/error code + */ +upm_result_t tsl2561_i2c_write_reg(tsl2561_context dev, uint8_t reg, + uint8_t value); + +/** + * Read from an I2C register from the device + * + * @param dev pointer to the sensor struct + * @param reg register to write value to + * @param data Data read in from the register + * @return upm_result_t UPM success/error code + */ +upm_result_t tsl2561_i2c_read_reg(tsl2561_context dev, uint8_t reg, + uint8_t* data); + + +#endif /* TSL2561_H_ */ diff --git a/src/tsl2561/tsl2561_fti.c b/src/tsl2561/tsl2561_fti.c new file mode 100644 index 00000000..b3a6026e --- /dev/null +++ b/src/tsl2561/tsl2561_fti.c @@ -0,0 +1,92 @@ +/* + * Author: Nandkishor Sonar , + * Abhishek Malik + * 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 "tsl2561.h" +#include "upm_fti.h" + +/** + * This file implements the Function Table Interface (FTI) for this sensor + */ + +const char upm_tsl2561_name[] = "TSL2561"; +const char upm_tsl2561_description[] = "Grove Digital Light Sensor"; +const upm_protocol_t upm_tsl2561_protocol[] = {UPM_I2C}; +const upm_sensor_t upm_tsl2561_category[] = {UPM_LIGHT}; + +// forward declarations +const upm_sensor_descriptor_t upm_tsl2561_get_descriptor (); +const void* upm_tsl2561_get_ft(upm_sensor_t sensor_type); +void* upm_tsl2561_init_name(); +void upm_tsl2561_close(void* dev); +upm_result_t upm_tsl2561_get_lux(const void* dev, float* lux); + +const upm_sensor_descriptor_t upm_tsl2561_get_descriptor (){ + upm_sensor_descriptor_t usd; + usd.name = upm_tsl2561_name; + usd.description = upm_tsl2561_description; + usd.protocol_size = 1; + usd.protocol = upm_tsl2561_protocol; + usd.category_size = 1; + usd.category = upm_tsl2561_category; + return usd; +} + +static const upm_sensor_ft ft = +{ + .upm_sensor_init_name = &upm_tsl2561_init_name, + .upm_sensor_close = &upm_tsl2561_close, + .upm_sensor_get_descriptor = &upm_tsl2561_get_descriptor +}; + +static const upm_light_ft lft = +{ + .upm_light_get_value = &upm_tsl2561_get_lux +}; + +const void* upm_tsl2561_get_ft(upm_sensor_t sensor_type){ + if(sensor_type == UPM_LIGHT){ + return &lft; + } + else if(sensor_type == UPM_SENSOR){ + return &ft; + } + return NULL; +} + +void* upm_tsl2561_init_name(){ + return NULL; +} + +void upm_tsl2561_close(void* dev){ + tsl2561_close((tsl2561_context)dev); +} + +upm_result_t upm_tsl2561_get_lux(const void* dev, float* lux){ + return tsl2561_get_lux((tsl2561_context)dev, lux); +}