You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
13 KiB

Zeal60/Zeal65/M60-A implementation (#3879) * Initial version of zeal60 * WIP * Fixes issue #900 * Adding RGB underglow functionality. Fixed a compile-time conflict caused by enabling RGB underglow functionality. * Refactor RPC protocol * Fix last merge * README for RGB underglow updated. * Additional README changes. * Adding RGBW strip software-based current-limiting functionality. * RGBW current-limiting functionality should be handled by RGBSTRIP_MAX_CURRENT_PER_LIGHT instead. * Updated README to reflect implementation of built-in current limiting. * Keymap readability improvements. * Minor keymap improvements. * Fixed LED driver init sequence, formatting * Dimming implementation tested, working. * Stab LEDs synced with spacebar hits in effects. * RGB underglow tested and functional. Simplified README for RGB underglow. * Undid accidental file deletion from previous merge conflict. Safer values for RGB underglow. * Improved arrow key positions in keymap. * Added functionality to correct uneven RGB underglow. Refactored related code. * Reverted to safer values for underglow. * Changes for v0.3 * Custom LED brightness scaling will take place after current adjustment in order to avoid being overridden. * Create keymap.c Added split backspace and split shift to ISO layout * Create config.h Turned on LEDs for new layout * Fixed bug where left spacebar stabilizer LED (LC06) would adopt color of row above. * Added hhkb_wilba keymap * Update keymap.c * Update keymap.c * Update keymap.c * Added indicators, full param setting via host * Added "mousekey" layout * Added Zeal65 support, factory test mode * Keycode safe range changed, caused bugs * Bumped EEPROM version due to change in QMK keycodes * Disable HHKB "blocked" LEDs if KC_NO in keymap * Added "disable_hhkb_blocker_leds" * Required overridden function for keymaps in EEPROM * Added polar coordinate mapping, effect speed * Force Raw HID interface number to 1 always * Fixed last merge from master * Added effect speed to default keymaps * add BACKLIGHT_ prefix to vars * add BACKLIGHT_ prefix to vars * Keymap speed effect; keymap improvements/fixes Readme updated to match changes * Refactored to use common IS31FL3731/I2C drivers * Fixed make rules, backlight disabled feature * Make split rightshift default for Zeal65 * Added M60-A as a "version" of Zeal60. * Renamed IS31FL3731 driver functions * Fix suspend_wakeup_init_kb() being defined twice * First pass refactor dynamic keymaps * Updated to changed I2C and ISSI drivers * Refactor zeal_color.* usage to quantum/color.* * Updated Zeal65, fixed dynamic_keymap * Major refactoring of Zeal60 backlight and API * Lots of little cleanups * Added readme.md * Added readme.md * Added LAYOUT_60*() macros, refactored and cleaned up default keymaps * Fix compile error in suspend.c * Added Zeal65 LAYOUT macros, info.json * Added rama/m60_a, deleted zeal60/keymaps/m60_a * Fixed rama/m60_a/keymaps/proto * Fixed compilation error for suspend.c * Requested changes for PR * Fixed readme.md images * Another readme.md fix * Added drashna's requested changes
5 years ago
Zeal60/Zeal65/M60-A implementation (#3879) * Initial version of zeal60 * WIP * Fixes issue #900 * Adding RGB underglow functionality. Fixed a compile-time conflict caused by enabling RGB underglow functionality. * Refactor RPC protocol * Fix last merge * README for RGB underglow updated. * Additional README changes. * Adding RGBW strip software-based current-limiting functionality. * RGBW current-limiting functionality should be handled by RGBSTRIP_MAX_CURRENT_PER_LIGHT instead. * Updated README to reflect implementation of built-in current limiting. * Keymap readability improvements. * Minor keymap improvements. * Fixed LED driver init sequence, formatting * Dimming implementation tested, working. * Stab LEDs synced with spacebar hits in effects. * RGB underglow tested and functional. Simplified README for RGB underglow. * Undid accidental file deletion from previous merge conflict. Safer values for RGB underglow. * Improved arrow key positions in keymap. * Added functionality to correct uneven RGB underglow. Refactored related code. * Reverted to safer values for underglow. * Changes for v0.3 * Custom LED brightness scaling will take place after current adjustment in order to avoid being overridden. * Create keymap.c Added split backspace and split shift to ISO layout * Create config.h Turned on LEDs for new layout * Fixed bug where left spacebar stabilizer LED (LC06) would adopt color of row above. * Added hhkb_wilba keymap * Update keymap.c * Update keymap.c * Update keymap.c * Added indicators, full param setting via host * Added "mousekey" layout * Added Zeal65 support, factory test mode * Keycode safe range changed, caused bugs * Bumped EEPROM version due to change in QMK keycodes * Disable HHKB "blocked" LEDs if KC_NO in keymap * Added "disable_hhkb_blocker_leds" * Required overridden function for keymaps in EEPROM * Added polar coordinate mapping, effect speed * Force Raw HID interface number to 1 always * Fixed last merge from master * Added effect speed to default keymaps * add BACKLIGHT_ prefix to vars * add BACKLIGHT_ prefix to vars * Keymap speed effect; keymap improvements/fixes Readme updated to match changes * Refactored to use common IS31FL3731/I2C drivers * Fixed make rules, backlight disabled feature * Make split rightshift default for Zeal65 * Added M60-A as a "version" of Zeal60. * Renamed IS31FL3731 driver functions * Fix suspend_wakeup_init_kb() being defined twice * First pass refactor dynamic keymaps * Updated to changed I2C and ISSI drivers * Refactor zeal_color.* usage to quantum/color.* * Updated Zeal65, fixed dynamic_keymap * Major refactoring of Zeal60 backlight and API * Lots of little cleanups * Added readme.md * Added readme.md * Added LAYOUT_60*() macros, refactored and cleaned up default keymaps * Fix compile error in suspend.c * Added Zeal65 LAYOUT macros, info.json * Added rama/m60_a, deleted zeal60/keymaps/m60_a * Fixed rama/m60_a/keymaps/proto * Fixed compilation error for suspend.c * Requested changes for PR * Fixed readme.md images * Another readme.md fix * Added drashna's requested changes
5 years ago
Zeal60/Zeal65/M60-A implementation (#3879) * Initial version of zeal60 * WIP * Fixes issue #900 * Adding RGB underglow functionality. Fixed a compile-time conflict caused by enabling RGB underglow functionality. * Refactor RPC protocol * Fix last merge * README for RGB underglow updated. * Additional README changes. * Adding RGBW strip software-based current-limiting functionality. * RGBW current-limiting functionality should be handled by RGBSTRIP_MAX_CURRENT_PER_LIGHT instead. * Updated README to reflect implementation of built-in current limiting. * Keymap readability improvements. * Minor keymap improvements. * Fixed LED driver init sequence, formatting * Dimming implementation tested, working. * Stab LEDs synced with spacebar hits in effects. * RGB underglow tested and functional. Simplified README for RGB underglow. * Undid accidental file deletion from previous merge conflict. Safer values for RGB underglow. * Improved arrow key positions in keymap. * Added functionality to correct uneven RGB underglow. Refactored related code. * Reverted to safer values for underglow. * Changes for v0.3 * Custom LED brightness scaling will take place after current adjustment in order to avoid being overridden. * Create keymap.c Added split backspace and split shift to ISO layout * Create config.h Turned on LEDs for new layout * Fixed bug where left spacebar stabilizer LED (LC06) would adopt color of row above. * Added hhkb_wilba keymap * Update keymap.c * Update keymap.c * Update keymap.c * Added indicators, full param setting via host * Added "mousekey" layout * Added Zeal65 support, factory test mode * Keycode safe range changed, caused bugs * Bumped EEPROM version due to change in QMK keycodes * Disable HHKB "blocked" LEDs if KC_NO in keymap * Added "disable_hhkb_blocker_leds" * Required overridden function for keymaps in EEPROM * Added polar coordinate mapping, effect speed * Force Raw HID interface number to 1 always * Fixed last merge from master * Added effect speed to default keymaps * add BACKLIGHT_ prefix to vars * add BACKLIGHT_ prefix to vars * Keymap speed effect; keymap improvements/fixes Readme updated to match changes * Refactored to use common IS31FL3731/I2C drivers * Fixed make rules, backlight disabled feature * Make split rightshift default for Zeal65 * Added M60-A as a "version" of Zeal60. * Renamed IS31FL3731 driver functions * Fix suspend_wakeup_init_kb() being defined twice * First pass refactor dynamic keymaps * Updated to changed I2C and ISSI drivers * Refactor zeal_color.* usage to quantum/color.* * Updated Zeal65, fixed dynamic_keymap * Major refactoring of Zeal60 backlight and API * Lots of little cleanups * Added readme.md * Added readme.md * Added LAYOUT_60*() macros, refactored and cleaned up default keymaps * Fix compile error in suspend.c * Added Zeal65 LAYOUT macros, info.json * Added rama/m60_a, deleted zeal60/keymaps/m60_a * Fixed rama/m60_a/keymaps/proto * Fixed compilation error for suspend.c * Requested changes for PR * Fixed readme.md images * Another readme.md fix * Added drashna's requested changes
5 years ago
Zeal60/Zeal65/M60-A implementation (#3879) * Initial version of zeal60 * WIP * Fixes issue #900 * Adding RGB underglow functionality. Fixed a compile-time conflict caused by enabling RGB underglow functionality. * Refactor RPC protocol * Fix last merge * README for RGB underglow updated. * Additional README changes. * Adding RGBW strip software-based current-limiting functionality. * RGBW current-limiting functionality should be handled by RGBSTRIP_MAX_CURRENT_PER_LIGHT instead. * Updated README to reflect implementation of built-in current limiting. * Keymap readability improvements. * Minor keymap improvements. * Fixed LED driver init sequence, formatting * Dimming implementation tested, working. * Stab LEDs synced with spacebar hits in effects. * RGB underglow tested and functional. Simplified README for RGB underglow. * Undid accidental file deletion from previous merge conflict. Safer values for RGB underglow. * Improved arrow key positions in keymap. * Added functionality to correct uneven RGB underglow. Refactored related code. * Reverted to safer values for underglow. * Changes for v0.3 * Custom LED brightness scaling will take place after current adjustment in order to avoid being overridden. * Create keymap.c Added split backspace and split shift to ISO layout * Create config.h Turned on LEDs for new layout * Fixed bug where left spacebar stabilizer LED (LC06) would adopt color of row above. * Added hhkb_wilba keymap * Update keymap.c * Update keymap.c * Update keymap.c * Added indicators, full param setting via host * Added "mousekey" layout * Added Zeal65 support, factory test mode * Keycode safe range changed, caused bugs * Bumped EEPROM version due to change in QMK keycodes * Disable HHKB "blocked" LEDs if KC_NO in keymap * Added "disable_hhkb_blocker_leds" * Required overridden function for keymaps in EEPROM * Added polar coordinate mapping, effect speed * Force Raw HID interface number to 1 always * Fixed last merge from master * Added effect speed to default keymaps * add BACKLIGHT_ prefix to vars * add BACKLIGHT_ prefix to vars * Keymap speed effect; keymap improvements/fixes Readme updated to match changes * Refactored to use common IS31FL3731/I2C drivers * Fixed make rules, backlight disabled feature * Make split rightshift default for Zeal65 * Added M60-A as a "version" of Zeal60. * Renamed IS31FL3731 driver functions * Fix suspend_wakeup_init_kb() being defined twice * First pass refactor dynamic keymaps * Updated to changed I2C and ISSI drivers * Refactor zeal_color.* usage to quantum/color.* * Updated Zeal65, fixed dynamic_keymap * Major refactoring of Zeal60 backlight and API * Lots of little cleanups * Added readme.md * Added readme.md * Added LAYOUT_60*() macros, refactored and cleaned up default keymaps * Fix compile error in suspend.c * Added Zeal65 LAYOUT macros, info.json * Added rama/m60_a, deleted zeal60/keymaps/m60_a * Fixed rama/m60_a/keymaps/proto * Fixed compilation error for suspend.c * Requested changes for PR * Fixed readme.md images * Another readme.md fix * Added drashna's requested changes
5 years ago
Zeal60/Zeal65/M60-A implementation (#3879) * Initial version of zeal60 * WIP * Fixes issue #900 * Adding RGB underglow functionality. Fixed a compile-time conflict caused by enabling RGB underglow functionality. * Refactor RPC protocol * Fix last merge * README for RGB underglow updated. * Additional README changes. * Adding RGBW strip software-based current-limiting functionality. * RGBW current-limiting functionality should be handled by RGBSTRIP_MAX_CURRENT_PER_LIGHT instead. * Updated README to reflect implementation of built-in current limiting. * Keymap readability improvements. * Minor keymap improvements. * Fixed LED driver init sequence, formatting * Dimming implementation tested, working. * Stab LEDs synced with spacebar hits in effects. * RGB underglow tested and functional. Simplified README for RGB underglow. * Undid accidental file deletion from previous merge conflict. Safer values for RGB underglow. * Improved arrow key positions in keymap. * Added functionality to correct uneven RGB underglow. Refactored related code. * Reverted to safer values for underglow. * Changes for v0.3 * Custom LED brightness scaling will take place after current adjustment in order to avoid being overridden. * Create keymap.c Added split backspace and split shift to ISO layout * Create config.h Turned on LEDs for new layout * Fixed bug where left spacebar stabilizer LED (LC06) would adopt color of row above. * Added hhkb_wilba keymap * Update keymap.c * Update keymap.c * Update keymap.c * Added indicators, full param setting via host * Added "mousekey" layout * Added Zeal65 support, factory test mode * Keycode safe range changed, caused bugs * Bumped EEPROM version due to change in QMK keycodes * Disable HHKB "blocked" LEDs if KC_NO in keymap * Added "disable_hhkb_blocker_leds" * Required overridden function for keymaps in EEPROM * Added polar coordinate mapping, effect speed * Force Raw HID interface number to 1 always * Fixed last merge from master * Added effect speed to default keymaps * add BACKLIGHT_ prefix to vars * add BACKLIGHT_ prefix to vars * Keymap speed effect; keymap improvements/fixes Readme updated to match changes * Refactored to use common IS31FL3731/I2C drivers * Fixed make rules, backlight disabled feature * Make split rightshift default for Zeal65 * Added M60-A as a "version" of Zeal60. * Renamed IS31FL3731 driver functions * Fix suspend_wakeup_init_kb() being defined twice * First pass refactor dynamic keymaps * Updated to changed I2C and ISSI drivers * Refactor zeal_color.* usage to quantum/color.* * Updated Zeal65, fixed dynamic_keymap * Major refactoring of Zeal60 backlight and API * Lots of little cleanups * Added readme.md * Added readme.md * Added LAYOUT_60*() macros, refactored and cleaned up default keymaps * Fix compile error in suspend.c * Added Zeal65 LAYOUT macros, info.json * Added rama/m60_a, deleted zeal60/keymaps/m60_a * Fixed rama/m60_a/keymaps/proto * Fixed compilation error for suspend.c * Requested changes for PR * Fixed readme.md images * Another readme.md fix * Added drashna's requested changes
5 years ago
  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 "dynamic_keymap.h"
  17. #include "keymap_introspection.h"
  18. #include "action.h"
  19. #include "eeprom.h"
  20. #include "progmem.h"
  21. #include "send_string.h"
  22. #include "keycodes.h"
  23. #ifdef VIA_ENABLE
  24. # include "via.h"
  25. # define DYNAMIC_KEYMAP_EEPROM_START (VIA_EEPROM_CONFIG_END)
  26. #else
  27. # include "eeconfig.h"
  28. # define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE)
  29. #endif
  30. #ifdef ENCODER_ENABLE
  31. # include "encoder.h"
  32. #else
  33. # define NUM_ENCODERS 0
  34. #endif
  35. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  36. # define DYNAMIC_KEYMAP_LAYER_COUNT 4
  37. #endif
  38. #ifndef DYNAMIC_KEYMAP_MACRO_COUNT
  39. # define DYNAMIC_KEYMAP_MACRO_COUNT 16
  40. #endif
  41. #ifndef TOTAL_EEPROM_BYTE_COUNT
  42. # error Unknown total EEPROM size. Cannot derive maximum for dynamic keymaps.
  43. #endif
  44. #ifndef DYNAMIC_KEYMAP_EEPROM_MAX_ADDR
  45. # define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR (TOTAL_EEPROM_BYTE_COUNT - 1)
  46. #endif
  47. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > (TOTAL_EEPROM_BYTE_COUNT - 1)
  48. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > " STR((TOTAL_EEPROM_BYTE_COUNT - 1))
  49. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is configured to use more space than what is available for the selected EEPROM driver
  50. #endif
  51. // Due to usage of uint16_t check for max 65535
  52. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > 65535
  53. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > 65535"
  54. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR must be less than 65536
  55. #endif
  56. // If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
  57. #ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
  58. # define DYNAMIC_KEYMAP_EEPROM_ADDR DYNAMIC_KEYMAP_EEPROM_START
  59. #endif
  60. // Dynamic encoders starts after dynamic keymaps
  61. #ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR
  62. # define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
  63. #endif
  64. // Dynamic macro starts after dynamic encoders, but only when using ENCODER_MAP
  65. #ifdef ENCODER_MAP_ENABLE
  66. # ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  67. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2))
  68. # endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  69. #else // ENCODER_MAP_ENABLE
  70. # ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  71. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR)
  72. # endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  73. #endif // ENCODER_MAP_ENABLE
  74. // Sanity check that dynamic keymaps fit in available EEPROM
  75. // If there's not 100 bytes available for macros, then something is wrong.
  76. // The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it,
  77. // or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has
  78. // more than the default.
  79. _Static_assert((DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) - (DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) >= 100, "Dynamic keymaps are configured to use more EEPROM than is available.");
  80. // Dynamic macros are stored after the keymaps and use what is available
  81. // up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR.
  82. #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
  83. # define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + 1)
  84. #endif
  85. #ifndef DYNAMIC_KEYMAP_MACRO_DELAY
  86. # define DYNAMIC_KEYMAP_MACRO_DELAY TAP_CODE_DELAY
  87. #endif
  88. uint8_t dynamic_keymap_get_layer_count(void) {
  89. return DYNAMIC_KEYMAP_LAYER_COUNT;
  90. }
  91. void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
  92. // TODO: optimize this with some left shifts
  93. return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
  94. }
  95. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
  96. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO;
  97. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  98. // Big endian, so we can read/write EEPROM directly from host if we want
  99. uint16_t keycode = eeprom_read_byte(address) << 8;
  100. keycode |= eeprom_read_byte(address + 1);
  101. return keycode;
  102. }
  103. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
  104. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return;
  105. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  106. // Big endian, so we can read/write EEPROM directly from host if we want
  107. eeprom_update_byte(address, (uint8_t)(keycode >> 8));
  108. eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
  109. }
  110. #ifdef ENCODER_MAP_ENABLE
  111. void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) {
  112. return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2);
  113. }
  114. uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
  115. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO;
  116. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  117. // Big endian, so we can read/write EEPROM directly from host if we want
  118. uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8;
  119. keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1);
  120. return keycode;
  121. }
  122. void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
  123. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return;
  124. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  125. // Big endian, so we can read/write EEPROM directly from host if we want
  126. eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8));
  127. eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF));
  128. }
  129. #endif // ENCODER_MAP_ENABLE
  130. void dynamic_keymap_reset(void) {
  131. // Reset the keymaps in EEPROM to what is in flash.
  132. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  133. for (int row = 0; row < MATRIX_ROWS; row++) {
  134. for (int column = 0; column < MATRIX_COLS; column++) {
  135. dynamic_keymap_set_keycode(layer, row, column, keycode_at_keymap_location_raw(layer, row, column));
  136. }
  137. }
  138. #ifdef ENCODER_MAP_ENABLE
  139. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  140. dynamic_keymap_set_encoder(layer, encoder, true, keycode_at_encodermap_location_raw(layer, encoder, true));
  141. dynamic_keymap_set_encoder(layer, encoder, false, keycode_at_encodermap_location_raw(layer, encoder, false));
  142. }
  143. #endif // ENCODER_MAP_ENABLE
  144. }
  145. }
  146. void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  147. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  148. void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  149. uint8_t *target = data;
  150. for (uint16_t i = 0; i < size; i++) {
  151. if (offset + i < dynamic_keymap_eeprom_size) {
  152. *target = eeprom_read_byte(source);
  153. } else {
  154. *target = 0x00;
  155. }
  156. source++;
  157. target++;
  158. }
  159. }
  160. void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  161. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  162. void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  163. uint8_t *source = data;
  164. for (uint16_t i = 0; i < size; i++) {
  165. if (offset + i < dynamic_keymap_eeprom_size) {
  166. eeprom_update_byte(target, *source);
  167. }
  168. source++;
  169. target++;
  170. }
  171. }
  172. uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
  173. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && row < MATRIX_ROWS && column < MATRIX_COLS) {
  174. return dynamic_keymap_get_keycode(layer_num, row, column);
  175. }
  176. return KC_NO;
  177. }
  178. #ifdef ENCODER_MAP_ENABLE
  179. uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  180. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && encoder_idx < NUM_ENCODERS) {
  181. return dynamic_keymap_get_encoder(layer_num, encoder_idx, clockwise);
  182. }
  183. return KC_NO;
  184. }
  185. #endif // ENCODER_MAP_ENABLE
  186. uint8_t dynamic_keymap_macro_get_count(void) {
  187. return DYNAMIC_KEYMAP_MACRO_COUNT;
  188. }
  189. uint16_t dynamic_keymap_macro_get_buffer_size(void) {
  190. return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE;
  191. }
  192. void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  193. void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  194. uint8_t *target = data;
  195. for (uint16_t i = 0; i < size; i++) {
  196. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  197. *target = eeprom_read_byte(source);
  198. } else {
  199. *target = 0x00;
  200. }
  201. source++;
  202. target++;
  203. }
  204. }
  205. void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  206. void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  207. uint8_t *source = data;
  208. for (uint16_t i = 0; i < size; i++) {
  209. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  210. eeprom_update_byte(target, *source);
  211. }
  212. source++;
  213. target++;
  214. }
  215. }
  216. void dynamic_keymap_macro_reset(void) {
  217. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  218. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  219. while (p != end) {
  220. eeprom_update_byte(p, 0);
  221. ++p;
  222. }
  223. }
  224. void dynamic_keymap_macro_send(uint8_t id) {
  225. if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
  226. return;
  227. }
  228. // Check the last byte of the buffer.
  229. // If it's not zero, then we are in the middle
  230. // of buffer writing, possibly an aborted buffer
  231. // write. So do nothing.
  232. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE - 1);
  233. if (eeprom_read_byte(p) != 0) {
  234. return;
  235. }
  236. // Skip N null characters
  237. // p will then point to the Nth macro
  238. p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  239. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  240. while (id > 0) {
  241. // If we are past the end of the buffer, then there is
  242. // no Nth macro in the buffer.
  243. if (p == end) {
  244. return;
  245. }
  246. if (eeprom_read_byte(p) == 0) {
  247. --id;
  248. }
  249. ++p;
  250. }
  251. // Send the macro string by making a temporary string.
  252. char data[8] = {0};
  253. // We already checked there was a null at the end of
  254. // the buffer, so this cannot go past the end
  255. while (1) {
  256. data[0] = eeprom_read_byte(p++);
  257. data[1] = 0;
  258. // Stop at the null terminator of this macro string
  259. if (data[0] == 0) {
  260. break;
  261. }
  262. if (data[0] == SS_QMK_PREFIX) {
  263. // Get the code
  264. data[1] = eeprom_read_byte(p++);
  265. // Unexpected null, abort.
  266. if (data[1] == 0) {
  267. return;
  268. }
  269. if (data[1] == SS_TAP_CODE || data[1] == SS_DOWN_CODE || data[1] == SS_UP_CODE) {
  270. // Get the keycode
  271. data[2] = eeprom_read_byte(p++);
  272. // Unexpected null, abort.
  273. if (data[2] == 0) {
  274. return;
  275. }
  276. // Null terminate
  277. data[3] = 0;
  278. } else if (data[1] == SS_DELAY_CODE) {
  279. // Get the number and '|'
  280. // At most this is 4 digits plus '|'
  281. uint8_t i = 2;
  282. while (1) {
  283. data[i] = eeprom_read_byte(p++);
  284. // Unexpected null, abort
  285. if (data[i] == 0) {
  286. return;
  287. }
  288. // Found '|', send it
  289. if (data[i] == '|') {
  290. data[i + 1] = 0;
  291. break;
  292. }
  293. // If haven't found '|' by i==6 then
  294. // number too big, abort
  295. if (i == 6) {
  296. return;
  297. }
  298. ++i;
  299. }
  300. }
  301. }
  302. send_string_with_delay(data, DYNAMIC_KEYMAP_MACRO_DELAY);
  303. }
  304. }