dynamic_keymap.c 13 KB

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