dynamic_keymap.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright 2017 Jason Williams (Wilba)
  2. * Copyright 2024-2025 Nick Brassel (@tzarc)
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "dynamic_keymap.h"
  18. #include "keymap_introspection.h"
  19. #include "action.h"
  20. #include "eeprom.h"
  21. #include "progmem.h"
  22. #include "send_string.h"
  23. #include "keycodes.h"
  24. #include "nvm_dynamic_keymap.h"
  25. #ifdef ENCODER_ENABLE
  26. # include "encoder.h"
  27. #else
  28. # define NUM_ENCODERS 0
  29. #endif
  30. #ifndef DYNAMIC_KEYMAP_MACRO_DELAY
  31. # define DYNAMIC_KEYMAP_MACRO_DELAY TAP_CODE_DELAY
  32. #endif
  33. uint8_t dynamic_keymap_get_layer_count(void) {
  34. return DYNAMIC_KEYMAP_LAYER_COUNT;
  35. }
  36. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
  37. return nvm_dynamic_keymap_read_keycode(layer, row, column);
  38. }
  39. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
  40. nvm_dynamic_keymap_update_keycode(layer, row, column, keycode);
  41. }
  42. #ifdef ENCODER_MAP_ENABLE
  43. uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
  44. return nvm_dynamic_keymap_read_encoder(layer, encoder_id, clockwise);
  45. }
  46. void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
  47. nvm_dynamic_keymap_update_encoder(layer, encoder_id, clockwise, keycode);
  48. }
  49. #endif // ENCODER_MAP_ENABLE
  50. void dynamic_keymap_reset(void) {
  51. // Erase the keymaps, if necessary.
  52. nvm_dynamic_keymap_erase();
  53. // Reset the keymaps in EEPROM to what is in flash.
  54. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  55. for (int row = 0; row < MATRIX_ROWS; row++) {
  56. for (int column = 0; column < MATRIX_COLS; column++) {
  57. dynamic_keymap_set_keycode(layer, row, column, keycode_at_keymap_location_raw(layer, row, column));
  58. }
  59. }
  60. #ifdef ENCODER_MAP_ENABLE
  61. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  62. dynamic_keymap_set_encoder(layer, encoder, true, keycode_at_encodermap_location_raw(layer, encoder, true));
  63. dynamic_keymap_set_encoder(layer, encoder, false, keycode_at_encodermap_location_raw(layer, encoder, false));
  64. }
  65. #endif // ENCODER_MAP_ENABLE
  66. }
  67. }
  68. void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  69. nvm_dynamic_keymap_read_buffer(offset, size, data);
  70. }
  71. void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  72. nvm_dynamic_keymap_update_buffer(offset, size, data);
  73. }
  74. uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
  75. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && row < MATRIX_ROWS && column < MATRIX_COLS) {
  76. return dynamic_keymap_get_keycode(layer_num, row, column);
  77. }
  78. return KC_NO;
  79. }
  80. #ifdef ENCODER_MAP_ENABLE
  81. uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  82. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && encoder_idx < NUM_ENCODERS) {
  83. return dynamic_keymap_get_encoder(layer_num, encoder_idx, clockwise);
  84. }
  85. return KC_NO;
  86. }
  87. #endif // ENCODER_MAP_ENABLE
  88. uint8_t dynamic_keymap_macro_get_count(void) {
  89. return DYNAMIC_KEYMAP_MACRO_COUNT;
  90. }
  91. uint16_t dynamic_keymap_macro_get_buffer_size(void) {
  92. return (uint16_t)nvm_dynamic_keymap_macro_size();
  93. }
  94. void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  95. nvm_dynamic_keymap_macro_read_buffer(offset, size, data);
  96. }
  97. void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  98. nvm_dynamic_keymap_macro_update_buffer(offset, size, data);
  99. }
  100. static uint8_t dynamic_keymap_read_byte(uint32_t offset) {
  101. uint8_t d;
  102. nvm_dynamic_keymap_macro_read_buffer(offset, 1, &d);
  103. return d;
  104. }
  105. typedef struct send_string_nvm_state_t {
  106. uint32_t offset;
  107. } send_string_nvm_state_t;
  108. char send_string_get_next_nvm(void *arg) {
  109. send_string_nvm_state_t *state = (send_string_nvm_state_t *)arg;
  110. char ret = dynamic_keymap_read_byte(state->offset);
  111. state->offset++;
  112. return ret;
  113. }
  114. void dynamic_keymap_macro_reset(void) {
  115. // Erase the macros, if necessary.
  116. nvm_dynamic_keymap_macro_erase();
  117. nvm_dynamic_keymap_macro_reset();
  118. }
  119. void dynamic_keymap_macro_send(uint8_t id) {
  120. if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
  121. return;
  122. }
  123. // Check the last byte of the buffer.
  124. // If it's not zero, then we are in the middle
  125. // of buffer writing, possibly an aborted buffer
  126. // write. So do nothing.
  127. if (dynamic_keymap_read_byte(nvm_dynamic_keymap_macro_size() - 1) != 0) {
  128. return;
  129. }
  130. // Skip N null characters
  131. // p will then point to the Nth macro
  132. uint32_t offset = 0;
  133. uint32_t end = nvm_dynamic_keymap_macro_size();
  134. while (id > 0) {
  135. // If we are past the end of the buffer, then there is
  136. // no Nth macro in the buffer.
  137. if (offset == end) {
  138. return;
  139. }
  140. if (dynamic_keymap_read_byte(offset) == 0) {
  141. --id;
  142. }
  143. ++offset;
  144. }
  145. send_string_nvm_state_t state = {.offset = 0};
  146. send_string_with_delay_impl(send_string_get_next_nvm, &state, DYNAMIC_KEYMAP_MACRO_DELAY);
  147. }