M5Unit-KEYBOARD 0.1.0 git rev:b58d024
Loading...
Searching...
No Matches
cardkb2_modifier_state.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_KEYBOARD_UTILITY_CARDKB2_MODIFIER_STATE_HPP
11#define M5_UNIT_KEYBOARD_UTILITY_CARDKB2_MODIFIER_STATE_HPP
12
13#include <cstdint>
14
16
17namespace m5 {
18namespace unit {
19namespace cardkb2 {
20
29public:
31
33 void onSymEdge(const bool pressed, const uint32_t now_ms)
34 {
35 _sym_held = pressed;
36 const ButtonEvent ev = _sym_detector.edge(pressed, now_ms);
37 if (ev == ButtonEvent::SingleClick) _sym_mode = !_sym_mode;
38 }
39
41 void onAaEdge(const bool pressed, const uint32_t now_ms)
42 {
43 const ButtonEvent ev = _caps_detector.edge(pressed, now_ms);
44 if (pressed) {
45 if (ev == ButtonEvent::PressDown && !_caps_lock) _caps_hold = true;
46 } else {
47 if ((ev == ButtonEvent::PressUp || ev == ButtonEvent::LongPressUp) && !_caps_lock) _caps_hold = false;
48 }
49 }
50
52 void onFnEdge(const bool pressed)
53 {
54 _fn = pressed;
55 }
56
58 void poll(const uint32_t now_ms)
59 {
60 const ButtonEvent sev = _sym_detector.poll(now_ms);
61 if (sev == ButtonEvent::SingleClick) _sym_mode = !_sym_mode;
62
63 const ButtonEvent cev = _caps_detector.poll(now_ms);
64 if (cev == ButtonEvent::SingleClick) {
65 if (_caps_lock) {
66 _caps_lock = false;
67 _caps_one_time = false;
68 } else {
69 _caps_one_time = true;
70 }
71 } else if (cev == ButtonEvent::DoubleClick) {
72 _caps_lock = !_caps_lock;
73 _caps_one_time = false;
74 }
75 }
76
77 inline bool fnActive() const
78 {
79 return _fn;
80 }
81 inline bool symActive() const
82 {
83 return _sym_mode || _sym_held;
84 }
85 inline bool capsActive() const
86 {
87 return _caps_one_time || _caps_lock || _caps_hold;
88 }
89
91 inline void consumeCapsOneShot()
92 {
93 if (_caps_one_time && !_caps_lock && !_caps_hold) _caps_one_time = false;
94 }
95
96 void reset()
97 {
98 _sym_detector.reset();
99 _caps_detector.reset();
100 _sym_mode = _sym_held = _fn = false;
101 _caps_one_time = _caps_lock = _caps_hold = false;
102 }
103
104private:
105 m5::unit::keyboard_bitwise::ButtonEventDetector _sym_detector{};
106 m5::unit::keyboard_bitwise::ButtonEventDetector _caps_detector{};
107 bool _sym_mode{false};
108 bool _sym_held{false};
109 bool _fn{false};
110 bool _caps_one_time{false};
111 bool _caps_lock{false};
112 bool _caps_hold{false};
113};
115
116} // namespace cardkb2
117} // namespace unit
118} // namespace m5
119
120#endif
Host-side port of the iot_button v4.1.4 click/long-press FSM.
ButtonEvent
Semantic button events (subset of iot_button needed for CardKB2 modifiers)
Definition button_event_detector.hpp:22
Tracks Sym (toggle + momentary), Caps (one-shot/lock/hold) and Fn (momentary) from key edges,...
Top level namespace of M5Stack.
Unit-related namespace.