Commit fd93a09a authored by aeprox's avatar aeprox
Browse files

Code formatting fixes

parent 0caf745d
...@@ -94,7 +94,6 @@ COMPONENTS_eagle.app.v6 = \ ...@@ -94,7 +94,6 @@ COMPONENTS_eagle.app.v6 = \
tsl2561/tsl2561lib.a \ tsl2561/tsl2561lib.a \
modules/libmodules.a modules/libmodules.a
LINKFLAGS_eagle.app.v6 = \ LINKFLAGS_eagle.app.v6 = \
-L../lib \ -L../lib \
-Wl,--gc-sections \ -Wl,--gc-sections \
......
...@@ -258,4 +258,5 @@ ...@@ -258,4 +258,5 @@
ROM_MODULES_SNTP \ ROM_MODULES_SNTP \
ROM_MODULES_BMP085 \ ROM_MODULES_BMP085 \
ROM_MODULES_TSL2561 ROM_MODULES_TSL2561
#endif #endif
...@@ -31,8 +31,8 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) { ...@@ -31,8 +31,8 @@ static int ICACHE_FLASH_ATTR tsl2561_init(lua_State* L) {
return luaL_error(L, "no i2c for D0"); return luaL_error(L, "no i2c for D0");
} }
uint8_t error = tsl2561Init(sda,scl); uint8_t error = tsl2561Init(sda, scl);
lua_pushnumber( L, error ); lua_pushnumber(L, error);
return 1; return 1;
} }
/* /*
...@@ -44,28 +44,27 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) { ...@@ -44,28 +44,27 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_settiming(lua_State* L) {
return luaL_error(L, "wrong arg range"); return luaL_error(L, "wrong arg range");
} }
uint8_t integration = luaL_checkinteger(L, 1); 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"); return luaL_error(L, "wrong range for arg integration");
} }
uint8_t gain = luaL_checkinteger(L, 2); 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"); return luaL_error(L, "wrong range for arg gain");
} }
lua_pushnumber( L, tsl2561SetTiming(integration, gain) ); lua_pushnumber(L, tsl2561SetTiming(integration, gain));
return 1; return 1;
} }
/* /*
* Lua: lux, error = tsl2561.getlux() * Lua: lux, error = tsl2561.getlux()
*/ */
static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) {
uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); uint8_t error = tsl2561GetLuminosity(&ch0, &ch1);
if (error){ if (error) {
lua_pushnumber(L, 0); lua_pushnumber(L, 0);
lua_pushnumber(L, error); lua_pushnumber(L, error);
} } else {
else{ lua_pushnumber(L, tsl2561CalculateLux(ch0, ch1));
lua_pushnumber(L, tsl2561CalculateLux(ch0,ch1));
lua_pushnumber(L, error); lua_pushnumber(L, error);
} }
return 2; return 2;
...@@ -74,10 +73,10 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) { ...@@ -74,10 +73,10 @@ static int ICACHE_FLASH_ATTR tsl2561_lua_calclux(lua_State* L) {
* Lua: tsl2561.getrawchannels() * Lua: tsl2561.getrawchannels()
*/ */
static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) { static int ICACHE_FLASH_ATTR tsl2561_lua_getchannels(lua_State* L) {
uint8_t error = tsl2561GetLuminosity(&ch0,&ch1); uint8_t error = tsl2561GetLuminosity(&ch0, &ch1);
lua_pushnumber( L, ch0 ); lua_pushnumber(L, ch0);
lua_pushnumber( L, ch1 ); lua_pushnumber(L, ch1);
lua_pushnumber( L, error ); lua_pushnumber(L, error);
return 3; return 3;
} }
...@@ -91,17 +90,17 @@ const LUA_REG_TYPE tsl2561_map[] = ...@@ -91,17 +90,17 @@ const LUA_REG_TYPE tsl2561_map[] =
{ LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )}, { LSTRKEY( "getrawchannels" ), LFUNCVAL( tsl2561_lua_getchannels )},
{ LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )}, { LSTRKEY( "init" ), LFUNCVAL( tsl2561_init )},
{ LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK ) }, { LSTRKEY( "TSL2561_OK" ), LNUMVAL( TSL2561_ERROR_OK )},
{ LSTRKEY( "TSL2561_ERROR_I2CINIT" ), LNUMVAL( TSL2561_ERROR_I2CINIT ) }, { LSTRKEY( "TSL2561_ERROR_I2CINIT" ), LNUMVAL( TSL2561_ERROR_I2CINIT )},
{ LSTRKEY( "TSL2561_ERROR_I2CBUSY" ), LNUMVAL( TSL2561_ERROR_I2CBUSY ) }, { LSTRKEY( "TSL2561_ERROR_I2CBUSY" ), LNUMVAL( TSL2561_ERROR_I2CBUSY )},
{ LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT ) }, { LSTRKEY( "TSL2561_ERROR_NOINIT" ), LNUMVAL( TSL2561_ERROR_NOINIT )},
{ LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST ) }, { LSTRKEY( "TSL2561_ERROR_LAST" ), LNUMVAL( TSL2561_ERROR_LAST )},
{ LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS ) }, { LSTRKEY( "INTEGRATIONTIME_13MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_13MS )},
{ LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS ) }, { LSTRKEY( "INTEGRATIONTIME_101MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_101MS )},
{ LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS ) }, { LSTRKEY( "INTEGRATIONTIME_402MS" ), LNUMVAL( TSL2561_INTEGRATIONTIME_402MS )},
{ LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X ) }, { LSTRKEY( "GAIN_1X" ), LNUMVAL( TSL2561_GAIN_1X )},
{ LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X ) }, { LSTRKEY( "GAIN_16X" ), LNUMVAL( TSL2561_GAIN_16X )},
{ LNILKEY, LNILVAL} { LNILKEY, LNILVAL}
}; };
......
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
/**************************************************************************/ /**************************************************************************/
#include "tsl2561.h" #include "tsl2561.h"
#include "platform.h" #include "platform.h"
...@@ -73,14 +73,12 @@ static bool _tsl2561Initialised = 0; ...@@ -73,14 +73,12 @@ static bool _tsl2561Initialised = 0;
static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS; static tsl2561IntegrationTime_t _tsl2561IntegrationTime = TSL2561_INTEGRATIONTIME_402MS;
static tsl2561Gain_t _tsl2561Gain = TSL2561_GAIN_1X; 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) tsl2561Error_t tsl2561Write8(uint8_t reg, uint8_t value) {
{
platform_i2c_send_start(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, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER);
platform_i2c_send_byte(tsl2561_i2c_id, reg); platform_i2c_send_byte(tsl2561_i2c_id, reg);
...@@ -92,10 +90,9 @@ tsl2561Error_t tsl2561Write8 (uint8_t reg, uint8_t value) ...@@ -92,10 +90,9 @@ tsl2561Error_t tsl2561Write8 (uint8_t reg, uint8_t value)
/**************************************************************************/ /**************************************************************************/
/*! /*!
@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) tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) {
{
platform_i2c_send_start(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, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER);
platform_i2c_send_byte(tsl2561_i2c_id, reg); platform_i2c_send_byte(tsl2561_i2c_id, reg);
...@@ -108,7 +105,7 @@ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) ...@@ -108,7 +105,7 @@ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value)
platform_i2c_send_start(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, TSL2561_ADDRESS, PLATFORM_I2C_DIRECTION_TRANSMITTER);
platform_i2c_send_byte(tsl2561_i2c_id, reg+1); platform_i2c_send_byte(tsl2561_i2c_id, reg + 1);
platform_i2c_send_stop(tsl2561_i2c_id); platform_i2c_send_stop(tsl2561_i2c_id);
platform_i2c_send_start(tsl2561_i2c_id); platform_i2c_send_start(tsl2561_i2c_id);
...@@ -125,36 +122,37 @@ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value) ...@@ -125,36 +122,37 @@ tsl2561Error_t tsl2561Read16(uint8_t reg, uint16_t *value)
/**************************************************************************/ /**************************************************************************/
/*! /*!
@brief Enables the device @brief Enables the device
*/ */
/**************************************************************************/ /**************************************************************************/
tsl2561Error_t tsl2561Enable(void) tsl2561Error_t tsl2561Enable(void) {
{ if (!_tsl2561Initialised)
if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; return TSL2561_ERROR_NOINIT;
// Enable the device by setting the control bit to 0x03 // Enable the device by setting the control bit to 0x03
return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWERON); 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) tsl2561Error_t tsl2561Disable(void) {
{ if (!_tsl2561Initialised)
if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; return TSL2561_ERROR_NOINIT;
// Turn the device off to save power // Turn the device off to save power
return tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_CONTROL, TSL2561_CONTROL_POWEROFF); 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 // Initialise I2C
platform_i2c_setup(tsl2561_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW); platform_i2c_setup(tsl2561_i2c_id, sda, scl, PLATFORM_I2C_SPEED_SLOW);
...@@ -171,21 +169,23 @@ tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl) ...@@ -171,21 +169,23 @@ tsl2561Error_t tsl2561Init(uint8_t sda, uint8_t scl)
/**************************************************************************/ /**************************************************************************/
/*! /*!
@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) tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gain_t gain) {
{ if (!_tsl2561Initialised)
if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; 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 // Enable the device by setting the control bit to 0x03
error = tsl2561Enable(); error = tsl2561Enable();
if (error) return error; if (error)
return error;
// set timing and gain on device // set timing and gain on device
error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain); error = tsl2561Write8(TSL2561_COMMAND_BIT | TSL2561_REGISTER_TIMING, integration | gain);
if (error) return error; if (error)
return error;
// Update value placeholders // Update value placeholders
_tsl2561IntegrationTime = integration; _tsl2561IntegrationTime = integration;
...@@ -193,7 +193,8 @@ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gai ...@@ -193,7 +193,8 @@ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gai
// Turn the device off to save power // Turn the device off to save power
error = tsl2561Disable(); error = tsl2561Disable();
if (error) return error; if (error)
return error;
return error; return error;
} }
...@@ -201,43 +202,48 @@ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gai ...@@ -201,43 +202,48 @@ tsl2561Error_t tsl2561SetTiming(tsl2561IntegrationTime_t integration, tsl2561Gai
/**************************************************************************/ /**************************************************************************/
/*! /*!
@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) tsl2561Error_t tsl2561GetLuminosity(uint16_t *broadband, uint16_t *ir) {
{ if (!_tsl2561Initialised)
if (!_tsl2561Initialised) return TSL2561_ERROR_NOINIT; 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 // Enable the device by setting the control bit to 0x03
error = tsl2561Enable(); error = tsl2561Enable();
if (error) return error; if (error)
return error;
// Wait x ms for ADC to complete // Wait x ms for ADC to complete
switch (_tsl2561IntegrationTime) switch (_tsl2561IntegrationTime) {
{
case TSL2561_INTEGRATIONTIME_13MS: case TSL2561_INTEGRATIONTIME_13MS:
os_delay_us(14000);//systickDelay(14); os_delay_us(14000); //systickDelay(14);
break; break;
case TSL2561_INTEGRATIONTIME_101MS: case TSL2561_INTEGRATIONTIME_101MS:
os_delay_us(102000);//systickDelay(102); os_delay_us(102000); //systickDelay(102);
break; break;
default: default:
os_delay_us(404000);//systickDelay(404); os_delay_us(404000); //systickDelay(404);
break; break;
} }
// Reads two byte value from channel 0 (visible + infrared) // Reads two byte value from channel 0 (visible + infrared)
error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband); error = tsl2561Read16(
if (error) return error; TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband);
if (error)
return error;
// Reads two byte value from channel 1 (infrared) // Reads two byte value from channel 1 (infrared)
error = tsl2561Read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir); error = tsl2561Read16(
if (error) return error; TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir);
if (error)
return error;
// Turn the device off to save power // Turn the device off to save power
error = tsl2561Disable(); error = tsl2561Disable();
if (error) return error; if (error)
return error;
return error; return error;
} }
...@@ -246,16 +252,14 @@ tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir) ...@@ -246,16 +252,14 @@ tsl2561Error_t tsl2561GetLuminosity (uint16_t *broadband, uint16_t *ir)
/*! /*!
@brief Calculates LUX from the supplied ch0 (broadband) and ch1 @brief Calculates LUX from the supplied ch0 (broadband) and ch1
(IR) readings (IR) readings
*/ */
/**************************************************************************/ /**************************************************************************/
uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) {
{
unsigned long chScale; unsigned long chScale;
unsigned long channel1; unsigned long channel1;
unsigned long channel0; unsigned long channel0;
switch (_tsl2561IntegrationTime) switch (_tsl2561IntegrationTime) {
{
case TSL2561_INTEGRATIONTIME_13MS: case TSL2561_INTEGRATIONTIME_13MS:
chScale = TSL2561_LUX_CHSCALE_TINT0; chScale = TSL2561_LUX_CHSCALE_TINT0;
break; break;
...@@ -268,7 +272,8 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) ...@@ -268,7 +272,8 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1)
} }
// Scale for gain (1x or 16x) // Scale for gain (1x or 16x)
if (!_tsl2561Gain) chScale = chScale << 4; if (!_tsl2561Gain)
chScale = chScale << 4;
// scale the channel values // scale the channel values
channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE; channel0 = (ch0 * chScale) >> TSL2561_LUX_CHSCALE;
...@@ -276,7 +281,8 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) ...@@ -276,7 +281,8 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1)
// find the ratio of the channel values (Channel1/Channel0) // find the ratio of the channel values (Channel1/Channel0)
unsigned long ratio1 = 0; unsigned long ratio1 = 0;
if (channel0 != 0) ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE+1)) / channel0; if (channel0 != 0)
ratio1 = (channel1 << (TSL2561_LUX_RATIOSCALE + 1)) / channel0;
// round the ratio value // round the ratio value
unsigned long ratio = (ratio1 + 1) >> 1; unsigned long ratio = (ratio1 + 1) >> 1;
...@@ -284,49 +290,59 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1) ...@@ -284,49 +290,59 @@ uint32_t tsl2561CalculateLux(uint16_t ch0, uint16_t ch1)
unsigned int b, m; unsigned int b, m;
#ifdef TSL2561_PACKAGE_CS #ifdef TSL2561_PACKAGE_CS
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1C)) {
{b=TSL2561_LUX_B1C; m=TSL2561_LUX_M1C;} b = TSL2561_LUX_B1C;
else if (ratio <= TSL2561_LUX_K2C) m = TSL2561_LUX_M1C;
{b=TSL2561_LUX_B2C; m=TSL2561_LUX_M2C;} } else if (ratio <= TSL2561_LUX_K2C) {
else if (ratio <= TSL2561_LUX_K3C) b = TSL2561_LUX_B2C;
{b=TSL2561_LUX_B3C; m=TSL2561_LUX_M3C;} m = TSL2561_LUX_M2C;
else if (ratio <= TSL2561_LUX_K4C) } else if (ratio <= TSL2561_LUX_K3C) {
{b=TSL2561_LUX_B4C; m=TSL2561_LUX_M4C;} b = TSL2561_LUX_B3C;
else if (ratio <= TSL2561_LUX_K5C) m = TSL2561_LUX_M3C;
{b=TSL2561_LUX_B5C; m=TSL2561_LUX_M5C;} } else if (ratio <= TSL2561_LUX_K4C) {
else if (ratio <= TSL2561_LUX_K6C) b = TSL2561_LUX_B4C;
{b=TSL2561_LUX_B6C; m=TSL2561_LUX_M6C;} m = TSL2561_LUX_M4C;
else if (ratio <= TSL2561_LUX_K7C) } else if (ratio <= TSL2561_LUX_K5C) {
{b=TSL2561_LUX_B7C; m=TSL2561_LUX_M7C;} b = TSL2561_LUX_B5C;
else if (ratio > TSL2561_LUX_K8C) m = TSL2561_LUX_M5C;
{b=TSL2561_LUX_B8C; m=TSL2561_LUX_M8C;} } 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 #else
if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T)) if ((ratio >= 0) && (ratio <= TSL2561_LUX_K1T))
{b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;} { b=TSL2561_LUX_B1T; m=TSL2561_LUX_M1T;}
else if (ratio <= TSL2561_LUX_K2T) else if (ratio <= TSL2561_LUX_K2T)
{b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;} { b=TSL2561_LUX_B2T; m=TSL2561_LUX_M2T;}
else if (ratio <= TSL2561_LUX_K3T) else if (ratio <= TSL2561_LUX_K3T)
{b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;} { b=TSL2561_LUX_B3T; m=TSL2561_LUX_M3T;}
else if (ratio <= TSL2561_LUX_K4T) else if (ratio <= TSL2561_LUX_K4T)
{b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;} { b=TSL2561_LUX_B4T; m=TSL2561_LUX_M4T;}
else if (ratio <= TSL2561_LUX_K5T) else if (ratio <= TSL2561_LUX_K5T)
{b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;} { b=TSL2561_LUX_B5T; m=TSL2561_LUX_M5T;}
else if (ratio <= TSL2561_LUX_K6T) else if (ratio <= TSL2561_LUX_K6T)
{b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;} { b=TSL2561_LUX_B6T; m=TSL2561_LUX_M6T;}
else if (ratio <= TSL2561_LUX_K7T) else if (ratio <= TSL2561_LUX_K7T)
{b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;} { b=TSL2561_LUX_B7T; m=TSL2561_LUX_M7T;}
else if (ratio > TSL2561_LUX_K8T) else if (ratio > TSL2561_LUX_K8T)
{b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;} { b=TSL2561_LUX_B8T; m=TSL2561_LUX_M8T;}
#endif #endif
unsigned long temp; unsigned long temp;
temp = ((channel0 * b) - (channel1 * m)); temp = ((channel0 * b) - (channel1 * m));
// do not allow negative lux value // do not allow negative lux value
if (temp < 0) temp = 0; if (temp < 0)
temp = 0;
// round lsb (2^(LUX_SCALE-1)) // round lsb (2^(LUX_SCALE-1))
temp += (1 << (TSL2561_LUX_LUXSCALE-1)); temp += (1 << (TSL2561_LUX_LUXSCALE - 1));
// strip off fractional portion // strip off fractional portion
uint32_t lux = temp >> TSL2561_LUX_LUXSCALE; uint32_t lux = temp >> TSL2561_LUX_LUXSCALE;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment