1 #include <twr_switch.h>
4 #define _TWR_SWITCH_SCAN_INTERVAL 50
5 #define _TWR_SWITCH_DEBOUNCE_TIME 20
6 #define _TWR_SWITCH_PULL_ADVANCE_TIME_US 50
8 static void _twr_switch_task(
void *param);
20 memset(
self, 0,
sizeof(&
self));
21 self->_channel = channel;
24 self->_scan_interval = _TWR_SWITCH_SCAN_INTERVAL;
25 self->_debounce_time = _TWR_SWITCH_DEBOUNCE_TIME;
26 self->_pull_advance_time = _TWR_SWITCH_PULL_ADVANCE_TIME_US;
36 if (self->_pull_advance_time > 1000)
38 self->_task_state = TWR_SWITCH_TASK_STATE_SET_PULL;
53 return self->_pin_state != 0 ? TWR_SWITCH_OPEN : TWR_SWITCH_CLOSE;
58 self->_event_handler = event_handler;
59 self->_event_param = event_param;
64 self->_scan_interval = scan_interval;
69 self->_debounce_time = debounce_time;
74 self->_pull_advance_time = pull_advance_time_us;
77 static void _twr_switch_task(
void *param)
81 switch (self->_task_state)
83 case TWR_SWITCH_TASK_STATE_MEASURE:
89 if (self->_pull_advance_time < 1000)
107 if (self->_pull_advance_time > 1000)
109 self->_task_state = TWR_SWITCH_TASK_STATE_SET_PULL;
115 pin_state = pin_state == 0 ? 1 : 0;
120 if (self->_pin_state != pin_state)
124 self->_tick_debounce = tick_now +
self->_debounce_time;
131 if (tick_now >= self->_tick_debounce)
133 self->_pin_state = pin_state;
135 if (self->_event_handler != NULL)
137 if (self->_pin_state != 0)
158 case TWR_SWITCH_TASK_STATE_SET_PULL:
164 self->_task_state = TWR_SWITCH_TASK_STATE_MEASURE;
void twr_gpio_set_pull(twr_gpio_channel_t channel, twr_gpio_pull_t pull)
Set pull-up/pull-down configuration for GPIO channel.
void twr_gpio_init(twr_gpio_channel_t channel)
Initialize GPIO channel.
twr_gpio_channel_t
GPIO channels.
twr_gpio_pull_t
GPIO pull-up/pull-down setting.
int twr_gpio_get_input(twr_gpio_channel_t channel)
Get input state for GPIO channel.
void twr_gpio_set_mode(twr_gpio_channel_t channel, twr_gpio_mode_t mode)
Set mode of operation for GPIO channel.
@ TWR_GPIO_MODE_INPUT
GPIO channel operates as input.
@ TWR_GPIO_PULL_NONE
GPIO channel has no pull-up/pull-down.
@ TWR_GPIO_PULL_DOWN
GPIO channel has pull-down.
@ TWR_GPIO_PULL_UP
GPIO channel has pull-up.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
twr_tick_t twr_scheduler_get_spin_tick(void)
Get current tick of spin in which task has been run.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
bool twr_switch_get_state(twr_switch_t *self)
Get state.
void twr_switch_init(twr_switch_t *self, twr_gpio_channel_t channel, twr_switch_type_t type, twr_switch_pull_t pull)
Initialize button.
void twr_switch_set_pull_advance_time(twr_switch_t *self, uint16_t pull_advance_time_us)
Set pull advance time (work only with dynamic pull up/down), default 50us, for time bigger than 1000u...
void twr_switch_set_debounce_time(twr_switch_t *self, twr_tick_t debounce_time)
Set debounce time (minimum sampling interval during which input cannot change to toggle its state),...
twr_switch_event_t
Callback events.
struct twr_switch_t twr_switch_t
State.
void twr_switch_set_scan_interval(twr_switch_t *self, twr_tick_t scan_interval)
Set scan interval (period of button input sampling), default 50ms.
void twr_switch_set_event_handler(twr_switch_t *self, void(*event_handler)(twr_switch_t *, twr_switch_event_t, void *), void *event_param)
Set callback function.
@ TWR_SWITCH_EVENT_OPENED
Event Opened.
@ TWR_SWITCH_EVENT_CLOSED
Event Closed.
@ TWR_SWITCH_PULL_NONE
Pull None.
@ TWR_SWITCH_PULL_DOWN
Pull Down.
@ TWR_SWITCH_PULL_UP
Pull Up.
@ TWR_SWITCH_PULL_DOWN_DYNAMIC
Pull Down dynamic (Turns pull only for measurement)
@ TWR_SWITCH_PULL_UP_DYNAMIC
Pull Up dynamic (Turns pull only for measurement)
@ TWR_SWITCH_TYPE_NC
Type Normally Closed.
#define TWR_TICK_INFINITY
Maximum timestamp value.
uint64_t twr_tick_t
Timestamp data type.
void twr_timer_init(void)
Initialize timer.
void twr_timer_delay(uint16_t microseconds)
Relative delay.
void twr_timer_stop(void)
Stop timer.
void twr_timer_start(void)
Start timer.