eeconfig.h 7.7 KB

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