utils
utilities in C for microcontrollers
button.h
Go to the documentation of this file.
1 
8 #ifndef BUTTON_UTILS_H
9 #define BUTTON_UTILS_H
10 
11 #include <stdbool.h>
12 
16 struct debouncer {
17  bool current_value;
18  int reset_count;
19  int current_count;
20 };
21 
28 void debouncer_init(
29  struct debouncer *self, int count, bool initial);
30 
34 void debouncer_set_count(struct debouncer *self, int count);
35 
41 bool debouncer_update(struct debouncer *self, bool value);
42 
46 bool debouncer_value(struct debouncer *self);
47 
52  EDGE_NONE,
53  EDGE_RISING,
54  EDGE_FALLING,
55 };
56 
60 struct edge_detector {
61  bool value;
62 };
63 
68 void edge_detector_init(struct edge_detector *self,
69  bool initial);
70 
77  edge_detector_update(struct edge_detector *self,
78  bool value);
79 
83 bool edge_detector_get_value(struct edge_detector *self);
84 
89  BUTTON_EVENT_PRESS,
90  BUTTON_EVENT_RELEASE,
91  BUTTON_EVENT_HOLD
92 };
93 
98  BUTTON_TIMER_IDLE,
99  BUTTON_TIMER_HOLD,
100  BUTTON_TIMER_REPEAT
101 };
102 
106 struct button {
108  void (*handler)(enum button_event, void *);
109  bool use_hold_timer;
110  bool use_repeat_timer;
111  enum button_timer_state timer_state;
112  unsigned long long time;
113  unsigned long long hold_time;
114  unsigned long long repeat_time;
115 };
116 
122 void button_init(struct button *self, bool initial,
123  void (*handler)(enum button_event, void *));
124 
130 void button_set_hold_time(struct button *self,
131  unsigned long long time, bool use);
137 void button_set_repeat_time(struct button *self,
138  unsigned long long time, bool use);
139 
143 bool button_get_value(struct button *self);
144 
151 bool button_update(struct button *self,
152  unsigned long long time,
153  bool value,
154  void *ctx);
155 
156 #endif //BUTTON_UTILS_H
157 
button_event
button events
Definition: button.h:88
bool button_get_value(struct button *self)
Definition: button.c:80
a software debouncer
Definition: button.h:16
void button_set_repeat_time(struct button *self, unsigned long long time, bool use)
Definition: button.c:74
button_timer_state
internal button timer state
Definition: button.h:97
bool debouncer_value(struct debouncer *self)
Definition: button.c:29
enum edge_detector_edge edge_detector_update(struct edge_detector *self, bool value)
Definition: button.c:39
bool edge_detector_get_value(struct edge_detector *self)
Definition: button.c:52
void edge_detector_init(struct edge_detector *self, bool initial)
Definition: button.c:33
void debouncer_set_count(struct debouncer *self, int count)
Definition: button.c:9
void button_set_hold_time(struct button *self, unsigned long long time, bool use)
Definition: button.c:68
bool debouncer_update(struct debouncer *self, bool value)
Definition: button.c:17
edge_detector_edge
Definition: button.h:51
button state
Definition: button.h:106
edge detector state
Definition: button.h:60
void button_init(struct button *self, bool initial, void(*handler)(enum button_event, void *))
Definition: button.c:56
bool button_update(struct button *self, unsigned long long time, bool value, void *ctx)
Definition: button.c:84
void debouncer_init(struct debouncer *self, int count, bool initial)
initialize a debouncer
Definition: button.c:3