Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow keyboards/keymaps to execute code at each main loop iteration #10530

Merged
merged 1 commit into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Comment on lines +308 to +311
Copy link

@Gentoli Gentoli Dec 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The arm_atsam implementation calls keyboard_task(); in its loop, thus the housekeeping calls should be covered by that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch; if you can raise a PR for it, it would be wonderful.

}

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