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.

399 lines
18 KiB

6 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
  1. # How to Customize Your Keyboard's Behavior
  2. For a lot of people a custom keyboard is about more than sending button presses to your computer. You want to be able to do things that are more complex than simple button presses and macros. QMK has hooks that allow you to inject code, override functionality, and otherwise customize how your keyboard behaves in different situations.
  3. This page does not assume any special knowledge about QMK, but reading [Understanding QMK](understanding_qmk.md) will help you understand what is going on at a more fundamental level.
  4. ## A Word on Core vs Keyboards vs Keymap :id=a-word-on-core-vs-keyboards-vs-keymap
  5. We have structured QMK as a hierarchy:
  6. * Core (`_quantum`)
  7. * Keyboard/Revision (`_kb`)
  8. * Keymap (`_user`)
  9. Each of the functions described below can be defined with a `_kb()` suffix or a `_user()` suffix. We intend for you to use the `_kb()` suffix at the Keyboard/Revision level, while the `_user()` suffix should be used at the Keymap level.
  10. When defining functions at the Keyboard/Revision level it is important that your `_kb()` implementation call `_user()` before executing anything else- otherwise the keymap level function will never be called.
  11. # Custom Keycodes
  12. By far the most common task is to change the behavior of an existing keycode or to create a new keycode. From a code standpoint the mechanism for each is very similar.
  13. ## Defining a New Keycode
  14. The first step to creating your own custom keycode(s) is to enumerate them. This means both naming them and assigning a unique number to that keycode. Rather than limit custom keycodes to a fixed range of numbers QMK provides the `SAFE_RANGE` macro. You can use `SAFE_RANGE` when enumerating your custom keycodes to guarantee that you get a unique number.
  15. Here is an example of enumerating 2 keycodes. After adding this block to your `keymap.c` you will be able to use `FOO` and `BAR` inside your keymap.
  16. ```c
  17. enum my_keycodes {
  18. FOO = SAFE_RANGE,
  19. BAR
  20. };
  21. ```
  22. ## Programming the Behavior of Any Keycode :id=programming-the-behavior-of-any-keycode
  23. When you want to override the behavior of an existing key, or define the behavior for a new key, you should use the `process_record_kb()` and `process_record_user()` functions. These are called by QMK during key processing before the actual key event is handled. If these functions return `true` QMK will process the keycodes as usual. That can be handy for extending the functionality of a key rather than replacing it. If these functions return `false` QMK will skip the normal key handling, and it will be up to you to send any key up or down events that are required.
  24. These function are called every time a key is pressed or released.
  25. ### Example `process_record_user()` Implementation
  26. This example does two things. It defines the behavior for a custom keycode called `FOO`, and it supplements our Enter key by playing a tone whenever it is pressed.
  27. ```c
  28. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  29. switch (keycode) {
  30. case FOO:
  31. if (record->event.pressed) {
  32. // Do something when pressed
  33. } else {
  34. // Do something else when release
  35. }
  36. return false; // Skip all further processing of this key
  37. case KC_ENTER:
  38. // Play a tone when enter is pressed
  39. if (record->event.pressed) {
  40. PLAY_SONG(tone_qwerty);
  41. }
  42. return true; // Let QMK send the enter press/release events
  43. default:
  44. return true; // Process all other keycodes normally
  45. }
  46. }
  47. ```
  48. ### `process_record_*` Function Documentation
  49. * Keyboard/Revision: `bool process_record_kb(uint16_t keycode, keyrecord_t *record)`
  50. * Keymap: `bool process_record_user(uint16_t keycode, keyrecord_t *record)`
  51. The `keycode` argument is whatever is defined in your keymap, eg `MO(1)`, `KC_L`, etc. You should use a `switch...case` block to handle these events.
  52. The `record` argument contains information about the actual press:
  53. ```c
  54. keyrecord_t record {
  55. keyevent_t event {
  56. keypos_t key {
  57. uint8_t col
  58. uint8_t row
  59. }
  60. bool pressed
  61. uint16_t time
  62. }
  63. }
  64. ```
  65. # Keyboard Initialization Code
  66. There are several steps in the keyboard initialization process. Depending on what you want to do, it will influence which function you should use.
  67. These are the three main initialization functions, listed in the order that they're called.
  68. * `keyboard_pre_init_*` - Happens before most anything is started. Good for hardware setup that you want running very early.
  69. * `matrix_init_*` - Happens midway through the firmware's startup process. Hardware is initialized, but features may not be yet.
  70. * `keyboard_post_init_*` - Happens at the end of the firmware's startup process. This is where you'd want to put "customization" code, for the most part.
  71. !> For most people, the `keyboard_post_init_user` function is what you want to call. For instance, this is where you want to set up things for RGB Underglow.
  72. ## Keyboard Pre Initialization code
  73. This runs very early during startup, even before the USB has been started.
  74. Shortly after this, the matrix is initialized.
  75. For most users, this shouldn't be used, as it's primarily for hardware oriented initialization.
  76. However, if you have hardware stuff that you need initialized, this is the best place for it (such as initializing LED pins).
  77. ### Example `keyboard_pre_init_user()` Implementation
  78. This example, at the keyboard level, sets up B0, B1, B2, B3, and B4 as LED pins.
  79. ```c
  80. void keyboard_pre_init_user(void) {
  81. // Call the keyboard pre init code.
  82. // Set our LED pins as output
  83. setPinOutput(B0);
  84. setPinOutput(B1);
  85. setPinOutput(B2);
  86. setPinOutput(B3);
  87. setPinOutput(B4);
  88. }
  89. ```
  90. ### `keyboard_pre_init_*` Function Documentation
  91. * Keyboard/Revision: `void keyboard_pre_init_kb(void)`
  92. * Keymap: `void keyboard_pre_init_user(void)`
  93. ## Matrix Initialization Code
  94. This is called when the matrix is initialized, and after some of the hardware has been set up, but before many of the features have been initialized.
  95. This is useful for setting up stuff that you may need elsewhere, but isn't hardware related nor is dependant on where it's started.
  96. ### `matrix_init_*` Function Documentation
  97. * Keyboard/Revision: `void matrix_init_kb(void)`
  98. * Keymap: `void matrix_init_user(void)`
  99. ## Keyboard Post Initialization code
  100. This is ran as the very last task in the keyboard initialization process. This is useful if you want to make changes to certain features, as they should be initialized by this point.
  101. ### Example `keyboard_post_init_user()` Implementation
  102. This example, running after everything else has initialized, sets up the rgb underglow configuration.
  103. ```c
  104. void keyboard_post_init_user(void) {
  105. // Call the post init code.
  106. rgblight_enable_noeeprom(); // enables Rgb, without saving settings
  107. rgblight_sethsv_noeeprom(180, 255, 255); // sets the color to teal/cyan without saving
  108. rgblight_mode_noeeprom(RGBLIGHT_MODE_BREATHING + 3); // sets mode to Fast breathing without saving
  109. }
  110. ```
  111. ### `keyboard_post_init_*` Function Documentation
  112. * Keyboard/Revision: `void keyboard_post_init_kb(void)`
  113. * Keymap: `void keyboard_post_init_user(void)`
  114. # Matrix Scanning Code
  115. Whenever possible you should customize your keyboard by using `process_record_*()` and hooking into events that way, to ensure that your code does not have a negative performance impact on your keyboard. However, in rare cases it is necessary to hook into the matrix scanning. Be extremely careful with the performance of code in these functions, as it will be called at least 10 times per second.
  116. ### Example `matrix_scan_*` Implementation
  117. This example has been deliberately omitted. You should understand enough about QMK internals to write this without an example before hooking into such a performance sensitive area. If you need help please [open an issue](https://github.com/qmk/qmk_firmware/issues/new) or [chat with us on Discord](https://discord.gg/Uq7gcHh).
  118. ### `matrix_scan_*` Function Documentation
  119. * Keyboard/Revision: `void matrix_scan_kb(void)`
  120. * Keymap: `void matrix_scan_user(void)`
  121. This function gets called at every matrix scan, which is basically as often as the MCU can handle. Be careful what you put here, as it will get run a lot.
  122. You should use this function if you need custom matrix scanning code. It can also be used for custom status output (such as LEDs or a display) or other functionality that you want to trigger regularly even when the user isn't typing.
  123. # Keyboard housekeeping
  124. * Keyboard/Revision: `void housekeeping_task_kb(void)`
  125. * Keymap: `void housekeeping_task_user(void)`
  126. This function gets called at the end of all QMK processing, before starting the next iteration. You can safely assume that QMK has dealt with the last matrix scan at the time that these functions are invoked -- layer states have been updated, USB reports have been sent, LEDs have been updated, and displays have been drawn.
  127. Similar to `matrix_scan_*`, these are called as often as the MCU can handle. To keep your board responsive, it's suggested to do as little as possible during these function calls, potentially throtting their behaviour if you do indeed require implementing something special.
  128. # Keyboard Idling/Wake Code
  129. If the board supports it, it can be "idled", by stopping a number of functions. A good example of this is RGB lights or backlights. This can save on power consumption, or may be better behavior for your keyboard.
  130. This is controlled by two functions: `suspend_power_down_*` and `suspend_wakeup_init_*`, which are called when the system board is idled and when it wakes up, respectively.
  131. ### Example suspend_power_down_user() and suspend_wakeup_init_user() Implementation
  132. ```c
  133. void suspend_power_down_user(void) {
  134. rgb_matrix_set_suspend_state(true);
  135. }
  136. void suspend_wakeup_init_user(void) {
  137. rgb_matrix_set_suspend_state(false);
  138. }
  139. ```
  140. ### Keyboard suspend/wake Function Documentation
  141. * Keyboard/Revision: `void suspend_power_down_kb(void)` and `void suspend_wakeup_init_user(void)`
  142. * Keymap: `void suspend_power_down_kb(void)` and `void suspend_wakeup_init_user(void)`
  143. # Layer Change Code :id=layer-change-code
  144. This runs code every time that the layers get changed. This can be useful for layer indication, or custom layer handling.
  145. ### Example `layer_state_set_*` Implementation
  146. This example shows how to set the [RGB Underglow](feature_rgblight.md) lights based on the layer, using the Planck as an example.
  147. ```c
  148. layer_state_t layer_state_set_user(layer_state_t state) {
  149. switch (get_highest_layer(state)) {
  150. case _RAISE:
  151. rgblight_setrgb (0x00, 0x00, 0xFF);
  152. break;
  153. case _LOWER:
  154. rgblight_setrgb (0xFF, 0x00, 0x00);
  155. break;
  156. case _PLOVER:
  157. rgblight_setrgb (0x00, 0xFF, 0x00);
  158. break;
  159. case _ADJUST:
  160. rgblight_setrgb (0x7A, 0x00, 0xFF);
  161. break;
  162. default: // for any other layers, or the default layer
  163. rgblight_setrgb (0x00, 0xFF, 0xFF);
  164. break;
  165. }
  166. return state;
  167. }
  168. ```
  169. Use the `IS_LAYER_ON_STATE(state, layer)` and `IS_LAYER_OFF_STATE(state, layer)` macros to check the status of a particular layer.
  170. Outside of `layer_state_set_*` functions, you can use the `IS_LAYER_ON(layer)` and `IS_LAYER_OFF(layer)` macros to check global layer state.
  171. ### `layer_state_set_*` Function Documentation
  172. * Keyboard/Revision: `layer_state_t layer_state_set_kb(layer_state_t state)`
  173. * Keymap: `layer_state_t layer_state_set_user(layer_state_t state)`
  174. The `state` is the bitmask of the active layers, as explained in the [Keymap Overview](keymap.md#keymap-layer-status)
  175. # Persistent Configuration (EEPROM)
  176. 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.
  177. 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.
  178. 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.
  179. * If you don't understand the example, then you may want to avoid using this feature, as it is rather complicated.
  180. ### Example Implementation
  181. 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!
  182. In your keymap.c file, add this to the top:
  183. ```c
  184. typedef union {
  185. uint32_t raw;
  186. struct {
  187. bool rgb_layer_change :1;
  188. };
  189. } user_config_t;
  190. user_config_t user_config;
  191. ```
  192. 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.
  193. 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.
  194. 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:
  195. ```c
  196. void keyboard_post_init_user(void) {
  197. // Call the keymap level matrix init.
  198. // Read the user config from EEPROM
  199. user_config.raw = eeconfig_read_user();
  200. // Set default layer, if enabled
  201. if (user_config.rgb_layer_change) {
  202. rgblight_enable_noeeprom();
  203. rgblight_sethsv_noeeprom_cyan();
  204. rgblight_mode_noeeprom(1);
  205. }
  206. }
  207. ```
  208. 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.
  209. ```c
  210. layer_state_t layer_state_set_user(layer_state_t state) {
  211. switch (get_highest_layer(state)) {
  212. case _RAISE:
  213. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom_magenta(); rgblight_mode_noeeprom(1); }
  214. break;
  215. case _LOWER:
  216. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom_red(); rgblight_mode_noeeprom(1); }
  217. break;
  218. case _PLOVER:
  219. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom_green(); rgblight_mode_noeeprom(1); }
  220. break;
  221. case _ADJUST:
  222. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom_white(); rgblight_mode_noeeprom(1); }
  223. break;
  224. default: // for any other layers, or the default layer
  225. if (user_config.rgb_layer_change) { rgblight_sethsv_noeeprom_cyan(); rgblight_mode_noeeprom(1); }
  226. break;
  227. }
  228. return state;
  229. }
  230. ```
  231. 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:
  232. ```c
  233. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  234. switch (keycode) {
  235. case FOO:
  236. if (record->event.pressed) {
  237. // Do something when pressed
  238. } else {
  239. // Do something else when release
  240. }
  241. return false; // Skip all further processing of this key
  242. case KC_ENTER:
  243. // Play a tone when enter is pressed
  244. if (record->event.pressed) {
  245. PLAY_SONG(tone_qwerty);
  246. }
  247. return true; // Let QMK send the enter press/release events
  248. case RGB_LYR: // This allows me to use underglow as layer indication, or as normal
  249. if (record->event.pressed) {
  250. user_config.rgb_layer_change ^= 1; // Toggles the status
  251. eeconfig_update_user(user_config.raw); // Writes the new status to EEPROM
  252. if (user_config.rgb_layer_change) { // if layer state indication is enabled,
  253. layer_state_set(layer_state); // then immediately update the layer color
  254. }
  255. }
  256. return false;
  257. case RGB_MODE_FORWARD ... RGB_MODE_GRADIENT: // For any of the RGB codes (see quantum_keycodes.h, L400 for reference)
  258. if (record->event.pressed) { //This disables layer indication, as it's assumed that if you're changing this ... you want that disabled
  259. if (user_config.rgb_layer_change) { // only if this is enabled
  260. user_config.rgb_layer_change = false; // disable it, and
  261. eeconfig_update_user(user_config.raw); // write the setings to EEPROM
  262. }
  263. }
  264. return true; break;
  265. default:
  266. return true; // Process all other keycodes normally
  267. }
  268. }
  269. ```
  270. 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 `EEP_RST` keycode or [Bootmagic](feature_bootmagic.md) functionallity. For example, if you want to set rgb layer indication by default, and save the default valued.
  271. ```c
  272. void eeconfig_init_user(void) { // EEPROM is getting reset!
  273. user_config.raw = 0;
  274. user_config.rgb_layer_change = true; // We want this enabled by default
  275. eeconfig_update_user(user_config.raw); // Write default value to EEPROM now
  276. // use the non noeeprom versions, to write these values to EEPROM too
  277. rgblight_enable(); // Enable RGB by default
  278. rgblight_sethsv_cyan(); // Set it to CYAN by default
  279. rgblight_mode(1); // set to solid by default
  280. }
  281. ```
  282. 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.
  283. ### 'EECONFIG' Function Documentation
  284. * Keyboard/Revision: `void eeconfig_init_kb(void)`, `uint32_t eeconfig_read_kb(void)` and `void eeconfig_update_kb(uint32_t val)`
  285. * Keymap: `void eeconfig_init_user(void)`, `uint32_t eeconfig_read_user(void)` and `void eeconfig_update_user(uint32_t val)`
  286. 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.