From 6cb90ea92416aee01b7fa966526bee989490452c Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 00:15:28 +0200 Subject: [PATCH 01/13] First commit --- app/include/user_modules.h | 1 + app/include/user_version.h | 2 +- app/modules/modules.h | 9 +- app/modules/tsl2561.c | 88 ++++++++++ app/tsl2561/tsl2561.c | 331 +++++++++++++++++++++++++++++++++++++ app/tsl2561/tsl2561.h | 160 ++++++++++++++++++ 6 files changed, 589 insertions(+), 2 deletions(-) create mode 100644 app/modules/tsl2561.c create mode 100644 app/tsl2561/tsl2561.c create mode 100644 app/tsl2561/tsl2561.h diff --git a/app/include/user_modules.h b/app/include/user_modules.h index 18fc5cfa..cc23f794 100644 --- a/app/include/user_modules.h +++ b/app/include/user_modules.h @@ -40,6 +40,7 @@ #define LUA_USE_MODULES_RTCFIFO #define LUA_USE_MODULES_SNTP //#define LUA_USE_MODULES_BMP085 +#define LUA_USE_MODULES_TSL2561 #endif /* LUA_USE_MODULES */ diff --git a/app/include/user_version.h b/app/include/user_version.h index 40ce7eb4..b4018097 100644 --- a/app/include/user_version.h +++ b/app/include/user_version.h @@ -7,6 +7,6 @@ #define NODE_VERSION_INTERNAL 0U #define NODE_VERSION "NodeMCU 0.9.6" -#define BUILD_DATE "build 20150627" +#define BUILD_DATE "build 20150822" #endif /* __USER_VERSION_H__ */ diff --git a/app/modules/modules.h b/app/modules/modules.h index 176f4591..db5a8b8b 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -221,6 +221,13 @@ #define ROM_MODULES_BMP085 #endif +#if defined(LUA_USE_MODULES_TSL2561) +#define MODULES_TSL2561 "TSL2561" +#define ROM_MODULES_TSL2561 \ + _ROM(MODULES_TSL2561, luaopen_tsl2561, tsl2561_map) +#else +#define ROM_MODULES_TSL2561 +#endif #define LUA_MODULES_ROM \ ROM_MODULES_GPIO \ @@ -250,5 +257,5 @@ ROM_MODULES_RTCFIFO \ ROM_MODULES_SNTP \ ROM_MODULES_BMP085 \ - + ROM_MODULES_TSL2561 \ #endif diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c new file mode 100644 index 00000000..3546f144 --- /dev/null +++ b/app/modules/tsl2561.c @@ -0,0 +1,88 @@ +/* + * tsl2561.c + * + * Created on: Aug 21, 2015 + * Author: Michael Lucas (Aeprox @github) + */ +#include "lualib.h" +#include "lauxlib.h" +#include "platform.h" +#include "auxmods.h" +#include "lrotable.h" +#include "../tsl2561/tsl2561.h" + +static uint16_t ch0; +static uint16_t ch1; + +static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { + uint32_t sda; + uint32_t scl; + + if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { + return luaL_error(L, "wrong arg range"); + } + + sda = luaL_checkinteger(L, 1); + scl = luaL_checkinteger(L, 2); + + if (scl == 0 || sda == 0) { + return luaL_error(L, "no i2c for D0"); + } + + uint8_t error = tsl2561Init(sda,scl); + lua_pushnumber( L, error ); + return 1; +} + +static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { + // check variables + if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { + return luaL_error(L, "wrong arg range"); + } + uint8_t integration = luaL_checkinteger(L, 1); + uint8_t gain = luaL_checkinteger(L, 2); + + lua_pushnumber( L, tsl2561SetTiming(integration, gain) ); + return 1; +} +static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { + lua_pushnumber( L, tsl2561CalculateLux(ch0,ch1) ); + return 1; +} + +static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { + uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); + lua_pushnumber( L, ch0 ); + lua_pushnumber( L, ch1 ); + lua_pushnumber( L, error ); + + return 1; +} + +#define MIN_OPT_LEVEL 2 +#include "lrodefs.h" +const LUA_REG_TYPE tsl2561_map[] = +{ + { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, + { LSTRKEY( "calclux" ), LFUNCVAL( tsl2561_lua_calclux )}, + { LSTRKEY( "channels_raw" ), LFUNCVAL( tsl2561_lua_getchannels )}, + { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, + + { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) }, + { LSTRKEY( "TSL2561_ERROR_I2CINIT" ), LNUMVAL( TSL2561_ERROR_I2CINIT ) }, + { LSTRKEY( "TSL2561_ERROR_I2CBUSY" ), LNUMVAL( TSL2561_ERROR_I2CBUSY ) }, + { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) }, + + { LSTRKEY( "TSL2561_INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, + { LSTRKEY( "TSL2561_INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, + { LSTRKEY( "TSL2561_INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, + { LSTRKEY( "TSL2561_GAIN_0X" ), LNUMVAL( TSL2561_GAIN_0X ) }, + { LSTRKEY( "TSL2561_GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, + + { LNILKEY, LNILVAL} +}; + +LUALIB_API int luaopen_tsl2561(lua_State *L) { + LREGISTER(L, "tsl2561", tsl2561_map); + return 1; +} diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c new file mode 100644 index 00000000..21b76ccb --- /dev/null +++ b/app/tsl2561/tsl2561.c @@ -0,0 +1,331 @@ +/**************************************************************************/ +/*! + @file tsl2561.c + @author K. Townsend (microBuilder.eu) + + @brief Drivers for the TAOS TSL2561 I2C digital luminosity sensor + + @section DESCRIPTION + + The TSL2561 is a 16-bit digital luminosity sensor the approximates + the human eye's response to light. It contains one broadband + photodiode that measures visible plus infrared light (channel 0) + and one infrared photodiode (channel 1). + + @section EXAMPLE + + @code + #include "drivers/sensors/tsl2561/tsl2561.h" + ... + uint16_t broadband, ir; + uint32_t lux; + + // Initialise luminosity sensor + tsl2561Init(); + + // Optional ... default setting is 400ms with no gain + // Set timing to 101ms with no gain + tsl2561SetTiming(TSL2561_INTEGRATIONTIME_101MS, TSL2561_GAIN_0X); + + // Check luminosity level and calculate lux + tsl2561GetLuminosity(&broadband, &ir); + lux = tsl2561CalculateLux(broadband, ir); + printf("Broadband: %u, IR: %u, Lux: %d %s", broadband, ir, lux, CFG_PRINTF_NEWLINE); + + @endcode + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2010, microBuilder SARL + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#include "tsl2561.h" +static const uint32_t tsl2561_i2c_id = 0; +static bool _tsl2561Initialised = 0; +static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; +static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_0X; + + +/**************************************************************************/ +/*! + @brief Writes an 8 bit values over I2C +*/ +/**************************************************************************/ +tsl2561Error_t tsl2561Write8 (uint8_t reg, uint8_t value) +{ + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg); + platform_i2c_send_byte(tsl2561_i2c_id, value); + platform_i2c_send_stop(tsl2561_i2c_id); + return TSL2561_ERROR_OK; +} + +/**************************************************************************/ +/*! + @brief Reads a 16 bit values over I2C +*/ +/**************************************************************************/ +tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) +{ + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg); + platform_i2c_send_stop(tsl2561_i2c_id); + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + uint8_t ch_high = platform_i2c_recv_byte(tsl2561_i2c_id, 0); + platform_i2c_send_stop(tsl2561_i2c_id); + + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg); + platform_i2c_send_stop(tsl2561_i2c_id); + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); + platform_i2c_send_stop(tsl2561_i2c_id); + + // Shift values to create properly formed integer (low byte first) + *value = (ch_low | (ch_high << 8)); + + return TSL2561_ERROR_OK; +} + +/**************************************************************************/ +/*! + @brief Enables the device +*/ +/**************************************************************************/ +tsl2561Error_t tsl2561Enable(void) +{ + if (!_tsl2561Initialised) tsl2561Init(); + + // Enable the device by setting the control bit to 0x03 + return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); +} + +/**************************************************************************/ +/*! + @brief Disables the device (putting it in lower power sleep mode) +*/ +/**************************************************************************/ +tsl2561Error_t tsl2561Disable(void) +{ + if (!_tsl2561Initialised) tsl2561Init(); + + // Turn the device off to save power + return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); +} + +/**************************************************************************/ +/*! + @brief Initialises the I2C block +*/ +/**************************************************************************/ +tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl) +{ + // Initialise I2C + platform_i2c_setup(tsl2561_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW); + + _tsl2561Initialised = 1; + + // Set default integration time and gain + tsl2561SetTiming(_tsl2561IntegrationTime, _tsl2561Gain); + + // Note: by default, the device is in power down mode on bootup + + return TSL2561_ERROR_OK; +} + +/**************************************************************************/ +/*! + @brief Sets the integration time and gain (controls sensitivity) +*/ +/**************************************************************************/ +tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain) +{ + if (!_tsl2561Initialised) tsl2561Init(); + + tsl2561Error_t error = TSL2561_ERROR_OK; + + // Enable the device by setting the control bit to 0x03 + error = tsl2561Enable(); + if (error) return error; + + // Turn the device off to save power + error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain); + if (error) return error; + + // Update value placeholders + _tsl2561IntegrationTime = integration; + _tsl2561Gain = gain; + + // Turn the device off to save power + error = tsl2561Disable(); + if (error) return error; + + return error; +} + +/**************************************************************************/ +/*! + @brief Reads the luminosity on both channels from the TSL2561 +*/ +/**************************************************************************/ +tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir) +{ + if (!_tsl2561Initialised) tsl2561Init(); + + tsl2561Error_t error = TSL2561_ERROR_OK; + + // Enable the device by setting the control bit to 0x03 + error = tsl2561Enable(); + if (error) return error; + + // Wait x ms for ADC to complete + switch (_tsl2561IntegrationTime) + { + case TSL2561_INTEGRATIONTIME_13MS: + systickDelay(14); + break; + case TSL2561_INTEGRATIONTIME_101MS: + systickDelay(102); + break; + default: + systickDelay(400); + break; + } + + // Reads two byte value from channel 0 (visible + infrared) + error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband); + if (error) return error; + + // Reads two byte value from channel 1 (infrared) + error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir); + if (error) return error; + + // Turn the device off to save power + error = tsl2561Disable(); + if (error) return error; + + return error; +} + +/**************************************************************************/ +/*! + @brief Calculates LUX from the supplied ch0 (broadband) and ch1 + (IR) readings +*/ +/**************************************************************************/ +uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) +{ + unsigned long chScale; + unsigned long channel1; + unsigned long channel0; + + switch (_tsl2561IntegrationTime) + { + case TSL2561_INTEGRATIONTIME_13MS: + chScale = TSL2561_LUX_CHSCALE_TINT0; + break; + case TSL2561_INTEGRATIONTIME_101MS: + chScale = TSL2561_LUX_CHSCALE_TINT1; + break; + default: // No scaling ... integration time = 402ms + chScale = (1 << TSL2561_LUX_CHSCALE); + break; + } + + // Scale for gain (1x or 16x) + if (!_tsl2561Gain) chScale = chScale << 4; + + // scale the channel values + channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE; + channel1 = (ch1 * chScale) >> TSL2561_LUX_CHSCALE; + + // find the ratio of the channel values (Channel1/Channel0) + unsigned long ratio1 = 0; + if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0; + + // round the ratio value + unsigned long ratio = (ratio1 + 1) >> 1; + + unsigned int b, m; + +#ifdef TSL2561_PACKAGE_CS + if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) + {b=TSL2561_LUX_B1C; m=TSL2561_LUX_M1C;} + else if (ratio <= TSL2561_LUX_K2C) + {b=TSL2561_LUX_B2C; m=TSL2561_LUX_M2C;} + else if (ratio <= TSL2561_LUX_K3C) + {b=TSL2561_LUX_B3C; m=TSL2561_LUX_M3C;} + else if (ratio <= TSL2561_LUX_K4C) + {b=TSL2561_LUX_B4C; m=TSL2561_LUX_M4C;} + else if (ratio <= TSL2561_LUX_K5C) + {b=TSL2561_LUX_B5C; m=TSL2561_LUX_M5C;} + else if (ratio <= TSL2561_LUX_K6C) + {b=TSL2561_LUX_B6C; m=TSL2561_LUX_M6C;} + else if (ratio <= TSL2561_LUX_K7C) + {b=TSL2561_LUX_B7C; m=TSL2561_LUX_M7C;} + else if (ratio > TSL2561_LUX_K8C) + {b=TSL2561_LUX_B8C; m=TSL2561_LUX_M8C;} +#else + if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) + {b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;} + else if (ratio <= TSL2561_LUX_K2T) + {b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;} + else if (ratio <= TSL2561_LUX_K3T) + {b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;} + else if (ratio <= TSL2561_LUX_K4T) + {b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;} + else if (ratio <= TSL2561_LUX_K5T) + {b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;} + else if (ratio <= TSL2561_LUX_K6T) + {b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;} + else if (ratio <= TSL2561_LUX_K7T) + {b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;} + else if (ratio > TSL2561_LUX_K8T) + {b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;} +#endif + + unsigned long temp; + temp = ((channel0 * b) - (channel1 * m)); + + // do not allow negative lux value + if (temp < 0) temp = 0; + + // round lsb (2^(LUX_SCALE-1)) + temp += (1 << (TSL2561_LUX_LUXSCALE-1)); + + // strip off fractional portion + uint32_t lux = temp >> TSL2561_LUX_LUXSCALE; + + // Signal I2C had no errors + return lux; +} diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h new file mode 100644 index 00000000..1ec143ec --- /dev/null +++ b/app/tsl2561/tsl2561.h @@ -0,0 +1,160 @@ +/**************************************************************************/ +/*! + @file tsl2561.h + @author K. Townsend (microBuilder.eu) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2010, microBuilder SARL + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#ifndef _TSL2561_H_ +#define _TSL2561_H_ + +#define TSL2561_PACKAGE_CS // Lux calculations differ slightly for CS package +// #define TSL2561_PACKAGE_T_FN_CL + +#define TSL2561_ADDRESS (0x72) // 0111001 shifted left 1 bit = 0x72 (ADDR = GND or floating) +#define TSL2561_READBIT (0x01) + +#define TSL2561_COMMAND_BIT (0x80) // Must be 1 +#define TSL2561_CLEAR_BIT (0x40) // Clears any pending interrupt (write 1 to clear) +#define TSL2561_WORD_BIT (0x20) // 1 = read/write word (rather than byte) +#define TSL2561_BLOCK_BIT (0x10) // 1 = using block read/write + +#define TSL2561_CONTROL_POWERON (0x03) +#define TSL2561_CONTROL_POWEROFF (0x00) + +#define TSL2561_LUX_LUXSCALE (14) // Scale by 2^14 +#define TSL2561_LUX_RATIOSCALE (9) // Scale ratio by 2^9 +#define TSL2561_LUX_CHSCALE (10) // Scale channel values by 2^10 +#define TSL2561_LUX_CHSCALE_TINT0 (0x7517) // 322/11 * 2^TSL2561_LUX_CHSCALE +#define TSL2561_LUX_CHSCALE_TINT1 (0x0FE7) // 322/81 * 2^TSL2561_LUX_CHSCALE + +// T, FN and CL package values +#define TSL2561_LUX_K1T (0x0040) // 0.125 * 2^RATIO_SCALE +#define TSL2561_LUX_B1T (0x01f2) // 0.0304 * 2^LUX_SCALE +#define TSL2561_LUX_M1T (0x01be) // 0.0272 * 2^LUX_SCALE +#define TSL2561_LUX_K2T (0x0080) // 0.250 * 2^RATIO_SCALE +#define TSL2561_LUX_B2T (0x0214) // 0.0325 * 2^LUX_SCALE +#define TSL2561_LUX_M2T (0x02d1) // 0.0440 * 2^LUX_SCALE +#define TSL2561_LUX_K3T (0x00c0) // 0.375 * 2^RATIO_SCALE +#define TSL2561_LUX_B3T (0x023f) // 0.0351 * 2^LUX_SCALE +#define TSL2561_LUX_M3T (0x037b) // 0.0544 * 2^LUX_SCALE +#define TSL2561_LUX_K4T (0x0100) // 0.50 * 2^RATIO_SCALE +#define TSL2561_LUX_B4T (0x0270) // 0.0381 * 2^LUX_SCALE +#define TSL2561_LUX_M4T (0x03fe) // 0.0624 * 2^LUX_SCALE +#define TSL2561_LUX_K5T (0x0138) // 0.61 * 2^RATIO_SCALE +#define TSL2561_LUX_B5T (0x016f) // 0.0224 * 2^LUX_SCALE +#define TSL2561_LUX_M5T (0x01fc) // 0.0310 * 2^LUX_SCALE +#define TSL2561_LUX_K6T (0x019a) // 0.80 * 2^RATIO_SCALE +#define TSL2561_LUX_B6T (0x00d2) // 0.0128 * 2^LUX_SCALE +#define TSL2561_LUX_M6T (0x00fb) // 0.0153 * 2^LUX_SCALE +#define TSL2561_LUX_K7T (0x029a) // 1.3 * 2^RATIO_SCALE +#define TSL2561_LUX_B7T (0x0018) // 0.00146 * 2^LUX_SCALE +#define TSL2561_LUX_M7T (0x0012) // 0.00112 * 2^LUX_SCALE +#define TSL2561_LUX_K8T (0x029a) // 1.3 * 2^RATIO_SCALE +#define TSL2561_LUX_B8T (0x0000) // 0.000 * 2^LUX_SCALE +#define TSL2561_LUX_M8T (0x0000) // 0.000 * 2^LUX_SCALE + +// CS package values +#define TSL2561_LUX_K1C (0x0043) // 0.130 * 2^RATIO_SCALE +#define TSL2561_LUX_B1C (0x0204) // 0.0315 * 2^LUX_SCALE +#define TSL2561_LUX_M1C (0x01ad) // 0.0262 * 2^LUX_SCALE +#define TSL2561_LUX_K2C (0x0085) // 0.260 * 2^RATIO_SCALE +#define TSL2561_LUX_B2C (0x0228) // 0.0337 * 2^LUX_SCALE +#define TSL2561_LUX_M2C (0x02c1) // 0.0430 * 2^LUX_SCALE +#define TSL2561_LUX_K3C (0x00c8) // 0.390 * 2^RATIO_SCALE +#define TSL2561_LUX_B3C (0x0253) // 0.0363 * 2^LUX_SCALE +#define TSL2561_LUX_M3C (0x0363) // 0.0529 * 2^LUX_SCALE +#define TSL2561_LUX_K4C (0x010a) // 0.520 * 2^RATIO_SCALE +#define TSL2561_LUX_B4C (0x0282) // 0.0392 * 2^LUX_SCALE +#define TSL2561_LUX_M4C (0x03df) // 0.0605 * 2^LUX_SCALE +#define TSL2561_LUX_K5C (0x014d) // 0.65 * 2^RATIO_SCALE +#define TSL2561_LUX_B5C (0x0177) // 0.0229 * 2^LUX_SCALE +#define TSL2561_LUX_M5C (0x01dd) // 0.0291 * 2^LUX_SCALE +#define TSL2561_LUX_K6C (0x019a) // 0.80 * 2^RATIO_SCALE +#define TSL2561_LUX_B6C (0x0101) // 0.0157 * 2^LUX_SCALE +#define TSL2561_LUX_M6C (0x0127) // 0.0180 * 2^LUX_SCALE +#define TSL2561_LUX_K7C (0x029a) // 1.3 * 2^RATIO_SCALE +#define TSL2561_LUX_B7C (0x0037) // 0.00338 * 2^LUX_SCALE +#define TSL2561_LUX_M7C (0x002b) // 0.00260 * 2^LUX_SCALE +#define TSL2561_LUX_K8C (0x029a) // 1.3 * 2^RATIO_SCALE +#define TSL2561_LUX_B8C (0x0000) // 0.000 * 2^LUX_SCALE +#define TSL2561_LUX_M8C (0x0000) // 0.000 * 2^LUX_SCALE + +enum +{ + TSL2561_REGISTER_CONTROL = 0x00, + TSL2561_REGISTER_TIMING = 0x01, + TSL2561_REGISTER_THRESHHOLDL_LOW = 0x02, + TSL2561_REGISTER_THRESHHOLDL_HIGH = 0x03, + TSL2561_REGISTER_THRESHHOLDH_LOW = 0x04, + TSL2561_REGISTER_THRESHHOLDH_HIGH = 0x05, + TSL2561_REGISTER_INTERRUPT = 0x06, + TSL2561_REGISTER_CRC = 0x08, + TSL2561_REGISTER_ID = 0x0A, + TSL2561_REGISTER_CHAN0_LOW = 0x0C, + TSL2561_REGISTER_CHAN0_HIGH = 0x0D, + TSL2561_REGISTER_CHAN1_LOW = 0x0E, + TSL2561_REGISTER_CHAN1_HIGH = 0x0F +}; + +typedef enum +{ + TSL2561_INTEGRATIONTIME_13MS = 0x00, // 13.7ms + TSL2561_INTEGRATIONTIME_101MS = 0x01, // 101ms + TSL2561_INTEGRATIONTIME_402MS = 0x02 // 402ms +} +tsl2561IntegrationTime_t; + +typedef enum +{ + TSL2561_GAIN_0X = 0x00, // No gain + TSL2561_GAIN_16X = 0x10, // 16x gain +} +tsl2561Gain_t; + +typedef enum +{ + TSL2561_ERROR_OK = 0, // Everything executed normally + TSL2561_ERROR_I2CINIT, // Unable to initialise I2C + TSL2561_ERROR_I2CBUSY, // I2C already in use + TSL2561_ERROR_LAST +} +tsl2561Error_t; + +tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl); +tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain); +tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir); +uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1); + +#endif + + From 223a1c8b86852c362714f0ca7f476e8bee7a6bfd Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 12:46:53 +0200 Subject: [PATCH 02/13] Small change --- app/tsl2561/tsl2561.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index 21b76ccb..6bfd8773 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -65,6 +65,7 @@ */ /**************************************************************************/ #include "tsl2561.h" + static const uint32_t tsl2561_i2c_id = 0; static bool _tsl2561Initialised = 0; static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; @@ -212,13 +213,13 @@ tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir) switch (_tsl2561IntegrationTime) { case TSL2561_INTEGRATIONTIME_13MS: - systickDelay(14); + os_delay_us(14000);//systickDelay(14); break; case TSL2561_INTEGRATIONTIME_101MS: - systickDelay(102); + os_delay_us(102000);//systickDelay(102); break; default: - systickDelay(400); + os_delay_us(400000);//systickDelay(400); break; } From 335ea87964cd0b49c4a26e96674b1e8b091c8324 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 14:25:47 +0200 Subject: [PATCH 03/13] Glue to integrate with nodeMCU makefiles etc --- app/Makefile | 7 +++++-- app/modules/modules.h | 2 +- app/tsl2561/Makefile | 49 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 app/tsl2561/Makefile diff --git a/app/Makefile b/app/Makefile index 8275a264..64c92a5e 100644 --- a/app/Makefile +++ b/app/Makefile @@ -39,7 +39,8 @@ SUBDIRS= \ spiffs \ cjson \ crypto \ - dhtlib + dhtlib \ + tsl2561 endif # } PDIR @@ -90,7 +91,9 @@ COMPONENTS_eagle.app.v6 = \ cjson/libcjson.a \ crypto/libcrypto.a \ dhtlib/libdhtlib.a \ - modules/libmodules.a + tsl2561/tsl2561lib.a \ + modules/libmodules.a + LINKFLAGS_eagle.app.v6 = \ -L../lib \ diff --git a/app/modules/modules.h b/app/modules/modules.h index db5a8b8b..b4bc09ed 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -257,5 +257,5 @@ ROM_MODULES_RTCFIFO \ ROM_MODULES_SNTP \ ROM_MODULES_BMP085 \ - ROM_MODULES_TSL2561 \ + ROM_MODULES_TSL2561 #endif diff --git a/app/tsl2561/Makefile b/app/tsl2561/Makefile new file mode 100644 index 00000000..34d6e7dc --- /dev/null +++ b/app/tsl2561/Makefile @@ -0,0 +1,49 @@ + +############################################################# +# Required variables for each makefile +# Discard this section from all parent makefiles +# Expected variables (with automatic defaults): +# CSRCS (all "C" files in the dir) +# SUBDIRS (all subdirs with a Makefile) +# GEN_LIBS - list of libs to be generated () +# GEN_IMAGES - list of images to be generated () +# COMPONENTS_xxx - a list of libs/objs in the form +# subdir/lib to be extracted and rolled up into +# a generated lib/image xxx.a () +# +ifndef PDIR +GEN_LIBS = tsl2561lib.a +endif + + +############################################################# +# Configuration i.e. compile options etc. +# Target specific stuff (defines etc.) goes in here! +# Generally values applying to a tree are captured in the +# makefile at its root level - these are then overridden +# for a subtree within the makefile rooted therein +# +#DEFINES += + +############################################################# +# Recursion Magic - Don't touch this!! +# +# Each subtree potentially has an include directory +# corresponding to the common APIs applicable to modules +# rooted at that subtree. Accordingly, the INCLUDE PATH +# of a module can only contain the include directories up +# its parent path, and not its siblings +# +# Required for each makefile to inherit from the parent +# + +INCLUDES := $(INCLUDES) -I $(PDIR)include +INCLUDES += -I ./ +INCLUDES += -I ./include +INCLUDES += -I ../include +INCLUDES += -I ../../include +INCLUDES += -I ../libc +INCLUDES += -I ../platform +PDIR := ../$(PDIR) +sinclude $(PDIR)Makefile + From 483dbebe24c57ff6bad7ce797d70b0e6ecc75479 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 14:26:42 +0200 Subject: [PATCH 04/13] Return error when calling functions before init TSL2561_ERROR_NOINIT --- app/modules/tsl2561.c | 1 + app/tsl2561/tsl2561.c | 10 ++++++---- app/tsl2561/tsl2561.h | 2 ++ 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index 3546f144..1a169dc4 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -71,6 +71,7 @@ const LUA_REG_TYPE tsl2561_map[] = { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) }, { LSTRKEY( "TSL2561_ERROR_I2CINIT" ), LNUMVAL( TSL2561_ERROR_I2CINIT ) }, { LSTRKEY( "TSL2561_ERROR_I2CBUSY" ), LNUMVAL( TSL2561_ERROR_I2CBUSY ) }, + { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT ) }, { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) }, { LSTRKEY( "TSL2561_INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index 6bfd8773..0ec40235 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -65,6 +65,8 @@ */ /**************************************************************************/ #include "tsl2561.h" +#include "platform.h" +#include "user_interface.h" static const uint32_t tsl2561_i2c_id = 0; static bool _tsl2561Initialised = 0; @@ -125,7 +127,7 @@ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) /**************************************************************************/ tsl2561Error_t tsl2561Enable(void) { - if (!_tsl2561Initialised) tsl2561Init(); + if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; // Enable the device by setting the control bit to 0x03 return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); @@ -138,7 +140,7 @@ tsl2561Error_t tsl2561Enable(void) /**************************************************************************/ tsl2561Error_t tsl2561Disable(void) { - if (!_tsl2561Initialised) tsl2561Init(); + if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; // Turn the device off to save power return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); @@ -171,7 +173,7 @@ tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl) /**************************************************************************/ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain) { - if (!_tsl2561Initialised) tsl2561Init(); + if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; tsl2561Error_t error = TSL2561_ERROR_OK; @@ -201,7 +203,7 @@ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gai /**************************************************************************/ tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir) { - if (!_tsl2561Initialised) tsl2561Init(); + if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; tsl2561Error_t error = TSL2561_ERROR_OK; diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h index 1ec143ec..061f263b 100644 --- a/app/tsl2561/tsl2561.h +++ b/app/tsl2561/tsl2561.h @@ -33,6 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /**************************************************************************/ +#include "c_types.h" #ifndef _TSL2561_H_ #define _TSL2561_H_ @@ -146,6 +147,7 @@ typedef enum TSL2561_ERROR_OK = 0, // Everything executed normally TSL2561_ERROR_I2CINIT, // Unable to initialise I2C TSL2561_ERROR_I2CBUSY, // I2C already in use + TSL2561_ERROR_NOINIT, // call init first TSL2561_ERROR_LAST } tsl2561Error_t; From d1758aee1b4da9ee4aca3a6c88b59c6a08653ebe Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 17:54:26 +0200 Subject: [PATCH 05/13] return correct number of variables --- app/modules/tsl2561.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index 1a169dc4..bd3c3ea4 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -56,7 +56,7 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { lua_pushnumber( L, ch1 ); lua_pushnumber( L, error ); - return 1; + return 3; } #define MIN_OPT_LEVEL 2 From 159e9c6acea3e38e66119ececf9c7972463163f3 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 17:55:07 +0200 Subject: [PATCH 06/13] make modulename lowercase --- app/modules/modules.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/modules/modules.h b/app/modules/modules.h index b4bc09ed..1279da87 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -222,7 +222,7 @@ #endif #if defined(LUA_USE_MODULES_TSL2561) -#define MODULES_TSL2561 "TSL2561" +#define MODULES_TSL2561 "tsl2561" #define ROM_MODULES_TSL2561 \ _ROM(MODULES_TSL2561, luaopen_tsl2561, tsl2561_map) #else From 7095c424774edea5812e105afa61dc8431d7ccdf Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 21:19:27 +0200 Subject: [PATCH 07/13] Attempt to fix reading of registers --- app/tsl2561/tsl2561.c | 26 ++++++++++++++------------ app/tsl2561/tsl2561.h | 6 +++--- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index 0ec40235..0d6cee26 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -1,7 +1,7 @@ /**************************************************************************/ /*! @file tsl2561.c - @author K. Townsend (microBuilder.eu) + @author K. Townsend (microBuilder.eu)/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) @brief Drivers for the TAOS TSL2561 I2C digital luminosity sensor @@ -21,7 +21,7 @@ uint32_t lux; // Initialise luminosity sensor - tsl2561Init(); + tsl2561Init(sda_pin, scl_pin); // Optional ... default setting is 400ms with no gain // Set timing to 101ms with no gain @@ -100,20 +100,22 @@ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); platform_i2c_send_byte(tsl2561_i2c_id, reg); platform_i2c_send_stop(tsl2561_i2c_id); + + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); + platform_i2c_send_stop(tsl2561_i2c_id); + + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg+1); + platform_i2c_send_stop(tsl2561_i2c_id); + platform_i2c_send_start(tsl2561_i2c_id); platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); uint8_t ch_high = platform_i2c_recv_byte(tsl2561_i2c_id, 0); platform_i2c_send_stop(tsl2561_i2c_id); - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); - platform_i2c_send_byte(tsl2561_i2c_id, reg); - platform_i2c_send_stop(tsl2561_i2c_id); - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); - uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); - platform_i2c_send_stop(tsl2561_i2c_id); - // Shift values to create properly formed integer (low byte first) *value = (ch_low | (ch_high << 8)); @@ -181,7 +183,7 @@ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gai error = tsl2561Enable(); if (error) return error; - // Turn the device off to save power + // set timing and gain on device error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain); if (error) return error; diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h index 061f263b..98cb9f6f 100644 --- a/app/tsl2561/tsl2561.h +++ b/app/tsl2561/tsl2561.h @@ -1,13 +1,13 @@ /**************************************************************************/ /*! @file tsl2561.h - @author K. Townsend (microBuilder.eu) + @author K. Townsend (microBuilder.eu) / Adapted for nodeMCU by Michael Lucas (Aeprox @github) @section LICENSE Software License Agreement (BSD License) - Copyright (c) 2010, microBuilder SARL + Copyright (c) 2010, microBuilder SARL/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) All rights reserved. Redistribution and use in source and binary forms, with or without @@ -41,7 +41,7 @@ #define TSL2561_PACKAGE_CS // Lux calculations differ slightly for CS package // #define TSL2561_PACKAGE_T_FN_CL -#define TSL2561_ADDRESS (0x72) // 0111001 shifted left 1 bit = 0x72 (ADDR = GND or floating) +#define TSL2561_ADDRESS (0x39) // or 0x29, or 0x49 #define TSL2561_READBIT (0x01) #define TSL2561_COMMAND_BIT (0x80) // Must be 1 From 4df5d5c166ea647430daca266e36838304c44f25 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 21:43:29 +0200 Subject: [PATCH 08/13] Fix channel1 reading Give sensor more time to integrate (400ms => 404ms) --- app/tsl2561/tsl2561.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index 0d6cee26..c3a2a197 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -223,7 +223,7 @@ tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir) os_delay_us(102000);//systickDelay(102); break; default: - os_delay_us(400000);//systickDelay(400); + os_delay_us(404000);//systickDelay(404); break; } From 29ee02f68002a65c374e70ec521e36a1472f7002 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 22:34:54 +0200 Subject: [PATCH 09/13] Revert version change --- app/include/user_version.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/include/user_version.h b/app/include/user_version.h index b4018097..40ce7eb4 100644 --- a/app/include/user_version.h +++ b/app/include/user_version.h @@ -7,6 +7,6 @@ #define NODE_VERSION_INTERNAL 0U #define NODE_VERSION "NodeMCU 0.9.6" -#define BUILD_DATE "build 20150822" +#define BUILD_DATE "build 20150627" #endif /* __USER_VERSION_H__ */ From 0caf745d8e12adbbef1c8a02b329fbd1f6f57b0e Mon Sep 17 00:00:00 2001 From: aeprox Date: Sun, 23 Aug 2015 01:42:39 +0200 Subject: [PATCH 10/13] Improve lua API interface --- app/modules/tsl2561.c | 47 ++++++++++++++++++++++++++++++++----------- app/tsl2561/tsl2561.c | 2 +- app/tsl2561/tsl2561.h | 4 ++-- 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index bd3c3ea4..18e39979 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -13,7 +13,9 @@ static uint16_t ch0; static uint16_t ch1; - +/* + * Lua: error = tsl2561.init(sdapin, sclpin) + */ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { uint32_t sda; uint32_t scl; @@ -33,23 +35,44 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { lua_pushnumber( L, error ); return 1; } - +/* + * Lua: error = tsl2561.settiming(integration, gain) + */ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { // check variables if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { return luaL_error(L, "wrong arg range"); } uint8_t integration = luaL_checkinteger(L, 1); + if(!((integration == TSL2561_INTEGRATIONTIME_13MS) ||(integration == TSL2561_INTEGRATIONTIME_101MS) || (integration == TSL2561_INTEGRATIONTIME_402MS))){ + return luaL_error(L, "wrong range for arg integration"); + } uint8_t gain = luaL_checkinteger(L, 2); + if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))){ + return luaL_error(L, "wrong range for arg gain"); + } lua_pushnumber( L, tsl2561SetTiming(integration, gain) ); return 1; } +/* + * Lua: lux, error = tsl2561.getlux() + */ static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { - lua_pushnumber( L, tsl2561CalculateLux(ch0,ch1) ); - return 1; + uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); + if (error){ + lua_pushnumber(L, 0); + lua_pushnumber(L, error); + } + else{ + lua_pushnumber(L, tsl2561CalculateLux(ch0,ch1)); + lua_pushnumber(L, error); + } + return 2; } - +/* + * Lua: tsl2561.getrawchannels() + */ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); lua_pushnumber( L, ch0 ); @@ -64,8 +87,8 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { const LUA_REG_TYPE tsl2561_map[] = { { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, - { LSTRKEY( "calclux" ), LFUNCVAL( tsl2561_lua_calclux )}, - { LSTRKEY( "channels_raw" ), LFUNCVAL( tsl2561_lua_getchannels )}, + { LSTRKEY( "getlux" ), LFUNCVAL( tsl2561_lua_calclux )}, + { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )}, { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) }, @@ -74,11 +97,11 @@ const LUA_REG_TYPE tsl2561_map[] = { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT ) }, { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) }, - { LSTRKEY( "TSL2561_INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, - { LSTRKEY( "TSL2561_INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, - { LSTRKEY( "TSL2561_INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, - { LSTRKEY( "TSL2561_GAIN_0X" ), LNUMVAL( TSL2561_GAIN_0X ) }, - { LSTRKEY( "TSL2561_GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, + { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, + { LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, + { LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, + { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X ) }, + { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, { LNILKEY, LNILVAL} }; diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index c3a2a197..9dd3d744 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -71,7 +71,7 @@ static const uint32_t tsl2561_i2c_id = 0; static bool _tsl2561Initialised = 0; static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; -static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_0X; +static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X; /**************************************************************************/ diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h index 98cb9f6f..f4b5b203 100644 --- a/app/tsl2561/tsl2561.h +++ b/app/tsl2561/tsl2561.h @@ -7,7 +7,7 @@ Software License Agreement (BSD License) - Copyright (c) 2010, microBuilder SARL/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) + Copyright (c) 2010, microBuilder SARL All rights reserved. Redistribution and use in source and binary forms, with or without @@ -137,7 +137,7 @@ tsl2561IntegrationTime_t; typedef enum { - TSL2561_GAIN_0X = 0x00, // No gain + TSL2561_GAIN_1X = 0x00, // No gain TSL2561_GAIN_16X = 0x10, // 16x gain } tsl2561Gain_t; From fd93a09a8805789ffac54b273f0e170d8b74aa4a Mon Sep 17 00:00:00 2001 From: aeprox Date: Sun, 23 Aug 2015 02:27:39 +0200 Subject: [PATCH 11/13] Code formatting fixes --- app/Makefile | 1 - app/modules/modules.h | 3 +- app/modules/tsl2561.c | 95 +++++---- app/tsl2561/tsl2561.c | 480 ++++++++++++++++++++++-------------------- 4 files changed, 297 insertions(+), 282 deletions(-) diff --git a/app/Makefile b/app/Makefile index 64c92a5e..2d1933ba 100644 --- a/app/Makefile +++ b/app/Makefile @@ -93,7 +93,6 @@ COMPONENTS_eagle.app.v6 = \ dhtlib/libdhtlib.a \ tsl2561/tsl2561lib.a \ modules/libmodules.a - LINKFLAGS_eagle.app.v6 = \ -L../lib \ diff --git a/app/modules/modules.h b/app/modules/modules.h index 1279da87..f9c4d595 100644 --- a/app/modules/modules.h +++ b/app/modules/modules.h @@ -257,5 +257,6 @@ ROM_MODULES_RTCFIFO \ ROM_MODULES_SNTP \ ROM_MODULES_BMP085 \ - ROM_MODULES_TSL2561 + ROM_MODULES_TSL2561 + #endif diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index 18e39979..81868f4f 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -2,7 +2,7 @@ * tsl2561.c * * Created on: Aug 21, 2015 - * Author: Michael Lucas (Aeprox @github) + * Author: Michael Lucas (Aeprox @github) */ #include "lualib.h" #include "lauxlib.h" @@ -14,70 +14,69 @@ static uint16_t ch0; static uint16_t ch1; /* - * Lua: error = tsl2561.init(sdapin, sclpin) + * Lua: error = tsl2561.init(sdapin, sclpin) */ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { - uint32_t sda; - uint32_t scl; + uint32_t sda; + uint32_t scl; - if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { - return luaL_error(L, "wrong arg range"); - } + if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { + return luaL_error(L, "wrong arg range"); + } - sda = luaL_checkinteger(L, 1); - scl = luaL_checkinteger(L, 2); + sda = luaL_checkinteger(L, 1); + scl = luaL_checkinteger(L, 2); - if (scl == 0 || sda == 0) { - return luaL_error(L, "no i2c for D0"); - } + if (scl == 0 || sda == 0) { + return luaL_error(L, "no i2c for D0"); + } - uint8_t error = tsl2561Init(sda,scl); - lua_pushnumber( L, error ); - return 1; + uint8_t error = tsl2561Init(sda, scl); + lua_pushnumber(L, error); + return 1; } /* - * Lua: error = tsl2561.settiming(integration, gain) + * Lua: error = tsl2561.settiming(integration, gain) */ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { // check variables if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { - return luaL_error(L, "wrong arg range"); + return luaL_error(L, "wrong arg range"); } uint8_t integration = luaL_checkinteger(L, 1); - if(!((integration == TSL2561_INTEGRATIONTIME_13MS) ||(integration == TSL2561_INTEGRATIONTIME_101MS) || (integration == TSL2561_INTEGRATIONTIME_402MS))){ + if (!((integration == TSL2561_INTEGRATIONTIME_13MS) || (integration == TSL2561_INTEGRATIONTIME_101MS) || (integration == TSL2561_INTEGRATIONTIME_402MS))) { return luaL_error(L, "wrong range for arg integration"); } uint8_t gain = luaL_checkinteger(L, 2); - if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))){ + if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))) { return luaL_error(L, "wrong range for arg gain"); } - lua_pushnumber( L, tsl2561SetTiming(integration, gain) ); + lua_pushnumber(L, tsl2561SetTiming(integration, gain)); return 1; } /* - * Lua: lux, error = tsl2561.getlux() + * Lua: lux, error = tsl2561.getlux() */ static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { - uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); - if (error){ + uint8_t error = tsl2561GetLuminosity(&ch0, &ch1); + if (error) { lua_pushnumber(L, 0); lua_pushnumber(L, error); - } - else{ - lua_pushnumber(L, tsl2561CalculateLux(ch0,ch1)); + } else { + lua_pushnumber(L, tsl2561CalculateLux(ch0, ch1)); lua_pushnumber(L, error); } return 2; } /* - * Lua: tsl2561.getrawchannels() + * Lua: tsl2561.getrawchannels() */ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { - uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); - lua_pushnumber( L, ch0 ); - lua_pushnumber( L, ch1 ); - lua_pushnumber( L, error ); + uint8_t error = tsl2561GetLuminosity(&ch0, &ch1); + lua_pushnumber(L, ch0); + lua_pushnumber(L, ch1); + lua_pushnumber(L, error); return 3; } @@ -86,27 +85,27 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { #include "lrodefs.h" const LUA_REG_TYPE tsl2561_map[] = { - { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, - { LSTRKEY( "getlux" ), LFUNCVAL( tsl2561_lua_calclux )}, - { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )}, - { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, + { LSTRKEY( "settiming" ), LFUNCVAL( tsl2561_lua_settiming)}, + { LSTRKEY( "getlux" ), LFUNCVAL( tsl2561_lua_calclux )}, + { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )}, + { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, - { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) }, - { LSTRKEY( "TSL2561_ERROR_I2CINIT" ), LNUMVAL( TSL2561_ERROR_I2CINIT ) }, - { LSTRKEY( "TSL2561_ERROR_I2CBUSY" ), LNUMVAL( TSL2561_ERROR_I2CBUSY ) }, - { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT ) }, - { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) }, + { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK )}, + { LSTRKEY( "TSL2561_ERROR_I2CINIT" ), LNUMVAL( TSL2561_ERROR_I2CINIT )}, + { LSTRKEY( "TSL2561_ERROR_I2CBUSY" ), LNUMVAL( TSL2561_ERROR_I2CBUSY )}, + { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT )}, + { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST )}, - { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, - { LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, - { LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, - { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X ) }, - { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, + { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS )}, + { LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS )}, + { LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS )}, + { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X )}, + { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X )}, - { LNILKEY, LNILVAL} + { LNILKEY, LNILVAL} }; LUALIB_API int luaopen_tsl2561(lua_State *L) { - LREGISTER(L, "tsl2561", tsl2561_map); - return 1; + LREGISTER(L, "tsl2561", tsl2561_map); + return 1; } diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index 9dd3d744..b37ba890 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -1,68 +1,68 @@ /**************************************************************************/ /*! - @file tsl2561.c - @author K. Townsend (microBuilder.eu)/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) - - @brief Drivers for the TAOS TSL2561 I2C digital luminosity sensor + @file tsl2561.c + @author K. Townsend (microBuilder.eu)/ Adapted for nodeMCU by Michael Lucas (Aeprox @github) + + @brief Drivers for the TAOS TSL2561 I2C digital luminosity sensor - @section DESCRIPTION + @section DESCRIPTION - The TSL2561 is a 16-bit digital luminosity sensor the approximates - the human eye's response to light. It contains one broadband - photodiode that measures visible plus infrared light (channel 0) - and one infrared photodiode (channel 1). + The TSL2561 is a 16-bit digital luminosity sensor the approximates + the human eye's response to light. It contains one broadband + photodiode that measures visible plus infrared light (channel 0) + and one infrared photodiode (channel 1). - @section EXAMPLE + @section EXAMPLE - @code - #include "drivers/sensors/tsl2561/tsl2561.h" - ... - uint16_t broadband, ir; - uint32_t lux; + @code + #include "drivers/sensors/tsl2561/tsl2561.h" + ... + uint16_t broadband, ir; + uint32_t lux; - // Initialise luminosity sensor - tsl2561Init(sda_pin, scl_pin); + // Initialise luminosity sensor + tsl2561Init(sda_pin, scl_pin); - // Optional ... default setting is 400ms with no gain - // Set timing to 101ms with no gain - tsl2561SetTiming(TSL2561_INTEGRATIONTIME_101MS, TSL2561_GAIN_0X); + // Optional ... default setting is 400ms with no gain + // Set timing to 101ms with no gain + tsl2561SetTiming(TSL2561_INTEGRATIONTIME_101MS, TSL2561_GAIN_0X); - // Check luminosity level and calculate lux - tsl2561GetLuminosity(&broadband, &ir); - lux = tsl2561CalculateLux(broadband, ir); - printf("Broadband: %u, IR: %u, Lux: %d %s", broadband, ir, lux, CFG_PRINTF_NEWLINE); + // Check luminosity level and calculate lux + tsl2561GetLuminosity(&broadband, &ir); + lux = tsl2561CalculateLux(broadband, ir); + printf("Broadband: %u, IR: %u, Lux: %d %s", broadband, ir, lux, CFG_PRINTF_NEWLINE); - @endcode + @endcode - @section LICENSE + @section LICENSE - Software License Agreement (BSD License) + Software License Agreement (BSD License) - Copyright (c) 2010, microBuilder SARL - All rights reserved. + Copyright (c) 2010, microBuilder SARL + All rights reserved. - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ /**************************************************************************/ #include "tsl2561.h" #include "platform.h" @@ -73,264 +73,280 @@ static bool _tsl2561Initialised = 0; static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X; - /**************************************************************************/ /*! - @brief Writes an 8 bit values over I2C -*/ + @brief Writes an 8 bit values over I2C + */ /**************************************************************************/ -tsl2561Error_t tsl2561Write8 (uint8_t reg, uint8_t value) -{ - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); - platform_i2c_send_byte(tsl2561_i2c_id, reg); - platform_i2c_send_byte(tsl2561_i2c_id, value); - platform_i2c_send_stop(tsl2561_i2c_id); - return TSL2561_ERROR_OK; +tsl2561Error_t tsl2561Write8(uint8_t reg, uint8_t value) { + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg); + platform_i2c_send_byte(tsl2561_i2c_id, value); + platform_i2c_send_stop(tsl2561_i2c_id); + return TSL2561_ERROR_OK; } /**************************************************************************/ /*! - @brief Reads a 16 bit values over I2C -*/ + @brief Reads a 16 bit values over I2C + */ /**************************************************************************/ -tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) -{ - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); - platform_i2c_send_byte(tsl2561_i2c_id, reg); - platform_i2c_send_stop(tsl2561_i2c_id); +tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) { + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg); + platform_i2c_send_stop(tsl2561_i2c_id); - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); - uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); - platform_i2c_send_stop(tsl2561_i2c_id); + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); + platform_i2c_send_stop(tsl2561_i2c_id); - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); - platform_i2c_send_byte(tsl2561_i2c_id, reg+1); - platform_i2c_send_stop(tsl2561_i2c_id); + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_byte(tsl2561_i2c_id, reg + 1); + platform_i2c_send_stop(tsl2561_i2c_id); - platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); - uint8_t ch_high = platform_i2c_recv_byte(tsl2561_i2c_id, 0); - platform_i2c_send_stop(tsl2561_i2c_id); + platform_i2c_send_start(tsl2561_i2c_id); + platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + uint8_t ch_high = platform_i2c_recv_byte(tsl2561_i2c_id, 0); + platform_i2c_send_stop(tsl2561_i2c_id); - // Shift values to create properly formed integer (low byte first) - *value = (ch_low | (ch_high << 8)); + // Shift values to create properly formed integer (low byte first) + *value = (ch_low | (ch_high << 8)); - return TSL2561_ERROR_OK; + return TSL2561_ERROR_OK; } /**************************************************************************/ /*! - @brief Enables the device -*/ + @brief Enables the device + */ /**************************************************************************/ -tsl2561Error_t tsl2561Enable(void) -{ - if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; +tsl2561Error_t tsl2561Enable(void) { + if (!_tsl2561Initialised) + return TSL2561_ERROR_NOINIT; - // Enable the device by setting the control bit to 0x03 - return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); + // Enable the device by setting the control bit to 0x03 + return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, + TSL2561_CONTROL_POWERON); } /**************************************************************************/ /*! - @brief Disables the device (putting it in lower power sleep mode) -*/ + @brief Disables the device (putting it in lower power sleep mode) + */ /**************************************************************************/ -tsl2561Error_t tsl2561Disable(void) -{ - if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; +tsl2561Error_t tsl2561Disable(void) { + if (!_tsl2561Initialised) + return TSL2561_ERROR_NOINIT; - // Turn the device off to save power - return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); + // Turn the device off to save power + return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, + TSL2561_CONTROL_POWEROFF); } /**************************************************************************/ /*! - @brief Initialises the I2C block -*/ + @brief Initialises the I2C block + */ /**************************************************************************/ -tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl) -{ +tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl) { // Initialise I2C platform_i2c_setup(tsl2561_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW); - - _tsl2561Initialised = 1; - // Set default integration time and gain - tsl2561SetTiming(_tsl2561IntegrationTime, _tsl2561Gain); + _tsl2561Initialised = 1; - // Note: by default, the device is in power down mode on bootup + // Set default integration time and gain + tsl2561SetTiming(_tsl2561IntegrationTime, _tsl2561Gain); - return TSL2561_ERROR_OK; + // Note: by default, the device is in power down mode on bootup + + return TSL2561_ERROR_OK; } /**************************************************************************/ /*! - @brief Sets the integration time and gain (controls sensitivity) -*/ + @brief Sets the integration time and gain (controls sensitivity) + */ /**************************************************************************/ -tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain) -{ - if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; +tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain) { + if (!_tsl2561Initialised) + return TSL2561_ERROR_NOINIT; - tsl2561Error_t error = TSL2561_ERROR_OK; + tsl2561Error_t error = TSL2561_ERROR_OK; - // Enable the device by setting the control bit to 0x03 - error = tsl2561Enable(); - if (error) return error; + // Enable the device by setting the control bit to 0x03 + error = tsl2561Enable(); + if (error) + return error; - // set timing and gain on device - error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain); - if (error) return error; + // set timing and gain on device + error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain); + if (error) + return error; - // Update value placeholders - _tsl2561IntegrationTime = integration; - _tsl2561Gain = gain; + // Update value placeholders + _tsl2561IntegrationTime = integration; + _tsl2561Gain = gain; - // Turn the device off to save power - error = tsl2561Disable(); - if (error) return error; + // Turn the device off to save power + error = tsl2561Disable(); + if (error) + return error; - return error; + return error; } /**************************************************************************/ /*! - @brief Reads the luminosity on both channels from the TSL2561 -*/ + @brief Reads the luminosity on both channels from the TSL2561 + */ /**************************************************************************/ -tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir) -{ - if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; +tsl2561Error_t tsl2561GetLuminosity(uint16_t *broadband, uint16_t *ir) { + if (!_tsl2561Initialised) + return TSL2561_ERROR_NOINIT; - tsl2561Error_t error = TSL2561_ERROR_OK; + tsl2561Error_t error = TSL2561_ERROR_OK; - // Enable the device by setting the control bit to 0x03 - error = tsl2561Enable(); - if (error) return error; + // Enable the device by setting the control bit to 0x03 + error = tsl2561Enable(); + if (error) + return error; - // Wait x ms for ADC to complete - switch (_tsl2561IntegrationTime) - { - case TSL2561_INTEGRATIONTIME_13MS: - os_delay_us(14000);//systickDelay(14); - break; - case TSL2561_INTEGRATIONTIME_101MS: - os_delay_us(102000);//systickDelay(102); - break; - default: - os_delay_us(404000);//systickDelay(404); - break; - } + // Wait x ms for ADC to complete + switch (_tsl2561IntegrationTime) { + case TSL2561_INTEGRATIONTIME_13MS: + os_delay_us(14000); //systickDelay(14); + break; + case TSL2561_INTEGRATIONTIME_101MS: + os_delay_us(102000); //systickDelay(102); + break; + default: + os_delay_us(404000); //systickDelay(404); + break; + } - // Reads two byte value from channel 0 (visible + infrared) - error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband); - if (error) return error; + // Reads two byte value from channel 0 (visible + infrared) + error = tsl2561Read16( + TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband); + if (error) + return error; - // Reads two byte value from channel 1 (infrared) - error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir); - if (error) return error; + // Reads two byte value from channel 1 (infrared) + error = tsl2561Read16( + TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir); + if (error) + return error; - // Turn the device off to save power - error = tsl2561Disable(); - if (error) return error; + // Turn the device off to save power + error = tsl2561Disable(); + if (error) + return error; - return error; + return error; } /**************************************************************************/ /*! - @brief Calculates LUX from the supplied ch0 (broadband) and ch1 - (IR) readings -*/ + @brief Calculates LUX from the supplied ch0 (broadband) and ch1 + (IR) readings + */ /**************************************************************************/ -uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) -{ - unsigned long chScale; - unsigned long channel1; - unsigned long channel0; +uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) { + unsigned long chScale; + unsigned long channel1; + unsigned long channel0; - switch (_tsl2561IntegrationTime) - { - case TSL2561_INTEGRATIONTIME_13MS: - chScale = TSL2561_LUX_CHSCALE_TINT0; - break; - case TSL2561_INTEGRATIONTIME_101MS: - chScale = TSL2561_LUX_CHSCALE_TINT1; - break; - default: // No scaling ... integration time = 402ms - chScale = (1 << TSL2561_LUX_CHSCALE); - break; - } + switch (_tsl2561IntegrationTime) { + case TSL2561_INTEGRATIONTIME_13MS: + chScale = TSL2561_LUX_CHSCALE_TINT0; + break; + case TSL2561_INTEGRATIONTIME_101MS: + chScale = TSL2561_LUX_CHSCALE_TINT1; + break; + default: // No scaling ... integration time = 402ms + chScale = (1 << TSL2561_LUX_CHSCALE); + break; + } - // Scale for gain (1x or 16x) - if (!_tsl2561Gain) chScale = chScale << 4; + // Scale for gain (1x or 16x) + if (!_tsl2561Gain) + chScale = chScale << 4; - // scale the channel values - channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE; - channel1 = (ch1 * chScale) >> TSL2561_LUX_CHSCALE; + // scale the channel values + channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE; + channel1 = (ch1 * chScale) >> TSL2561_LUX_CHSCALE; - // find the ratio of the channel values (Channel1/Channel0) - unsigned long ratio1 = 0; - if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0; + // find the ratio of the channel values (Channel1/Channel0) + unsigned long ratio1 = 0; + if (channel0 != 0) + ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE + 1)) / channel0; - // round the ratio value - unsigned long ratio = (ratio1 + 1) >> 1; + // round the ratio value + unsigned long ratio = (ratio1 + 1) >> 1; - unsigned int b, m; + unsigned int b, m; #ifdef TSL2561_PACKAGE_CS - if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) - {b=TSL2561_LUX_B1C; m=TSL2561_LUX_M1C;} - else if (ratio <= TSL2561_LUX_K2C) - {b=TSL2561_LUX_B2C; m=TSL2561_LUX_M2C;} - else if (ratio <= TSL2561_LUX_K3C) - {b=TSL2561_LUX_B3C; m=TSL2561_LUX_M3C;} - else if (ratio <= TSL2561_LUX_K4C) - {b=TSL2561_LUX_B4C; m=TSL2561_LUX_M4C;} - else if (ratio <= TSL2561_LUX_K5C) - {b=TSL2561_LUX_B5C; m=TSL2561_LUX_M5C;} - else if (ratio <= TSL2561_LUX_K6C) - {b=TSL2561_LUX_B6C; m=TSL2561_LUX_M6C;} - else if (ratio <= TSL2561_LUX_K7C) - {b=TSL2561_LUX_B7C; m=TSL2561_LUX_M7C;} - else if (ratio > TSL2561_LUX_K8C) - {b=TSL2561_LUX_B8C; m=TSL2561_LUX_M8C;} + if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) { + b = TSL2561_LUX_B1C; + m = TSL2561_LUX_M1C; + } else if (ratio <= TSL2561_LUX_K2C) { + b = TSL2561_LUX_B2C; + m = TSL2561_LUX_M2C; + } else if (ratio <= TSL2561_LUX_K3C) { + b = TSL2561_LUX_B3C; + m = TSL2561_LUX_M3C; + } else if (ratio <= TSL2561_LUX_K4C) { + b = TSL2561_LUX_B4C; + m = TSL2561_LUX_M4C; + } else if (ratio <= TSL2561_LUX_K5C) { + b = TSL2561_LUX_B5C; + m = TSL2561_LUX_M5C; + } else if (ratio <= TSL2561_LUX_K6C) { + b = TSL2561_LUX_B6C; + m = TSL2561_LUX_M6C; + } else if (ratio <= TSL2561_LUX_K7C) { + b = TSL2561_LUX_B7C; + m = TSL2561_LUX_M7C; + } else if (ratio > TSL2561_LUX_K8C) { + b = TSL2561_LUX_B8C; + m = TSL2561_LUX_M8C; + } #else - if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) - {b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;} - else if (ratio <= TSL2561_LUX_K2T) - {b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;} - else if (ratio <= TSL2561_LUX_K3T) - {b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;} - else if (ratio <= TSL2561_LUX_K4T) - {b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;} - else if (ratio <= TSL2561_LUX_K5T) - {b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;} - else if (ratio <= TSL2561_LUX_K6T) - {b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;} - else if (ratio <= TSL2561_LUX_K7T) - {b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;} - else if (ratio > TSL2561_LUX_K8T) - {b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;} + if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) + { b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;} + else if (ratio <= TSL2561_LUX_K2T) + { b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;} + else if (ratio <= TSL2561_LUX_K3T) + { b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;} + else if (ratio <= TSL2561_LUX_K4T) + { b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;} + else if (ratio <= TSL2561_LUX_K5T) + { b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;} + else if (ratio <= TSL2561_LUX_K6T) + { b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;} + else if (ratio <= TSL2561_LUX_K7T) + { b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;} + else if (ratio > TSL2561_LUX_K8T) + { b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;} #endif - unsigned long temp; - temp = ((channel0 * b) - (channel1 * m)); + unsigned long temp; + temp = ((channel0 * b) - (channel1 * m)); - // do not allow negative lux value - if (temp < 0) temp = 0; + // do not allow negative lux value + if (temp < 0) + temp = 0; - // round lsb (2^(LUX_SCALE-1)) - temp += (1 << (TSL2561_LUX_LUXSCALE-1)); + // round lsb (2^(LUX_SCALE-1)) + temp += (1 << (TSL2561_LUX_LUXSCALE - 1)); - // strip off fractional portion - uint32_t lux = temp >> TSL2561_LUX_LUXSCALE; + // strip off fractional portion + uint32_t lux = temp >> TSL2561_LUX_LUXSCALE; - // Signal I2C had no errors - return lux; + // Signal I2C had no errors + return lux; } From 0f6a0c59a1fd5e87eeed63d155468dea4979c044 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sun, 23 Aug 2015 14:52:15 +0200 Subject: [PATCH 12/13] Documentation update --- app/modules/tsl2561.c | 17 +++++++++-------- app/tsl2561/tsl2561.h | 2 +- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index 81868f4f..d5b0c3c4 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -13,8 +13,9 @@ static uint16_t ch0; static uint16_t ch1; -/* - * Lua: error = tsl2561.init(sdapin, sclpin) + +/* Initialises the device on pins sdapin and sclpin + * Lua: status = tsl2561.init(sdapin, sclpin) */ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { uint32_t sda; @@ -35,8 +36,8 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { lua_pushnumber(L, error); return 1; } -/* - * Lua: error = tsl2561.settiming(integration, gain) +/* Sets the integration time and gain settings of the device + * Lua: status = tsl2561.settiming(integration, gain) */ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { // check variables @@ -55,8 +56,8 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { lua_pushnumber(L, tsl2561SetTiming(integration, gain)); return 1; } -/* - * Lua: lux, error = tsl2561.getlux() +/* Reads sensor values from device and return calculated lux + * Lua: lux, status = tsl2561.getlux() */ static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { uint8_t error = tsl2561GetLuminosity(&ch0, &ch1); @@ -69,8 +70,8 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { } return 2; } -/* - * Lua: tsl2561.getrawchannels() +/* Reads sensor values from device and returns them + * Lua: ch0, ch1, status = tsl2561.getrawchannels() */ static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { uint8_t error = tsl2561GetLuminosity(&ch0, &ch1); diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h index f4b5b203..1308f681 100644 --- a/app/tsl2561/tsl2561.h +++ b/app/tsl2561/tsl2561.h @@ -41,7 +41,7 @@ #define TSL2561_PACKAGE_CS // Lux calculations differ slightly for CS package // #define TSL2561_PACKAGE_T_FN_CL -#define TSL2561_ADDRESS (0x39) // or 0x29, or 0x49 +#define TSL2561_ADDRESS (0x39) // GND=>0x29, float=>0x39 or VDD=>0x49 #define TSL2561_READBIT (0x01) #define TSL2561_COMMAND_BIT (0x80) // Must be 1 From 537cd68078a7b0f53ab456869cff84dda09e1690 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sun, 23 Aug 2015 15:59:49 +0200 Subject: [PATCH 13/13] Made device package and address configurable at runtime --- app/modules/tsl2561.c | 37 +++++++++++++-- app/tsl2561/tsl2561.c | 108 +++++++++++++++++++++++------------------- app/tsl2561/tsl2561.h | 24 ++++++++-- 3 files changed, 111 insertions(+), 58 deletions(-) diff --git a/app/modules/tsl2561.c b/app/modules/tsl2561.c index d5b0c3c4..a0abbb96 100644 --- a/app/modules/tsl2561.c +++ b/app/modules/tsl2561.c @@ -15,12 +15,12 @@ static uint16_t ch0; static uint16_t ch1; /* Initialises the device on pins sdapin and sclpin - * Lua: status = tsl2561.init(sdapin, sclpin) + * Lua: status = tsl2561.init(sdapin, sclpin, address(optional), package(optional)) */ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { uint32_t sda; uint32_t scl; - + // check parameters if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) { return luaL_error(L, "wrong arg range"); } @@ -31,8 +31,28 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { if (scl == 0 || sda == 0) { return luaL_error(L, "no i2c for D0"); } - + // init I2C uint8_t error = tsl2561Init(sda, scl); + + // Parse optional parameters + if (lua_isnumber(L, 3)) { + uint8_t address = luaL_checkinteger(L, 3); + if (!((address == TSL2561_ADDRESS_GND) || (address == TSL2561_ADDRESS_FLOAT) || (address == TSL2561_ADDRESS_VDD))) { + return luaL_error(L, "Invalid argument: address"); + } + else{ + tsl2561SetAddress(address); + } + } + if (lua_isnumber(L, 4)) { + uint8_t package = luaL_checkinteger(L, 4); + if (!((package == TSL2561_PACKAGE_T_FN_CL) || (package == TSL2561_PACKAGE_CS))) { + return luaL_error(L, "Invalid argument: package"); + } + else{ + tsl2561SetPackage(package); + } + } lua_pushnumber(L, error); return 1; } @@ -46,11 +66,11 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { } uint8_t integration = luaL_checkinteger(L, 1); if (!((integration == TSL2561_INTEGRATIONTIME_13MS) || (integration == TSL2561_INTEGRATIONTIME_101MS) || (integration == TSL2561_INTEGRATIONTIME_402MS))) { - return luaL_error(L, "wrong range for arg integration"); + return luaL_error(L, "Invalid argument: integration"); } uint8_t gain = luaL_checkinteger(L, 2); if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))) { - return luaL_error(L, "wrong range for arg gain"); + return luaL_error(L, "Invalid argument: gain"); } lua_pushnumber(L, tsl2561SetTiming(integration, gain)); @@ -103,6 +123,13 @@ const LUA_REG_TYPE tsl2561_map[] = { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X )}, { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X )}, + { LSTRKEY( "PACKAGE_CS" ), LNUMVAL( TSL2561_PACKAGE_CS )}, + { LSTRKEY( "PACKAGE_T_FN_CL" ), LNUMVAL( TSL2561_PACKAGE_T_FN_CL )}, + + { LSTRKEY( "ADDRESS_GND" ), LNUMVAL( TSL2561_ADDRESS_GND )}, + { LSTRKEY( "ADDRESS_FLOAT" ), LNUMVAL( TSL2561_ADDRESS_FLOAT )}, + { LSTRKEY( "ADDRESS_VDD" ), LNUMVAL( TSL2561_ADDRESS_VDD )}, + { LNILKEY, LNILVAL} }; diff --git a/app/tsl2561/tsl2561.c b/app/tsl2561/tsl2561.c index b37ba890..a2a1af49 100644 --- a/app/tsl2561/tsl2561.c +++ b/app/tsl2561/tsl2561.c @@ -72,6 +72,8 @@ static const uint32_t tsl2561_i2c_id = 0; static bool _tsl2561Initialised = 0; static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X; +static tsl2561Address_t tsl2561Address = TSL2561_ADDRESS_FLOAT; +static tsl2561Package_t tsl2561Package = TSL2561_PACKAGE_T_FN_CL; /**************************************************************************/ /*! @@ -80,7 +82,7 @@ static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X; /**************************************************************************/ tsl2561Error_t tsl2561Write8(uint8_t reg, uint8_t value) { platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_TRANSMITTER); platform_i2c_send_byte(tsl2561_i2c_id, reg); platform_i2c_send_byte(tsl2561_i2c_id, value); platform_i2c_send_stop(tsl2561_i2c_id); @@ -94,22 +96,22 @@ tsl2561Error_t tsl2561Write8(uint8_t reg, uint8_t value) { /**************************************************************************/ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) { platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_TRANSMITTER); platform_i2c_send_byte(tsl2561_i2c_id, reg); platform_i2c_send_stop(tsl2561_i2c_id); platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_RECEIVER); uint8_t ch_low = platform_i2c_recv_byte(tsl2561_i2c_id, 0); platform_i2c_send_stop(tsl2561_i2c_id); platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER); + platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_TRANSMITTER); platform_i2c_send_byte(tsl2561_i2c_id, reg + 1); platform_i2c_send_stop(tsl2561_i2c_id); platform_i2c_send_start(tsl2561_i2c_id); - platform_i2c_send_address(tsl2561_i2c_id, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_RECEIVER); + platform_i2c_send_address(tsl2561_i2c_id, tsl2561Address, PLATFORM_I2C_DIRECTION_RECEIVER); uint8_t ch_high = platform_i2c_recv_byte(tsl2561_i2c_id, 0); platform_i2c_send_stop(tsl2561_i2c_id); @@ -147,6 +149,15 @@ tsl2561Error_t tsl2561Disable(void) { TSL2561_CONTROL_POWEROFF); } +void tsl2561SetAddress(uint8_t address){ + tsl2561Address = address; + return; +} +void tsl2561SetPackage(uint8_t package){ + tsl2561Package = package; + return; +} + /**************************************************************************/ /*! @brief Initialises the I2C block @@ -289,50 +300,51 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) { unsigned int b, m; -#ifdef TSL2561_PACKAGE_CS - if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) { - b = TSL2561_LUX_B1C; - m = TSL2561_LUX_M1C; - } else if (ratio <= TSL2561_LUX_K2C) { - b = TSL2561_LUX_B2C; - m = TSL2561_LUX_M2C; - } else if (ratio <= TSL2561_LUX_K3C) { - b = TSL2561_LUX_B3C; - m = TSL2561_LUX_M3C; - } else if (ratio <= TSL2561_LUX_K4C) { - b = TSL2561_LUX_B4C; - m = TSL2561_LUX_M4C; - } else if (ratio <= TSL2561_LUX_K5C) { - b = TSL2561_LUX_B5C; - m = TSL2561_LUX_M5C; - } else if (ratio <= TSL2561_LUX_K6C) { - b = TSL2561_LUX_B6C; - m = TSL2561_LUX_M6C; - } else if (ratio <= TSL2561_LUX_K7C) { - b = TSL2561_LUX_B7C; - m = TSL2561_LUX_M7C; - } else if (ratio > TSL2561_LUX_K8C) { - b = TSL2561_LUX_B8C; - m = TSL2561_LUX_M8C; + if (tsl2561Package == TSL2561_PACKAGE_CS){ + if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) { + b = TSL2561_LUX_B1C; + m = TSL2561_LUX_M1C; + } else if (ratio <= TSL2561_LUX_K2C) { + b = TSL2561_LUX_B2C; + m = TSL2561_LUX_M2C; + } else if (ratio <= TSL2561_LUX_K3C) { + b = TSL2561_LUX_B3C; + m = TSL2561_LUX_M3C; + } else if (ratio <= TSL2561_LUX_K4C) { + b = TSL2561_LUX_B4C; + m = TSL2561_LUX_M4C; + } else if (ratio <= TSL2561_LUX_K5C) { + b = TSL2561_LUX_B5C; + m = TSL2561_LUX_M5C; + } else if (ratio <= TSL2561_LUX_K6C) { + b = TSL2561_LUX_B6C; + m = TSL2561_LUX_M6C; + } else if (ratio <= TSL2561_LUX_K7C) { + b = TSL2561_LUX_B7C; + m = TSL2561_LUX_M7C; + } else if (ratio > TSL2561_LUX_K8C) { + b = TSL2561_LUX_B8C; + m = TSL2561_LUX_M8C; + } + } + else{ + if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) + { b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;} + else if (ratio <= TSL2561_LUX_K2T) + { b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;} + else if (ratio <= TSL2561_LUX_K3T) + { b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;} + else if (ratio <= TSL2561_LUX_K4T) + { b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;} + else if (ratio <= TSL2561_LUX_K5T) + { b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;} + else if (ratio <= TSL2561_LUX_K6T) + { b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;} + else if (ratio <= TSL2561_LUX_K7T) + { b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;} + else if (ratio > TSL2561_LUX_K8T) + { b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;} } -#else - if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) - { b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;} - else if (ratio <= TSL2561_LUX_K2T) - { b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;} - else if (ratio <= TSL2561_LUX_K3T) - { b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;} - else if (ratio <= TSL2561_LUX_K4T) - { b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;} - else if (ratio <= TSL2561_LUX_K5T) - { b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;} - else if (ratio <= TSL2561_LUX_K6T) - { b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;} - else if (ratio <= TSL2561_LUX_K7T) - { b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;} - else if (ratio > TSL2561_LUX_K8T) - { b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;} -#endif unsigned long temp; temp = ((channel0 * b) - (channel1 * m)); diff --git a/app/tsl2561/tsl2561.h b/app/tsl2561/tsl2561.h index 1308f681..dd166432 100644 --- a/app/tsl2561/tsl2561.h +++ b/app/tsl2561/tsl2561.h @@ -38,10 +38,6 @@ #ifndef _TSL2561_H_ #define _TSL2561_H_ -#define TSL2561_PACKAGE_CS // Lux calculations differ slightly for CS package -// #define TSL2561_PACKAGE_T_FN_CL - -#define TSL2561_ADDRESS (0x39) // GND=>0x29, float=>0x39 or VDD=>0x49 #define TSL2561_READBIT (0x01) #define TSL2561_COMMAND_BIT (0x80) // Must be 1 @@ -152,11 +148,29 @@ typedef enum } tsl2561Error_t; +// GND=>0x29, float=>0x39 or VDD=>0x49 +typedef enum +{ + TSL2561_ADDRESS_GND = 0x29, + TSL2561_ADDRESS_FLOAT = 0x39, + TSL2561_ADDRESS_VDD = 0x49, + +} +tsl2561Address_t; + +// Lux calculations differ slightly for CS package +typedef enum +{ + TSL2561_PACKAGE_CS = 0, + TSL2561_PACKAGE_T_FN_CL +}tsl2561Package_t; + tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl); tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain); tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir); uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1); - +void tsl2561SetAddress(uint8_t address); +void tsl2561SetPackage(uint8_t package); #endif