dynamic_keymap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /* Copyright 2017 Jason Williams (Wilba)
  2. *
  3. * 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. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "keymap.h" // to get keymaps[][][]
  17. #include "eeprom.h"
  18. #include "progmem.h" // to read default from flash
  19. #include "quantum.h" // for send_string()
  20. #include "dynamic_keymap.h"
  21. #include "fnv.h"
  22. #ifdef VIA_ENABLE
  23. # include "via.h" // for VIA_EEPROM_CONFIG_END
  24. # define DYNAMIC_KEYMAP_EEPROM_START (VIA_EEPROM_CONFIG_END)
  25. #else
  26. # define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE)
  27. #endif
  28. #ifdef ENCODER_ENABLE
  29. # include "encoder.h"
  30. #else
  31. # define NUM_ENCODERS 0
  32. #endif
  33. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  34. # define DYNAMIC_KEYMAP_LAYER_COUNT 4
  35. #endif
  36. #ifndef DYNAMIC_KEYMAP_MACRO_COUNT
  37. # define DYNAMIC_KEYMAP_MACRO_COUNT 16
  38. #endif
  39. #ifndef TOTAL_EEPROM_BYTE_COUNT
  40. # error Unknown total EEPROM size. Cannot derive maximum for dynamic keymaps.
  41. #endif
  42. #ifndef DYNAMIC_KEYMAP_EEPROM_MAX_ADDR
  43. # define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR (TOTAL_EEPROM_BYTE_COUNT - 1)
  44. #endif
  45. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > (TOTAL_EEPROM_BYTE_COUNT - 1)
  46. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > " STR((TOTAL_EEPROM_BYTE_COUNT - 1))
  47. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is configured to use more space than what is available for the selected EEPROM driver
  48. #endif
  49. // Due to usage of uint16_t check for max 65535
  50. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > 65535
  51. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > 65535"
  52. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR must be less than 65536
  53. #endif
  54. // If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
  55. #ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
  56. # define DYNAMIC_KEYMAP_EEPROM_ADDR DYNAMIC_KEYMAP_EEPROM_START
  57. #endif
  58. // Dynamic encoders starts after dynamic keymaps
  59. #ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR
  60. # define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
  61. #endif
  62. // Dynamic macro starts after dynamic encoders, but only when using ENCODER_MAP
  63. #ifdef ENCODER_MAP_ENABLE
  64. # ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  65. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2))
  66. # endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  67. #else // ENCODER_MAP_ENABLE
  68. # ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  69. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR)
  70. # endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  71. #endif // ENCODER_MAP_ENABLE
  72. // Sanity check that dynamic keymaps fit in available EEPROM
  73. // If there's not 100 bytes available for macros, then something is wrong.
  74. // The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it,
  75. // or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has
  76. // more than the default.
  77. _Static_assert((DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) - (DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) >= 100, "Dynamic keymaps are configured to use more EEPROM than is available.");
  78. // Dynamic macros are stored after the keymaps and use what is available
  79. // up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR.
  80. #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
  81. # define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + 1)
  82. #endif
  83. #ifndef DYNAMIC_KEYMAP_MACRO_DELAY
  84. # define DYNAMIC_KEYMAP_MACRO_DELAY TAP_CODE_DELAY
  85. #endif
  86. uint8_t dynamic_keymap_get_layer_count(void) {
  87. return DYNAMIC_KEYMAP_LAYER_COUNT;
  88. }
  89. void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
  90. // TODO: optimize this with some left shifts
  91. return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
  92. }
  93. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
  94. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO;
  95. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  96. // Big endian, so we can read/write EEPROM directly from host if we want
  97. uint16_t keycode = eeprom_read_byte(address) << 8;
  98. keycode |= eeprom_read_byte(address + 1);
  99. return keycode;
  100. }
  101. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
  102. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return;
  103. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  104. // Big endian, so we can read/write EEPROM directly from host if we want
  105. eeprom_update_byte(address, (uint8_t)(keycode >> 8));
  106. eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
  107. }
  108. #ifdef ENCODER_MAP_ENABLE
  109. void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) {
  110. return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2);
  111. }
  112. uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
  113. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO;
  114. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  115. // Big endian, so we can read/write EEPROM directly from host if we want
  116. uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8;
  117. keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1);
  118. return keycode;
  119. }
  120. void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
  121. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return;
  122. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  123. // Big endian, so we can read/write EEPROM directly from host if we want
  124. eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8));
  125. eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF));
  126. }
  127. #endif // ENCODER_MAP_ENABLE
  128. static uint32_t dynamic_keymap_compute_hash(void) {
  129. Fnv32_t hash = FNV1_32A_INIT;
  130. uint16_t keycode;
  131. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  132. for (int row = 0; row < MATRIX_ROWS; row++) {
  133. for (int column = 0; column < MATRIX_COLS; column++) {
  134. keycode = pgm_read_word(&keymaps[layer][row][column]);
  135. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  136. }
  137. }
  138. #ifdef ENCODER_MAP_ENABLE
  139. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  140. keycode = pgm_read_word(&encoder_map[layer][encoder][0]);
  141. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  142. keycode = pgm_read_word(&encoder_map[layer][encoder][1]);
  143. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  144. }
  145. #endif // ENCODER_MAP_ENABLE
  146. }
  147. return hash;
  148. }
  149. static uint32_t dynamic_keymap_hash(void) {
  150. static uint32_t hash = 0;
  151. static uint8_t s_init = 0;
  152. if (!s_init) {
  153. s_init = 1;
  154. hash = dynamic_keymap_compute_hash();
  155. }
  156. return hash;
  157. }
  158. bool dynamic_keymap_is_valid(void) {
  159. return eeprom_read_dword(EECONFIG_KEYMAP_HASH) == dynamic_keymap_hash();
  160. }
  161. void dynamic_keymap_reset(void) {
  162. // Reset the keymaps in EEPROM to what is in flash.
  163. // All keyboards using dynamic keymaps should define a layout
  164. // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT.
  165. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  166. for (int row = 0; row < MATRIX_ROWS; row++) {
  167. for (int column = 0; column < MATRIX_COLS; column++) {
  168. dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column]));
  169. }
  170. }
  171. #ifdef ENCODER_MAP_ENABLE
  172. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  173. dynamic_keymap_set_encoder(layer, encoder, true, pgm_read_word(&encoder_map[layer][encoder][0]));
  174. dynamic_keymap_set_encoder(layer, encoder, false, pgm_read_word(&encoder_map[layer][encoder][1]));
  175. }
  176. #endif // ENCODER_MAP_ENABLE
  177. }
  178. eeprom_update_dword(EECONFIG_KEYMAP_HASH, dynamic_keymap_hash());
  179. }
  180. void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  181. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  182. void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  183. uint8_t *target = data;
  184. for (uint16_t i = 0; i < size; i++) {
  185. if (offset + i < dynamic_keymap_eeprom_size) {
  186. *target = eeprom_read_byte(source);
  187. } else {
  188. *target = 0x00;
  189. }
  190. source++;
  191. target++;
  192. }
  193. }
  194. void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  195. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  196. void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  197. uint8_t *source = data;
  198. for (uint16_t i = 0; i < size; i++) {
  199. if (offset + i < dynamic_keymap_eeprom_size) {
  200. eeprom_update_byte(target, *source);
  201. }
  202. source++;
  203. target++;
  204. }
  205. }
  206. // This overrides the one in quantum/keymap_common.c
  207. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
  208. if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row < MATRIX_ROWS && key.col < MATRIX_COLS) {
  209. return dynamic_keymap_get_keycode(layer, key.row, key.col);
  210. }
  211. #ifdef ENCODER_MAP_ENABLE
  212. else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CW && key.col < NUM_ENCODERS) {
  213. return dynamic_keymap_get_encoder(layer, key.col, true);
  214. } else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CCW && key.col < NUM_ENCODERS) {
  215. return dynamic_keymap_get_encoder(layer, key.col, false);
  216. }
  217. #endif // ENCODER_MAP_ENABLE
  218. return KC_NO;
  219. }
  220. uint8_t dynamic_keymap_macro_get_count(void) {
  221. return DYNAMIC_KEYMAP_MACRO_COUNT;
  222. }
  223. uint16_t dynamic_keymap_macro_get_buffer_size(void) {
  224. return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE;
  225. }
  226. void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  227. void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  228. uint8_t *target = data;
  229. for (uint16_t i = 0; i < size; i++) {
  230. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  231. *target = eeprom_read_byte(source);
  232. } else {
  233. *target = 0x00;
  234. }
  235. source++;
  236. target++;
  237. }
  238. }
  239. void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  240. void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  241. uint8_t *source = data;
  242. for (uint16_t i = 0; i < size; i++) {
  243. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  244. eeprom_update_byte(target, *source);
  245. }
  246. source++;
  247. target++;
  248. }
  249. }
  250. void dynamic_keymap_macro_reset(void) {
  251. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  252. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  253. while (p != end) {
  254. eeprom_update_byte(p, 0);
  255. ++p;
  256. }
  257. }
  258. void dynamic_keymap_macro_send(uint8_t id) {
  259. if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
  260. return;
  261. }
  262. // Check the last byte of the buffer.
  263. // If it's not zero, then we are in the middle
  264. // of buffer writing, possibly an aborted buffer
  265. // write. So do nothing.
  266. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE - 1);
  267. if (eeprom_read_byte(p) != 0) {
  268. return;
  269. }
  270. // Skip N null characters
  271. // p will then point to the Nth macro
  272. p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  273. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  274. while (id > 0) {
  275. // If we are past the end of the buffer, then the buffer
  276. // contents are garbage, i.e. there were not DYNAMIC_KEYMAP_MACRO_COUNT
  277. // nulls in the buffer.
  278. if (p == end) {
  279. return;
  280. }
  281. if (eeprom_read_byte(p) == 0) {
  282. --id;
  283. }
  284. ++p;
  285. }
  286. // Send the macro string one or three chars at a time
  287. // by making temporary 1 or 3 char strings
  288. char data[4] = {0, 0, 0, 0};
  289. // We already checked there was a null at the end of
  290. // the buffer, so this cannot go past the end
  291. while (1) {
  292. data[0] = eeprom_read_byte(p++);
  293. data[1] = 0;
  294. // Stop at the null terminator of this macro string
  295. if (data[0] == 0) {
  296. break;
  297. }
  298. // If the char is magic (tap, down, up),
  299. // add the next char (key to use) and send a 3 char string.
  300. if (data[0] == SS_TAP_CODE || data[0] == SS_DOWN_CODE || data[0] == SS_UP_CODE) {
  301. data[1] = data[0];
  302. data[0] = SS_QMK_PREFIX;
  303. data[2] = eeprom_read_byte(p++);
  304. if (data[2] == 0) {
  305. break;
  306. }
  307. }
  308. send_string_with_delay(data, DYNAMIC_KEYMAP_MACRO_DELAY);
  309. }
  310. }