You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
7.2 KiB

  1. # Persistent Configuration (EEPROM)
  2. This allows you to configure persistent settings for your keyboard. These settings are stored in the EEPROM of your controller, and are retained even after power loss. The settings can be read with `eeconfig_read_kb` and `eeconfig_read_user`, and can be written to using `eeconfig_update_kb` and `eeconfig_update_user`. This is useful for features that you want to be able to toggle (like toggling rgb layer indication). Additionally, you can use `eeconfig_init_kb` and `eeconfig_init_user` to set the default values for the EEPROM.
  3. The complicated part here, is that there are a bunch of ways that you can store and access data via EEPROM, and there is no "correct" way to do this. However, you only have a DWORD (4 bytes) for each function.
  4. Keep in mind that EEPROM has a limited number of writes. While this is very high, it's not the only thing writing to the EEPROM, and if you write too often, you can potentially drastically shorten the life of your MCU.
  5. * If you don't understand the example, then you may want to avoid using this feature, as it is rather complicated.
  6. ## Example Implementation
  7. This is an example of how to add settings, and read and write it. We're using the user keymap for the example here. This is a complex function, and has a lot going on. In fact, it uses a lot of the above functions to work!
  8. In your keymap.c file, add this to the top:
  9. ```c
  10. typedef union {
  11. uint32_t raw;
  12. struct {
  13. bool rgb_layer_change :1;
  14. };
  15. } user_config_t;
  16. user_config_t user_config;
  17. ```
  18. This sets up a 32 bit structure that we can store settings with in memory, and write to the EEPROM. Using this removes the need to define variables, since they're defined in this structure. Remember that `bool` (boolean) values use 1 bit, `uint8_t` uses 8 bits, `uint16_t` uses up 16 bits. You can mix and match, but changing the order can cause issues, as it will change the values that are read and written.
  19. We're using `rgb_layer_change`, for the `layer_state_set_*` function, and use `keyboard_post_init_user` and `process_record_user` to configure everything.
  20. Now, using the `keyboard_post_init_user` code above, you want to add `eeconfig_read_user()` to it, to populate the structure you've just created. And you can then immediately use this structure to control functionality in your keymap. And It should look like:
  21. ```c
  22. void keyboard_post_init_user(void) {
  23. // Call the keymap level matrix init.
  24. // Read the user config from EEPROM
  25. user_config.raw = eeconfig_read_user();
  26. // Set default layer, if enabled
  27. if (user_config.rgb_layer_change) {
  28. rgblight_enable_noeeprom();
  29. rgblight_sethsv_noeeprom(HSV_CYAN);
  30. rgblight_mode_noeeprom(1);
  31. }
  32. }
  33. ```
  34. The above function will use the EEPROM config immediately after reading it, to set the default layer's RGB color. The "raw" value of it is converted in a usable structure based on the "union" that you created above.
  35. ```c
  36. layer_state_t layer_state_set_user(layer_state_t state) {
  37. switch (get_highest_layer(state)) {
  38. case _RAISE:
  39. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom(HSV_MAGENTA); rgblight_mode_noeeprom(1); }
  40. break;
  41. case _LOWER:
  42. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom(HSV_RED); rgblight_mode_noeeprom(1); }
  43. break;
  44. case _PLOVER:
  45. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom(HSV_GREEN); rgblight_mode_noeeprom(1); }
  46. break;
  47. case _ADJUST:
  48. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom(HSV_WHITE); rgblight_mode_noeeprom(1); }
  49. break;
  50. default: // for any other layers, or the default layer
  51. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom(HSV_CYAN); rgblight_mode_noeeprom(1); }
  52. break;
  53. }
  54. return state;
  55. }
  56. ```
  57. This will cause the RGB underglow to be changed ONLY if the value was enabled. Now to configure this value, create a new keycode for `process_record_user` called `RGB_LYR`. Additionally, we want to make sure that if you use the normal RGB codes, that it turns off Using the example above, make it look this:
  58. ```c
  59. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  60. switch (keycode) {
  61. case FOO:
  62. if (record->event.pressed) {
  63. // Do something when pressed
  64. } else {
  65. // Do something else when release
  66. }
  67. return false; // Skip all further processing of this key
  68. case KC_ENTER:
  69. // Play a tone when enter is pressed
  70. if (record->event.pressed) {
  71. PLAY_SONG(tone_qwerty);
  72. }
  73. return true; // Let QMK send the enter press/release events
  74. case RGB_LYR: // This allows me to use underglow as layer indication, or as normal
  75. if (record->event.pressed) {
  76. user_config.rgb_layer_change ^= 1; // Toggles the status
  77. eeconfig_update_user(user_config.raw); // Writes the new status to EEPROM
  78. if (user_config.rgb_layer_change) { // if layer state indication is enabled,
  79. layer_state_set(layer_state); // then immediately update the layer color
  80. }
  81. }
  82. return false;
  83. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // For any of the RGB codes (see quantum_keycodes.h, L400 for reference)
  84. if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  85. if (user_config.rgb_layer_change) { // only if this is enabled
  86. user_config.rgb_layer_change = false; // disable it, and
  87. eeconfig_update_user(user_config.raw); // write the setings to EEPROM
  88. }
  89. }
  90. return true; break;
  91. default:
  92. return true; // Process all other keycodes normally
  93. }
  94. }
  95. ```
  96. And lastly, you want to add the `eeconfig_init_user` function, so that when the EEPROM is reset, you can specify default values, and even custom actions. To force an EEPROM reset, use the `EE_CLR` keycode or [Bootmagic Lite](feature_bootmagic.md) functionallity. For example, if you want to set rgb layer indication by default, and save the default valued.
  97. ```c
  98. void eeconfig_init_user(void) { // EEPROM is getting reset!
  99. user_config.raw = 0;
  100. user_config.rgb_layer_change = true; // We want this enabled by default
  101. eeconfig_update_user(user_config.raw); // Write default value to EEPROM now
  102. // use the non noeeprom versions, to write these values to EEPROM too
  103. rgblight_enable(); // Enable RGB by default
  104. rgblight_sethsv(HSV_CYAN); // Set it to CYAN by default
  105. rgblight_mode(1); // set to solid by default
  106. }
  107. ```
  108. And you're done. The RGB layer indication will only work if you want it to. And it will be saved, even after unplugging the board. And if you use any of the RGB codes, it will disable the layer indication, so that it stays on the mode and color that you set it to.
  109. ## 'EECONFIG' Function Documentation
  110. * Keyboard/Revision: `void eeconfig_init_kb(void)`, `uint32_t eeconfig_read_kb(void)` and `void eeconfig_update_kb(uint32_t val)`
  111. * Keymap: `void eeconfig_init_user(void)`, `uint32_t eeconfig_read_user(void)` and `void eeconfig_update_user(uint32_t val)`
  112. The `val` is the value of the data that you want to write to EEPROM. And the `eeconfig_read_*` function return a 32 bit (DWORD) value from the EEPROM.