dynamic_keymap.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "dynamic_keymap.h"
  19. #ifdef DYNAMIC_KEYMAP_ENABLE
  20. #ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
  21. #error DYNAMIC_KEYMAP_EEPROM_ADDR not defined
  22. #endif
  23. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  24. #error DYNAMIC_KEYMAP_LAYER_COUNT not defined
  25. #endif
  26. #define KC_EENULL 0xFFFF // TODO: move to enum quantum_keycodes
  27. void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column)
  28. {
  29. // TODO: optimize this with some left shifts
  30. return ((void*)DYNAMIC_KEYMAP_EEPROM_ADDR) + ( layer * MATRIX_ROWS * MATRIX_COLS * 2 ) +
  31. ( row * MATRIX_COLS * 2 ) + ( column * 2 );
  32. }
  33. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column)
  34. {
  35. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  36. // Big endian, so we can read/write EEPROM directly from host if we want
  37. uint16_t keycode = eeprom_read_byte(address) << 8;
  38. keycode |= eeprom_read_byte(address + 1);
  39. return keycode;
  40. }
  41. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode)
  42. {
  43. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  44. // Big endian, so we can read/write EEPROM directly from host if we want
  45. eeprom_update_byte(address, (uint8_t)(keycode >> 8));
  46. eeprom_update_byte(address+1, (uint8_t)(keycode & 0xFF));
  47. }
  48. void dynamic_keymap_clear_all(void)
  49. {
  50. // Save "empty" keymaps.
  51. for ( int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++ )
  52. {
  53. for ( int row = 0; row < MATRIX_ROWS; row++ )
  54. {
  55. for ( int column = 0; column < MATRIX_COLS; column++ )
  56. {
  57. dynamic_keymap_set_keycode(layer, row, column, KC_EENULL);
  58. }
  59. }
  60. }
  61. }
  62. // This overrides the one in quantum/keymap_common.c
  63. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  64. {
  65. // This used to test EEPROM for magic bytes, but it was redundant.
  66. // Test for EEPROM usage change (fresh install, address change, etc.)
  67. // externally and call dynamic_keymap_default_save()
  68. if ( layer < DYNAMIC_KEYMAP_LAYER_COUNT &&
  69. key.row < MATRIX_ROWS && // possibly redundant
  70. key.col < MATRIX_COLS ) // possibly redundant
  71. {
  72. uint16_t keycode = dynamic_keymap_get_keycode(layer, key.row, key.col);
  73. // If keycode is not "empty", return it, otherwise
  74. // drop down to return the one in flash
  75. if ( keycode != KC_EENULL)
  76. {
  77. return keycode;
  78. }
  79. }
  80. return pgm_read_word(&keymaps[layer][key.row][key.col]);
  81. }
  82. #endif // DYNAMIC_KEYMAP_ENABLE