1 #include <twr_opt3001.h>
3 #define _TWR_OPT3001_DELAY_RUN 50
4 #define _TWR_OPT3001_DELAY_INITIALIZATION 50
5 #define _TWR_OPT3001_DELAY_MEASUREMENT 1000
7 static void _twr_opt3001_task_interval(
void *param);
9 static void _twr_opt3001_task_measure(
void *param);
13 memset(
self, 0,
sizeof(*
self));
15 self->_i2c_channel = i2c_channel;
16 self->_i2c_address = i2c_address;
21 self->_tick_ready = _TWR_OPT3001_DELAY_RUN;
37 self->_event_handler = event_handler;
38 self->_event_param = event_param;
43 self->_update_interval = interval;
59 if (self->_measurement_active)
64 self->_measurement_active =
true;
73 if (!self->_illuminance_valid)
78 *raw =
self->_reg_result;
92 *lux = 0.01f * (float) (1 << (raw >> 12)) * (
float) (raw & 0xfff);
97 static void _twr_opt3001_task_interval(
void *param)
106 static void _twr_opt3001_task_measure(
void *param)
112 switch (self->_state)
114 case TWR_OPT3001_STATE_ERROR:
116 self->_illuminance_valid =
false;
118 self->_measurement_active =
false;
120 if (self->_event_handler != NULL)
125 self->_state = TWR_OPT3001_STATE_INITIALIZE;
129 case TWR_OPT3001_STATE_INITIALIZE:
131 self->_state = TWR_OPT3001_STATE_ERROR;
138 self->_state = TWR_OPT3001_STATE_MEASURE;
140 self->_tick_ready =
twr_tick_get() + _TWR_OPT3001_DELAY_INITIALIZATION;
142 if (self->_measurement_active)
149 case TWR_OPT3001_STATE_MEASURE:
151 self->_state = TWR_OPT3001_STATE_ERROR;
158 self->_state = TWR_OPT3001_STATE_READ;
164 case TWR_OPT3001_STATE_READ:
166 self->_state = TWR_OPT3001_STATE_ERROR;
168 uint16_t reg_configuration;
175 if ((reg_configuration & 0x0680) != 0x0080)
185 self->_illuminance_valid =
true;
187 self->_state = TWR_OPT3001_STATE_UPDATE;
191 case TWR_OPT3001_STATE_UPDATE:
193 self->_measurement_active =
false;
195 if (self->_event_handler != NULL)
200 self->_state = TWR_OPT3001_STATE_MEASURE;
206 self->_state = TWR_OPT3001_STATE_ERROR;
void twr_i2c_init(twr_i2c_channel_t channel, twr_i2c_speed_t speed)
Initialize I2C channel.
bool twr_i2c_memory_write_16b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t data)
Memory write 2 bytes to I2C channel.
bool twr_i2c_memory_read_16b(twr_i2c_channel_t channel, uint8_t device_address, uint32_t memory_address, uint16_t *data)
Memory read 2 bytes from I2C channel.
twr_i2c_channel_t
I2C channels.
@ TWR_I2C_SPEED_400_KHZ
I2C communication speed is 400 kHz.
void twr_opt3001_init(twr_opt3001_t *self, twr_i2c_channel_t i2c_channel, uint8_t i2c_address)
Initialize OPT3001 driver.
void twr_opt3001_set_event_handler(twr_opt3001_t *self, void(*event_handler)(twr_opt3001_t *, twr_opt3001_event_t, void *), void *event_param)
Set callback function.
struct twr_opt3001_t twr_opt3001_t
OPT3001 instance.
void twr_opt3001_set_update_interval(twr_opt3001_t *self, twr_tick_t interval)
Set measurement interval.
bool twr_opt3001_get_illuminance_lux(twr_opt3001_t *self, float *lux)
Get measured illuminance in lux.
void twr_opt3001_deinit(twr_opt3001_t *self)
Deinitialize OPT3001 driver.
bool twr_opt3001_measure(twr_opt3001_t *self)
Start measurement manually.
twr_opt3001_event_t
Callback events.
bool twr_opt3001_get_illuminance_raw(twr_opt3001_t *self, uint16_t *raw)
Get measured illuminance as raw value.
@ TWR_OPT3001_EVENT_UPDATE
Update event.
@ TWR_OPT3001_EVENT_ERROR
Error event.
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
void twr_scheduler_plan_current_absolute(twr_tick_t tick)
Schedule current task to absolute tick.
void twr_scheduler_plan_absolute(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to absolute tick.
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
void twr_scheduler_plan_relative(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to tick relative from current spin.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
#define TWR_TICK_INFINITY
Maximum timestamp value.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.