eeconfig.h 7.9 KB

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