unleashed-firmware/applications/external/lightmeter/lib/MAX44009/MAX44009.c

36 lines
1.1 KiB
C
Raw Normal View History

2023-07-13 20:32:41 +00:00
#include <MAX44009.h>
#include <math.h>
#include <furi.h>
2023-07-22 17:30:21 +00:00
uint8_t max44009_addr = MAX44009_ADDR;
2023-07-13 20:32:41 +00:00
void max44009_init() {
furi_hal_i2c_acquire(I2C_BUS);
2023-07-22 17:30:21 +00:00
furi_hal_i2c_write_reg_8(
I2C_BUS, max44009_addr, MAX44009_REG_CONFIG, MAX44009_REG_CONFIG_CONT_MODE, I2C_TIMEOUT);
2023-07-13 20:32:41 +00:00
furi_hal_i2c_release(I2C_BUS);
}
2023-07-22 17:30:21 +00:00
void max44009_init_with_addr(uint8_t addr) {
max44009_addr = (addr << 1);
return max44009_init();
}
2023-07-13 20:32:41 +00:00
int max44009_read_light(float* result) {
uint8_t data_one = 0;
uint8_t exp, mantissa;
int status;
furi_hal_i2c_acquire(I2C_BUS);
furi_hal_i2c_read_reg_8(I2C_BUS, MAX44009_ADDR, MAX44009_REG_LUX_HI, &data_one, I2C_TIMEOUT);
exp = (data_one & MAX44009_REG_LUX_HI_EXP_MASK) >> 4;
mantissa = (data_one & MAX44009_REG_LUX_HI_MANT_HI_MASK) << 4;
2023-07-22 17:30:21 +00:00
status = furi_hal_i2c_read_reg_8(
I2C_BUS, MAX44009_ADDR, MAX44009_REG_LUX_LO, &data_one, I2C_TIMEOUT);
2023-07-13 20:32:41 +00:00
mantissa |= (data_one & MAX44009_REG_LUX_LO_MANT_LO_MASK);
furi_hal_i2c_release(I2C_BUS);
*result = (float)pow(2, exp) * mantissa * 0.045;
FURI_LOG_D("MAX44009", "exp %d, mant %d, lux %f", exp, mantissa, (double)*result);
return status;
2023-07-22 17:30:21 +00:00
}