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_ex.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. Copyright 2013 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_ex.h"
  18. #include "debug.h"
  19. #ifdef KEYMAP_EX_ENABLE
  20. void keymap_ex_init(void) {
  21. if (!check_keymap_in_eeprom()) {
  22. write_keymap_to_eeprom();
  23. }
  24. }
  25. void keymap_ex_disable(void) {
  26. eeprom_write_word((void*)EECONFIG_KEYMAP_CHECKSUM, eeprom_read_word((void*)EECONFIG_KEYMAP_CHECKSUM) + 1);
  27. }
  28. bool check_keymap_in_eeprom(void) {
  29. uint16_t checksum_in_eeprom = eeprom_read_word(&((keymap_ex_t*)EECONFIG_KEYMAP_EX)->checksum);
  30. uint16_t checksum = EECONFIG_MAGIC_NUMBER;
  31. for (uint16_t i = 0; i < KEYMAP_SIZE; i += 2) {
  32. checksum += eeprom_read_word((void*)(EECONFIG_KEYMAP_FN_ACTIONS + i));
  33. }
  34. #ifdef DEBUG
  35. eeprom_write_word((void*)(EECONFIG_KEYMAP_DEBUG), checksum);
  36. #endif
  37. return (checksum_in_eeprom == checksum);
  38. }
  39. void write_keymap_to_eeprom(void) {
  40. uint16_t checksum = EECONFIG_MAGIC_NUMBER;
  41. const uint16_t *fn_actions = fn_actions_pointer();
  42. const uint8_t *keymaps = keymaps_pointer();
  43. // write fn_actions
  44. if (fn_actions != NULL) {
  45. uint16_t fn_actions_count_in_flash = fn_actions_count();
  46. for (uint16_t i = 0; i < FN_ACTIONS_COUNT; i++) {
  47. uint16_t fn_action = 0;
  48. if (i < fn_actions_count_in_flash) {
  49. fn_action = pgm_read_word(fn_actions + i);
  50. }
  51. eeconfig_write_keymap_fn_action(i, fn_action);
  52. checksum += fn_action;
  53. }
  54. }
  55. // write keymaps
  56. if (keymaps != NULL) {
  57. uint16_t keys_count_in_flash = keys_count();
  58. for (uint16_t i = 0; i < KEYS_COUNT; i++) {
  59. uint8_t keymap = 0;
  60. if (i < keys_count_in_flash) {
  61. keymap = pgm_read_byte(keymaps + i);
  62. }
  63. eeconfig_write_keymap_key_by_index(i, keymap);
  64. uint16_t keymap_word = keymap;
  65. if (i & 1) {
  66. keymap_word = keymap << 8;
  67. }
  68. checksum += keymap_word;
  69. }
  70. }
  71. // write checksum
  72. eeprom_write_word(&((keymap_ex_t*)EECONFIG_KEYMAP_EX)->checksum, checksum);
  73. }
  74. uint8_t eeconfig_read_keymap_key(uint8_t layer, uint8_t row, uint8_t col) {
  75. //return eeprom_read_byte(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->keymaps[layer][row][col]);
  76. return eeprom_read_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col)));
  77. }
  78. void eeconfig_write_keymap_key(uint8_t layer, uint8_t row, uint8_t col, uint8_t key) {
  79. //return eeprom_write_byte(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->keymaps[layer][row][col], key);
  80. return eeprom_write_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + KEY_OFFSET(layer, row, col)), key);
  81. }
  82. uint8_t eeconfig_read_keymap_key_by_index(uint16_t index) {
  83. //return eeprom_read_byte(((keymap_ex_t*)(EECONFIG_KEYMAP_EX)) + index);
  84. return eeprom_read_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + index));
  85. }
  86. void eeconfig_write_keymap_key_by_index(uint16_t index, uint8_t key) {
  87. //return eeprom_write_byte(((keymap_ex_t*)(EECONFIG_KEYMAP_EX)) + index, key);
  88. return eeprom_write_byte((void*)(EECONFIG_KEYMAP_KEYMAPS + index), key);
  89. }
  90. uint16_t eeconfig_read_keymap_fn_action(uint8_t index) {
  91. //return eeprom_read_word(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->fn_actions[index]);
  92. return eeprom_read_word((void*)(EECONFIG_KEYMAP_FN_ACTIONS + FN_ACTION_OFFSET(index)));
  93. }
  94. void eeconfig_write_keymap_fn_action(uint8_t index, uint16_t fn_action) {
  95. //return eeprom_write_word(&((keymap_ex_t*)(EECONFIG_KEYMAP_EX))->fn_actions[index], fn_action);
  96. return eeprom_write_word((void*)(EECONFIG_KEYMAP_FN_ACTIONS + FN_ACTION_OFFSET(index)), fn_action);
  97. }
  98. #endif