Added macros to Dynamic Keymaps, Zeal60 RGB backlight improvements (#4520)
[jackhill/qmk/firmware.git] / docs / feature_backlight.md
CommitLineData
9b879b12 1# Backlighting
2
98fa82ce 3Many keyboards support backlit keys by way of individual LEDs placed through or underneath the keyswitches. QMK is able to control the brightness of these LEDs by switching them on and off rapidly in a certain ratio, a technique known as *Pulse Width Modulation*, or PWM. By altering the duty cycle of the PWM signal, it creates the illusion of dimming.
9b879b12 4
98fa82ce 5The MCU can only supply so much current to its GPIO pins. Instead of powering the backlight directly from the MCU, the backlight pin is connected to a transistor or MOSFET that switches the power to the LEDs.
9b879b12 6
9b7a3a0b 7## Usage
98fa82ce 8
9b7a3a0b 9Most keyboards have backlighting enabled by default if they support it, but if it is not working for you, check that your `rules.mk` includes the following:
98fa82ce 10
9b7a3a0b 11```make
12BACKLIGHT_ENABLE = yes
13```
14
15You should then be able to use the keycodes below to change the backlight level.
98fa82ce 16
17## Keycodes
9b879b12 18
4c675a83 19|Key |Description |
20|---------|------------------------------------------|
21|`BL_TOGG`|Turn the backlight on or off |
22|`BL_STEP`|Cycle through backlight levels |
cfcf0fd3 23|`BL_ON` |Set the backlight to max brightness |
24|`BL_OFF` |Turn the backlight off |
25|`BL_INC` |Increase the backlight level |
26|`BL_DEC` |Decrease the backlight level |
27|`BL_BRTG`|Toggle backlight breathing |
4931510a 28
9b7a3a0b 29## Caveats
30
31This feature is distinct from both the [RGB underglow](feature_rgblight.md) and [RGB matrix](feature_rgb_matrix.md) features as it usually allows for only a single colour per switch, though you can obviously use multiple different coloured LEDs on a keyboard.
32
33Hardware PWM is only supported on certain pins of the MCU, so if the backlighting is not connected to one of them, a software implementation will be used, and backlight breathing will not be available. Currently the supported pins are `B5`, `B6`, `B7`, and `C6`.
34
98fa82ce 35## Configuration
4931510a 36
98fa82ce 37To change the behaviour of the backlighting, `#define` these in your `config.h`:
4931510a 38
98fa82ce 39|Define |Default |Description |
40|---------------------|-------------|-------------------------------------------------------------------------------------------------------------|
41|`BACKLIGHT_PIN` |`B7` |The pin that controls the LEDs. Unless you are designing your own keyboard, you shouldn't need to change this|
42|`BACKLIGHT_LEVELS` |`3` |The number of brightness levels (maximum 15 excluding off) |
43|`BACKLIGHT_BREATHING`|*Not defined*|Enable backlight breathing, if hardware PWM is used |
44|`BREATHING_PERIOD` |`6` |The length of one backlight "breath" in seconds |
4931510a 45
98fa82ce 46## Hardware PWM Implementation
4931510a 47
98fa82ce 48When using the supported pins for backlighting, QMK will use a hardware timer configured to output a PWM signal. This timer will count up to `ICRx` (by default `0xFFFF`) before resetting to 0.
49The desired brightness is calculated and stored in the `OCRxx` register. When the counter reaches this value, the backlight pin will go low, and is pulled high again when the counter resets.
50In this way `OCRxx` essentially controls the duty cycle of the LEDs, and thus the brightness, where `0x0000` is completely off and `0xFFFF` is completely on.
4931510a 51
98fa82ce 52The breathing effect is achieved by registering an interrupt handler for `TIMER1_OVF_vect` that is called whenever the counter resets, roughly 244 times per second.
53In this handler, the value of an incrementing counter is mapped onto a precomputed brightness curve. To turn off breathing, the interrupt handler is simply disabled, and the brightness reset to the level stored in EEPROM.
eb19fb5b
DJ
54
55## Backlight Functions
56
0cda2f43
PT
57|Function |Description |
58|----------|-----------------------------------------------------------|
59|`backlight_toggle()` |Turn the backlight on or off |
60|`backlight_enable()` |Turn the backlight on |
61|`backlight_disable()` |Turn the backlight off |
62|`backlight_step()` |Cycle through backlight levels |
63|`backlight_increase()` |Increase the backlight level |
64|`backlight_decrease()` |Decrease the backlight level |
65|`backlight_level(x)` |Sets the backlight level to specified level |
66|`get_backlight_level()` |Return the current backlight level |
67|`is_backlight_enabled()`|Return whether the backlight is currently on |
eb19fb5b
DJ
68
69### Backlight Breathing Functions
70
71|Function |Description |
72|----------|----------------------------------------------------------|
73|`breathing_toggle()` |Turn the backlight breathing on or off |
74|`breathing_enable()` |Turns on backlight breathing |
75|`breathing_disable()` |Turns off backlight breathing |