2015-08-22 00:15:28 +02:00
|
|
|
/*
|
|
|
|
* tsl2561.c
|
|
|
|
*
|
|
|
|
* Created on: Aug 21, 2015
|
2015-08-23 02:27:39 +02:00
|
|
|
* Author: Michael Lucas (Aeprox @github)
|
2015-08-22 00:15:28 +02:00
|
|
|
*/
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-08-22 00:15:28 +02:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "../tsl2561/tsl2561.h"
|
|
|
|
|
|
|
|
static uint16_t ch0;
|
|
|
|
static uint16_t ch1;
|
2015-08-23 14:52:15 +02:00
|
|
|
|
|
|
|
/* Initialises the device on pins sdapin and sclpin
|
2015-08-23 15:59:49 +02:00
|
|
|
* Lua: status = tsl2561.init(sdapin, sclpin, address(optional), package(optional))
|
2015-08-23 01:42:39 +02:00
|
|
|
*/
|
2015-08-22 00:15:28 +02:00
|
|
|
static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) {
|
2015-08-23 02:27:39 +02:00
|
|
|
uint32_t sda;
|
|
|
|
uint32_t scl;
|
2015-08-23 15:59:49 +02:00
|
|
|
// check parameters
|
2015-08-23 02:27:39 +02:00
|
|
|
if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
|
|
|
|
return luaL_error(L, "wrong arg range");
|
|
|
|
}
|
2015-08-22 00:15:28 +02:00
|
|
|
|
2015-08-23 02:27:39 +02:00
|
|
|
sda = luaL_checkinteger(L, 1);
|
|
|
|
scl = luaL_checkinteger(L, 2);
|
2015-08-22 00:15:28 +02:00
|
|
|
|
2015-08-23 02:27:39 +02:00
|
|
|
if (scl == 0 || sda == 0) {
|
|
|
|
return luaL_error(L, "no i2c for D0");
|
|
|
|
}
|
2015-08-23 15:59:49 +02:00
|
|
|
// init I2C
|
2015-08-23 02:27:39 +02:00
|
|
|
uint8_t error = tsl2561Init(sda, scl);
|
2015-08-23 15:59:49 +02:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
2015-08-23 02:27:39 +02:00
|
|
|
lua_pushnumber(L, error);
|
|
|
|
return 1;
|
2015-08-22 00:15:28 +02:00
|
|
|
}
|
2015-08-23 14:52:15 +02:00
|
|
|
/* Sets the integration time and gain settings of the device
|
|
|
|
* Lua: status = tsl2561.settiming(integration, gain)
|
2015-08-23 01:42:39 +02:00
|
|
|
*/
|
2015-08-22 00:15:28 +02:00
|
|
|
static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) {
|
|
|
|
// check variables
|
|
|
|
if (!lua_isnumber(L, 1) || !lua_isnumber(L, 2)) {
|
2015-08-23 02:27:39 +02:00
|
|
|
return luaL_error(L, "wrong arg range");
|
2015-08-22 00:15:28 +02:00
|
|
|
}
|
|
|
|
uint8_t integration = luaL_checkinteger(L, 1);
|
2015-08-23 02:27:39 +02:00
|
|
|
if (!((integration == TSL2561_INTEGRATIONTIME_13MS) || (integration == TSL2561_INTEGRATIONTIME_101MS) || (integration == TSL2561_INTEGRATIONTIME_402MS))) {
|
2015-08-23 15:59:49 +02:00
|
|
|
return luaL_error(L, "Invalid argument: integration");
|
2015-08-23 01:42:39 +02:00
|
|
|
}
|
2015-08-22 00:15:28 +02:00
|
|
|
uint8_t gain = luaL_checkinteger(L, 2);
|
2015-08-23 02:27:39 +02:00
|
|
|
if (!((gain == TSL2561_GAIN_16X) || (gain == TSL2561_GAIN_1X))) {
|
2015-08-23 15:59:49 +02:00
|
|
|
return luaL_error(L, "Invalid argument: gain");
|
2015-08-23 01:42:39 +02:00
|
|
|
}
|
2015-08-22 00:15:28 +02:00
|
|
|
|
2015-08-23 02:27:39 +02:00
|
|
|
lua_pushnumber(L, tsl2561SetTiming(integration, gain));
|
2015-08-22 00:15:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2015-08-23 14:52:15 +02:00
|
|
|
/* Reads sensor values from device and return calculated lux
|
|
|
|
* Lua: lux, status = tsl2561.getlux()
|
2015-08-23 01:42:39 +02:00
|
|
|
*/
|
2015-08-22 00:15:28 +02:00
|
|
|
static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) {
|
2015-08-23 02:27:39 +02:00
|
|
|
uint8_t error = tsl2561GetLuminosity(&ch0, &ch1);
|
|
|
|
if (error) {
|
2015-08-23 01:42:39 +02:00
|
|
|
lua_pushnumber(L, 0);
|
|
|
|
lua_pushnumber(L, error);
|
2015-08-23 02:27:39 +02:00
|
|
|
} else {
|
|
|
|
lua_pushnumber(L, tsl2561CalculateLux(ch0, ch1));
|
2015-08-23 01:42:39 +02:00
|
|
|
lua_pushnumber(L, error);
|
|
|
|
}
|
|
|
|
return 2;
|
2015-08-22 00:15:28 +02:00
|
|
|
}
|
2015-08-23 14:52:15 +02:00
|
|
|
/* Reads sensor values from device and returns them
|
|
|
|
* Lua: ch0, ch1, status = tsl2561.getrawchannels()
|
2015-08-23 01:42:39 +02:00
|
|
|
*/
|
2015-08-22 00:15:28 +02:00
|
|
|
static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) {
|
2015-08-23 02:27:39 +02:00
|
|
|
uint8_t error = tsl2561GetLuminosity(&ch0, &ch1);
|
|
|
|
lua_pushnumber(L, ch0);
|
|
|
|
lua_pushnumber(L, ch1);
|
|
|
|
lua_pushnumber(L, error);
|
2015-08-22 00:15:28 +02:00
|
|
|
|
2015-08-22 17:54:26 +02:00
|
|
|
return 3;
|
2015-08-22 00:15:28 +02:00
|
|
|
}
|
|
|
|
|
2015-12-13 03:29:37 +01:00
|
|
|
// Module function map
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_BEGIN(tsl2561)
|
|
|
|
LROT_FUNCENTRY( settiming, tsl2561_lua_settiming )
|
|
|
|
LROT_FUNCENTRY( getlux, tsl2561_lua_calclux )
|
|
|
|
LROT_FUNCENTRY( getrawchannels, tsl2561_lua_getchannels )
|
|
|
|
LROT_FUNCENTRY( init, tsl2561_init )
|
|
|
|
LROT_NUMENTRY( TSL2561_OK, TSL2561_ERROR_OK )
|
|
|
|
LROT_NUMENTRY( TSL2561_ERROR_I2CINIT, TSL2561_ERROR_I2CINIT )
|
|
|
|
LROT_NUMENTRY( TSL2561_ERROR_I2CBUSY, TSL2561_ERROR_I2CBUSY )
|
|
|
|
LROT_NUMENTRY( TSL2561_ERROR_NOINIT, TSL2561_ERROR_NOINIT )
|
|
|
|
LROT_NUMENTRY( TSL2561_ERROR_LAST, TSL2561_ERROR_LAST )
|
|
|
|
LROT_NUMENTRY( INTEGRATIONTIME_13MS, TSL2561_INTEGRATIONTIME_13MS )
|
|
|
|
LROT_NUMENTRY( INTEGRATIONTIME_101MS, TSL2561_INTEGRATIONTIME_101MS )
|
|
|
|
LROT_NUMENTRY( INTEGRATIONTIME_402MS, TSL2561_INTEGRATIONTIME_402MS )
|
|
|
|
LROT_NUMENTRY( GAIN_1X, TSL2561_GAIN_1X )
|
|
|
|
LROT_NUMENTRY( GAIN_16X, TSL2561_GAIN_16X )
|
|
|
|
LROT_NUMENTRY( PACKAGE_CS, TSL2561_PACKAGE_CS )
|
|
|
|
LROT_NUMENTRY( PACKAGE_T_FN_CL, TSL2561_PACKAGE_T_FN_CL )
|
|
|
|
LROT_NUMENTRY( ADDRESS_GND, TSL2561_ADDRESS_GND )
|
|
|
|
LROT_NUMENTRY( ADDRESS_FLOAT, TSL2561_ADDRESS_FLOAT )
|
|
|
|
LROT_NUMENTRY( ADDRESS_VDD, TSL2561_ADDRESS_VDD )
|
|
|
|
LROT_END( tsl2561, NULL, 0 )
|
2015-08-22 00:15:28 +02:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
NODEMCU_MODULE(TSL2561, "tsl2561", tsl2561, NULL);
|