dynamic_keymap.c 13 KB

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