Return error when calling functions before init

TSL2561_ERROR_NOINIT
This commit is contained in:
aeprox 2015-08-22 14:26:42 +02:00
parent 335ea87964
commit 483dbebe24
3 changed files with 9 additions and 4 deletions

View File

@ -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 ) },

View File

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

View File

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