Firmware SDK
twr_switch.h
1 #ifndef TWR_SWITCH_H
2 #define TWR_SWITCH_H
3 
4 #include <twr_gpio.h>
5 #include <twr_tick.h>
6 #include <twr_scheduler.h>
7 
11 
12 #define TWR_SWITCH_OPEN false
13 #define TWR_SWITCH_CLOSE true
14 
16 
17 typedef enum
18 {
21 
24 
26 
28 
29 typedef enum
30 {
33 
36 
38 
40 
41 typedef enum
42 {
45 
48 
51 
54 
57 
59 
61 
62 typedef struct twr_switch_t twr_switch_t;
63 
65 
66 typedef enum
67 {
68  TWR_SWITCH_TASK_STATE_MEASURE = 0,
69  TWR_SWITCH_TASK_STATE_SET_PULL = 1,
70 
71 } twr_switch_task_state_t;
72 
73 struct twr_switch_t
74 {
75  twr_gpio_channel_t _channel;
76  twr_switch_type_t _type;
77  twr_switch_pull_t _pull;
78  void (*_event_handler)(twr_switch_t *, twr_switch_event_t, void *);
79  void *_event_param;
80  twr_tick_t _update_interval;
81  twr_switch_task_state_t _task_state;
82  int _pin_state;
83  twr_scheduler_task_id_t _task_id;
84  twr_tick_t _scan_interval;
85  twr_tick_t _debounce_time;
86  twr_tick_t _tick_debounce;
87  uint16_t _pull_advance_time;
88 };
89 
91 
96 
98 
103 
104 void twr_switch_set_event_handler(twr_switch_t *self, void (*event_handler)(twr_switch_t *,twr_switch_event_t, void*), void *event_param);
105 
110 
112 
116 
117 void twr_switch_set_scan_interval(twr_switch_t *self, twr_tick_t scan_interval);
118 
122 
123 void twr_switch_set_debounce_time(twr_switch_t *self, twr_tick_t debounce_time);
124 
128 
129 void twr_switch_set_pull_advance_time(twr_switch_t *self, uint16_t pull_advance_time_us);
130 
132 
133 #endif // TWR_SWITCH_H
twr_gpio_channel_t
GPIO channels.
Definition: twr_gpio.h:13
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: twr_scheduler.h:22
bool twr_switch_get_state(twr_switch_t *self)
Get state.
Definition: twr_switch.c:51
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.
Definition: twr_switch.c:18
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...
Definition: twr_switch.c:72
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),...
Definition: twr_switch.c:67
twr_switch_event_t
Callback events.
Definition: twr_switch.h:18
struct twr_switch_t twr_switch_t
State.
Definition: twr_switch.h:62
twr_switch_pull_t
Pull.
Definition: twr_switch.h:42
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.
Definition: twr_switch.c:62
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.
Definition: twr_switch.c:56
twr_switch_type_t
Type.
Definition: twr_switch.h:30
@ TWR_SWITCH_EVENT_OPENED
Event Opened.
Definition: twr_switch.h:20
@ TWR_SWITCH_EVENT_CLOSED
Event Closed.
Definition: twr_switch.h:23
@ TWR_SWITCH_PULL_NONE
Pull None.
Definition: twr_switch.h:44
@ TWR_SWITCH_PULL_DOWN
Pull Down.
Definition: twr_switch.h:53
@ TWR_SWITCH_PULL_UP
Pull Up.
Definition: twr_switch.h:47
@ TWR_SWITCH_PULL_DOWN_DYNAMIC
Pull Down dynamic (Turns pull only for measurement)
Definition: twr_switch.h:56
@ TWR_SWITCH_PULL_UP_DYNAMIC
Pull Up dynamic (Turns pull only for measurement)
Definition: twr_switch.h:50
@ TWR_SWITCH_TYPE_NC
Type Normally Closed.
Definition: twr_switch.h:35
@ TWR_SWITCH_TYPE_NO
Type Normally Open.
Definition: twr_switch.h:32
uint64_t twr_tick_t
Timestamp data type.
Definition: twr_tick.h:16