1 #include <twr_button.h>
3 #define _TWR_BUTTON_SCAN_INTERVAL 20
4 #define _TWR_BUTTON_DEBOUNCE_TIME 50
5 #define _TWR_BUTTON_CLICK_TIMEOUT 500
6 #define _TWR_BUTTON_HOLD_TIME 2000
8 static void _twr_button_task(
void *param);
12 static int _twr_button_gpio_get_input(
twr_button_t *
self);
16 .
init = _twr_button_gpio_init,
17 .get_input = _twr_button_gpio_get_input,
22 memset(
self, 0,
sizeof(*
self));
24 self->_channel.gpio = gpio_channel;
25 self->_gpio_pull = gpio_pull;
26 self->_idle_state = idle_state;
28 self->_scan_interval = _TWR_BUTTON_SCAN_INTERVAL;
29 self->_debounce_time = _TWR_BUTTON_DEBOUNCE_TIME;
30 self->_click_timeout = _TWR_BUTTON_CLICK_TIMEOUT;
31 self->_hold_time = _TWR_BUTTON_HOLD_TIME;
34 self->_driver = &_twr_button_driver_gpio;
35 self->_driver->
init(
self);
45 memset(
self, 0,
sizeof(*
self));
47 self->_channel.virtual = channel;
48 self->_idle_state = idle_state;
50 self->_scan_interval = _TWR_BUTTON_SCAN_INTERVAL;
51 self->_debounce_time = _TWR_BUTTON_DEBOUNCE_TIME;
52 self->_click_timeout = _TWR_BUTTON_CLICK_TIMEOUT;
53 self->_hold_time = _TWR_BUTTON_HOLD_TIME;
56 self->_driver = driver;
58 if (self->_driver->init != NULL)
60 self->_driver->
init(
self);
68 self->_event_handler = event_handler;
69 self->_event_param = event_param;
71 if (event_handler == NULL)
85 self->_scan_interval = scan_interval;
90 self->_debounce_time = debounce_time;
95 self->_click_timeout = click_timeout;
100 self->_hold_time = hold_time;
103 static void _twr_button_task(
void *param)
111 if (self->_driver->get_input != NULL)
113 pin_state =
self->_driver->get_input(
self);
117 pin_state =
self->_idle_state;
120 if (self->_idle_state)
122 pin_state = pin_state == 0 ? 1 : 0;
125 if ((self->_state == 0 && pin_state != 0) || (self->_state != 0 && pin_state == 0))
129 self->_tick_debounce = tick_now +
self->_debounce_time;
132 if (tick_now >= self->_tick_debounce)
134 self->_state =
self->_state == 0 ? 1 : 0;
136 if (self->_state != 0)
138 self->_tick_click_timeout = tick_now +
self->_click_timeout;
139 self->_tick_hold_threshold = tick_now +
self->_hold_time;
140 self->_hold_signalized =
false;
142 if (self->_event_handler != NULL)
149 if (self->_event_handler != NULL)
154 if (tick_now < self->_tick_click_timeout)
156 if (self->_event_handler != NULL)
169 if (self->_state != 0)
171 if (!self->_hold_signalized)
173 if (tick_now >= self->_tick_hold_threshold)
175 self->_hold_signalized =
true;
177 if (self->_event_handler != NULL)
193 static int _twr_button_gpio_get_input(
twr_button_t *
self)
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.
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.
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.
#define TWR_TICK_INFINITY
Maximum timestamp value.
uint64_t twr_tick_t
Timestamp data type.
bool(* init)(void)
Callback for initialization.