summaryrefslogtreecommitdiff
path: root/quantum/dynamic_keymap.c
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2025-03-21 23:38:34 +1100
committerGitHub <noreply@github.com>2025-03-21 23:38:34 +1100
commit2b00b846dce1d1267dfdb2a3c2972a367cc0dd44 (patch)
tree6d801a8d83c58ee646b789bfa9ab912857ee7d8c /quantum/dynamic_keymap.c
parentc9d62ddc78e879053241202b288d0129073b07dc (diff)
Non-volatile memory data repository pattern (#24356)
* First batch of eeconfig conversions. * Offset and length for datablocks. * `via`, `dynamic_keymap`. * Fix filename. * Commentary. * wilba leds * satisfaction75 * satisfaction75 * more keyboard whack-a-mole * satisfaction75 * omnikeyish * more whack-a-mole * `generic_features.mk` to automatically pick up nvm repositories * thievery * deferred variable resolve * whitespace * convert api to structs/unions * convert api to structs/unions * convert api to structs/unions * fixups * code-side docs * code size fix * rollback * nvm_xxxxx_erase * Updated location of eeconfig magic numbers so non-EEPROM nvm drivers can use them too. * Fixup build. * Fixup compilation error with encoders. * Build fixes. * Add `via_ci` keymap to onekey to exercise VIA bindings (and thus dynamic keymap et.al.), fixup compilation errors based on preprocessor+sizeof. * Build failure rectification.
Diffstat (limited to 'quantum/dynamic_keymap.c')
-rw-r--r--quantum/dynamic_keymap.c203
1 files changed, 38 insertions, 165 deletions
diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c
index beb7f9d18f..756b232f59 100644
--- a/quantum/dynamic_keymap.c
+++ b/quantum/dynamic_keymap.c
@@ -1,4 +1,5 @@
1/* Copyright 2017 Jason Williams (Wilba) 1/* Copyright 2017 Jason Williams (Wilba)
2 * Copyright 2024-2025 Nick Brassel (@tzarc)
2 * 3 *
3 * This program is free software: you can redistribute it and/or modify 4 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by 5 * it under the terms of the GNU General Public License as published by
@@ -21,14 +22,7 @@
21#include "progmem.h" 22#include "progmem.h"
22#include "send_string.h" 23#include "send_string.h"
23#include "keycodes.h" 24#include "keycodes.h"
24 25#include "nvm_dynamic_keymap.h"
25#ifdef VIA_ENABLE
26# include "via.h"
27# define DYNAMIC_KEYMAP_EEPROM_START (VIA_EEPROM_CONFIG_END)
28#else
29# include "eeconfig.h"
30# define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE)
31#endif
32 26
33#ifdef ENCODER_ENABLE 27#ifdef ENCODER_ENABLE
34# include "encoder.h" 28# include "encoder.h"
@@ -36,67 +30,6 @@
36# define NUM_ENCODERS 0 30# define NUM_ENCODERS 0
37#endif 31#endif
38 32
39#ifndef DYNAMIC_KEYMAP_LAYER_COUNT
40# define DYNAMIC_KEYMAP_LAYER_COUNT 4
41#endif
42
43#ifndef DYNAMIC_KEYMAP_MACRO_COUNT
44# define DYNAMIC_KEYMAP_MACRO_COUNT 16
45#endif
46
47#ifndef TOTAL_EEPROM_BYTE_COUNT
48# error Unknown total EEPROM size. Cannot derive maximum for dynamic keymaps.
49#endif
50
51#ifndef DYNAMIC_KEYMAP_EEPROM_MAX_ADDR
52# define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR (TOTAL_EEPROM_BYTE_COUNT - 1)
53#endif
54
55#if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > (TOTAL_EEPROM_BYTE_COUNT - 1)
56# pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > " STR((TOTAL_EEPROM_BYTE_COUNT - 1))
57# error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is configured to use more space than what is available for the selected EEPROM driver
58#endif
59
60// Due to usage of uint16_t check for max 65535
61#if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > 65535
62# pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > 65535"
63# error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR must be less than 65536
64#endif
65
66// If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
67#ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
68# define DYNAMIC_KEYMAP_EEPROM_ADDR DYNAMIC_KEYMAP_EEPROM_START
69#endif
70
71// Dynamic encoders starts after dynamic keymaps
72#ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR
73# define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
74#endif
75
76// Dynamic macro starts after dynamic encoders, but only when using ENCODER_MAP
77#ifdef ENCODER_MAP_ENABLE
78# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
79# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2))
80# endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
81#else // ENCODER_MAP_ENABLE
82# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
83# define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR)
84# endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
85#endif // ENCODER_MAP_ENABLE
86
87// Sanity check that dynamic keymaps fit in available EEPROM
88// If there's not 100 bytes available for macros, then something is wrong.
89// The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it,
90// or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has
91// more than the default.
92_Static_assert((DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) - (DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) >= 100, "Dynamic keymaps are configured to use more EEPROM than is available.");
93
94// Dynamic macros are stored after the keymaps and use what is available
95// up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR.
96#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
97# define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + 1)
98#endif
99
100#ifndef DYNAMIC_KEYMAP_MACRO_DELAY 33#ifndef DYNAMIC_KEYMAP_MACRO_DELAY
101# define DYNAMIC_KEYMAP_MACRO_DELAY TAP_CODE_DELAY 34# define DYNAMIC_KEYMAP_MACRO_DELAY TAP_CODE_DELAY
102#endif 35#endif
@@ -105,52 +38,28 @@ uint8_t dynamic_keymap_get_layer_count(void) {
105 return DYNAMIC_KEYMAP_LAYER_COUNT; 38 return DYNAMIC_KEYMAP_LAYER_COUNT;
106} 39}
107 40
108void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
109 // TODO: optimize this with some left shifts
110 return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
111}
112
113uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) { 41uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
114 if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO; 42 return nvm_dynamic_keymap_read_keycode(layer, row, column);
115 void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
116 // Big endian, so we can read/write EEPROM directly from host if we want
117 uint16_t keycode = eeprom_read_byte(address) << 8;
118 keycode |= eeprom_read_byte(address + 1);
119 return keycode;
120} 43}
121 44
122void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) { 45void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
123 if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return; 46 nvm_dynamic_keymap_update_keycode(layer, row, column, keycode);
124 void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
125 // Big endian, so we can read/write EEPROM directly from host if we want
126 eeprom_update_byte(address, (uint8_t)(keycode >> 8));
127 eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
128} 47}
129 48
130#ifdef ENCODER_MAP_ENABLE 49#ifdef ENCODER_MAP_ENABLE
131void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) {
132 return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2);
133}
134
135uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) { 50uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
136 if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO; 51 return nvm_dynamic_keymap_read_encoder(layer, encoder_id, clockwise);
137 void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
138 // Big endian, so we can read/write EEPROM directly from host if we want
139 uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8;
140 keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1);
141 return keycode;
142} 52}
143 53
144void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) { 54void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
145 if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return; 55 nvm_dynamic_keymap_update_encoder(layer, encoder_id, clockwise, keycode);
146 void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
147 // Big endian, so we can read/write EEPROM directly from host if we want
148 eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8));
149 eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF));
150} 56}
151#endif // ENCODER_MAP_ENABLE 57#endif // ENCODER_MAP_ENABLE
152 58
153void dynamic_keymap_reset(void) { 59void dynamic_keymap_reset(void) {
60 // Erase the keymaps, if necessary.
61 nvm_dynamic_keymap_erase();
62
154 // Reset the keymaps in EEPROM to what is in flash. 63 // Reset the keymaps in EEPROM to what is in flash.
155 for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) { 64 for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
156 for (int row = 0; row < MATRIX_ROWS; row++) { 65 for (int row = 0; row < MATRIX_ROWS; row++) {
@@ -168,31 +77,11 @@ void dynamic_keymap_reset(void) {
168} 77}
169 78
170void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) { 79void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
171 uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2; 80 nvm_dynamic_keymap_read_buffer(offset, size, data);
172 void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
173 uint8_t *target = data;
174 for (uint16_t i = 0; i < size; i++) {
175 if (offset + i < dynamic_keymap_eeprom_size) {
176 *target = eeprom_read_byte(source);
177 } else {
178 *target = 0x00;
179 }
180 source++;
181 target++;
182 }
183} 81}
184 82
185void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) { 83void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
186 uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2; 84 nvm_dynamic_keymap_update_buffer(offset, size, data);
187 void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
188 uint8_t *source = data;
189 for (uint16_t i = 0; i < size; i++) {
190 if (offset + i < dynamic_keymap_eeprom_size) {
191 eeprom_update_byte(target, *source);
192 }
193 source++;
194 target++;
195 }
196} 85}
197 86
198uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) { 87uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
@@ -216,53 +105,38 @@ uint8_t dynamic_keymap_macro_get_count(void) {
216} 105}
217 106
218uint16_t dynamic_keymap_macro_get_buffer_size(void) { 107uint16_t dynamic_keymap_macro_get_buffer_size(void) {
219 return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE; 108 return (uint16_t)nvm_dynamic_keymap_macro_size();
220} 109}
221 110
222void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) { 111void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
223 void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset); 112 nvm_dynamic_keymap_macro_read_buffer(offset, size, data);
224 uint8_t *target = data;
225 for (uint16_t i = 0; i < size; i++) {
226 if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
227 *target = eeprom_read_byte(source);
228 } else {
229 *target = 0x00;
230 }
231 source++;
232 target++;
233 }
234} 113}
235 114
236void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) { 115void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
237 void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset); 116 nvm_dynamic_keymap_macro_update_buffer(offset, size, data);
238 uint8_t *source = data;
239 for (uint16_t i = 0; i < size; i++) {
240 if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
241 eeprom_update_byte(target, *source);
242 }
243 source++;
244 target++;
245 }
246} 117}
247 118
248typedef struct send_string_eeprom_state_t { 119static uint8_t dynamic_keymap_read_byte(uint32_t offset) {
249 const uint8_t *ptr; 120 uint8_t d;
250} send_string_eeprom_state_t; 121 nvm_dynamic_keymap_macro_read_buffer(offset, 1, &d);
122 return d;
123}
251 124
252char send_string_get_next_eeprom(void *arg) { 125typedef struct send_string_nvm_state_t {
253 send_string_eeprom_state_t *state = (send_string_eeprom_state_t *)arg; 126 uint32_t offset;
254 char ret = eeprom_read_byte(state->ptr); 127} send_string_nvm_state_t;
255 state->ptr++; 128
129char send_string_get_next_nvm(void *arg) {
130 send_string_nvm_state_t *state = (send_string_nvm_state_t *)arg;
131 char ret = dynamic_keymap_read_byte(state->offset);
132 state->offset++;
256 return ret; 133 return ret;
257} 134}
258 135
259void dynamic_keymap_macro_reset(void) { 136void dynamic_keymap_macro_reset(void) {
260 void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR); 137 // Erase the macros, if necessary.
261 void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE); 138 nvm_dynamic_keymap_macro_erase();
262 while (p != end) { 139 nvm_dynamic_keymap_macro_reset();
263 eeprom_update_byte(p, 0);
264 ++p;
265 }
266} 140}
267 141
268void dynamic_keymap_macro_send(uint8_t id) { 142void dynamic_keymap_macro_send(uint8_t id) {
@@ -274,27 +148,26 @@ void dynamic_keymap_macro_send(uint8_t id) {
274 // If it's not zero, then we are in the middle 148 // If it's not zero, then we are in the middle
275 // of buffer writing, possibly an aborted buffer 149 // of buffer writing, possibly an aborted buffer
276 // write. So do nothing. 150 // write. So do nothing.
277 void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE - 1); 151 if (dynamic_keymap_read_byte(nvm_dynamic_keymap_macro_size() - 1) != 0) {
278 if (eeprom_read_byte(p) != 0) {
279 return; 152 return;
280 } 153 }
281 154
282 // Skip N null characters 155 // Skip N null characters
283 // p will then point to the Nth macro 156 // p will then point to the Nth macro
284 p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR); 157 uint32_t offset = 0;
285 void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE); 158 uint32_t end = nvm_dynamic_keymap_macro_size();
286 while (id > 0) { 159 while (id > 0) {
287 // If we are past the end of the buffer, then there is 160 // If we are past the end of the buffer, then there is
288 // no Nth macro in the buffer. 161 // no Nth macro in the buffer.
289 if (p == end) { 162 if (offset == end) {
290 return; 163 return;
291 } 164 }
292 if (eeprom_read_byte(p) == 0) { 165 if (dynamic_keymap_read_byte(offset) == 0) {
293 --id; 166 --id;
294 } 167 }
295 ++p; 168 ++offset;
296 } 169 }
297 170
298 send_string_eeprom_state_t state = {p}; 171 send_string_nvm_state_t state = {.offset = 0};
299 send_string_with_delay_impl(send_string_get_next_eeprom, &state, DYNAMIC_KEYMAP_MACRO_DELAY); 172 send_string_with_delay_impl(send_string_get_next_nvm, &state, DYNAMIC_KEYMAP_MACRO_DELAY);
300} 173}