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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

keymap_in_eeprom.c 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. Copyright 2013,2014 Kai Ryu <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/pgmspace.h>
  15. #include <avr/eeprom.h>
  16. #include "eeconfig.h"
  17. #include "keymap_in_eeprom.h"
  18. #include "matrix.h"
  19. #include "debug.h"
  20. #ifdef KEYMAP_IN_EEPROM_ENABLE
  21. void keymap_in_eeprom_init(void) {
  22. if (!check_keymap_in_eeprom()) {
  23. write_keymap_to_eeprom();
  24. }
  25. }
  26. void keymap_in_eeprom_disable(void) {
  27. eeprom_write_word(EECONFIG_KEYMAP_CHECKSUM, eeprom_read_word(EECONFIG_KEYMAP_CHECKSUM) + 1);
  28. }
  29. bool check_keymap_in_eeprom(void) {
  30. uint16_t checksum_in_eeprom = eeprom_read_word(EECONFIG_KEYMAP_CHECKSUM);
  31. uint16_t checksum = EECONFIG_MAGIC_NUMBER;
  32. for (uint16_t i = 0; i < KEYMAP_SIZE; i++) {
  33. uint8_t byte = eeprom_read_byte((uint8_t *)EECONFIG_KEYMAP_FN_ACTIONS + i);
  34. uint16_t word = (i & 1) ? byte << 8 : byte;
  35. checksum += word;
  36. }
  37. #ifdef DEBUG
  38. eeprom_write_word(EECONFIG_KEYMAP_DEBUG, checksum);
  39. #endif
  40. return (checksum_in_eeprom == checksum);
  41. }
  42. void write_keymap_to_eeprom(void) {
  43. uint16_t checksum = EECONFIG_MAGIC_NUMBER;
  44. const uint16_t *fn_actions = fn_actions_pointer();
  45. const uint8_t *keymaps = keymaps_pointer();
  46. // write fn_actions
  47. if (fn_actions != NULL) {
  48. uint16_t fn_actions_count_in_flash = fn_actions_count();
  49. for (uint16_t i = 0; i < FN_ACTIONS_COUNT; i++) {
  50. uint16_t fn_action = 0;
  51. if (i < fn_actions_count_in_flash) {
  52. fn_action = pgm_read_word(fn_actions + i);
  53. }
  54. eeconfig_write_keymap_fn_action(i, fn_action);
  55. checksum += fn_action;
  56. }
  57. }
  58. // write keymaps
  59. if (keymaps != NULL) {
  60. uint16_t keys_count_in_flash = keys_count();
  61. for (uint16_t i = 0; i < KEYS_COUNT; i++) {
  62. uint8_t keymap = 0;
  63. if (i < keys_count_in_flash) {
  64. keymap = pgm_read_byte(keymaps + i);
  65. }
  66. eeconfig_write_keymap_key_by_index(i, keymap);
  67. uint16_t keymap_word = keymap;
  68. if (i & 1) {
  69. keymap_word = keymap << 8;
  70. }
  71. checksum += keymap_word;
  72. }
  73. }
  74. // write checksum
  75. eeprom_write_word(EECONFIG_KEYMAP_CHECKSUM, checksum);
  76. }
  77. uint16_t eeconfig_read_keymap_fn_action(uint8_t index) {
  78. return eeprom_read_word(EECONFIG_KEYMAP_FN_ACTIONS + index);
  79. }
  80. void eeconfig_write_keymap_fn_action(uint8_t index, uint16_t fn_action) {
  81. return eeprom_write_word(EECONFIG_KEYMAP_FN_ACTIONS + index, fn_action);
  82. }
  83. uint8_t eeconfig_read_keymap_key(uint8_t layer, uint8_t row, uint8_t col) {
  84. return eeprom_read_byte(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col));
  85. }
  86. void eeconfig_write_keymap_key(uint8_t layer, uint8_t row, uint8_t col, uint8_t key) {
  87. return eeprom_write_byte(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col), key);
  88. }
  89. uint8_t eeconfig_read_keymap_key_by_index(uint16_t index) {
  90. return eeprom_read_byte(EECONFIG_KEYMAP_KEYMAPS + index);
  91. }
  92. void eeconfig_write_keymap_key_by_index(uint16_t index, uint8_t key) {
  93. return eeprom_write_byte(EECONFIG_KEYMAP_KEYMAPS + index, key);
  94. }
  95. #endif