aboutsummaryrefslogtreecommitdiff
path: root/quantum/dynamic_keymap.c
diff options
context:
space:
mode:
authorskullY <skullydazed@gmail.com>2019-08-30 11:19:03 -0700
committerskullydazed <skullydazed@users.noreply.github.com>2019-08-30 15:01:52 -0700
commitb624f32f944acdc59dcb130674c09090c5c404cb (patch)
treebc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /quantum/dynamic_keymap.c
parent61af76a10d00aba185b8338604171de490a13e3b (diff)
downloadqmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz
qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip
clang-format changes
Diffstat (limited to 'quantum/dynamic_keymap.c')
-rw-r--r--quantum/dynamic_keymap.c348
1 files changed, 161 insertions, 187 deletions
diff --git a/quantum/dynamic_keymap.c b/quantum/dynamic_keymap.c
index 38400e36f..ca056f630 100644
--- a/quantum/dynamic_keymap.c
+++ b/quantum/dynamic_keymap.c
@@ -15,224 +15,198 @@
15 */ 15 */
16 16
17#include "config.h" 17#include "config.h"
18#include "keymap.h" // to get keymaps[][][] 18#include "keymap.h" // to get keymaps[][][]
19#include "tmk_core/common/eeprom.h" 19#include "tmk_core/common/eeprom.h"
20#include "progmem.h" // to read default from flash 20#include "progmem.h" // to read default from flash
21#include "quantum.h" // for send_string() 21#include "quantum.h" // for send_string()
22#include "dynamic_keymap.h" 22#include "dynamic_keymap.h"
23 23
24#ifdef DYNAMIC_KEYMAP_ENABLE 24#ifdef DYNAMIC_KEYMAP_ENABLE
25 25
26#ifndef DYNAMIC_KEYMAP_EEPROM_ADDR 26# ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
27#error DYNAMIC_KEYMAP_EEPROM_ADDR not defined 27# error DYNAMIC_KEYMAP_EEPROM_ADDR not defined
28#endif 28# endif
29 29
30#ifndef DYNAMIC_KEYMAP_LAYER_COUNT 30# ifndef DYNAMIC_KEYMAP_LAYER_COUNT
31#error DYNAMIC_KEYMAP_LAYER_COUNT not defined 31# error DYNAMIC_KEYMAP_LAYER_COUNT not defined
32#endif 32# endif
33 33
34#ifndef DYNAMIC_KEYMAP_MACRO_COUNT 34# ifndef DYNAMIC_KEYMAP_MACRO_COUNT
35#error DYNAMIC_KEYMAP_MACRO_COUNT not defined 35# error DYNAMIC_KEYMAP_MACRO_COUNT not defined
36#endif 36# endif
37 37
38#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR 38# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
39#error DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR not defined 39# error DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR not defined
40#endif 40# endif
41 41
42#ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE 42# ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
43#error DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE not defined 43# error DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE not defined
44#endif 44# endif
45 45
46uint8_t dynamic_keymap_get_layer_count(void) 46uint8_t dynamic_keymap_get_layer_count(void) { return DYNAMIC_KEYMAP_LAYER_COUNT; }
47{
48 return DYNAMIC_KEYMAP_LAYER_COUNT;
49}
50 47
51void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) 48void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
52{ 49 // TODO: optimize this with some left shifts
53 // TODO: optimize this with some left shifts 50 return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
54 return ((void*)DYNAMIC_KEYMAP_EEPROM_ADDR) + ( layer * MATRIX_ROWS * MATRIX_COLS * 2 ) +
55 ( row * MATRIX_COLS * 2 ) + ( column * 2 );
56} 51}
57 52
58uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) 53uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
59{ 54 void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
60 void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); 55 // Big endian, so we can read/write EEPROM directly from host if we want
61 // Big endian, so we can read/write EEPROM directly from host if we want 56 uint16_t keycode = eeprom_read_byte(address) << 8;
62 uint16_t keycode = eeprom_read_byte(address) << 8; 57 keycode |= eeprom_read_byte(address + 1);
63 keycode |= eeprom_read_byte(address + 1); 58 return keycode;
64 return keycode;
65} 59}
66 60
67void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) 61void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
68{ 62 void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
69 void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column); 63 // Big endian, so we can read/write EEPROM directly from host if we want
70 // Big endian, so we can read/write EEPROM directly from host if we want 64 eeprom_update_byte(address, (uint8_t)(keycode >> 8));
71 eeprom_update_byte(address, (uint8_t)(keycode >> 8)); 65 eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
72 eeprom_update_byte(address+1, (uint8_t)(keycode & 0xFF));
73} 66}
74 67
75void dynamic_keymap_reset(void) 68void dynamic_keymap_reset(void) {
76{ 69 // Reset the keymaps in EEPROM to what is in flash.
77 // Reset the keymaps in EEPROM to what is in flash. 70 // All keyboards using dynamic keymaps should define a layout
78 // All keyboards using dynamic keymaps should define a layout 71 // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT.
79 // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT. 72 for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
80 for ( int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++ ) { 73 for (int row = 0; row < MATRIX_ROWS; row++) {
81 for ( int row = 0; row < MATRIX_ROWS; row++ ) { 74 for (int column = 0; column < MATRIX_COLS; column++) {
82 for ( int column = 0; column < MATRIX_COLS; column++ ) { 75 dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column]));
83 dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column])); 76 }
84 } 77 }
85 } 78 }
86 }
87} 79}
88 80
89void dynamic_keymap_get_buffer( uint16_t offset, uint16_t size, uint8_t *data ) 81void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
90{ 82 uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
91 uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2; 83 void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
92 void *source = (void*)(DYNAMIC_KEYMAP_EEPROM_ADDR+offset); 84 uint8_t *target = data;
93 uint8_t *target = data; 85 for (uint16_t i = 0; i < size; i++) {
94 for ( uint16_t i = 0; i < size; i++ ) { 86 if (offset + i < dynamic_keymap_eeprom_size) {
95 if ( offset + i < dynamic_keymap_eeprom_size ) { 87 *target = eeprom_read_byte(source);
96 *target = eeprom_read_byte(source); 88 } else {
97 } else { 89 *target = 0x00;
98 *target = 0x00; 90 }
99 } 91 source++;
100 source++; 92 target++;
101 target++; 93 }
102 }
103} 94}
104 95
105void dynamic_keymap_set_buffer( uint16_t offset, uint16_t size, uint8_t *data ) 96void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
106{ 97 uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
107 uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2; 98 void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
108 void *target = (void*)(DYNAMIC_KEYMAP_EEPROM_ADDR+offset); 99 uint8_t *source = data;
109 uint8_t *source = data; 100 for (uint16_t i = 0; i < size; i++) {
110 for ( uint16_t i = 0; i < size; i++ ) { 101 if (offset + i < dynamic_keymap_eeprom_size) {
111 if ( offset + i < dynamic_keymap_eeprom_size ) { 102 eeprom_update_byte(target, *source);
112 eeprom_update_byte(target, *source); 103 }
113 } 104 source++;
114 source++; 105 target++;
115 target++; 106 }
116 }
117} 107}
118 108
119// This overrides the one in quantum/keymap_common.c 109// This overrides the one in quantum/keymap_common.c
120uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) 110uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
121{ 111 if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row < MATRIX_ROWS && key.col < MATRIX_COLS) {
122 if ( layer < DYNAMIC_KEYMAP_LAYER_COUNT && 112 return dynamic_keymap_get_keycode(layer, key.row, key.col);
123 key.row < MATRIX_ROWS && 113 } else {
124 key.col < MATRIX_COLS ) { 114 return KC_NO;
125 return dynamic_keymap_get_keycode(layer, key.row, key.col); 115 }
126 } else {
127 return KC_NO;
128 }
129}
130
131
132
133uint8_t dynamic_keymap_macro_get_count(void)
134{
135 return DYNAMIC_KEYMAP_MACRO_COUNT;
136}
137
138uint16_t dynamic_keymap_macro_get_buffer_size(void)
139{
140 return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE;
141} 116}
142 117
143void dynamic_keymap_macro_get_buffer( uint16_t offset, uint16_t size, uint8_t *data ) 118uint8_t dynamic_keymap_macro_get_count(void) { return DYNAMIC_KEYMAP_MACRO_COUNT; }
144{ 119
145 void *source = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR+offset); 120uint16_t dynamic_keymap_macro_get_buffer_size(void) { return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE; }
146 uint8_t *target = data; 121
147 for ( uint16_t i = 0; i < size; i++ ) { 122void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
148 if ( offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE ) { 123 void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
149 *target = eeprom_read_byte(source); 124 uint8_t *target = data;
150 } else { 125 for (uint16_t i = 0; i < size; i++) {
151 *target = 0x00; 126 if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
152 } 127 *target = eeprom_read_byte(source);
153 source++; 128 } else {
154 target++; 129 *target = 0x00;
155 } 130 }
131 source++;
132 target++;
133 }
156} 134}
157 135
158void dynamic_keymap_macro_set_buffer( uint16_t offset, uint16_t size, uint8_t *data ) 136void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
159{ 137 void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
160 void *target = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR+offset); 138 uint8_t *source = data;
161 uint8_t *source = data; 139 for (uint16_t i = 0; i < size; i++) {
162 for ( uint16_t i = 0; i < size; i++ ) { 140 if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
163 if ( offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE ) { 141 eeprom_update_byte(target, *source);
164 eeprom_update_byte(target, *source); 142 }
165 } 143 source++;
166 source++; 144 target++;
167 target++; 145 }
168 }
169} 146}
170 147
171void dynamic_keymap_macro_reset(void) 148void dynamic_keymap_macro_reset(void) {
172{ 149 void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
173 void *p = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR); 150 void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
174 void *end = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR+DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE); 151 while (p != end) {
175 while ( p != end ) { 152 eeprom_update_byte(p, 0);
176 eeprom_update_byte(p, 0); 153 ++p;
177 ++p; 154 }
178 }
179} 155}
180 156
181void dynamic_keymap_macro_send( uint8_t id ) 157void dynamic_keymap_macro_send(uint8_t id) {
182{ 158 if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
183 if ( id >= DYNAMIC_KEYMAP_MACRO_COUNT ) { 159 return;
184 return; 160 }
185 } 161
186 162 // Check the last byte of the buffer.
187 // Check the last byte of the buffer. 163 // If it's not zero, then we are in the middle
188 // If it's not zero, then we are in the middle 164 // of buffer writing, possibly an aborted buffer
189 // of buffer writing, possibly an aborted buffer 165 // write. So do nothing.
190 // write. So do nothing. 166 void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE - 1);
191 void *p = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR+DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE-1); 167 if (eeprom_read_byte(p) != 0) {
192 if ( eeprom_read_byte(p) != 0 ) { 168 return;
193 return; 169 }
194 } 170
195 171 // Skip N null characters
196 // Skip N null characters 172 // p will then point to the Nth macro
197 // p will then point to the Nth macro 173 p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
198 p = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR); 174 void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
199 void *end = (void*)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR+DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE); 175 while (id > 0) {
200 while ( id > 0 ) { 176 // If we are past the end of the buffer, then the buffer
201 // If we are past the end of the buffer, then the buffer 177 // contents are garbage, i.e. there were not DYNAMIC_KEYMAP_MACRO_COUNT
202 // contents are garbage, i.e. there were not DYNAMIC_KEYMAP_MACRO_COUNT 178 // nulls in the buffer.
203 // nulls in the buffer. 179 if (p == end) {
204 if ( p == end ) { 180 return;
205 return; 181 }
206 } 182 if (eeprom_read_byte(p) == 0) {
207 if ( eeprom_read_byte(p) == 0 ) { 183 --id;
208 --id; 184 }
209 } 185 ++p;
210 ++p; 186 }
211 } 187
212 188 // Send the macro string one or two chars at a time
213 // Send the macro string one or two chars at a time 189 // by making temporary 1 or 2 char strings
214 // by making temporary 1 or 2 char strings 190 char data[3] = {0, 0, 0};
215 char data[3] = { 0, 0, 0 }; 191 // We already checked there was a null at the end of
216 // We already checked there was a null at the end of 192 // the buffer, so this cannot go past the end
217 // the buffer, so this cannot go past the end 193 while (1) {
218 while ( 1 ) { 194 data[0] = eeprom_read_byte(p++);
219 data[0] = eeprom_read_byte(p++); 195 data[1] = 0;
220 data[1] = 0; 196 // Stop at the null terminator of this macro string
221 // Stop at the null terminator of this macro string 197 if (data[0] == 0) {
222 if ( data[0] == 0 ) { 198 break;
223 break; 199 }
224 } 200 // If the char is magic (tap, down, up),
225 // If the char is magic (tap, down, up), 201 // add the next char (key to use) and send a 2 char string.
226 // add the next char (key to use) and send a 2 char string. 202 if (data[0] == SS_TAP_CODE || data[0] == SS_DOWN_CODE || data[0] == SS_UP_CODE) {
227 if ( data[0] == SS_TAP_CODE || data[0] == SS_DOWN_CODE || data[0] == SS_UP_CODE ) { 203 data[1] = eeprom_read_byte(p++);
228 data[1] = eeprom_read_byte(p++); 204 if (data[1] == 0) {
229 if ( data[1] == 0 ) { 205 break;
230 break; 206 }
231 } 207 }
232 } 208 send_string(data);
233 send_string(data); 209 }
234 }
235} 210}
236 211
237#endif // DYNAMIC_KEYMAP_ENABLE 212#endif // DYNAMIC_KEYMAP_ENABLE
238