eeconfig.h 7.8 KB

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