eeconfig.h 8.8 KB

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