import { _ as _export_sfc, c as createElementBlock, o as openBlock, a8 as createStaticVNode } from "./chunks/framework.B9AX-CPi.js"; const __pageData = JSON.parse('{"title":"LED Indicators","description":"","frontmatter":{},"headers":[],"relativePath":"features/led_indicators.md","filePath":"features/led_indicators.md"}'); const _sfc_main = { name: "features/led_indicators.md" }; const _hoisted_1 = /* @__PURE__ */ createStaticVNode('

LED Indicators ​

TIP

LED indicators on split keyboards will require state information synced to the slave half (e.g. #define SPLIT_LED_STATE_ENABLE). See data sync options for more details.

QMK provides methods to read 5 of the LEDs defined in the HID spec:

There are three ways to get the lock LED state:

WARNING

The host_keyboard_led_state() may reflect an updated state before led_update_user() is called.

Deprecated functions that provide the LED state as uint8_t:

Configuration Options ​

To configure the indicators, #define these in your config.h:

DefineDefaultDescription
LED_NUM_LOCK_PINNot definedThe pin that controls the Num Lock LED
LED_CAPS_LOCK_PINNot definedThe pin that controls the Caps Lock LED
LED_SCROLL_LOCK_PINNot definedThe pin that controls the Scroll Lock LED
LED_COMPOSE_PINNot definedThe pin that controls the Compose LED
LED_KANA_PINNot definedThe pin that controls the Kana LED
LED_PIN_ON_STATE1The state of the indicator pins when the LED is "on" - 1 for high, 0 for low

Unless you are designing your own keyboard, you generally should not need to change the above config options.

LED update function ​

When the configuration options do not provide enough flexibility, the following callbacks allow custom control of the LED behavior. These functions will be called when one of those 5 LEDs changes state:

Both receives LED state as a struct parameter. Returning true in led_update_user() will allow the keyboard level code in led_update_kb() to run as well. Returning false will override the keyboard level code, depending on how the keyboard level function is set up.

Example of keyboard LED update implementation ​

This is a template indicator function that can be implemented on keyboard level code:

c
bool led_update_kb(led_t led_state) {\n    bool res = led_update_user(led_state);\n    if(res) {\n        // gpio_write_pin sets the pin high for 1 and low for 0.\n        // In this example the pins are inverted, setting\n        // it low/0 turns it on, and high/1 turns the LED off.\n        // This behavior depends on whether the LED is between the pin\n        // and VCC or the pin and GND.\n        gpio_write_pin(B0, !led_state.num_lock);\n        gpio_write_pin(B1, !led_state.caps_lock);\n        gpio_write_pin(B2, !led_state.scroll_lock);\n        gpio_write_pin(B3, !led_state.compose);\n        gpio_write_pin(B4, !led_state.kana);\n    }\n    return res;\n}

Example of user LED update implementation ​

This is an incomplete example will play a sound if Caps Lock is turned on or off. It returns true to allow keyboard LED function to maintain their state.

c
#ifdef AUDIO_ENABLE\n  float caps_on[][2] = SONG(CAPS_LOCK_ON_SOUND);\n  float caps_off[][2] = SONG(CAPS_LOCK_OFF_SOUND);\n#endif\n\nbool led_update_user(led_t led_state) {\n    #ifdef AUDIO_ENABLE\n    static uint8_t caps_state = 0;\n    if (caps_state != led_state.caps_lock) {\n        led_state.caps_lock ? PLAY_SONG(caps_on) : PLAY_SONG(caps_off);\n        caps_state = led_state.caps_lock;\n    }\n    #endif\n    return true;\n}

Host keyboard LED state ​

The host_keyboard_led_state() function will report the LED state returned from the host computer as led_t. This is useful for reading the LED state outside led_update_*. For example, you can get the boolean state of Caps Lock from the host with:

c
bool caps = host_keyboard_led_state().caps_lock;

led_update_ports() ​

This function writes the LED state to the actual hardware. Call it manually from your led_update_*() callbacks to modify the handling of the standard keyboard LEDs. For example when repurposing a standard LED indicator as layer indicator.

Setting Physical LED State ​

Some keyboard implementations provide convenient methods for setting the state of the physical LEDs.

Ergodox Boards ​

The Ergodox implementations provide ergodox_right_led_1/2/3_on/off() to turn individual LEDs on or off, as well as ergodox_right_led_on/off(uint8_t led) to turn them on or off by their index.

In addition, it is possible to specify the brightness level of all LEDs with ergodox_led_all_set(uint8_t n); of individual LEDs with ergodox_right_led_1/2/3_set(uint8_t n); or by index with ergodox_right_led_set(uint8_t led, uint8_t n).

Ergodox boards also define LED_BRIGHTNESS_LO for the lowest brightness and LED_BRIGHTNESS_HI for the highest brightness (which is the default).

', 34); const _hoisted_35 = [ _hoisted_1 ]; function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) { return openBlock(), createElementBlock("div", null, _hoisted_35); } const led_indicators = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]); export { __pageData, led_indicators as default };