20b787fc12
8da1898 Merge remote-tracking branch 'flabbergast/chibios' f0fdd8a ChibiOS: update instructions.md. 24dd0b5 ChibiOS: update mk to match chibios/master. cdee266 Merge branch 'develop' into chibios 221663c Fix signatre of layer state change hooks 5a364cb Merge branch 'hotfix-chibios' into develop af664cd Merge branch 'newapi' into develop 04feeaa Clean hook codes 0ca106d Fix code and table in hook.txt 714d418 Add hook document from njbair's work b715129 Chibios: add default location for chibios-contrib. fb3a323 Merge branch 'master' into newapi 8b7fbbd Merge branch 'master' into develop e49851a Chibios: add default location for chibios-contrib. git-subtree-dir: tmk_core git-subtree-split: 8da189851ba83b9bb46a4290cd76b7b7fef6134e
112 lines
3.6 KiB
Plaintext
112 lines
3.6 KiB
Plaintext
Hooks
|
|
-----
|
|
Hooks allow you to execute custom code at certain predefined points in the firmware execution. To use them, just define the hook function in your keymap file.
|
|
|
|
The following hooks are available available:
|
|
|
|
Hook function | Timing
|
|
--------------------------------|-----------------------------------------------
|
|
`hook_early_init(void)` | Early in the boot process, before the matrix is initialized and before a connection is made with the host. Thus, this hook has access to very few parameters, but it is a good place to define any custom parameters needed by other early processes.
|
|
`hook_late_init(void)` | Near the end of the boot process, after Boot Magic has run and LEDs have been initialized.
|
|
`hook_bootmagic(void)` | During the Boot Magic window, after EEPROM and Bootloader checks are made, but before any other built-in Boot Magic checks are made.
|
|
`hook_usb_wakeup(void)` | When the device wakes up from USB suspend state.
|
|
`hook_usb_suspend_entry(void)` | When the device enters USB suspend state.
|
|
`hook_usb_suspend_loop(void)` | Continuously, while the device is in USB suspend state. *Default action:* power down and periodically check the matrix, causing wakeup if needed.
|
|
`hook_keyboard_loop(void)` | Continuously, during the main loop, after the matrix is checked.
|
|
`hook_matrix_change(keyevent_t event)` | When a matrix state change is detected, before any other actions are processed.
|
|
`hook_layer_change(uint32_t layer_state)` | When any layer is changed.
|
|
`hook_default_layer_change(uint32_t default_layer_state)` | When any default layer is changed.
|
|
`hook_keyboard_leds_change(uint8_t led_status)` | Whenever a change in the LED status is performed. *Default action:* call `keyboard_set_leds(led_status)`
|
|
|
|
|
|
|
|
|
|
|
|
### Hooks Examples
|
|
|
|
You can try these out by copying the code to your keymap file, or any .c file in the Makefile `SRC`.
|
|
|
|
#### Activate keymap layer 5 on startup
|
|
|
|
```C
|
|
#include "action_layer.h"
|
|
|
|
void hook_late_init(void)
|
|
{
|
|
layer_on(5);
|
|
print("Layer 5 enabled!");
|
|
}
|
|
```
|
|
|
|
#### Blink the Caps Lock LED every .5 seconds
|
|
|
|
```C
|
|
#include "timer.h"
|
|
#include "led.h"
|
|
|
|
bool my_led_status = 0;
|
|
uint16_t my_led_timer;
|
|
|
|
void hook_keyboard_loop(void)
|
|
{
|
|
// check if we've reached 500 milliseconds yet...
|
|
if (timer_elapsed(my_led_timer) > 500)
|
|
{
|
|
// we've reached 500 milliseconds!
|
|
// reset the timer
|
|
my_led_timer = timer_read();
|
|
|
|
// check the current LED state
|
|
if (my_led_status)
|
|
{
|
|
// LED is on, so let's turn it off
|
|
led_set(host_keyboard_leds() & ~(1<<USB_LED_CAPS_LOCK));
|
|
my_led_status = 0;
|
|
}
|
|
else
|
|
{
|
|
// LED is off, so let's turn it on
|
|
led_set(host_keyboard_leds() | (1<<USB_LED_CAPS_LOCK));
|
|
my_led_status = 1;
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
#### Flash the Caps Lock LED for 20ms on every keypress
|
|
```C
|
|
include "timer.h"
|
|
#include "led.h"
|
|
|
|
bool my_led_status = 0;
|
|
uint16_t my_led_timer;
|
|
|
|
void hook_matrix_change(keyevent_t event)
|
|
{
|
|
// only flash LED for key press events, not key release events.
|
|
if (event.pressed)
|
|
{
|
|
// check the current LED status and reverse it
|
|
led_set(host_keyboard_leds() ^ (1<<USB_LED_CAPS_LOCK));
|
|
|
|
my_led_status = 1;
|
|
my_led_timer = timer_read();
|
|
}
|
|
}
|
|
|
|
void hook_keyboard_loop(void)
|
|
{
|
|
if (my_led_status)
|
|
{
|
|
// check if we've reached 20 milliseconds yet...
|
|
if (timer_elapsed(my_led_timer) > 50)
|
|
{
|
|
led_set(host_keyboard_leds());
|
|
|
|
my_led_status = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
```
|