Made device package and address configurable at runtime

This commit is contained in:
aeprox 2015-08-23 15:59:49 +02:00
parent 0f6a0c59a1
commit 537cd68078
3 changed files with 111 additions and 58 deletions

View File

@ -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}
};

View File

@ -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,7 +300,7 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) {
unsigned int b, m;
#ifdef TSL2561_PACKAGE_CS
if (tsl2561Package == TSL2561_PACKAGE_CS){
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) {
b = TSL2561_LUX_B1C;
m = TSL2561_LUX_M1C;
@ -315,7 +326,8 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) {
b = TSL2561_LUX_B8C;
m = TSL2561_LUX_M8C;
}
#else
}
else{
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T))
{ b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;}
else if (ratio <= TSL2561_LUX_K2T)
@ -332,7 +344,7 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) {
{ 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));

View File

@ -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