|
| 1 | +/* Keyboard State Tracker |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/Arduino-Source |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef PokemonAutomation_Controllers_KeyboardStateTracker_H |
| 8 | +#define PokemonAutomation_Controllers_KeyboardStateTracker_H |
| 9 | + |
| 10 | +#include <deque> |
| 11 | +#include <set> |
| 12 | +#include "Common/Cpp/Time.h" |
| 13 | + |
| 14 | +namespace PokemonAutomation{ |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +class KeyboardStateTracker{ |
| 19 | +public: |
| 20 | + KeyboardStateTracker(std::chrono::milliseconds debounce_period = std::chrono::milliseconds(10)) |
| 21 | + : m_debounce_period(debounce_period) |
| 22 | + {} |
| 23 | + |
| 24 | + |
| 25 | +public: |
| 26 | + void clear(){ |
| 27 | + m_committed.clear(); |
| 28 | + m_pending.clear(); |
| 29 | + } |
| 30 | + void press(uint32_t key){ |
| 31 | + WallClock now = current_time(); |
| 32 | + m_pending.emplace_back(Event{now, true, key}); |
| 33 | + move_old_events(now); |
| 34 | + } |
| 35 | + void release(uint32_t key){ |
| 36 | + WallClock now = current_time(); |
| 37 | + m_pending.emplace_back(Event{now, false, key}); |
| 38 | + move_old_events(now); |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +public: |
| 43 | + WallClock next_state_change() const{ |
| 44 | + return m_pending.empty() |
| 45 | + ? WallClock::max() |
| 46 | + : m_pending.begin()->timestamp + m_debounce_period; |
| 47 | + } |
| 48 | + std::set<uint32_t> get_currently_pressed(){ |
| 49 | + WallClock now = current_time(); |
| 50 | + move_old_events(now); |
| 51 | + |
| 52 | + // Copy the current committed set. |
| 53 | + std::set<uint32_t> ret = m_committed; |
| 54 | + |
| 55 | + // Now process all the pending ones - ignoring the release events. |
| 56 | + for (const Event& event : m_pending){ |
| 57 | + if (event.press){ |
| 58 | + ret.insert(event.key); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return ret; |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | +private: |
| 67 | + // Move all pending events that are old enough to the committed set. |
| 68 | + void move_old_events(WallClock now){ |
| 69 | + WallClock threshold = now - m_debounce_period; |
| 70 | + while (!m_pending.empty()){ |
| 71 | + Event& event = m_pending.front(); |
| 72 | + |
| 73 | + // Always commit presses. |
| 74 | + if (event.press){ |
| 75 | + m_committed.insert(event.key); |
| 76 | + m_pending.pop_front(); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + // Release is old enough. Commit it. |
| 81 | + if (event.timestamp < threshold){ |
| 82 | + m_committed.erase(event.key); |
| 83 | + m_pending.pop_front(); |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + // We've hit an uncommittable release. We're done. |
| 88 | + break; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +private: |
| 94 | + struct Event{ |
| 95 | + WallClock timestamp; |
| 96 | + bool press; |
| 97 | + uint32_t key; |
| 98 | + }; |
| 99 | + std::chrono::milliseconds m_debounce_period; |
| 100 | + std::set<uint32_t> m_committed; |
| 101 | + std::deque<Event> m_pending; |
| 102 | +}; |
| 103 | + |
| 104 | + |
| 105 | + |
| 106 | +} |
| 107 | +#endif |
0 commit comments