-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathEEPROM-Keymap.cpp
170 lines (139 loc) · 5.42 KB
/
EEPROM-Keymap.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* -*- mode: c++ -*-
* Kaleidoscope-EEPROM-Keymap -- EEPROM-based keymap support.
* Copyright (C) 2017, 2018, 2019, 2021 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "kaleidoscope/plugin/EEPROM-Keymap.h"
#include <Arduino.h> // for PSTR, F, __FlashStringHelper
#include <Kaleidoscope-EEPROM-Settings.h> // for EEPROMSettings
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial
#include <stdint.h> // for uint8_t, uint16_t
#include "kaleidoscope/KeyAddr.h" // for KeyAddr, MatrixAddr, MatrixAddr<>::Range
#include "kaleidoscope/Runtime.h" // for Runtime, Runtime_
#include "kaleidoscope/device/device.h" // for VirtualProps::Storage, Device, Base<>::St...
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult, EventHandlerResult::OK
#include "kaleidoscope/key_defs.h" // for Key, Key_NoKey
#include "kaleidoscope/layers.h" // for Layer_, Layer, layer_count
namespace kaleidoscope {
namespace plugin {
uint16_t EEPROMKeymap::keymap_base_;
uint8_t EEPROMKeymap::max_layers_;
uint8_t EEPROMKeymap::progmem_layers_;
EventHandlerResult EEPROMKeymap::onSetup() {
::EEPROMSettings.onSetup();
progmem_layers_ = layer_count;
return EventHandlerResult::OK;
}
EventHandlerResult EEPROMKeymap::onNameQuery() {
return ::Focus.sendName(F("EEPROMKeymap"));
}
void EEPROMKeymap::setup(uint8_t max) {
layer_count = max;
if (::EEPROMSettings.ignoreHardcodedLayers()) {
Layer.getKey = getKey;
} else {
layer_count += progmem_layers_;
Layer.getKey = getKeyExtended;
}
max_layers(max);
}
void EEPROMKeymap::max_layers(uint8_t max) {
max_layers_ = max;
keymap_base_ = ::EEPROMSettings.requestSlice(max_layers_ * Runtime.device().numKeys() * 2);
}
Key EEPROMKeymap::getKey(uint8_t layer, KeyAddr key_addr) {
if (layer >= max_layers_)
return Key_NoKey;
uint16_t pos = ((layer * Runtime.device().numKeys()) + key_addr.toInt()) * 2;
return Key(Runtime.storage().read(keymap_base_ + pos + 1), // key_code
Runtime.storage().read(keymap_base_ + pos)); // flags
}
Key EEPROMKeymap::getKeyExtended(uint8_t layer, KeyAddr key_addr) {
// If the layer is within PROGMEM bounds, look it up from there
if (layer < progmem_layers_) {
return Layer.getKeyFromPROGMEM(layer, key_addr);
}
// If the layer is outside of PROGMEM, look up from EEPROM
return getKey(layer - progmem_layers_, key_addr);
}
uint16_t EEPROMKeymap::keymap_base() {
return keymap_base_;
}
void EEPROMKeymap::updateKey(uint16_t base_pos, Key key) {
Runtime.storage().update(keymap_base_ + base_pos * 2, key.getFlags());
Runtime.storage().update(keymap_base_ + base_pos * 2 + 1, key.getKeyCode());
}
void EEPROMKeymap::dumpKeymap(uint8_t layers, Key (*getkey)(uint8_t, KeyAddr)) {
for (uint8_t layer = 0; layer < layers; layer++) {
for (auto key_addr : KeyAddr::all()) {
Key k = (*getkey)(layer, key_addr);
::Focus.send(k);
}
}
}
EventHandlerResult EEPROMKeymap::onFocusEvent(const char *input) {
const char *cmd_custom = PSTR("keymap.custom");
const char *cmd_default = PSTR("keymap.default");
const char *cmd_onlyCustom = PSTR("keymap.onlyCustom");
if (::Focus.inputMatchesHelp(input))
return ::Focus.printHelp(cmd_custom, cmd_default, cmd_onlyCustom);
if (::Focus.inputMatchesCommand(input, cmd_onlyCustom)) {
if (::Focus.isEOL()) {
::Focus.send((uint8_t)::EEPROMSettings.ignoreHardcodedLayers());
} else {
bool v;
::Focus.read((uint8_t &)v);
::EEPROMSettings.ignoreHardcodedLayers(v);
layer_count = max_layers_;
if (v) {
Layer.getKey = getKey;
} else {
layer_count += progmem_layers_;
Layer.getKey = getKeyExtended;
}
}
return EventHandlerResult::EVENT_CONSUMED;
}
if (::Focus.inputMatchesCommand(input, cmd_default)) {
// By using a cast to the appropriate function type,
// tell the compiler which overload of getKeyFromPROGMEM
// we actully want.
//
dumpKeymap(progmem_layers_,
static_cast<Key (*)(uint8_t, KeyAddr)>(Layer_::getKeyFromPROGMEM));
return EventHandlerResult::EVENT_CONSUMED;
}
if (!::Focus.inputMatchesCommand(input, cmd_custom))
return EventHandlerResult::OK;
if (::Focus.isEOL()) {
// By using a cast to the appropriate function type,
// tell the compiler which overload of getKey
// we actually want.
//
dumpKeymap(max_layers_, static_cast<Key (*)(uint8_t, KeyAddr)>(getKey));
} else {
uint16_t i = 0;
while (!::Focus.isEOL() && (i < (uint16_t)Runtime.device().numKeys() * max_layers_)) {
Key k;
::Focus.read(k);
updateKey(i, k);
i++;
}
Runtime.storage().commit();
}
return EventHandlerResult::EVENT_CONSUMED;
}
} // namespace plugin
} // namespace kaleidoscope
kaleidoscope::plugin::EEPROMKeymap EEPROMKeymap;