1 #include <twr_analog_sensor.h>
3 static void _twr_analog_sensor_task_interval(
void *param);
5 static void _twr_analog_sensor_task_measure(
void *param);
11 memset(
self, 0,
sizeof(*
self));
13 self->_adc_channel = adc_channel;
14 self->_driver = driver;
23 if (self->_driver != NULL && self->_driver->init != NULL)
25 self->_driver->init(
self);
31 self->_event_handler = event_handler;
32 self->_event_param = event_param;
37 self->_update_interval = interval;
53 if (self->_measurement_active)
58 self->_measurement_active =
true;
67 if (self->_state != TWR_ANALOG_SENSOR_STATE_UPDATE)
72 *result =
self->_value;
77 static void _twr_analog_sensor_task_interval(
void *param)
86 static void _twr_analog_sensor_task_measure(
void *param)
94 case TWR_ANALOG_SENSOR_STATE_ERROR:
96 self->_measurement_active =
false;
98 if (self->_event_handler != NULL)
103 self->_state = TWR_ANALOG_SENSOR_STATE_ENABLE;
107 case TWR_ANALOG_SENSOR_STATE_ENABLE:
109 self->_state = TWR_ANALOG_SENSOR_STATE_MEASURE;
111 if (self->_driver != NULL && self->_driver->enable != NULL)
113 self->_driver->enable(
self);
115 if (self->_driver->get_settling_interval != NULL)
125 case TWR_ANALOG_SENSOR_STATE_MEASURE:
129 self->_state = TWR_ANALOG_SENSOR_STATE_ERROR;
134 self->_state = TWR_ANALOG_SENSOR_STATE_DISABLE;
138 case TWR_ANALOG_SENSOR_STATE_DISABLE:
140 if (self->_driver != NULL && self->_driver->disable != NULL)
142 self->_driver->disable(
self);
145 self->_state = TWR_ANALOG_SENSOR_STATE_UPDATE;
149 case TWR_ANALOG_SENSOR_STATE_UPDATE:
151 self->_measurement_active =
false;
153 if (self->_event_handler != NULL)
158 self->_state = TWR_ANALOG_SENSOR_STATE_ENABLE;
164 self->_state = TWR_ANALOG_SENSOR_STATE_ERROR;
179 self->_state = TWR_ANALOG_SENSOR_STATE_DISABLE;
183 self->_state = TWR_ANALOG_SENSOR_STATE_ERROR;
bool twr_adc_set_event_handler(twr_adc_channel_t channel, void(*event_handler)(twr_adc_channel_t, twr_adc_event_t, void *), void *event_param)
Set callback function.
twr_adc_event_t
ADC event.
twr_adc_channel_t
ADC channel.
bool twr_adc_get_value(twr_adc_channel_t channel, uint16_t *result)
Reads the ADC channel value.
void twr_adc_init()
Initialize ADC converter.
@ TWR_ADC_EVENT_DONE
ADC event.
void twr_analog_sensor_init(twr_analog_sensor_t *self, twr_adc_channel_t adc_channel, const twr_analog_sensor_driver_t *driver)
Initialize generic analog sensor.
bool twr_analog_sensor_measure(twr_analog_sensor_t *self)
Start measurement manually.
twr_analog_sensor_event_t
Callback events.
struct twr_analog_sensor_t twr_analog_sensor_t
Analog sensor instance.
void twr_analog_sensor_set_update_interval(twr_analog_sensor_t *self, twr_tick_t interval)
Set measurement interval.
void twr_analog_sensor_set_event_handler(twr_analog_sensor_t *self, void(*event_handler)(twr_analog_sensor_t *, twr_analog_sensor_event_t, void *), void *event_param)
Set callback function.
@ TWR_ANALOG_SENSOR_EVENT_ERROR
Error event.
@ TWR_ANALOG_SENSOR_EVENT_UPDATE
Update 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_absolute(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to absolute tick.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
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.
uint64_t twr_tick_t
Timestamp data type.
Analog sensor driver interface.