dynamic_keymap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "via.h" // for default VIA_EEPROM_ADDR_END
  22. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  23. # define DYNAMIC_KEYMAP_LAYER_COUNT 4
  24. #endif
  25. #ifndef DYNAMIC_KEYMAP_MACRO_COUNT
  26. # define DYNAMIC_KEYMAP_MACRO_COUNT 16
  27. #endif
  28. #ifndef TOTAL_EEPROM_BYTE_COUNT
  29. # error Unknown total EEPROM size. Cannot derive maximum for dynamic keymaps.
  30. #endif
  31. #ifndef DYNAMIC_KEYMAP_EEPROM_MAX_ADDR
  32. # define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR (TOTAL_EEPROM_BYTE_COUNT - 1)
  33. #endif
  34. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > (TOTAL_EEPROM_BYTE_COUNT - 1)
  35. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > " STR((TOTAL_EEPROM_BYTE_COUNT - 1))
  36. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is configured to use more space than what is available for the selected EEPROM driver
  37. #endif
  38. // Due to usage of uint16_t check for max 65535
  39. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > 65535
  40. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > 65535"
  41. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR must be less than 65536
  42. #endif
  43. // If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
  44. // default it start after VIA_EEPROM_CUSTOM_ADDR+VIA_EEPROM_CUSTOM_SIZE
  45. #ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
  46. # ifdef VIA_EEPROM_CUSTOM_CONFIG_ADDR
  47. # define DYNAMIC_KEYMAP_EEPROM_ADDR (VIA_EEPROM_CUSTOM_CONFIG_ADDR + VIA_EEPROM_CUSTOM_CONFIG_SIZE)
  48. # else
  49. # error DYNAMIC_KEYMAP_EEPROM_ADDR not defined
  50. # endif
  51. #endif
  52. // Dynamic encoders starts after dynamic keymaps
  53. #ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR
  54. # define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
  55. #endif
  56. // Dynamic macro starts after dynamic encoders
  57. #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  58. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2))
  59. #endif
  60. // Sanity check that dynamic keymaps fit in available EEPROM
  61. // If there's not 100 bytes available for macros, then something is wrong.
  62. // The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it,
  63. // or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has
  64. // more than the default.
  65. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR < 100
  66. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) " < 100"
  67. # error Dynamic keymaps are configured to use more EEPROM than is available.
  68. #endif
  69. // Dynamic macros are stored after the keymaps and use what is available
  70. // up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR.
  71. #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
  72. # define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + 1)
  73. #endif
  74. uint8_t dynamic_keymap_get_layer_count(void) {
  75. return DYNAMIC_KEYMAP_LAYER_COUNT;
  76. }
  77. void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
  78. // TODO: optimize this with some left shifts
  79. return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
  80. }
  81. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
  82. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO;
  83. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  84. // Big endian, so we can read/write EEPROM directly from host if we want
  85. uint16_t keycode = eeprom_read_byte(address) << 8;
  86. keycode |= eeprom_read_byte(address + 1);
  87. return keycode;
  88. }
  89. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
  90. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return;
  91. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  92. // Big endian, so we can read/write EEPROM directly from host if we want
  93. eeprom_update_byte(address, (uint8_t)(keycode >> 8));
  94. eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
  95. }
  96. #ifdef ENCODER_MAP_ENABLE
  97. void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) {
  98. return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2);
  99. }
  100. uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
  101. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO;
  102. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  103. // Big endian, so we can read/write EEPROM directly from host if we want
  104. uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8;
  105. keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1);
  106. return keycode;
  107. }
  108. void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
  109. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return;
  110. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  111. // Big endian, so we can read/write EEPROM directly from host if we want
  112. eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8));
  113. eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF));
  114. }
  115. #endif // ENCODER_MAP_ENABLE
  116. void dynamic_keymap_reset(void) {
  117. // Reset the keymaps in EEPROM to what is in flash.
  118. // All keyboards using dynamic keymaps should define a layout
  119. // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT.
  120. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  121. for (int row = 0; row < MATRIX_ROWS; row++) {
  122. for (int column = 0; column < MATRIX_COLS; column++) {
  123. dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column]));
  124. }
  125. }
  126. #ifdef ENCODER_MAP_ENABLE
  127. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  128. dynamic_keymap_set_encoder(layer, encoder, true, pgm_read_word(&encoder_map[layer][encoder][0]));
  129. dynamic_keymap_set_encoder(layer, encoder, false, pgm_read_word(&encoder_map[layer][encoder][1]));
  130. }
  131. #endif // ENCODER_MAP_ENABLE
  132. }
  133. }
  134. void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  135. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  136. void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  137. uint8_t *target = data;
  138. for (uint16_t i = 0; i < size; i++) {
  139. if (offset + i < dynamic_keymap_eeprom_size) {
  140. *target = eeprom_read_byte(source);
  141. } else {
  142. *target = 0x00;
  143. }
  144. source++;
  145. target++;
  146. }
  147. }
  148. void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  149. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  150. void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  151. uint8_t *source = data;
  152. for (uint16_t i = 0; i < size; i++) {
  153. if (offset + i < dynamic_keymap_eeprom_size) {
  154. eeprom_update_byte(target, *source);
  155. }
  156. source++;
  157. target++;
  158. }
  159. }
  160. // This overrides the one in quantum/keymap_common.c
  161. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
  162. if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row < MATRIX_ROWS && key.col < MATRIX_COLS) {
  163. return dynamic_keymap_get_keycode(layer, key.row, key.col);
  164. }
  165. #ifdef ENCODER_MAP_ENABLE
  166. else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CW && key.col < NUM_ENCODERS) {
  167. return dynamic_keymap_get_encoder(layer, key.col, true);
  168. } else if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row == KEYLOC_ENCODER_CCW && key.col < NUM_ENCODERS) {
  169. return dynamic_keymap_get_encoder(layer, key.col, false);
  170. }
  171. #endif // ENCODER_MAP_ENABLE
  172. return KC_NO;
  173. }
  174. uint8_t dynamic_keymap_macro_get_count(void) {
  175. return DYNAMIC_KEYMAP_MACRO_COUNT;
  176. }
  177. uint16_t dynamic_keymap_macro_get_buffer_size(void) {
  178. return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE;
  179. }
  180. void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  181. void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  182. uint8_t *target = data;
  183. for (uint16_t i = 0; i < size; i++) {
  184. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  185. *target = eeprom_read_byte(source);
  186. } else {
  187. *target = 0x00;
  188. }
  189. source++;
  190. target++;
  191. }
  192. }
  193. void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  194. void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  195. uint8_t *source = data;
  196. for (uint16_t i = 0; i < size; i++) {
  197. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  198. eeprom_update_byte(target, *source);
  199. }
  200. source++;
  201. target++;
  202. }
  203. }
  204. void dynamic_keymap_macro_reset(void) {
  205. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  206. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  207. while (p != end) {
  208. eeprom_update_byte(p, 0);
  209. ++p;
  210. }
  211. }
  212. void dynamic_keymap_macro_send(uint8_t id) {
  213. if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
  214. return;
  215. }
  216. // Check the last byte of the buffer.
  217. // If it's not zero, then we are in the middle
  218. // of buffer writing, possibly an aborted buffer
  219. // write. So do nothing.
  220. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE - 1);
  221. if (eeprom_read_byte(p) != 0) {
  222. return;
  223. }
  224. // Skip N null characters
  225. // p will then point to the Nth macro
  226. p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  227. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  228. while (id > 0) {
  229. // If we are past the end of the buffer, then the buffer
  230. // contents are garbage, i.e. there were not DYNAMIC_KEYMAP_MACRO_COUNT
  231. // nulls in the buffer.
  232. if (p == end) {
  233. return;
  234. }
  235. if (eeprom_read_byte(p) == 0) {
  236. --id;
  237. }
  238. ++p;
  239. }
  240. // Send the macro string one or three chars at a time
  241. // by making temporary 1 or 3 char strings
  242. char data[4] = {0, 0, 0, 0};
  243. // We already checked there was a null at the end of
  244. // the buffer, so this cannot go past the end
  245. while (1) {
  246. data[0] = eeprom_read_byte(p++);
  247. data[1] = 0;
  248. // Stop at the null terminator of this macro string
  249. if (data[0] == 0) {
  250. break;
  251. }
  252. // If the char is magic (tap, down, up),
  253. // add the next char (key to use) and send a 3 char string.
  254. if (data[0] == SS_TAP_CODE || data[0] == SS_DOWN_CODE || data[0] == SS_UP_CODE) {
  255. data[1] = data[0];
  256. data[0] = SS_QMK_PREFIX;
  257. data[2] = eeprom_read_byte(p++);
  258. if (data[2] == 0) {
  259. break;
  260. }
  261. }
  262. send_string(data);
  263. }
  264. }