mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-17 10:32:44 +01:00
drivers/tsl2561: rework API for consistency
This commit is contained in:
parent
d92b4c6759
commit
95a4d8d4e7
@ -65,35 +65,33 @@ extern "C" {
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @brief Device descriptor for the TSL2561 sensor
|
||||
* @brief Device initialization parameters
|
||||
*/
|
||||
typedef struct {
|
||||
i2c_t i2c_dev; /**< I2C device which is used */
|
||||
uint8_t addr; /**< address on I2C bus */
|
||||
uint8_t gain; /**< gain */
|
||||
uint8_t integration; /**< integration time */
|
||||
} tsl2561_t;
|
||||
} tsl2561_params_t;
|
||||
|
||||
/**
|
||||
* @brief Device initialization parameters
|
||||
* @brief Device descriptor for the TSL2561 sensor
|
||||
*/
|
||||
typedef tsl2561_t tsl2561_params_t;
|
||||
typedef struct {
|
||||
tsl2561_params_t params; /**< device initialization parameters */
|
||||
} tsl2561_t;
|
||||
|
||||
/**
|
||||
* @brief Initialize the given TSL2561 device
|
||||
*
|
||||
* @param[out] dev Initialized device descriptor of BMP180 device
|
||||
* @param[in] i2c I2C bus the sensor is connected to
|
||||
* @param[in] addr I2C address of the sensor on the I2C bus
|
||||
* @param[in] gain TSL2561 gain
|
||||
* @param[in] integration TSL2561 integration time
|
||||
* @param[in] params Initialization parameters
|
||||
*
|
||||
* @return 0 on success
|
||||
* @return -1 if given I2C is not available
|
||||
* @return -2 if not a TSL2561 sensor
|
||||
*/
|
||||
int tsl2561_init(tsl2561_t *dev, i2c_t i2c, uint8_t addr,
|
||||
uint8_t gain, uint8_t integration);
|
||||
int tsl2561_init(tsl2561_t *dev, const tsl2561_params_t *params);
|
||||
|
||||
/**
|
||||
* @brief Read illuminance value from the given TSL2561 device, returned in lx.
|
||||
|
@ -30,6 +30,11 @@
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
#define DEV_I2C (dev->params.i2c_dev)
|
||||
#define DEV_ADDR (dev->params.addr)
|
||||
#define DEV_GAIN (dev->params.gain)
|
||||
#define DEV_INTEGRATION (dev->params.integration)
|
||||
|
||||
/* internal helpers */
|
||||
static void _enable(const tsl2561_t *dev);
|
||||
static void _disable(const tsl2561_t *dev);
|
||||
@ -40,17 +45,14 @@ static void _print_init_info(const tsl2561_t *dev);
|
||||
* TSL2561 Core API *
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
int tsl2561_init(tsl2561_t *dev,
|
||||
i2c_t i2c, uint8_t addr, uint8_t gain, uint8_t integration)
|
||||
int tsl2561_init(tsl2561_t *dev, const tsl2561_params_t *params)
|
||||
{
|
||||
dev->i2c_dev = i2c;
|
||||
dev->addr = addr;
|
||||
dev->gain = gain;
|
||||
dev->integration = integration;
|
||||
dev->params = *params;
|
||||
|
||||
_print_init_info(dev);
|
||||
|
||||
/* Initialize I2C interface */
|
||||
if (i2c_init_master(dev->i2c_dev, I2C_SPEED_NORMAL)) {
|
||||
if (i2c_init_master(DEV_I2C, I2C_SPEED_NORMAL)) {
|
||||
DEBUG("[Error] I2C device not enabled\n");
|
||||
return TSL2561_NOI2C;
|
||||
}
|
||||
@ -58,13 +60,13 @@ int tsl2561_init(tsl2561_t *dev,
|
||||
DEBUG("[Info] I2C device initialized with success!\n");
|
||||
|
||||
/* Acquire exclusive access */
|
||||
i2c_acquire(dev->i2c_dev);
|
||||
i2c_acquire(DEV_I2C);
|
||||
|
||||
DEBUG("[Info] Access acquired !\n");
|
||||
|
||||
/* Verify sensor ID */
|
||||
uint8_t id;
|
||||
i2c_read_reg(dev->i2c_dev, dev->addr,
|
||||
i2c_read_reg(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_REGISTER_ID, &id);
|
||||
DEBUG("[Info] ID ? %d\n", id);
|
||||
if (id != TSL2561_ID ) {
|
||||
@ -75,16 +77,16 @@ int tsl2561_init(tsl2561_t *dev,
|
||||
_enable(dev);
|
||||
|
||||
/* configuring gain and integration time */
|
||||
i2c_write_reg(dev->i2c_dev, dev->addr,
|
||||
i2c_write_reg(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING,
|
||||
dev->integration | dev->gain);
|
||||
DEV_INTEGRATION | DEV_GAIN);
|
||||
|
||||
#if ENABLE_DEBUG
|
||||
uint8_t timing;
|
||||
i2c_read_reg(dev->i2c_dev, dev->addr,
|
||||
i2c_read_reg(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_REGISTER_TIMING, &timing);
|
||||
DEBUG("[Info] Timing ? %d (expected: %d)\n",
|
||||
timing, dev->integration | dev->gain);
|
||||
timing, DEV_INTEGRATION | DEV_GAIN);
|
||||
#endif
|
||||
|
||||
_disable(dev);
|
||||
@ -107,7 +109,7 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
|
||||
uint32_t channel_1;
|
||||
uint32_t channel_0;
|
||||
|
||||
switch (dev->integration) {
|
||||
switch (DEV_INTEGRATION) {
|
||||
case TSL2561_INTEGRATIONTIME_13MS:
|
||||
channel_scale = TSL2561_CHSCALE_TINT0;
|
||||
break;
|
||||
@ -122,7 +124,7 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
|
||||
}
|
||||
|
||||
/* Scale for gain (1x or 16x) */
|
||||
if (!dev->gain) {
|
||||
if (!DEV_GAIN) {
|
||||
channel_scale = channel_scale << 4;
|
||||
}
|
||||
|
||||
@ -187,12 +189,12 @@ uint16_t tsl2561_read_illuminance(const tsl2561_t *dev)
|
||||
static void _enable(const tsl2561_t *dev)
|
||||
{
|
||||
/* enabling device */
|
||||
i2c_write_reg(dev->i2c_dev, dev->addr,
|
||||
i2c_write_reg(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL,
|
||||
TSL2561_CONTROL_POWERON);
|
||||
#if ENABLE_DEBUG
|
||||
uint8_t en;
|
||||
i2c_read_reg(dev->i2c_dev, dev->addr,
|
||||
i2c_read_reg(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &en);
|
||||
DEBUG("[Info] Enabled ? %s\n", en == 3 ? "true" : "false");
|
||||
#endif
|
||||
@ -202,13 +204,13 @@ static void _enable(const tsl2561_t *dev)
|
||||
static void _disable(const tsl2561_t *dev)
|
||||
{
|
||||
/* disabling device */
|
||||
i2c_write_reg(dev->i2c_dev, dev->addr,
|
||||
i2c_write_reg(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL,
|
||||
TSL2561_CONTROL_POWEROFF );
|
||||
|
||||
#if ENABLE_DEBUG
|
||||
uint8_t dis;
|
||||
i2c_read_reg(dev->i2c_dev, dev->addr,
|
||||
i2c_read_reg(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_REGISTER_CONTROL, &dis);
|
||||
DEBUG("[Info] Disabled ? %s\n", dis == 0 ? "true": "false");
|
||||
#endif
|
||||
@ -220,7 +222,7 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
|
||||
_enable(dev);
|
||||
|
||||
/* Wait integration time in ms for ADC to complete */
|
||||
switch (dev->integration) {
|
||||
switch (DEV_INTEGRATION) {
|
||||
case TSL2561_INTEGRATIONTIME_13MS:
|
||||
xtimer_usleep(13700);
|
||||
break;
|
||||
@ -236,7 +238,7 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
|
||||
|
||||
char buffer[2] = { 0 };
|
||||
/* Read full spectrum channel */
|
||||
i2c_read_regs(dev->i2c_dev, dev->addr,
|
||||
i2c_read_regs(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN0,
|
||||
buffer, 2);
|
||||
*full = (buffer[1] << 8) | buffer[0];
|
||||
@ -244,7 +246,7 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
|
||||
memset(buffer, 0, sizeof(buffer));
|
||||
|
||||
/* Read infrared spectrum channel */
|
||||
i2c_read_regs(dev->i2c_dev, dev->addr,
|
||||
i2c_read_regs(DEV_I2C, DEV_ADDR,
|
||||
TSL2561_COMMAND_MODE | TSL2561_COMMAND_WORD | TSL2561_REGISTER_CHAN1,
|
||||
buffer, 2);
|
||||
*ir = (buffer[1] << 8) | buffer[0];
|
||||
@ -255,9 +257,9 @@ static void _read_data(const tsl2561_t *dev, uint16_t *full, uint16_t *ir)
|
||||
|
||||
static void _print_init_info(const tsl2561_t *dev)
|
||||
{
|
||||
DEBUG("[Info] I2C device: %d\n", dev->i2c_dev);
|
||||
DEBUG("[Info] Address: %d\n", dev->addr);
|
||||
switch(dev->gain) {
|
||||
DEBUG("[Info] I2C device: %d\n", DEV_I2C);
|
||||
DEBUG("[Info] Address: %d\n", DEV_ADDR);
|
||||
switch(DEV_GAIN) {
|
||||
case TSL2561_GAIN_1X:
|
||||
DEBUG("[Info] Gain: 1X\n");
|
||||
break;
|
||||
@ -267,11 +269,11 @@ static void _print_init_info(const tsl2561_t *dev)
|
||||
break;
|
||||
|
||||
default:
|
||||
DEBUG("[Info] Invalid gain %d\n", dev->gain);
|
||||
DEBUG("[Info] Invalid gain %d\n", DEV_GAIN);
|
||||
break;
|
||||
}
|
||||
|
||||
switch(dev->integration) {
|
||||
switch(DEV_INTEGRATION) {
|
||||
case TSL2561_INTEGRATIONTIME_13MS:
|
||||
DEBUG("[Info] Integration time: 13ms\n");
|
||||
break;
|
||||
@ -285,7 +287,7 @@ static void _print_init_info(const tsl2561_t *dev)
|
||||
DEBUG("[Info] Integration time: n/a\n");
|
||||
break;
|
||||
default:
|
||||
DEBUG("[Info] Invalid integration time %d\n", dev->integration);
|
||||
DEBUG("[Info] Invalid integration time %d\n", DEV_INTEGRATION);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user