10#ifndef M5_UNIT_KEYBOARD_UTILITY_BITWISE_STATE_HPP
11#define M5_UNIT_KEYBOARD_UTILITY_BITWISE_STATE_HPP
20namespace keyboard_bitwise {
31 using bits_t = std::bitset<N>;
42 std::array<uint32_t, N> press_at{};
43 std::array<uint32_t, N> last_repeat_at{};
45 uint32_t holding_threshold_ms{800};
46 uint32_t repeat_initial_ms{400};
47 uint32_t repeat_rate_ms{400};
49 inline void resetOneShot()
57 inline void resetAll()
64 last_repeat_at.fill(0U);
67 inline void setKey(
const size_t kidx,
const bool is_pressed,
const uint32_t now_ms)
69 if (kidx >= N)
return;
71 if (!now.test(kidx)) {
72 press_at[kidx] = now_ms;
73 last_repeat_at[kidx] = 0U;
81 inline void computeEdges()
83 const bits_t changed = now ^ prev;
84 pressed = changed & now;
85 released = changed & ~now;
88 inline void tickHoldRepeat(
const uint32_t now_ms)
90 for (
size_t i = 0; i < N; ++i) {
92 const uint32_t elapsed = now_ms - press_at[i];
93 if (elapsed >= holding_threshold_ms) {
94 if (!holding.test(i)) was_hold.set(i);
97 if (elapsed >= repeat_initial_ms) {
98 if (last_repeat_at[i] == 0U || (now_ms - last_repeat_at[i]) >= repeat_rate_ms) {
100 last_repeat_at[i] = now_ms;
105 last_repeat_at[i] = 0U;
110 inline void commitPrev()
Top level namespace of M5Stack.
Per-key state tracking (now / prev / edge / hold / repeat) with timestamps.