drivers/isl29020: rework driver API and params definition

This commit is contained in:
Alexandre Abadie 2018-02-28 18:03:43 +01:00 committed by dylad
parent c3363587be
commit 0b78a1bec5
3 changed files with 51 additions and 50 deletions

View File

@ -33,15 +33,6 @@ extern "C" {
*/
#define ISL29020_DEFAULT_ADDRESS 0x44
/**
* @brief Device descriptor for ISL29020 sensors
*/
typedef struct {
i2c_t i2c; /**< I2C device the sensor is connected to */
uint8_t address; /**< I2C bus address of the sensor */
float lux_fac; /**< factor to calculate actual lux value */
} isl29020_t;
/**
* @brief Possible modes for the ISL29020 sensor
*/
@ -70,20 +61,24 @@ typedef struct {
isl29020_mode_t mode; /**< measurement mode to use */
} isl29020_params_t;
/**
* @brief Device descriptor for ISL29020 sensors
*/
typedef struct {
isl29020_params_t params; /**< device initialization parameters */
float lux_fac; /**< factor to calculate actual lux value */
} isl29020_t;
/**
* @brief Initialize a new ISL29020 device
*
* @param[in] dev device descriptor of an ISL29020 device
* @param[in] i2c I2C device the sensor is connected to
* @param[in] address I2C address of the sensor
* @param[in] range measurement range
* @param[in] mode configure if sensor reacts to ambient or infrared light
* @param[in] params initialization parameters
*
* @return 0 on success
* @return -1 on error
*/
int isl29020_init(isl29020_t *dev, i2c_t i2c, uint8_t address,
isl29020_range_t range, isl29020_mode_t mode);
int isl29020_init(isl29020_t *dev, const isl29020_params_t *params);
/**
* @brief Read a lighting value from the sensor, the result is given in lux

View File

@ -44,10 +44,15 @@ extern "C" {
#define ISL29020_PARAM_MODE (ISL29020_MODE_AMBIENT)
#endif
#define ISL29020_PARAMS_DEFAULT { .i2c = ISL29020_PARAM_I2C, \
.addr = ISL29020_PARAM_ADDR, \
#ifndef ISL29020_PARAMS
#define ISL29020_PARAMS { .i2c = ISL29020_PARAM_I2C, \
.addr = ISL29020_PARAM_ADDR, \
.range = ISL29020_PARAM_RANGE, \
.mode = ISL29020_PARAM_MODE,}
.mode = ISL29020_PARAM_MODE }
#endif
#ifndef ISL29020_SAUL_INFO
#define ISL29020_SAUL_INFO { .name = "isl29020" }
#endif
/**@}*/
/**
@ -55,11 +60,7 @@ extern "C" {
*/
static const isl29020_params_t isl29020_params[] =
{
#ifdef ISL29020_PARAMS_CUSTOM
ISL29020_PARAMS_CUSTOM,
#else
ISL29020_PARAMS_DEFAULT,
#endif
ISL29020_PARAMS
};
/**
@ -67,7 +68,7 @@ static const isl29020_params_t isl29020_params[] =
*/
static const saul_reg_info_t isl29020_saul_info[] =
{
{ .name = "isl29020" }
ISL29020_SAUL_INFO
};
#ifdef __cplusplus

View File

@ -26,27 +26,32 @@
#define ENABLE_DEBUG (0)
#include "debug.h"
int isl29020_init(isl29020_t *dev, i2c_t i2c, uint8_t address,
isl29020_range_t range, isl29020_mode_t mode)
#define DEV_I2C (dev->params.i2c)
#define DEV_ADDR (dev->params.addr)
#define DEV_RANGE (dev->params.range)
#define DEV_MODE (dev->params.mode)
int isl29020_init(isl29020_t *dev, const isl29020_params_t *params)
{
dev->params = *params;
int res;
uint8_t tmp;
/* initialize device descriptor */
dev->i2c = i2c;
dev->address = address;
dev->lux_fac = (float)((1 << (10 + (2 * range))) - 1) / 0xffff;
dev->lux_fac = (float)((1 << (10 + (2 * DEV_RANGE))) - 1) / 0xffff;
/* acquire exclusive access to the bus */
i2c_acquire(dev->i2c);
i2c_acquire(DEV_I2C);
/* initialize the I2C bus */
i2c_init_master(i2c, I2C_SPEED_NORMAL);
i2c_init_master(DEV_I2C, I2C_SPEED_NORMAL);
/* configure and enable the sensor */
tmp = ISL29020_CMD_EN | ISL29020_CMD_MODE | ISL29020_RES_INT_16 | range | (mode << 5);
res = i2c_write_reg(dev->i2c, address, ISL29020_REG_CMD, tmp);
tmp = (ISL29020_CMD_EN | ISL29020_CMD_MODE |
ISL29020_RES_INT_16 | DEV_RANGE | (DEV_MODE << 5));
res = i2c_write_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, tmp);
/* release the bus for other threads */
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
if (res < 1) {
return -1;
}
@ -59,11 +64,11 @@ int isl29020_read(const isl29020_t *dev)
uint16_t res;
int ret;
i2c_acquire(dev->i2c);
i2c_acquire(DEV_I2C);
/* read lighting value */
ret = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_LDATA, &low);
ret += i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_HDATA, &high);
i2c_release(dev->i2c);
ret = i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_LDATA, &low);
ret += i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_HDATA, &high);
i2c_release(DEV_I2C);
if (ret < 2) {
return -1;
}
@ -78,19 +83,19 @@ int isl29020_enable(const isl29020_t *dev)
int res;
uint8_t tmp;
i2c_acquire(dev->i2c);
res = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_CMD, &tmp);
i2c_acquire(DEV_I2C);
res = i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, &tmp);
if (res < 1) {
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return -1;
}
tmp |= ISL29020_CMD_EN;
res = i2c_write_reg(dev->i2c, dev->address, ISL29020_REG_CMD, tmp);
res = i2c_write_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, tmp);
if (res < 1) {
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return -1;
}
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return 0;
}
@ -99,18 +104,18 @@ int isl29020_disable(const isl29020_t *dev)
int res;
uint8_t tmp;
i2c_acquire(dev->i2c);
res = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_CMD, &tmp);
i2c_acquire(DEV_I2C);
res = i2c_read_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, &tmp);
if (res < 1) {
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return -1;
}
tmp &= ~(ISL29020_CMD_EN);
res = i2c_write_reg(dev->i2c, dev->address, ISL29020_REG_CMD, tmp);
res = i2c_write_reg(DEV_I2C, DEV_ADDR, ISL29020_REG_CMD, tmp);
if (res < 1) {
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return -1;
}
i2c_release(dev->i2c);
i2c_release(DEV_I2C);
return 0;
}