Skip to content

Commit a0bbf45

Browse files
committed
Add TogglerCompact
1 parent 23a4097 commit a0bbf45

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

util/debouncer_compact.hh

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#pragma once
2+
#include <cstdint>
3+
4+
// 1-byte version of toggler
5+
struct TogglerCompact {
6+
7+
protected:
8+
uint8_t is_high_ : 1;
9+
uint8_t got_rising_edge_ : 1;
10+
uint8_t got_falling_edge_ : 1;
11+
12+
public:
13+
TogglerCompact()
14+
: is_high_{false}
15+
, got_rising_edge_{false}
16+
, got_falling_edge_{false} {
17+
}
18+
19+
void clear_events() {
20+
got_falling_edge_ = false;
21+
got_rising_edge_ = false;
22+
}
23+
24+
void reset() {
25+
is_high_ = false;
26+
clear_events();
27+
}
28+
29+
operator bool() const {
30+
return is_high();
31+
}
32+
33+
bool is_high() const {
34+
return is_high_;
35+
}
36+
37+
bool is_pressed() const volatile {
38+
return is_high_;
39+
}
40+
41+
bool is_just_pressed() {
42+
if (got_rising_edge_) {
43+
got_rising_edge_ = false;
44+
return true;
45+
} else
46+
return false;
47+
}
48+
49+
bool just_went_high() {
50+
return is_just_pressed();
51+
}
52+
53+
bool is_just_released() {
54+
if (got_falling_edge_) {
55+
got_falling_edge_ = false;
56+
return true;
57+
} else
58+
return false;
59+
}
60+
bool just_went_low() {
61+
return is_just_released();
62+
}
63+
64+
void register_rising_edge() {
65+
is_high_ = true;
66+
got_rising_edge_ = true;
67+
got_falling_edge_ = false;
68+
}
69+
70+
void register_falling_edge() {
71+
is_high_ = false;
72+
got_falling_edge_ = true;
73+
got_rising_edge_ = false;
74+
}
75+
76+
void process(bool newstate) {
77+
register_state(newstate);
78+
}
79+
80+
void register_state(bool newstate) {
81+
if (newstate == is_high_)
82+
return;
83+
84+
is_high_ = newstate;
85+
86+
if (newstate)
87+
got_rising_edge_ = true;
88+
else
89+
got_falling_edge_ = true;
90+
}
91+
92+
void set_state_no_events(unsigned x) {
93+
is_high_ = x ? true : false;
94+
}
95+
96+
void copy_state(const TogglerCompact &other) {
97+
is_high_ = other.is_high_;
98+
got_rising_edge_ = other.got_rising_edge_;
99+
got_falling_edge_ = other.got_falling_edge_;
100+
}
101+
102+
// Update the toggler with more recent events from another toggler
103+
void update_state(const TogglerCompact &other) {
104+
if (other.got_falling_edge_)
105+
got_falling_edge_ = true;
106+
107+
else if (other.got_rising_edge_)
108+
got_rising_edge_ = true;
109+
110+
is_high_ = other.is_high_;
111+
}
112+
113+
// Update the toggler with more recent events AND clear the other toggler
114+
void transfer_events(TogglerCompact &other) {
115+
update_state(other);
116+
other.got_falling_edge_ = false;
117+
other.got_rising_edge_ = false;
118+
}
119+
120+
TogglerCompact &operator=(const TogglerCompact &other) {
121+
copy_state(other);
122+
return *this;
123+
}
124+
};
125+
126+
static_assert(sizeof(TogglerCompact) == 1);

0 commit comments

Comments
 (0)