Firmware SDK
twr_button.h
1 #ifndef _TWR_BUTTON_H
2 #define _TWR_BUTTON_H
3 
4 #include <twr_gpio.h>
5 #include <twr_tick.h>
6 #include <twr_scheduler.h>
7 
11 
13 
14 typedef enum
15 {
18 
21 
24 
27 
29 
31 
32 typedef struct twr_button_t twr_button_t;
33 
35 
36 typedef struct
37 {
39  void (*init)(twr_button_t *self);
40 
42  int (*get_input)(twr_button_t *self);
43 
45 
47 
48 typedef union
49 {
50  twr_gpio_channel_t gpio;
51  int virtual;
52 
53 } twr_button_channel_t;
54 
55 struct twr_button_t
56 {
57  twr_button_channel_t _channel;
58  const twr_button_driver_t *_driver;
59  twr_gpio_pull_t _gpio_pull;
60  int _idle_state;
61  void (*_event_handler)(twr_button_t *, twr_button_event_t, void *);
62  void *_event_param;
63  twr_tick_t _scan_interval;
64  twr_tick_t _debounce_time;
65  twr_tick_t _click_timeout;
66  twr_tick_t _hold_time;
67  twr_tick_t _tick_debounce;
68  twr_tick_t _tick_click_timeout;
69  twr_tick_t _tick_hold_threshold;
70  int _state;
71  bool _hold_signalized;
72  twr_scheduler_task_id_t _task_id;
73 };
74 
76 
82 
83 void twr_button_init(twr_button_t *self, twr_gpio_channel_t gpio_channel, twr_gpio_pull_t gpio_pull, int idle_state);
84 
90 
91 void twr_button_init_virtual(twr_button_t *self, int channel, const twr_button_driver_t *driver, int idle_state);
92 
97 
98 void twr_button_set_event_handler(twr_button_t *self, void (*event_handler)(twr_button_t *, twr_button_event_t, void *), void *event_param);
99 
103 
104 void twr_button_set_scan_interval(twr_button_t *self, twr_tick_t scan_interval);
105 
109 
110 void twr_button_set_debounce_time(twr_button_t *self, twr_tick_t debounce_time);
111 
115 
116 void twr_button_set_click_timeout(twr_button_t *self, twr_tick_t click_timeout);
117 
121 
122 void twr_button_set_hold_time(twr_button_t *self, twr_tick_t hold_time);
123 
125 
126 #endif // _TWR_BUTTON_H
struct twr_button_t twr_button_t
Button instance.
Definition: twr_button.h:32
void twr_button_set_click_timeout(twr_button_t *self, twr_tick_t click_timeout)
Set click timeout (maximum interval within which button has to be released to recognize click event)
Definition: twr_button.c:93
void twr_button_set_debounce_time(twr_button_t *self, twr_tick_t debounce_time)
Set debounce time (minimum sampling interval during which input cannot change to toggle its state)
Definition: twr_button.c:88
twr_button_event_t
Callback events.
Definition: twr_button.h:15
void twr_button_init_virtual(twr_button_t *self, int channel, const twr_button_driver_t *driver, int idle_state)
Initialize virtual button.
Definition: twr_button.c:43
void twr_button_set_hold_time(twr_button_t *self, twr_tick_t hold_time)
Set hold time (interval after which hold event is recognized when button is steadily pressed)
Definition: twr_button.c:98
void twr_button_set_scan_interval(twr_button_t *self, twr_tick_t scan_interval)
Set scan interval (period of button input sampling)
Definition: twr_button.c:83
void twr_button_init(twr_button_t *self, twr_gpio_channel_t gpio_channel, twr_gpio_pull_t gpio_pull, int idle_state)
Initialize button.
Definition: twr_button.c:20
void twr_button_set_event_handler(twr_button_t *self, void(*event_handler)(twr_button_t *, twr_button_event_t, void *), void *event_param)
Set callback function.
Definition: twr_button.c:66
@ TWR_BUTTON_EVENT_HOLD
Event button hold (pressed for longer time)
Definition: twr_button.h:26
@ TWR_BUTTON_EVENT_PRESS
Event button pressed.
Definition: twr_button.h:17
@ TWR_BUTTON_EVENT_RELEASE
Event button released.
Definition: twr_button.h:20
@ TWR_BUTTON_EVENT_CLICK
Event button clicked (pressed and released within certain time)
Definition: twr_button.h:23
twr_gpio_channel_t
GPIO channels.
Definition: twr_gpio.h:13
twr_gpio_pull_t
GPIO pull-up/pull-down setting.
Definition: twr_gpio.h:88
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16
Button driver interface.
Definition: twr_button.h:37