-
Notifications
You must be signed in to change notification settings - Fork 9
/
keyscan.h
104 lines (79 loc) · 2.97 KB
/
keyscan.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef KEYSCAN_H_
#define KEYSCAN_H_
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "FreeRTOS.h"
#include "base.h"
#include "layout.h"
#include "semphr.h"
#include "utils.h"
// Each registration creates at most one instance of the handler. If
// CAN_OVERRIDE is true, then another registration with CAN_OVERRIDE equals
// false will override it.
#define REGISTER_CUSTOM_KEYCODE_HANDLER(KEYCODE, CAN_OVERRIDE, CLS) \
status register_##KEYCODE = KeyScan::RegisterCustomKeycodeHandler( \
(KEYCODE), (CAN_OVERRIDE), \
[]() -> CustomKeycodeHandler* { return new CLS(); });
class KeyScan;
class CustomKeycodeHandler {
public:
// Key state is always called regardless whether it's a state transition.
virtual void ProcessKeyState(Keycode kc, bool is_pressed, size_t key_idx) {}
// Key events are only called when the key goes from pressed to released or
// released to pressed, after debouncing.
virtual void ProcessKeyEvent(Keycode kc, bool is_pressed, size_t key_idx) {}
// Getting a reference to the KeyScan instance
virtual void SetKeyScan(KeyScan* keyscan) { key_scan_ = keyscan; }
// Get the name of this custom key
virtual std::string GetName() const = 0;
protected:
KeyScan* key_scan_;
};
class KeyScan : public GenericInputDevice {
public:
using CustomKeycodeHandlerCreator = std::function<CustomKeycodeHandler*()>;
KeyScan();
void InputLoopStart() override;
void InputTick() override;
void SetConfigMode(bool is_config_mode) override;
static status RegisterCustomKeycodeHandler(
uint8_t keycode, bool overridable, CustomKeycodeHandlerCreator creator);
Status SetLayerStatus(uint8_t layer, bool active);
Status ToggleLayerStatus(uint8_t layer);
std::vector<uint8_t> GetActiveLayers();
void SetMouseButtonState(uint8_t mouse_key, bool is_pressed);
void ConfigUp();
void ConfigDown();
void ConfigSelect();
protected:
struct DebounceTimer {
uint8_t tick_count : 7;
uint8_t pressed : 1;
DebounceTimer() : tick_count(0), pressed(0) {}
};
class HandlerRegistry {
public:
static status RegisterHandler(uint8_t keycode, bool overridable,
CustomKeycodeHandlerCreator creator);
static CustomKeycodeHandler* RegisteredHandlerFactory(uint8_t keycode,
KeyScan* outer);
private:
static HandlerRegistry* GetRegistry();
std::map<uint8_t, std::pair<bool, CustomKeycodeHandlerCreator>>
custom_handlers_;
std::map<uint8_t, CustomKeycodeHandler*> handler_singletons_;
};
virtual void SinkGPIODelay();
virtual void NotifyOutput(const std::vector<uint8_t>& pressed_keycode);
virtual void LayerChanged();
std::vector<DebounceTimer> debounce_timer_;
std::vector<bool> active_layers_;
// SemaphoreHandle_t semaphore_;
bool is_config_mode_;
};
Status RegisterKeyscan(uint8_t tag);
#endif /* KEYSCAN_H_ */