eeconfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include "debug.h"
  5. #include "eeconfig.h"
  6. #include "action_layer.h"
  7. #include "nvm_eeconfig.h"
  8. #include "keycode_config.h"
  9. #ifdef BACKLIGHT_ENABLE
  10. # include "backlight.h"
  11. #endif // BACKLIGHT_ENABLE
  12. #ifdef AUDIO_ENABLE
  13. # include "audio.h"
  14. #endif // AUDIO_ENABLE
  15. #ifdef RGBLIGHT_ENABLE
  16. # include "rgblight.h"
  17. #endif // RGBLIGHT_ENABLE
  18. #ifdef RGB_MATRIX_ENABLE
  19. # include "rgb_matrix_types.h"
  20. #endif // RGB_MATRIX_ENABLE
  21. #ifdef LED_MATRIX_ENABLE
  22. # include "led_matrix_types.h"
  23. #endif // LED_MATRIX_ENABLE
  24. #ifdef UNICODE_COMMON_ENABLE
  25. # include "unicode.h"
  26. #endif // UNICODE_COMMON_ENABLE
  27. #ifdef HAPTIC_ENABLE
  28. # include "haptic.h"
  29. #endif // HAPTIC_ENABLE
  30. #ifdef CONNECTION_ENABLE
  31. # include "connection.h"
  32. #endif // CONNECTION_ENABLE
  33. #ifdef COMMUNITY_MODULES_ENABLE
  34. # include "community_modules.h"
  35. #endif
  36. #ifdef VIA_ENABLE
  37. bool via_eeprom_is_valid(void);
  38. void via_eeprom_set_valid(bool valid);
  39. void eeconfig_init_via(void);
  40. #else
  41. bool dynamic_keymap_is_valid(void);
  42. void dynamic_keymap_reset(void);
  43. void dynamic_keymap_macro_reset(void);
  44. #endif // VIA_ENABLE
  45. #ifndef NKRO_DEFAULT_ON
  46. # define NKRO_DEFAULT_ON false
  47. #endif
  48. __attribute__((weak)) void eeconfig_init_user(void) {
  49. #if (EECONFIG_USER_DATA_SIZE) == 0
  50. // Reset user EEPROM value to blank, rather than to a set value
  51. eeconfig_update_user(0);
  52. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  53. }
  54. __attribute__((weak)) void eeconfig_init_kb(void) {
  55. #if (EECONFIG_KB_DATA_SIZE) == 0
  56. // Reset Keyboard EEPROM value to blank, rather than to a set value
  57. eeconfig_update_kb(0);
  58. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  59. eeconfig_init_user();
  60. }
  61. void eeconfig_init_quantum(void) {
  62. nvm_eeconfig_erase();
  63. eeconfig_enable();
  64. debug_config_t debug_config = {0};
  65. eeconfig_update_debug(&debug_config);
  66. default_layer_state = (layer_state_t)1 << 0;
  67. eeconfig_update_default_layer(default_layer_state);
  68. keymap_config_t keymap_config = {
  69. .swap_control_capslock = false,
  70. .capslock_to_control = false,
  71. .swap_lalt_lgui = false,
  72. .swap_ralt_rgui = false,
  73. .no_gui = false,
  74. .swap_grave_esc = false,
  75. .swap_backslash_backspace = false,
  76. .nkro = NKRO_DEFAULT_ON,
  77. .swap_lctl_lgui = false,
  78. .swap_rctl_rgui = false,
  79. .oneshot_enable = true, // Enable oneshot by default
  80. .swap_escape_capslock = false,
  81. .autocorrect_enable = true, // Enable autocorrect by default
  82. };
  83. eeconfig_update_keymap(&keymap_config);
  84. #ifdef BACKLIGHT_ENABLE
  85. backlight_config_t backlight_config = {0};
  86. eeconfig_update_backlight(&backlight_config);
  87. #endif // BACKLIGHT_ENABLE
  88. #ifdef AUDIO_ENABLE
  89. audio_config_t audio_config = {0};
  90. eeconfig_update_audio(&audio_config);
  91. #endif // AUDIO_ENABLE
  92. #ifdef RGBLIGHT_ENABLE
  93. extern void eeconfig_update_rgblight_default(void);
  94. eeconfig_update_rgblight_default();
  95. #endif // RGBLIGHT_ENABLE
  96. #ifdef UNICODE_COMMON_ENABLE
  97. unicode_config_t unicode_config = {0};
  98. eeconfig_update_unicode_mode(&unicode_config);
  99. #endif // UNICODE_COMMON_ENABLE
  100. #ifdef STENO_ENABLE
  101. eeconfig_update_steno_mode(0);
  102. #endif // STENO_ENABLE
  103. #ifdef RGB_MATRIX_ENABLE
  104. extern void eeconfig_update_rgb_matrix_default(void);
  105. eeconfig_update_rgb_matrix_default();
  106. #endif
  107. #ifdef LED_MATRIX_ENABLE
  108. extern void eeconfig_update_led_matrix_default(void);
  109. eeconfig_update_led_matrix_default();
  110. #endif // LED_MATRIX_ENABLE
  111. #ifdef HAPTIC_ENABLE
  112. haptic_config_t haptic_config = {0};
  113. eeconfig_update_haptic(&haptic_config);
  114. haptic_reset();
  115. #endif // HAPTIC_ENABLE
  116. #ifdef CONNECTION_ENABLE
  117. extern void eeconfig_update_connection_default(void);
  118. eeconfig_update_connection_default();
  119. #endif // CONNECTION_ENABLE
  120. #if (EECONFIG_KB_DATA_SIZE) > 0
  121. eeconfig_init_kb_datablock();
  122. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  123. #if (EECONFIG_USER_DATA_SIZE) > 0
  124. eeconfig_init_user_datablock();
  125. #endif // (EECONFIG_USER_DATA_SIZE) > 0
  126. #ifdef COMMUNITY_MODULES_ENABLE
  127. eeconfig_init_modules_datablock();
  128. #endif // COMMUNITY_MODULES_ENABLE
  129. #if defined(VIA_ENABLE)
  130. // Invalidate VIA eeprom config, and then reset.
  131. // Just in case if power is lost mid init, this makes sure that it gets
  132. // properly re-initialized.
  133. eeconfig_init_via();
  134. #elif defined(DYNAMIC_KEYMAP_ENABLE)
  135. dynamic_keymap_reset();
  136. dynamic_keymap_macro_reset();
  137. #endif
  138. eeconfig_init_kb();
  139. #ifdef RGB_MATRIX_ENABLE
  140. extern void eeconfig_force_flush_rgb_matrix(void);
  141. eeconfig_force_flush_rgb_matrix();
  142. #endif // RGB_MATRIX_ENABLE
  143. #ifdef LED_MATRIX_ENABLE
  144. extern void eeconfig_force_flush_led_matrix(void);
  145. eeconfig_force_flush_led_matrix();
  146. #endif // LED_MATRIX_ENABLE
  147. }
  148. void eeconfig_init(void) {
  149. eeconfig_init_quantum();
  150. }
  151. void eeconfig_enable(void) {
  152. nvm_eeconfig_enable();
  153. }
  154. void eeconfig_disable(void) {
  155. nvm_eeconfig_disable();
  156. }
  157. bool eeconfig_is_enabled(void) {
  158. bool is_eeprom_enabled = nvm_eeconfig_is_enabled();
  159. #if defined(VIA_ENABLE)
  160. if (is_eeprom_enabled) {
  161. is_eeprom_enabled = via_eeprom_is_valid();
  162. }
  163. #elif defined(DYNAMIC_KEYMAP_ENABLE)
  164. if (is_eeprom_enabled) {
  165. is_eeprom_enabled = dynamic_keymap_is_valid();
  166. }
  167. #endif
  168. return is_eeprom_enabled;
  169. }
  170. bool eeconfig_is_disabled(void) {
  171. bool is_eeprom_disabled = nvm_eeconfig_is_disabled();
  172. #if defined(VIA_ENABLE)
  173. if (!is_eeprom_disabled) {
  174. is_eeprom_disabled = !via_eeprom_is_valid();
  175. }
  176. #elif defined(DYNAMIC_KEYMAP_ENABLE)
  177. if (!is_eeprom_disabled) {
  178. is_eeprom_disabled = !dynamic_keymap_is_valid();
  179. }
  180. #endif
  181. return is_eeprom_disabled;
  182. }
  183. void eeconfig_read_debug(debug_config_t *debug_config) {
  184. nvm_eeconfig_read_debug(debug_config);
  185. }
  186. void eeconfig_update_debug(const debug_config_t *debug_config) {
  187. nvm_eeconfig_update_debug(debug_config);
  188. }
  189. layer_state_t eeconfig_read_default_layer(void) {
  190. return nvm_eeconfig_read_default_layer();
  191. }
  192. void eeconfig_update_default_layer(layer_state_t state) {
  193. nvm_eeconfig_update_default_layer(state);
  194. }
  195. void eeconfig_read_keymap(keymap_config_t *keymap_config) {
  196. nvm_eeconfig_read_keymap(keymap_config);
  197. }
  198. void eeconfig_update_keymap(const keymap_config_t *keymap_config) {
  199. nvm_eeconfig_update_keymap(keymap_config);
  200. }
  201. #ifdef AUDIO_ENABLE
  202. void eeconfig_read_audio(audio_config_t *audio_config) {
  203. nvm_eeconfig_read_audio(audio_config);
  204. }
  205. void eeconfig_update_audio(const audio_config_t *audio_config) {
  206. nvm_eeconfig_update_audio(audio_config);
  207. }
  208. #endif // AUDIO_ENABLE
  209. #ifdef UNICODE_COMMON_ENABLE
  210. void eeconfig_read_unicode_mode(unicode_config_t *unicode_config) {
  211. return nvm_eeconfig_read_unicode_mode(unicode_config);
  212. }
  213. void eeconfig_update_unicode_mode(const unicode_config_t *unicode_config) {
  214. nvm_eeconfig_update_unicode_mode(unicode_config);
  215. }
  216. #endif // UNICODE_COMMON_ENABLE
  217. #ifdef BACKLIGHT_ENABLE
  218. void eeconfig_read_backlight(backlight_config_t *backlight_config) {
  219. nvm_eeconfig_read_backlight(backlight_config);
  220. }
  221. void eeconfig_update_backlight(const backlight_config_t *backlight_config) {
  222. nvm_eeconfig_update_backlight(backlight_config);
  223. }
  224. #endif // BACKLIGHT_ENABLE
  225. #ifdef STENO_ENABLE
  226. uint8_t eeconfig_read_steno_mode(void) {
  227. return nvm_eeconfig_read_steno_mode();
  228. }
  229. void eeconfig_update_steno_mode(uint8_t val) {
  230. nvm_eeconfig_update_steno_mode(val);
  231. }
  232. #endif // STENO_ENABLE
  233. #ifdef RGB_MATRIX_ENABLE
  234. void eeconfig_read_rgb_matrix(rgb_config_t *rgb_matrix_config) {
  235. nvm_eeconfig_read_rgb_matrix(rgb_matrix_config);
  236. }
  237. void eeconfig_update_rgb_matrix(const rgb_config_t *rgb_matrix_config) {
  238. nvm_eeconfig_update_rgb_matrix(rgb_matrix_config);
  239. }
  240. #endif // RGB_MATRIX_ENABLE
  241. #ifdef LED_MATRIX_ENABLE
  242. void eeconfig_read_led_matrix(led_eeconfig_t *led_matrix_config) {
  243. nvm_eeconfig_read_led_matrix(led_matrix_config);
  244. }
  245. void eeconfig_update_led_matrix(const led_eeconfig_t *led_matrix_config) {
  246. nvm_eeconfig_update_led_matrix(led_matrix_config);
  247. }
  248. #endif // LED_MATRIX_ENABLE
  249. #ifdef RGBLIGHT_ENABLE
  250. void eeconfig_read_rgblight(rgblight_config_t *rgblight_config) {
  251. nvm_eeconfig_read_rgblight(rgblight_config);
  252. }
  253. void eeconfig_update_rgblight(const rgblight_config_t *rgblight_config) {
  254. nvm_eeconfig_update_rgblight(rgblight_config);
  255. }
  256. #endif // RGBLIGHT_ENABLE
  257. #if (EECONFIG_KB_DATA_SIZE) == 0
  258. uint32_t eeconfig_read_kb(void) {
  259. return nvm_eeconfig_read_kb();
  260. }
  261. void eeconfig_update_kb(uint32_t val) {
  262. nvm_eeconfig_update_kb(val);
  263. }
  264. #endif // (EECONFIG_KB_DATA_SIZE) == 0
  265. #if (EECONFIG_USER_DATA_SIZE) == 0
  266. uint32_t eeconfig_read_user(void) {
  267. return nvm_eeconfig_read_user();
  268. }
  269. void eeconfig_update_user(uint32_t val) {
  270. nvm_eeconfig_update_user(val);
  271. }
  272. #endif // (EECONFIG_USER_DATA_SIZE) == 0
  273. #ifdef HAPTIC_ENABLE
  274. void eeconfig_read_haptic(haptic_config_t *haptic_config) {
  275. nvm_eeconfig_read_haptic(haptic_config);
  276. }
  277. void eeconfig_update_haptic(const haptic_config_t *haptic_config) {
  278. nvm_eeconfig_update_haptic(haptic_config);
  279. }
  280. #endif // HAPTIC_ENABLE
  281. #ifdef CONNECTION_ENABLE
  282. void eeconfig_read_connection(connection_config_t *config) {
  283. nvm_eeconfig_read_connection(config);
  284. }
  285. void eeconfig_update_connection(const connection_config_t *config) {
  286. nvm_eeconfig_update_connection(config);
  287. }
  288. #endif // CONNECTION_ENABLE
  289. bool eeconfig_read_handedness(void) {
  290. return nvm_eeconfig_read_handedness();
  291. }
  292. void eeconfig_update_handedness(bool val) {
  293. nvm_eeconfig_update_handedness(val);
  294. }
  295. #if (EECONFIG_KB_DATA_SIZE) > 0
  296. bool eeconfig_is_kb_datablock_valid(void) {
  297. return nvm_eeconfig_is_kb_datablock_valid();
  298. }
  299. uint32_t eeconfig_read_kb_datablock(void *data, uint32_t offset, uint32_t length) {
  300. return nvm_eeconfig_read_kb_datablock(data, offset, length);
  301. }
  302. uint32_t eeconfig_update_kb_datablock(const void *data, uint32_t offset, uint32_t length) {
  303. return nvm_eeconfig_update_kb_datablock(data, offset, length);
  304. }
  305. __attribute__((weak)) void eeconfig_init_kb_datablock(void) {
  306. nvm_eeconfig_init_kb_datablock();
  307. }
  308. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  309. #if (EECONFIG_USER_DATA_SIZE) > 0
  310. bool eeconfig_is_user_datablock_valid(void) {
  311. return nvm_eeconfig_is_user_datablock_valid();
  312. }
  313. uint32_t eeconfig_read_user_datablock(void *data, uint32_t offset, uint32_t length) {
  314. return nvm_eeconfig_read_user_datablock(data, offset, length);
  315. }
  316. uint32_t eeconfig_update_user_datablock(const void *data, uint32_t offset, uint32_t length) {
  317. return nvm_eeconfig_update_user_datablock(data, offset, length);
  318. }
  319. __attribute__((weak)) void eeconfig_init_user_datablock(void) {
  320. nvm_eeconfig_init_user_datablock();
  321. }
  322. #endif // (EECONFIG_USER_DATA_SIZE) > 0
  323. void eeconfig_prepare_datablocks(void) {
  324. #if (EECONFIG_KB_DATA_SIZE) > 0
  325. if (!eeconfig_is_kb_datablock_valid()) {
  326. eeconfig_init_kb_datablock();
  327. }
  328. #endif // (EECONFIG_KB_DATA_SIZE) > 0
  329. #if (EECONFIG_USER_DATA_SIZE) > 0
  330. if (!eeconfig_is_user_datablock_valid()) {
  331. eeconfig_init_user_datablock();
  332. }
  333. #endif // (EECONFIG_USER_DATA_SIZE) > 0
  334. #ifdef COMMUNITY_MODULES_ENABLE
  335. eeconfig_prepare_modules_datablocks();
  336. #endif // COMMUNITY_MODULES_ENABLE
  337. }