dynamic_keymap.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "config.h"
  17. #include "keymap.h" // to get keymaps[][][]
  18. #include "tmk_core/common/eeprom.h"
  19. #include "progmem.h" // to read default from flash
  20. #include "quantum.h" // for send_string()
  21. #include "dynamic_keymap.h"
  22. #include "via.h" // for default VIA_EEPROM_ADDR_END
  23. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  24. # define DYNAMIC_KEYMAP_LAYER_COUNT 4
  25. #endif
  26. #ifndef DYNAMIC_KEYMAP_MACRO_COUNT
  27. # define DYNAMIC_KEYMAP_MACRO_COUNT 16
  28. #endif
  29. // If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
  30. // default it start after VIA_EEPROM_CUSTOM_ADDR+VIA_EEPROM_CUSTOM_SIZE
  31. #ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
  32. # ifdef VIA_EEPROM_CUSTOM_CONFIG_ADDR
  33. # define DYNAMIC_KEYMAP_EEPROM_ADDR (VIA_EEPROM_CUSTOM_CONFIG_ADDR + VIA_EEPROM_CUSTOM_CONFIG_SIZE)
  34. # else
  35. # error DYNAMIC_KEYMAP_EEPROM_ADDR not defined
  36. # endif
  37. #endif
  38. // Dynamic macro starts after dynamic keymaps
  39. #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  40. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
  41. #endif
  42. // Dynamic macro uses up all remaining memory
  43. // Assumes 1K EEPROM on ATMega32U4
  44. // Override for anything different
  45. #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
  46. # define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (1024 - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR)
  47. #endif
  48. uint8_t dynamic_keymap_get_layer_count(void) { return DYNAMIC_KEYMAP_LAYER_COUNT; }
  49. void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
  50. // TODO: optimize this with some left shifts
  51. return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
  52. }
  53. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
  54. 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
  56. uint16_t keycode = eeprom_read_byte(address) << 8;
  57. keycode |= eeprom_read_byte(address + 1);
  58. return keycode;
  59. }
  60. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
  61. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  62. // Big endian, so we can read/write EEPROM directly from host if we want
  63. eeprom_update_byte(address, (uint8_t)(keycode >> 8));
  64. eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
  65. }
  66. void dynamic_keymap_reset(void) {
  67. // Reset the keymaps in EEPROM to what is in flash.
  68. // All keyboards using dynamic keymaps should define a layout
  69. // for the same number of layers as DYNAMIC_KEYMAP_LAYER_COUNT.
  70. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  71. for (int row = 0; row < MATRIX_ROWS; row++) {
  72. for (int column = 0; column < MATRIX_COLS; column++) {
  73. dynamic_keymap_set_keycode(layer, row, column, pgm_read_word(&keymaps[layer][row][column]));
  74. }
  75. }
  76. }
  77. }
  78. void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  79. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  80. void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  81. uint8_t *target = data;
  82. for (uint16_t i = 0; i < size; i++) {
  83. if (offset + i < dynamic_keymap_eeprom_size) {
  84. *target = eeprom_read_byte(source);
  85. } else {
  86. *target = 0x00;
  87. }
  88. source++;
  89. target++;
  90. }
  91. }
  92. void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  93. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  94. void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  95. uint8_t *source = data;
  96. for (uint16_t i = 0; i < size; i++) {
  97. if (offset + i < dynamic_keymap_eeprom_size) {
  98. eeprom_update_byte(target, *source);
  99. }
  100. source++;
  101. target++;
  102. }
  103. }
  104. // This overrides the one in quantum/keymap_common.c
  105. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key) {
  106. if (layer < DYNAMIC_KEYMAP_LAYER_COUNT && key.row < MATRIX_ROWS && key.col < MATRIX_COLS) {
  107. return dynamic_keymap_get_keycode(layer, key.row, key.col);
  108. } else {
  109. return KC_NO;
  110. }
  111. }
  112. uint8_t dynamic_keymap_macro_get_count(void) { return DYNAMIC_KEYMAP_MACRO_COUNT; }
  113. uint16_t dynamic_keymap_macro_get_buffer_size(void) { return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE; }
  114. void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  115. void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  116. uint8_t *target = data;
  117. for (uint16_t i = 0; i < size; i++) {
  118. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  119. *target = eeprom_read_byte(source);
  120. } else {
  121. *target = 0x00;
  122. }
  123. source++;
  124. target++;
  125. }
  126. }
  127. void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  128. void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  129. uint8_t *source = data;
  130. for (uint16_t i = 0; i < size; i++) {
  131. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  132. eeprom_update_byte(target, *source);
  133. }
  134. source++;
  135. target++;
  136. }
  137. }
  138. void dynamic_keymap_macro_reset(void) {
  139. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  140. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  141. while (p != end) {
  142. eeprom_update_byte(p, 0);
  143. ++p;
  144. }
  145. }
  146. void dynamic_keymap_macro_send(uint8_t id) {
  147. if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
  148. return;
  149. }
  150. // Check the last byte of the buffer.
  151. // If it's not zero, then we are in the middle
  152. // of buffer writing, possibly an aborted buffer
  153. // write. So do nothing.
  154. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE - 1);
  155. if (eeprom_read_byte(p) != 0) {
  156. return;
  157. }
  158. // Skip N null characters
  159. // p will then point to the Nth macro
  160. p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  161. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  162. while (id > 0) {
  163. // If we are past the end of the buffer, then the buffer
  164. // contents are garbage, i.e. there were not DYNAMIC_KEYMAP_MACRO_COUNT
  165. // nulls in the buffer.
  166. if (p == end) {
  167. return;
  168. }
  169. if (eeprom_read_byte(p) == 0) {
  170. --id;
  171. }
  172. ++p;
  173. }
  174. // Send the macro string one or two chars at a time
  175. // by making temporary 1 or 2 char strings
  176. char data[3] = {0, 0, 0};
  177. // We already checked there was a null at the end of
  178. // the buffer, so this cannot go past the end
  179. while (1) {
  180. data[0] = eeprom_read_byte(p++);
  181. data[1] = 0;
  182. // Stop at the null terminator of this macro string
  183. if (data[0] == 0) {
  184. break;
  185. }
  186. // If the char is magic (tap, down, up),
  187. // add the next char (key to use) and send a 2 char string.
  188. if (data[0] == SS_TAP_CODE || data[0] == SS_DOWN_CODE || data[0] == SS_UP_CODE) {
  189. data[1] = eeprom_read_byte(p++);
  190. if (data[1] == 0) {
  191. break;
  192. }
  193. }
  194. send_string(data);
  195. }
  196. }