eeconfig.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "action_layer.h" // layer_state_t
  19. // Size of EEPROM dedicated to keyboard- and user-specific data
  20. #ifndef EECONFIG_KB_DATA_SIZE
  21. # define EECONFIG_KB_DATA_SIZE 0
  22. #endif
  23. #ifndef EECONFIG_KB_DATA_VERSION
  24. # define EECONFIG_KB_DATA_VERSION (EECONFIG_KB_DATA_SIZE)
  25. #endif
  26. #ifndef EECONFIG_USER_DATA_SIZE
  27. # define EECONFIG_USER_DATA_SIZE 0
  28. #endif
  29. #ifndef EECONFIG_USER_DATA_VERSION
  30. # define EECONFIG_USER_DATA_VERSION (EECONFIG_USER_DATA_SIZE)
  31. #endif
  32. /* debug bit */
  33. #define EECONFIG_DEBUG_ENABLE (1 << 0)
  34. #define EECONFIG_DEBUG_MATRIX (1 << 1)
  35. #define EECONFIG_DEBUG_KEYBOARD (1 << 2)
  36. #define EECONFIG_DEBUG_MOUSE (1 << 3)
  37. /* keyconf bit */
  38. #define EECONFIG_KEYMAP_SWAP_CONTROL_CAPSLOCK (1 << 0)
  39. #define EECONFIG_KEYMAP_CAPSLOCK_TO_CONTROL (1 << 1)
  40. #define EECONFIG_KEYMAP_SWAP_LALT_LGUI (1 << 2)
  41. #define EECONFIG_KEYMAP_SWAP_RALT_RGUI (1 << 3)
  42. #define EECONFIG_KEYMAP_NO_GUI (1 << 4)
  43. #define EECONFIG_KEYMAP_SWAP_GRAVE_ESC (1 << 5)
  44. #define EECONFIG_KEYMAP_SWAP_BACKSLASH_BACKSPACE (1 << 6)
  45. #define EECONFIG_KEYMAP_NKRO (1 << 7)
  46. bool eeconfig_is_enabled(void);
  47. bool eeconfig_is_disabled(void);
  48. void eeconfig_init(void);
  49. void eeconfig_init_quantum(void);
  50. void eeconfig_init_kb(void);
  51. void eeconfig_init_user(void);
  52. void eeconfig_enable(void);
  53. void eeconfig_disable(void);
  54. void eeconfig_prepare_datablocks(void);
  55. typedef union debug_config_t debug_config_t;
  56. void eeconfig_read_debug(debug_config_t *debug_config) __attribute__((nonnull));
  57. void eeconfig_update_debug(const debug_config_t *debug_config) __attribute__((nonnull));
  58. layer_state_t eeconfig_read_default_layer(void);
  59. void eeconfig_update_default_layer(layer_state_t state);
  60. typedef union keymap_config_t keymap_config_t;
  61. void eeconfig_read_keymap(keymap_config_t *keymap_config) __attribute__((nonnull));
  62. void eeconfig_update_keymap(const keymap_config_t *keymap_config) __attribute__((nonnull));
  63. #ifdef AUDIO_ENABLE
  64. typedef union audio_config_t audio_config_t;
  65. void eeconfig_read_audio(audio_config_t *audio_config) __attribute__((nonnull));
  66. void eeconfig_update_audio(const audio_config_t *audio_config) __attribute__((nonnull));
  67. #endif // AUDIO_ENABLE
  68. #ifdef UNICODE_COMMON_ENABLE
  69. typedef union unicode_config_t unicode_config_t;
  70. void eeconfig_read_unicode_mode(unicode_config_t *unicode_config) __attribute__((nonnull));
  71. void eeconfig_update_unicode_mode(const unicode_config_t *unicode_config) __attribute__((nonnull));
  72. #endif // UNICODE_COMMON_ENABLE
  73. #ifdef BACKLIGHT_ENABLE
  74. typedef union backlight_config_t backlight_config_t;
  75. void eeconfig_read_backlight(backlight_config_t *backlight_config) __attribute__((nonnull));
  76. void eeconfig_update_backlight(const backlight_config_t *backlight_config) __attribute__((nonnull));
  77. #endif // BACKLIGHT_ENABLE
  78. #ifdef STENO_ENABLE
  79. uint8_t eeconfig_read_steno_mode(void);
  80. void eeconfig_update_steno_mode(uint8_t val);
  81. #endif // STENO_ENABLE
  82. #ifdef RGB_MATRIX_ENABLE
  83. typedef union rgb_config_t rgb_config_t;
  84. void eeconfig_read_rgb_matrix(rgb_config_t *rgb_matrix_config) __attribute__((nonnull));
  85. void eeconfig_update_rgb_matrix(const rgb_config_t *rgb_matrix_config) __attribute__((nonnull));
  86. #endif // RGB_MATRIX_ENABLE
  87. #ifdef LED_MATRIX_ENABLE
  88. typedef union led_eeconfig_t led_eeconfig_t;
  89. void eeconfig_read_led_matrix(led_eeconfig_t *led_matrix_config) __attribute__((nonnull));
  90. void eeconfig_update_led_matrix(const led_eeconfig_t *led_matrix_config) __attribute__((nonnull));
  91. #endif // LED_MATRIX_ENABLE
  92. #ifdef RGBLIGHT_ENABLE
  93. typedef union rgblight_config_t rgblight_config_t;
  94. void eeconfig_read_rgblight(rgblight_config_t *rgblight_config) __attribute__((nonnull));
  95. void eeconfig_update_rgblight(const rgblight_config_t *rgblight_config) __attribute__((nonnull));
  96. #endif // RGBLIGHT_ENABLE
  97. #if (EECONFIG_KB_DATA_SIZE) == 0
  98. uint32_t eeconfig_read_kb(void);
  99. void eeconfig_update_kb(uint32_t val);
  100. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  101. #if (EECONFIG_USER_DATA_SIZE) == 0
  102. uint32_t eeconfig_read_user(void);
  103. void eeconfig_update_user(uint32_t val);
  104. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  105. #ifdef HAPTIC_ENABLE
  106. typedef union haptic_config_t haptic_config_t;
  107. void eeconfig_read_haptic(haptic_config_t *haptic_config) __attribute__((nonnull));
  108. void eeconfig_update_haptic(const haptic_config_t *haptic_config) __attribute__((nonnull));
  109. #endif
  110. #ifdef CONNECTION_ENABLE
  111. typedef union connection_config_t connection_config_t;
  112. void eeconfig_read_connection(connection_config_t *config);
  113. void eeconfig_update_connection(const connection_config_t *config);
  114. #endif
  115. bool eeconfig_read_handedness(void);
  116. void eeconfig_update_handedness(bool val);
  117. #if (EECONFIG_KB_DATA_SIZE) > 0
  118. bool eeconfig_is_kb_datablock_valid(void);
  119. uint32_t eeconfig_read_kb_datablock(void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
  120. uint32_t eeconfig_update_kb_datablock(const void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
  121. void eeconfig_init_kb_datablock(void);
  122. # define eeconfig_read_kb_datablock_field(__object, __field) eeconfig_read_kb_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
  123. # define eeconfig_update_kb_datablock_field(__object, __field) eeconfig_update_kb_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
  124. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  125. #if (EECONFIG_USER_DATA_SIZE) > 0
  126. bool eeconfig_is_user_datablock_valid(void);
  127. uint32_t eeconfig_read_user_datablock(void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
  128. uint32_t eeconfig_update_user_datablock(const void *data, uint32_t offset, uint32_t length) __attribute__((nonnull));
  129. void eeconfig_init_user_datablock(void);
  130. # define eeconfig_read_user_datablock_field(__object, __field) eeconfig_read_user_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
  131. # define eeconfig_update_user_datablock_field(__object, __field) eeconfig_update_user_datablock(&(__object.__field), offsetof(typeof(__object), __field), sizeof(__object.__field))
  132. #endif // (EECONFIG_USER_DATA_SIZE) > 0
  133. // Any "checked" debounce variant used requires implementation of:
  134. // -- bool eeconfig_check_valid_##name(void)
  135. // -- void eeconfig_post_flush_##name(void)
  136. #define EECONFIG_DEBOUNCE_HELPER_CHECKED(name, config) \
  137. static uint8_t dirty_##name = false; \
  138. \
  139. bool eeconfig_check_valid_##name(void); \
  140. void eeconfig_post_flush_##name(void); \
  141. \
  142. static inline void eeconfig_init_##name(void) { \
  143. dirty_##name = true; \
  144. if (eeconfig_check_valid_##name()) { \
  145. eeconfig_read_##name(&config); \
  146. dirty_##name = false; \
  147. } \
  148. } \
  149. static inline void eeconfig_flush_##name(bool force) { \
  150. if (force || dirty_##name) { \
  151. eeconfig_update_##name(&config); \
  152. eeconfig_post_flush_##name(); \
  153. dirty_##name = false; \
  154. } \
  155. } \
  156. static inline void eeconfig_flush_##name##_task(uint16_t timeout) { \
  157. static uint16_t flush_timer = 0; \
  158. if (timer_elapsed(flush_timer) > timeout) { \
  159. eeconfig_flush_##name(false); \
  160. flush_timer = timer_read(); \
  161. } \
  162. } \
  163. static inline void eeconfig_flag_##name(bool v) { \
  164. dirty_##name |= v; \
  165. } \
  166. static inline void eeconfig_write_##name(typeof(config) *conf) { \
  167. if (memcmp(&config, conf, sizeof(config)) != 0) { \
  168. memcpy(&config, conf, sizeof(config)); \
  169. eeconfig_flag_##name(true); \
  170. } \
  171. }
  172. #define EECONFIG_DEBOUNCE_HELPER(name, config) \
  173. EECONFIG_DEBOUNCE_HELPER_CHECKED(name, config) \
  174. \
  175. bool eeconfig_check_valid_##name(void) { \
  176. return true; \
  177. } \
  178. void eeconfig_post_flush_##name(void) {}