|
|
|
@ -14,6 +14,7 @@
|
|
|
|
|
* @brief Device driver implementation for the ISL29020 light sensor |
|
|
|
|
* |
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de> |
|
|
|
|
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de> |
|
|
|
|
* |
|
|
|
|
* @} |
|
|
|
|
*/ |
|
|
|
@ -36,12 +37,16 @@ int isl29020_init(isl29020_t *dev, i2c_t i2c, uint8_t address,
|
|
|
|
|
dev->address = address; |
|
|
|
|
dev->lux_fac = (float)((1 << (10 + (2 * range))) - 1) / 0xffff; |
|
|
|
|
|
|
|
|
|
/* Acquire exclusive access to the bus. */ |
|
|
|
|
i2c_acquire(dev->i2c); |
|
|
|
|
/* initialize the I2C bus */ |
|
|
|
|
i2c_init_master(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); |
|
|
|
|
/* Release the bus for other threads. */ |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
if (res < 1) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
@ -53,9 +58,11 @@ int isl29020_read(isl29020_t *dev)
|
|
|
|
|
char low, high; |
|
|
|
|
uint16_t res; |
|
|
|
|
|
|
|
|
|
i2c_acquire(dev->i2c); |
|
|
|
|
/* read lightning value */ |
|
|
|
|
res = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_LDATA, &low); |
|
|
|
|
res += i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_HDATA, &high); |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
if (res < 2) { |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
@ -70,15 +77,19 @@ int isl29020_enable(isl29020_t *dev)
|
|
|
|
|
int res; |
|
|
|
|
char tmp; |
|
|
|
|
|
|
|
|
|
i2c_acquire(dev->i2c); |
|
|
|
|
res = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_CMD, &tmp); |
|
|
|
|
if (res < 1) { |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
tmp |= ISL29020_CMD_EN; |
|
|
|
|
res = i2c_write_reg(dev->i2c, dev->address, ISL29020_REG_CMD, tmp); |
|
|
|
|
if (res < 1) { |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -87,14 +98,18 @@ int isl29020_disable(isl29020_t *dev)
|
|
|
|
|
int res; |
|
|
|
|
char tmp; |
|
|
|
|
|
|
|
|
|
i2c_acquire(dev->i2c); |
|
|
|
|
res = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_CMD, &tmp); |
|
|
|
|
if (res < 1) { |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
tmp &= ~(ISL29020_CMD_EN); |
|
|
|
|
res = i2c_write_reg(dev->i2c, dev->address, ISL29020_REG_CMD, tmp); |
|
|
|
|
if (res < 1) { |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
i2c_release(dev->i2c); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|