From 483dbebe24c57ff6bad7ce797d70b0e6ecc75479 Mon Sep 17 00:00:00 2001 From: aeprox Date: Sat, 22 Aug 2015 14:26:42 +0200 Subject: [PATCH] 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;