diff options
Diffstat (limited to 'docs/custom_quantum_functions.md')
-rw-r--r-- | docs/custom_quantum_functions.md | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/docs/custom_quantum_functions.md b/docs/custom_quantum_functions.md index 2917fbad26..5d63f3cfb7 100644 --- a/docs/custom_quantum_functions.md +++ b/docs/custom_quantum_functions.md @@ -202,6 +202,62 @@ This function gets called at the end of all QMK processing, before starting the 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. +### Example `void housekeeping_task_user(void)` implementation + +This example will show you how to use `void housekeeping_task_user(void)` to turn off [RGB Light](feature_rgblight.md). For RGB Matrix, the [builtin](https://docs.qmk.fm/#/feature_rgb_matrix?id=additional-configh-options) `RGB_MATRIX_TIMEOUT` should be used. + +First, add the following lines to your keymap's `config.h`: + +```c +#define RGBLIGHT_SLEEP // enable rgblight_suspend() and rgblight_wakeup() in keymap.c +#define RGBLIGHT_TIMEOUT 900000 // ms to wait until rgblight time out, 900K ms is 15min. +``` + +Next, add the following code to your `keymap.c`: + +```c +static uint32_t key_timer; // timer for last keyboard activity, use 32bit value and function to make longer idle time possible +static void refresh_rgb(void); // refreshes the activity timer and RGB, invoke whenever any activity happens +static void check_rgb_timeout(void); // checks if enough time has passed for RGB to timeout +bool is_rgb_timeout = false; // store if RGB has timed out or not in a boolean + +void refresh_rgb(void) { + key_timer = timer_read32(); // store time of last refresh + if (is_rgb_timeout) + { + is_rgb_timeout = false; + rgblight_wakeup(); + } +} +void check_rgb_timeout(void) { + if (!is_rgb_timeout && timer_elapsed32(key_timer) > RGBLIGHT_TIMEOUT) // check if RGB has already timeout and if enough time has passed + { + rgblight_suspend(); + is_rgb_timeout = true; + } +} +/* Then, call the above functions from QMK's built in post processing functions like so */ +/* Runs at the end of each scan loop, check if RGB timeout has occured or not */ +void housekeeping_task_user(void) { +#ifdef RGBLIGHT_TIMEOUT + check_rgb_timeout(); +#endif +} +/* Runs after each key press, check if activity occurred */ +void post_process_record_user(uint16_t keycode, keyrecord_t *record) { +#ifdef RGBLIGHT_TIMEOUT + if (record->event.pressed) + refresh_rgb(); +#endif +} +/* Runs after each encoder tick, check if activity occurred */ +void post_encoder_update_user(uint8_t index, bool clockwise) { +#ifdef RGBLIGHT_TIMEOUT + refresh_rgb(); +#endif +} +``` + # Keyboard Idling/Wake Code 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. @@ -209,7 +265,7 @@ If the board supports it, it can be "idled", by stopping a number of functions. 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. -### Example suspend_power_down_user() and suspend_wakeup_init_user() Implementation +### Example `suspend_power_down_user()` and `suspend_wakeup_init_user()` Implementation ```c |