Skip to content

Commit

Permalink
Add housekeeping task callbacks so that keyboards/keymaps are capable…
Browse files Browse the repository at this point in the history
… of executing code for each main loop iteration. (#10530)
  • Loading branch information
tzarc authored Oct 23, 2020
1 parent 801506e commit eee2f77
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/custom_quantum_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,14 @@ This function gets called at every matrix scan, which is basically as often as t

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.

# Keyboard housekeeping

* Keyboard/Revision: `void housekeeping_task_kb(void)`
* Keymap: `void housekeeping_task_user(void)`

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.

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.

# Keyboard Idling/Wake Code

Expand Down
17 changes: 17 additions & 0 deletions tmk_core/common/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ __attribute__((weak)) bool is_keyboard_master(void) { return true; }
*/
__attribute__((weak)) bool should_process_keypress(void) { return is_keyboard_master(); }

/** \brief housekeeping_task_kb
*
* Override this function if you have a need to execute code for every keyboard main loop iteration.
* This is specific to keyboard-level functionality.
*/
__attribute__((weak)) void housekeeping_task_kb(void) {}

/** \brief housekeeping_task_user
*
* Override this function if you have a need to execute code for every keyboard main loop iteration.
* This is specific to user/keymap-level functionality.
*/
__attribute__((weak)) void housekeeping_task_user(void) {}

/** \brief keyboard_init
*
* FIXME: needs doc
Expand Down Expand Up @@ -309,6 +323,9 @@ void keyboard_task(void) {
uint8_t keys_processed = 0;
#endif

housekeeping_task_kb();
housekeeping_task_user();

#if defined(OLED_DRIVER_ENABLE) && !defined(OLED_DISABLE_TIMEOUT)
uint8_t ret = matrix_scan();
#else
Expand Down
3 changes: 3 additions & 0 deletions tmk_core/common/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ void keyboard_pre_init_user(void);
void keyboard_post_init_kb(void);
void keyboard_post_init_user(void);

void housekeeping_task_kb(void);
void housekeeping_task_user(void);

#ifdef __cplusplus
}
#endif
Expand Down
4 changes: 4 additions & 0 deletions tmk_core/protocol/arm_atsam/main_arm_atsam.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ int main(void) {
// dprintf("5v=%u 5vu=%u dlow=%u dhi=%u gca=%u gcd=%u\r\n", v_5v, v_5v_avg, v_5v_avg - V5_LOW, v_5v_avg - V5_HIGH, gcr_actual, gcr_desired);
}
#endif // CONSOLE_ENABLE

// Run housekeeping
housekeeping_task_kb();
housekeeping_task_user();
}

return 1;
Expand Down
4 changes: 4 additions & 0 deletions tmk_core/protocol/chibios/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,5 +265,9 @@ int main(void) {
#ifdef RAW_ENABLE
raw_hid_task();
#endif

// Run housekeeping
housekeeping_task_kb();
housekeeping_task_user();
}
}
4 changes: 4 additions & 0 deletions tmk_core/protocol/lufa/lufa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,10 @@ int main(void) {
#if !defined(INTERRUPT_CONTROL_ENDPOINT)
USB_USBTask();
#endif

// Run housekeeping
housekeeping_task_kb();
housekeeping_task_user();
}
}

Expand Down
4 changes: 4 additions & 0 deletions tmk_core/protocol/vusb/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ int main(void) {
console_task();
}
#endif

// Run housekeeping
housekeeping_task_kb();
housekeeping_task_user();
} else if (suspend_wakeup_condition()) {
usb_remote_wakeup();
}
Expand Down

0 comments on commit eee2f77

Please sign in to comment.