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.

194 lines
7.8 KiB

  1. /*
  2. Copyright 2013 Jun Wako <wakojun@gmail.com>
  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. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "eeprom.h"
  18. #ifndef EECONFIG_MAGIC_NUMBER
  19. # define EECONFIG_MAGIC_NUMBER (uint16_t)0xFEE6 // When changing, decrement this value to avoid future re-init issues
  20. #endif
  21. #define EECONFIG_MAGIC_NUMBER_OFF (uint16_t)0xFFFF
  22. /* EEPROM parameter address */
  23. #define EECONFIG_MAGIC (uint16_t *)0
  24. #define EECONFIG_DEBUG (uint8_t *)2
  25. #define EECONFIG_DEFAULT_LAYER (uint8_t *)3
  26. #define EECONFIG_KEYMAP (uint16_t *)4
  27. #define EECONFIG_BACKLIGHT (uint8_t *)6
  28. #define EECONFIG_AUDIO (uint8_t *)7
  29. #define EECONFIG_RGBLIGHT (uint32_t *)8
  30. #define EECONFIG_UNICODEMODE (uint8_t *)12
  31. #define EECONFIG_STENOMODE (uint8_t *)13
  32. // EEHANDS for two handed boards
  33. #define EECONFIG_HANDEDNESS (uint8_t *)14
  34. #define EECONFIG_KEYBOARD (uint32_t *)15
  35. #define EECONFIG_USER (uint32_t *)19
  36. #define EECONFIG_UNUSED (uint8_t *)23
  37. // Mutually exclusive
  38. #define EECONFIG_LED_MATRIX (uint32_t *)24
  39. #define EECONFIG_RGB_MATRIX (uint64_t *)24
  40. #define EECONFIG_HAPTIC (uint32_t *)32
  41. #define EECONFIG_RGBLIGHT_EXTENDED (uint8_t *)36
  42. // Size of EEPROM being used for core data storage
  43. #define EECONFIG_BASE_SIZE 37
  44. // Size of EEPROM dedicated to keyboard- and user-specific data
  45. #ifndef EECONFIG_KB_DATA_SIZE
  46. # define EECONFIG_KB_DATA_SIZE 0
  47. #endif
  48. #ifndef EECONFIG_KB_DATA_VERSION
  49. # define EECONFIG_KB_DATA_VERSION (EECONFIG_KB_DATA_SIZE)
  50. #endif
  51. #ifndef EECONFIG_USER_DATA_SIZE
  52. # define EECONFIG_USER_DATA_SIZE 0
  53. #endif
  54. #ifndef EECONFIG_USER_DATA_VERSION
  55. # define EECONFIG_USER_DATA_VERSION (EECONFIG_USER_DATA_SIZE)
  56. #endif
  57. #define EECONFIG_KB_DATABLOCK ((uint8_t *)(EECONFIG_BASE_SIZE))
  58. #define EECONFIG_USER_DATABLOCK ((uint8_t *)((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE)))
  59. // Size of EEPROM being used, other code can refer to this for available EEPROM
  60. #define EECONFIG_SIZE ((EECONFIG_BASE_SIZE) + (EECONFIG_KB_DATA_SIZE) + (EECONFIG_USER_DATA_SIZE))
  61. /* debug bit */
  62. #define EECONFIG_DEBUG_ENABLE (1 << 0)
  63. #define EECONFIG_DEBUG_MATRIX (1 << 1)
  64. #define EECONFIG_DEBUG_KEYBOARD (1 << 2)
  65. #define EECONFIG_DEBUG_MOUSE (1 << 3)
  66. /* keyconf bit */
  67. #define EECONFIG_KEYMAP_SWAP_CONTROL_CAPSLOCK (1 << 0)
  68. #define EECONFIG_KEYMAP_CAPSLOCK_TO_CONTROL (1 << 1)
  69. #define EECONFIG_KEYMAP_SWAP_LALT_LGUI (1 << 2)
  70. #define EECONFIG_KEYMAP_SWAP_RALT_RGUI (1 << 3)
  71. #define EECONFIG_KEYMAP_NO_GUI (1 << 4)
  72. #define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1 << 5)
  73. #define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1 << 6)
  74. #define EECONFIG_KEYMAP_NKRO (1 << 7)
  75. bool eeconfig_is_enabled(void);
  76. bool eeconfig_is_disabled(void);
  77. void eeconfig_init(void);
  78. void eeconfig_init_quantum(void);
  79. void eeconfig_init_kb(void);
  80. void eeconfig_init_user(void);
  81. void eeconfig_enable(void);
  82. void eeconfig_disable(void);
  83. uint8_t eeconfig_read_debug(void);
  84. void eeconfig_update_debug(uint8_t val);
  85. uint8_t eeconfig_read_default_layer(void);
  86. void eeconfig_update_default_layer(uint8_t val);
  87. uint16_t eeconfig_read_keymap(void);
  88. void eeconfig_update_keymap(uint16_t val);
  89. #ifdef AUDIO_ENABLE
  90. uint8_t eeconfig_read_audio(void);
  91. void eeconfig_update_audio(uint8_t val);
  92. #endif
  93. #if (EECONFIG_KB_DATA_SIZE) == 0
  94. uint32_t eeconfig_read_kb(void);
  95. void eeconfig_update_kb(uint32_t val);
  96. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  97. #if (EECONFIG_USER_DATA_SIZE) == 0
  98. uint32_t eeconfig_read_user(void);
  99. void eeconfig_update_user(uint32_t val);
  100. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  101. #ifdef HAPTIC_ENABLE
  102. uint32_t eeconfig_read_haptic(void);
  103. void eeconfig_update_haptic(uint32_t val);
  104. #endif
  105. bool eeconfig_read_handedness(void);
  106. void eeconfig_update_handedness(bool val);
  107. #if (EECONFIG_KB_DATA_SIZE) > 0
  108. bool eeconfig_is_kb_datablock_valid(void);
  109. void eeconfig_read_kb_datablock(void *data);
  110. void eeconfig_update_kb_datablock(const void *data);
  111. void eeconfig_init_kb_datablock(void);
  112. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  113. #if (EECONFIG_USER_DATA_SIZE) > 0
  114. bool eeconfig_is_user_datablock_valid(void);
  115. void eeconfig_read_user_datablock(void *data);
  116. void eeconfig_update_user_datablock(const void *data);
  117. void eeconfig_init_user_datablock(void);
  118. #endif // (EECONFIG_USER_DATA_SIZE) > 0
  119. // Any "checked" debounce variant used requires implementation of:
  120. // -- bool eeconfig_check_valid_##name(void)
  121. // -- void eeconfig_post_flush_##name(void)
  122. #define EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \
  123. static uint8_t dirty_##name = false; \
  124. \
  125. bool eeconfig_check_valid_##name(void); \
  126. void eeconfig_post_flush_##name(void); \
  127. \
  128. static inline void eeconfig_init_##name(void) { \
  129. dirty_##name = true; \
  130. if (eeconfig_check_valid_##name()) { \
  131. eeprom_read_block(&config, offset, sizeof(config)); \
  132. dirty_##name = false; \
  133. } \
  134. } \
  135. static inline void eeconfig_flush_##name(bool force) { \
  136. if (force || dirty_##name) { \
  137. eeprom_update_block(&config, offset, sizeof(config)); \
  138. eeconfig_post_flush_##name(); \
  139. dirty_##name = false; \
  140. } \
  141. } \
  142. static inline void eeconfig_flush_##name##_task(uint16_t timeout) { \
  143. static uint16_t flush_timer = 0; \
  144. if (timer_elapsed(flush_timer) > timeout) { \
  145. eeconfig_flush_##name(false); \
  146. flush_timer = timer_read(); \
  147. } \
  148. } \
  149. static inline void eeconfig_flag_##name(bool v) { \
  150. dirty_##name |= v; \
  151. } \
  152. static inline void eeconfig_write_##name(typeof(config) *conf) { \
  153. if (memcmp(&config, conf, sizeof(config)) != 0) { \
  154. memcpy(&config, conf, sizeof(config)); \
  155. eeconfig_flag_##name(true); \
  156. } \
  157. }
  158. #define EECONFIG_DEBOUNCE_HELPER(name, offset, config) \
  159. EECONFIG_DEBOUNCE_HELPER_CHECKED(name, offset, config) \
  160. \
  161. bool eeconfig_check_valid_##name(void) { \
  162. return true; \
  163. } \
  164. void eeconfig_post_flush_##name(void) {}