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 3.8KB

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