You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

541 lines
32 KiB

Add function to support split-keyboard in rgblight.[ch]. (#5020) * add temporary file that is rgblight.c call graph * add rgblight_update_hook() * update rgblight-call-graph.dot (temporary file) * add more hook point * add TODO comment * temporary Revert "add TODO comment" This reverts commit df6165aac9b3a31d1d3e31ce52aadc134b84eac2. * temporary Revert "add more hook point" This reverts commit 64592b06f3bcdaac47c59f922018a273bef76776. * temporary Revert "add rgblight_update_hook()" This reverts commit 432b74c912ed4333e6633e20a1bcda10c6a10eaf. * add rgblight_update_hook() * add more hook point * add TODO comment * implement rgblight_update_hook() * remove rgblight_update_hook(), add RGBLIGHT_SPLIT_SET_CHANGE_XXXX rgblight_update_hook() is too large. change to simple flag setting. * shrink rgblight_config_t * implement rgblight_update_sync() Note: The animation synchronization process has not been implemented yet. * update quantum/rgblight-call-graph.dot (temporary file) * rmove quantum/rgblight-call-graph.dot (temporary file) * update rgblight.c * Add temporary code to Helix keyboard 'five_rows' keymap to test rgblight.c . * fix build break rgblight_update_sync() when all animation off * fix quantum/rgblight.c:rgblight_disable_XX() add RGBLIGHT_SPLIT_SET_CHANGE_MODE * quantum/rgblight.c change code order: move rgblight_update_sync() * add mode_base_table[] to quantum/rgblight.c * quantum/rgblight.c use mode_base_table[] and rgblight_status.base_mode * quantum/rgblkght.c animation timer integration * quantum/rgblkght.c add animation sync for split keyboard * fix mode_base_table[] and snake effect * fix build break keyboards/mxss. keyboards/mxss's local rgblight.c need old version rgblight.h * rgblight.c: fix animation sync * quantum/rgblight.c: fix snake effect sync * quantum/rgblight.c: animation sync interverl 30 sec * quantum/rgblight.c: fix rgblight_effect_rainbow_swirl() and rgblight_effect_knight() * quantum/rgblight.c: add macro RGBLIGHT_SPLIT_ANIMATION * cherry-pick from 'rgblight_modes.h sample implementation' * fix RGBLIGHT_SPLIT_ANIMATION check position * Update temporary code in Helix keyboard 'five_rows' keymap to test rgblight.c * Reduce the firmware size by 1500 bytes when rgblight_effect_breathing() is enabled. * Changed to rgblight_sethsv_eeprom_helper() for easier reading. * add fail-safe code to quantum/rgblight.c:rgblight_task(),rgblight_timer_enable() * remove temporary code in Helix keyboard 'five_rows' keymap * quantum/rgblight.c: add split-keyboard master side sync functions add functions: uint8_t rgblight_get_change_flags(void); void rgblight_clear_change_flags(void); void rgblight_get_syncinfo(rgblight_syncinfo_t *syncinfo); change function: void rgblight_update_sync(rgblight_syncinfo_t *syncinfo, bool write_to_eeprom); * Change rgblight_update_sync() to use write_to_eeprom. * remove TODO comment from quantum/rgblight.h * Revert "fix build break keyboards/mxss." This reverts commit 90b9a1aa7d8af226751500e49e3ea0214cc4e024. (Separated this change into the newly opened PR #5461.) * Revert "Reduce the firmware size by 1500 bytes when rgblight_effect_breathing() is enabled." This reverts commit b61004e63e82cf5334cee4def4ba10cffa88885f. * update quantum/rgblight.c: Code size reduction when not using RGBLIGHT_SPLIT. * Add temporary code to Helix keyboard 'five_rows' keymap to test rgblight.c . * add temporary pdhelix(Patched Helix) code * Add temporary code to split_common/transport.c to test rgblight.c. * Finish testing rgblight.c with helix keyboard. Revert "Add temporary code to Helix keyboard 'five_rows' keymap to test rgblight.c ." This reverts commit 0bf81a4723a977adc0cb09b4272ee5c9b4f2bbbb. * Finish testing rgblight.c with quantum/split_common code. Revert "Add temporary code to split_common/transport.c to test rgblight.c." This reverts commit 71db3e24eef40d4c455fb9fd1664e4487c9d927a. * remove temporary pdhelix(Patched Helix) code This reverts commit 5287e51a394741bcb6028c7cfc0dd0c984645f76. * Added description of RGBLIGHT_SPLIT macro to docs/feature_rgblight.md. * add RGBLIGHT_SPLIT_SET_CHANGE_HSVS to rgblight_init() * Changed to restart animation only when changing mode. When changing hue, sat and val, the animation is not restarted and continues.
5 years ago
  1. # RGB Lighting
  2. QMK has the ability to control RGB LEDs attached to your keyboard. This is commonly called *underglow*, due to the LEDs often being mounted on the bottom of the keyboard, producing a nice diffused effect when combined with a translucent case.
  3. ![Planck with RGB Underglow](https://raw.githubusercontent.com/qmk/qmk_firmware/3774a7fcdab5544fc787f4c200be05fcd417e31f/keyboards/planck/keymaps/yang/planck-with-rgb-underglow.jpg)
  4. Some keyboards come with RGB LEDs preinstalled. Others must have them installed after the fact. See the [Hardware Modification](#hardware-modification) section for information on adding RGB lighting to your keyboard.
  5. Currently QMK supports the following addressable LEDs (however, the white LED in RGBW variants is not supported):
  6. * WS2811, WS2812, WS2812B, WS2812C, etc.
  7. * SK6812, SK6812MINI, SK6805
  8. * APA102
  9. These LEDs are called "addressable" because instead of using a wire per color, each LED contains a small microchip that understands a special protocol sent over a single wire. The chip passes on the remaining data to the next LED, allowing them to be chained together. In this way, you can easily control the color of the individual LEDs.
  10. ## Usage
  11. On keyboards with onboard RGB LEDs, it is usually enabled by default. If it is not working for you, check that your `rules.mk` includes the following:
  12. ```make
  13. RGBLIGHT_ENABLE = yes
  14. ```
  15. For APA102 LEDs, add the following to your `rules.mk`:
  16. ```make
  17. RGBLIGHT_ENABLE = yes
  18. RGBLIGHT_DRIVER = APA102
  19. ```
  20. At minimum you must define the data pin your LED strip is connected to, and the number of LEDs in the strip, in your `config.h`. For APA102 LEDs, you must also define the clock pin. If your keyboard has onboard RGB LEDs, and you are simply creating a keymap, you usually won't need to modify these.
  21. |Define |Description |
  22. |---------------|---------------------------------------------------------------------------------------------------------|
  23. |`RGB_DI_PIN` |The pin connected to the data pin of the LEDs |
  24. |`RGB_CI_PIN` |The pin connected to the clock pin of the LEDs (APA102 only) |
  25. |`RGBLED_NUM` |The number of LEDs connected |
  26. |`RGBLED_SPLIT` |(Optional) For split keyboards, the number of LEDs connected on each half directly wired to `RGB_DI_PIN` |
  27. Then you should be able to use the keycodes below to change the RGB lighting to your liking.
  28. ### Color Selection
  29. QMK uses [Hue, Saturation, and Value](https://en.wikipedia.org/wiki/HSL_and_HSV) to select colors rather than RGB. The color wheel below demonstrates how this works.
  30. <img src="gitbook/images/color-wheel.svg" alt="HSV Color Wheel" width="250"/>
  31. Changing the **Hue** cycles around the circle.<br>
  32. Changing the **Saturation** moves between the inner and outer sections of the wheel, affecting the intensity of the color.<br>
  33. Changing the **Value** sets the overall brightness.<br>
  34. ![QMK Color Wheel with HSV Values](https://i.imgur.com/vkYVo66.jpg)
  35. ## Keycodes
  36. |Key |Aliases |Description |
  37. |-------------------|----------|--------------------------------------------------------------------|
  38. |`RGB_TOG` | |Toggle RGB lighting on or off |
  39. |`RGB_MODE_FORWARD` |`RGB_MOD` |Cycle through modes, reverse direction when Shift is held |
  40. |`RGB_MODE_REVERSE` |`RGB_RMOD`|Cycle through modes in reverse, forward direction when Shift is held|
  41. |`RGB_HUI` | |Increase hue, decrease hue when Shift is held |
  42. |`RGB_HUD` | |Decrease hue, increase hue when Shift is held |
  43. |`RGB_SAI` | |Increase saturation, decrease saturation when Shift is held |
  44. |`RGB_SAD` | |Decrease saturation, increase saturation when Shift is held |
  45. |`RGB_VAI` | |Increase value (brightness), decrease value when Shift is held |
  46. |`RGB_VAD` | |Decrease value (brightness), increase value when Shift is held |
  47. |`RGB_MODE_PLAIN` |`RGB_M_P `|Static (no animation) mode |
  48. |`RGB_MODE_BREATHE` |`RGB_M_B` |Breathing animation mode |
  49. |`RGB_MODE_RAINBOW` |`RGB_M_R` |Rainbow animation mode |
  50. |`RGB_MODE_SWIRL` |`RGB_M_SW`|Swirl animation mode |
  51. |`RGB_MODE_SNAKE` |`RGB_M_SN`|Snake animation mode |
  52. |`RGB_MODE_KNIGHT` |`RGB_M_K` |"Knight Rider" animation mode |
  53. |`RGB_MODE_XMAS` |`RGB_M_X` |Christmas animation mode |
  54. |`RGB_MODE_GRADIENT`|`RGB_M_G` |Static gradient animation mode |
  55. |`RGB_MODE_RGBTEST` |`RGB_M_T` |Red, Green, Blue test animation mode |
  56. !> By default, if you have both the RGB Light and the [RGB Matrix](feature_rgb_matrix.md) feature enabled, these keycodes will work for both features, at the same time. You can disable the keycode functionality by defining the `*_DISABLE_KEYCODES` option for the specific feature.
  57. ## Configuration
  58. Your RGB lighting can be configured by placing these `#define`s in your `config.h`:
  59. |Define |Default |Description |
  60. |---------------------|-------------|-----------------------------------------------------------------------------|
  61. |`RGBLIGHT_HUE_STEP` |`10` |The number of steps to cycle through the hue by |
  62. |`RGBLIGHT_SAT_STEP` |`17` |The number of steps to increment the saturation by |
  63. |`RGBLIGHT_VAL_STEP` |`17` |The number of steps to increment the brightness by |
  64. |`RGBLIGHT_LIMIT_VAL` |`255` |The maximum brightness level |
  65. |`RGBLIGHT_SLEEP` |*Not defined*|If defined, the RGB lighting will be switched off when the host goes to sleep|
  66. |`RGBLIGHT_SPLIT` |*Not defined*|If defined, synchronization functionality for split keyboards is added|
  67. |`RGBLIGHT_DISABLE_KEYCODES`|*not defined*|If defined, disables the ability to control RGB Light from the keycodes. You must use code functions to control the feature|
  68. ## Effects and Animations
  69. Not only can this lighting be whatever color you want,
  70. if `RGBLIGHT_EFFECT_xxxx` or `RGBLIGHT_ANIMATIONS` is defined, you also have a number of animation modes at your disposal:
  71. |Mode number symbol |Additional number |Description |
  72. |-----------------------------|-------------------|---------------------------------------|
  73. |`RGBLIGHT_MODE_STATIC_LIGHT` | *None* |Solid color (this mode is always enabled) |
  74. |`RGBLIGHT_MODE_BREATHING` | 0,1,2,3 |Solid color breathing |
  75. |`RGBLIGHT_MODE_RAINBOW_MOOD` | 0,1,2 |Cycling rainbow |
  76. |`RGBLIGHT_MODE_RAINBOW_SWIRL`| 0,1,2,3,4,5 |Swirling rainbow |
  77. |`RGBLIGHT_MODE_SNAKE` | 0,1,2,3,4,5 |Snake |
  78. |`RGBLIGHT_MODE_KNIGHT` | 0,1,2 |Knight |
  79. |`RGBLIGHT_MODE_CHRISTMAS` | *None* |Christmas |
  80. |`RGBLIGHT_MODE_STATIC_GRADIENT`| 0,1,..,9 |Static gradient |
  81. |`RGBLIGHT_MODE_RGB_TEST` | *None* |RGB Test |
  82. |`RGBLIGHT_MODE_ALTERNATING` | *None* |Alternating |
  83. |`RGBLIGHT_MODE_TWINKLE` | 0,1,2,3,4,5 |Twinkle |
  84. Check out [this video](https://youtube.com/watch?v=VKrpPAHlisY) for a demonstration.
  85. Note: For versions older than 0.6.117, The mode numbers were written directly. In `quantum/rgblight.h` there is a contrast table between the old mode number and the current symbol.
  86. ### Effect and Animation Toggles
  87. Use these defines to add or remove animations from the firmware. When you are running low on flash space, it can be helpful to disable animations you are not using.
  88. |Define |Default |Description |
  89. |------------------------------------|-------------|-------------------------------------------------------------------------|
  90. |`RGBLIGHT_ANIMATIONS` |*Not defined*|Enable all additional animation modes. |
  91. |`RGBLIGHT_EFFECT_ALTERNATING` |*Not defined*|Enable alternating animation mode. |
  92. |`RGBLIGHT_EFFECT_BREATHING` |*Not defined*|Enable breathing animation mode. |
  93. |`RGBLIGHT_EFFECT_CHRISTMAS` |*Not defined*|Enable christmas animation mode. |
  94. |`RGBLIGHT_EFFECT_KNIGHT` |*Not defined*|Enable knight animation mode. |
  95. |`RGBLIGHT_EFFECT_RAINBOW_MOOD` |*Not defined*|Enable rainbow mood animation mode. |
  96. |`RGBLIGHT_EFFECT_RAINBOW_SWIRL` |*Not defined*|Enable rainbow swirl animation mode. |
  97. |`RGBLIGHT_EFFECT_RGB_TEST` |*Not defined*|Enable RGB test animation mode. |
  98. |`RGBLIGHT_EFFECT_SNAKE` |*Not defined*|Enable snake animation mode. |
  99. |`RGBLIGHT_EFFECT_STATIC_GRADIENT` |*Not defined*|Enable static gradient mode. |
  100. |`RGBLIGHT_EFFECT_TWINKLE` |*Not defined*|Enable twinkle animation mode. |
  101. ### Effect and Animation Settings
  102. The following options are used to tweak the various animations:
  103. |Define |Default |Description |
  104. |------------------------------------|-------------|-----------------------------------------------------------------------------------------------|
  105. |`RGBLIGHT_EFFECT_BREATHE_CENTER` |*Not defined*|If defined, used to calculate the curve for the breathing animation. Valid values are 1.0 to 2.7 |
  106. |`RGBLIGHT_EFFECT_BREATHE_MAX` |`255` |The maximum brightness for the breathing mode. Valid values are 1 to 255 |
  107. |`RGBLIGHT_EFFECT_CHRISTMAS_INTERVAL`|`40` |How long (in milliseconds) to wait between animation steps for the "Christmas" animation |
  108. |`RGBLIGHT_EFFECT_CHRISTMAS_STEP` |`2` |The number of LEDs to group the red/green colors by for the "Christmas" animation |
  109. |`RGBLIGHT_EFFECT_KNIGHT_LED_NUM` |`RGBLED_NUM` |The number of LEDs to have the "Knight" animation travel |
  110. |`RGBLIGHT_EFFECT_KNIGHT_LENGTH` |`3` |The number of LEDs to light up for the "Knight" animation |
  111. |`RGBLIGHT_EFFECT_KNIGHT_OFFSET` |`0` |The number of LEDs to start the "Knight" animation from the start of the strip by |
  112. |`RGBLIGHT_RAINBOW_SWIRL_RANGE` |`255` |Range adjustment for the rainbow swirl effect to get different swirls |
  113. |`RGBLIGHT_EFFECT_SNAKE_LENGTH` |`4` |The number of LEDs to light up for the "Snake" animation |
  114. |`RGBLIGHT_EFFECT_TWINKLE_LIFE` |`200` |Adjusts how quickly each LED brightens and dims when twinkling (in animation steps) |
  115. |`RGBLIGHT_EFFECT_TWINKLE_PROBABILITY`|`1/127` |Adjusts how likely each LED is to twinkle (on each animation step) |
  116. ### Example Usage to Reduce Memory Footprint
  117. 1. Remove `RGBLIGHT_ANIMATIONS` from `config.h`.
  118. 1. Selectively add the animations you want to enable. The following would enable two animations and save about 4KiB:
  119. ```diff
  120. #undef RGBLED_NUM
  121. -#define RGBLIGHT_ANIMATIONS
  122. +#define RGBLIGHT_EFFECT_STATIC_GRADIENT
  123. +#define RGBLIGHT_EFFECT_RAINBOW_SWIRL
  124. #define RGBLED_NUM 12
  125. #define RGBLIGHT_HUE_STEP 8
  126. #define RGBLIGHT_SAT_STEP 8
  127. ```
  128. ### Animation Speed
  129. You can also modify the speeds that the different modes animate at:
  130. Here is a quick demo on Youtube (with NPKC KC60) (https://www.youtube.com/watch?v=VKrpPAHlisY).
  131. ```c
  132. // How long (in milliseconds) to wait between animation steps for each of the "Solid color breathing" animations
  133. const uint8_t RGBLED_BREATHING_INTERVALS[] PROGMEM = {30, 20, 10, 5};
  134. // How long (in milliseconds) to wait between animation steps for each of the "Cycling rainbow" animations
  135. const uint8_t RGBLED_RAINBOW_MOOD_INTERVALS[] PROGMEM = {120, 60, 30};
  136. // How long (in milliseconds) to wait between animation steps for each of the "Swirling rainbow" animations
  137. const uint8_t RGBLED_RAINBOW_SWIRL_INTERVALS[] PROGMEM = {100, 50, 20};
  138. // How long (in milliseconds) to wait between animation steps for each of the "Snake" animations
  139. const uint8_t RGBLED_SNAKE_INTERVALS[] PROGMEM = {100, 50, 20};
  140. // How long (in milliseconds) to wait between animation steps for each of the "Knight" animations
  141. const uint8_t RGBLED_KNIGHT_INTERVALS[] PROGMEM = {127, 63, 31};
  142. // How long (in milliseconds) to wait between animation steps for each of the "Twinkle" animations
  143. const uint8_t RGBLED_TWINKLE_INTERVALS[] PROGMEM = {50, 25, 10};
  144. // These control which hues are selected for each of the "Static gradient" modes
  145. const uint8_t RGBLED_GRADIENT_RANGES[] PROGMEM = {255, 170, 127, 85, 64};
  146. ```
  147. ## Lighting Layers
  148. ?> **Note:** Lighting Layers is an RGB Light feature, it will not work for RGB Matrix. See [RGB Matrix Indicators](feature_rgb_matrix.md?indicators) for details on how to do so.
  149. By including `#define RGBLIGHT_LAYERS` in your `config.h` file you can enable lighting layers. These make
  150. it easy to use your underglow LEDs as status indicators to show which keyboard layer is currently active, or the state of caps lock, all without disrupting any animations. [Here's a video](https://youtu.be/uLGE1epbmdY) showing an example of what you can do.
  151. ### Defining Lighting Layers :id=defining-lighting-layers
  152. By default, 8 layers are possible. This can be expanded to as many as 32 by overriding the definition of `RGBLIGHT_MAX_LAYERS` in `config.h` (e.g. `#define RGBLIGHT_MAX_LAYERS 32`). Please note, if you use a split keyboard, you will need to flash both sides of the split after changing this. Also, increasing the maximum will increase the firmware size, and will slow sync on split keyboards.
  153. To define a layer, we modify `keymap.c` to list the LED ranges and the colors we want to overlay on them using an array of `rgblight_segment_t` using the `RGBLIGHT_LAYER_SEGMENTS` macro. We can define multiple layers and enable/disable them independently:
  154. ```c
  155. // Light LEDs 6 to 9 and 12 to 15 red when caps lock is active. Hard to ignore!
  156. const rgblight_segment_t PROGMEM my_capslock_layer[] = RGBLIGHT_LAYER_SEGMENTS(
  157. {6, 4, HSV_RED}, // Light 4 LEDs, starting with LED 6
  158. {12, 4, HSV_RED} // Light 4 LEDs, starting with LED 12
  159. );
  160. // Light LEDs 9 & 10 in cyan when keyboard layer 1 is active
  161. const rgblight_segment_t PROGMEM my_layer1_layer[] = RGBLIGHT_LAYER_SEGMENTS(
  162. {9, 2, HSV_CYAN}
  163. );
  164. // Light LEDs 11 & 12 in purple when keyboard layer 2 is active
  165. const rgblight_segment_t PROGMEM my_layer2_layer[] = RGBLIGHT_LAYER_SEGMENTS(
  166. {11, 2, HSV_PURPLE}
  167. );
  168. // Light LEDs 13 & 14 in green when keyboard layer 3 is active
  169. const rgblight_segment_t PROGMEM my_layer3_layer[] = RGBLIGHT_LAYER_SEGMENTS(
  170. {13, 2, HSV_GREEN}
  171. );
  172. // etc..
  173. ```
  174. We combine these layers into an array using the `RGBLIGHT_LAYERS_LIST` macro, and assign it to the `rgblight_layers` variable during keyboard setup. Note that you can only define up to 8 lighting layers. Any extra layers will be ignored. Since the different lighting layers overlap, the order matters in the array, with later layers taking precedence:
  175. ```c
  176. // Now define the array of layers. Later layers take precedence
  177. const rgblight_segment_t* const PROGMEM my_rgb_layers[] = RGBLIGHT_LAYERS_LIST(
  178. my_capslock_layer,
  179. my_layer1_layer, // Overrides caps lock layer
  180. my_layer2_layer, // Overrides other layers
  181. my_layer3_layer // Overrides other layers
  182. );
  183. void keyboard_post_init_user(void) {
  184. // Enable the LED layers
  185. rgblight_layers = my_rgb_layers;
  186. }
  187. ```
  188. Note: For split keyboards with two controllers, both sides need to be flashed when updating the contents of rgblight_layers.
  189. ### Enabling and disabling lighting layers :id=enabling-lighting-layers
  190. Everything above just configured the definition of each lighting layer.
  191. We can now enable and disable the lighting layers whenever the state of the keyboard changes:
  192. ```c
  193. bool led_update_user(led_t led_state) {
  194. rgblight_set_layer_state(0, led_state.caps_lock);
  195. return true;
  196. }
  197. layer_state_t default_layer_state_set_user(layer_state_t state) {
  198. rgblight_set_layer_state(1, layer_state_cmp(state, _DVORAK));
  199. return state;
  200. }
  201. layer_state_t layer_state_set_user(layer_state_t state) {
  202. rgblight_set_layer_state(2, layer_state_cmp(state, _FN));
  203. rgblight_set_layer_state(3, layer_state_cmp(state, _ADJUST));
  204. return state;
  205. }
  206. ```
  207. ### Lighting layer blink :id=lighting-layer-blink
  208. By including `#define RGBLIGHT_LAYER_BLINK` in your `config.h` file you can turn a lighting
  209. layer on for a specified duration. Once the specified number of milliseconds has elapsed
  210. the layer will be turned off. This is useful, e.g., if you want to acknowledge some
  211. action (e.g. toggling some setting):
  212. ```c
  213. const rgblight_segment_t PROGMEM _yes_layer[] = RGBLIGHT_LAYER_SEGMENTS( {9, 6, HSV_GREEN} );
  214. const rgblight_segment_t PROGMEM _no_layer[] = RGBLIGHT_LAYER_SEGMENTS( {9, 6, HSV_RED} );
  215. const rgblight_segment_t* const PROGMEM _rgb_layers[] =
  216. RGBLIGHT_LAYERS_LIST( _yes_layer, _no_layer );
  217. void keyboard_post_init_user(void) {
  218. rgblight_layers = _rgb_layers;
  219. }
  220. // Note we user post_process_record_user because we want the state
  221. // after the flag has been flipped...
  222. void post_process_record_user(uint16_t keycode, keyrecord_t *record) {
  223. switch (keycode) {
  224. case DEBUG:
  225. rgblight_blink_layer(debug_enable ? 0 : 1, 500);
  226. break;
  227. case NK_TOGG:
  228. case NK_ON:
  229. case NK_OFF:
  230. rgblight_blink_layer(keymap_config.nkro ? 0 : 1, 500);
  231. break;
  232. }
  233. }
  234. ```
  235. ### Overriding RGB Lighting on/off status
  236. Normally lighting layers are not shown when RGB Lighting is disabled (e.g. with `RGB_TOG` keycode). If you would like lighting layers to work even when the RGB Lighting is otherwise off, add `#define RGBLIGHT_LAYERS_OVERRIDE_RGB_OFF` to your `config.h`.
  237. ## Functions
  238. If you need to change your RGB lighting in code, for example in a macro to change the color whenever you switch layers, QMK provides a set of functions to assist you. See [`rgblight.h`](https://github.com/qmk/qmk_firmware/blob/master/quantum/rgblight.h) for the full list, but the most commonly used functions include:
  239. ### Utility Functions
  240. |Function |Description |
  241. |--------------------------------------------|-------------------------------------------------------------------|
  242. |`sethsv(hue, sat, val, ledbuf)` |Set ledbuf to the given HSV value |
  243. |`sethsv_raw(hue, sat, val, ledbuf)` |Set ledbuf to the given HSV value without RGBLIGHT_LIMIT_VAL check |
  244. |`setrgb(r, g, b, ledbuf)` |Set ledbuf to the given RGB value where `r`/`g`/`b` |
  245. ### Low level Functions
  246. |Function |Description |
  247. |--------------------------------------------|-------------------------------------------|
  248. |`rgblight_set()` |Flash out led buffers to LEDs |
  249. |`rgblight_set_clipping_range(pos, num)` |Set clipping Range. see [Clipping Range](#clipping-range) |
  250. Example:
  251. ```c
  252. sethsv(HSV_WHITE, (LED_TYPE *)&led[0]); // led 0
  253. sethsv(HSV_RED, (LED_TYPE *)&led[1]); // led 1
  254. sethsv(HSV_GREEN, (LED_TYPE *)&led[2]); // led 2
  255. rgblight_set(); // Utility functions do not call rgblight_set() automatically, so they need to be called explicitly.
  256. ```
  257. ### Effects and Animations Functions
  258. #### effect range setting
  259. |Function |Description |
  260. |--------------------------------------------|------------------|
  261. |`rgblight_set_effect_range(pos, num)` |Set Effects Range |
  262. #### direct operation
  263. |Function |Description |
  264. |--------------------------------------------|-------------|
  265. |`rgblight_setrgb_at(r, g, b, index)` |Set a single LED to the given RGB value, where `r`/`g`/`b` are between 0 and 255 and `index` is between 0 and `RGBLED_NUM` (not written to EEPROM) |
  266. |`rgblight_sethsv_at(h, s, v, index)` |Set a single LED to the given HSV value, where `h`/`s`/`v` are between 0 and 255, and `index` is between 0 and `RGBLED_NUM` (not written to EEPROM) |
  267. |`rgblight_setrgb_range(r, g, b, start, end)`|Set a continuous range of LEDs to the given RGB value, where `r`/`g`/`b` are between 0 and 255 and `start`(included) and `stop`(excluded) are between 0 and `RGBLED_NUM` (not written to EEPROM)|
  268. |`rgblight_sethsv_range(h, s, v, start, end)`|Set a continuous range of LEDs to the given HSV value, where `h`/`s`/`v` are between 0 and 255, and `start`(included) and `stop`(excluded) are between 0 and `RGBLED_NUM` (not written to EEPROM)|
  269. |`rgblight_setrgb(r, g, b)` |Set effect range LEDs to the given RGB value where `r`/`g`/`b` are between 0 and 255 (not written to EEPROM) |
  270. |`rgblight_setrgb_master(r, g, b)` |Set the LEDs on the master side to the given RGB value, where `r`/`g`/`b` are between 0 and 255 (not written to EEPROM) |
  271. |`rgblight_setrgb_slave(r, g, b)` |Set the LEDs on the slave side to the given RGB value, where `r`/`g`/`b` are between 0 and 255 (not written to EEPROM) |
  272. |`rgblight_sethsv_master(h, s, v)` |Set the LEDs on the master side to the given HSV value, where `h`/`s`/`v` are between 0 and 255 (not written to EEPROM) |
  273. |`rgblight_sethsv_slave(h, s, v)` |Set the LEDs on the slave side to the given HSV value, where `h`/`s`/`v` are between 0 and 255 (not written to EEPROM) |
  274. Example:
  275. ```c
  276. rgblight_sethsv(HSV_WHITE, 0); // led 0
  277. rgblight_sethsv(HSV_RED, 1); // led 1
  278. rgblight_sethsv(HSV_GREEN, 2); // led 2
  279. // The above functions automatically calls rgblight_set(), so there is no need to call it explicitly.
  280. // Note that it is inefficient to call repeatedly.
  281. ```
  282. #### effect mode change
  283. |Function |Description |
  284. |--------------------------------------------|-------------|
  285. |`rgblight_mode(x)` |Set the mode, if RGB animations are enabled |
  286. |`rgblight_mode_noeeprom(x)` |Set the mode, if RGB animations are enabled (not written to EEPROM) |
  287. |`rgblight_step()` |Change the mode to the next RGB animation in the list of enabled RGB animations |
  288. |`rgblight_step_noeeprom()` |Change the mode to the next RGB animation in the list of enabled RGB animations (not written to EEPROM) |
  289. |`rgblight_step_reverse()` |Change the mode to the previous RGB animation in the list of enabled RGB animations |
  290. |`rgblight_step_reverse_noeeprom()` |Change the mode to the previous RGB animation in the list of enabled RGB animations (not written to EEPROM) |
  291. |`rgblight_reload_from_eeprom()` |Reload the effect configuration (enabled, mode and color) from EEPROM |
  292. #### effects mode disable/enable
  293. |Function |Description |
  294. |--------------------------------------------|-------------|
  295. |`rgblight_toggle()` |Toggle effect range LEDs between on and off |
  296. |`rgblight_toggle_noeeprom()` |Toggle effect range LEDs between on and off (not written to EEPROM) |
  297. |`rgblight_enable()` |Turn effect range LEDs on, based on their previous state |
  298. |`rgblight_enable_noeeprom()` |Turn effect range LEDs on, based on their previous state (not written to EEPROM) |
  299. |`rgblight_disable()` |Turn effect range LEDs off |
  300. |`rgblight_disable_noeeprom()` |Turn effect range LEDs off (not written to EEPROM) |
  301. #### hue, sat, val change
  302. |Function |Description |
  303. |--------------------------------------------|-------------|
  304. |`rgblight_increase_hue()` |Increase the hue for effect range LEDs. This wraps around at maximum hue |
  305. |`rgblight_increase_hue_noeeprom()` |Increase the hue for effect range LEDs. This wraps around at maximum hue (not written to EEPROM) |
  306. |`rgblight_decrease_hue()` |Decrease the hue for effect range LEDs. This wraps around at minimum hue |
  307. |`rgblight_decrease_hue_noeeprom()` |Decrease the hue for effect range LEDs. This wraps around at minimum hue (not written to EEPROM) |
  308. |`rgblight_increase_sat()` |Increase the saturation for effect range LEDs. This stops at maximum saturation |
  309. |`rgblight_increase_sat_noeeprom()` |Increase the saturation for effect range LEDs. This stops at maximum saturation (not written to EEPROM) |
  310. |`rgblight_decrease_sat()` |Decrease the saturation for effect range LEDs. This stops at minimum saturation |
  311. |`rgblight_decrease_sat_noeeprom()` |Decrease the saturation for effect range LEDs. This stops at minimum saturation (not written to EEPROM) |
  312. |`rgblight_increase_val()` |Increase the value for effect range LEDs. This stops at maximum value |
  313. |`rgblight_increase_val_noeeprom()` |Increase the value for effect range LEDs. This stops at maximum value (not written to EEPROM) |
  314. |`rgblight_decrease_val()` |Decrease the value for effect range LEDs. This stops at minimum value |
  315. |`rgblight_decrease_val_noeeprom()` |Decrease the value for effect range LEDs. This stops at minimum value (not written to EEPROM) |
  316. |`rgblight_sethsv(h, s, v)` |Set effect range LEDs to the given HSV value where `h`/`s`/`v` are between 0 and 255 |
  317. |`rgblight_sethsv_noeeprom(h, s, v)` |Set effect range LEDs to the given HSV value where `h`/`s`/`v` are between 0 and 255 (not written to EEPROM) |
  318. #### Speed functions
  319. |Function |Description |
  320. |--------------------------------------------|-------------|
  321. |`rgblight_increase_speed()` |Increases the animation speed |
  322. |`rgblight_increase_speed_noeeprom()` |Increases the animation speed (not written to EEPROM) |
  323. |`rgblight_decrease_speed()` |Decreases the animation speed |
  324. |`rgblight_decrease_speed_noeeprom()` |Decreases the animation speed (not written to EEPROM) |
  325. |`rgblight_set_speed()` |Sets the speed. Value is between 0 and 255 |
  326. |`rgblight_set_speed_noeeprom()` |Sets the speed. Value is between 0 and 255 (not written to EEPROM) |
  327. #### layer functions
  328. |Function |Description |
  329. |--------------------------------------------|-------------|
  330. |`rgblight_get_layer_state(i)` |Returns `true` if lighting layer `i` is enabled |
  331. |`rgblight_set_layer_state(i, is_on)` |Enable or disable lighting layer `i` based on value of `bool is_on` |
  332. #### query
  333. |Function |Description |
  334. |-----------------------|---------------------------|
  335. |`rgblight_is_enabled()`|Gets current on/off status |
  336. |`rgblight_get_mode()` |Gets current mode |
  337. |`rgblight_get_hue()` |Gets current hue |
  338. |`rgblight_get_sat()` |Gets current sat |
  339. |`rgblight_get_val()` |Gets current val |
  340. |`rgblight_get_speed()` |Gets current speed |
  341. ## Colors
  342. These are shorthands to popular colors. The `RGB` ones can be passed to the `setrgb` functions, while the `HSV` ones to the `sethsv` functions.
  343. |RGB |HSV |
  344. |-------------------|-------------------|
  345. |`RGB_WHITE` |`HSV_WHITE` |
  346. |`RGB_RED` |`HSV_RED` |
  347. |`RGB_CORAL` |`HSV_CORAL` |
  348. |`RGB_ORANGE` |`HSV_ORANGE` |
  349. |`RGB_GOLDENROD` |`HSV_GOLDENROD` |
  350. |`RGB_GOLD` |`HSV_GOLD` |
  351. |`RGB_YELLOW` |`HSV_YELLOW` |
  352. |`RGB_CHARTREUSE` |`HSV_CHARTREUSE` |
  353. |`RGB_GREEN` |`HSV_GREEN` |
  354. |`RGB_SPRINGGREEN` |`HSV_SPRINGGREEN` |
  355. |`RGB_TURQUOISE` |`HSV_TURQUOISE` |
  356. |`RGB_TEAL` |`HSV_TEAL` |
  357. |`RGB_CYAN` |`HSV_CYAN` |
  358. |`RGB_AZURE` |`HSV_AZURE` |
  359. |`RGB_BLUE` |`HSV_BLUE` |
  360. |`RGB_PURPLE` |`HSV_PURPLE` |
  361. |`RGB_MAGENTA` |`HSV_MAGENTA` |
  362. |`RGB_PINK` |`HSV_PINK` |
  363. ```c
  364. rgblight_setrgb(RGB_ORANGE);
  365. rgblight_sethsv_noeeprom(HSV_GREEN);
  366. rgblight_setrgb_at(RGB_GOLD, 3);
  367. rgblight_sethsv_range(HSV_WHITE, 0, 6);
  368. ```
  369. These are defined in [`rgblight_list.h`](https://github.com/qmk/qmk_firmware/blob/master/quantum/rgblight_list.h). Feel free to add to this list!
  370. ## Changing the order of the LEDs
  371. If you want to make the logical order of LEDs different from the electrical connection order, you can do this by defining the `RGBLIGHT_LED_MAP` macro in your `config.h`.
  372. Normally, the contents of the LED buffer are output to the LEDs in the same order.
  373. <img src="https://user-images.githubusercontent.com/2170248/55743718-01866c80-5a6e-11e9-8134-25419928327a.JPG" alt="simple dicrect" width="50%"/>
  374. By defining `RGBLIGHT_LED_MAP` as in the example below, you can specify the LED with addressing in reverse order of the electrical connection order.
  375. ```c
  376. // config.h
  377. #define RGBLED_NUM 4
  378. #define RGBLIGHT_LED_MAP { 3, 2, 1, 0 }
  379. ```
  380. <img src="https://user-images.githubusercontent.com/2170248/55743725-08ad7a80-5a6e-11e9-83ed-126a2b0209fc.JPG" alt="simple mapped" width="50%"/>
  381. For keyboards that use the RGB LEDs as a backlight for each key, you can also define it as in the example below.
  382. ```c
  383. // config.h
  384. #define RGBLED_NUM 30
  385. /* RGB LED Conversion macro from physical array to electric array */
  386. #define LED_LAYOUT( \
  387. L00, L01, L02, L03, L04, L05, \
  388. L10, L11, L12, L13, L14, L15, \
  389. L20, L21, L22, L23, L24, L25, \
  390. L30, L31, L32, L33, L34, L35, \
  391. L40, L41, L42, L43, L44, L45 ) \
  392. { \
  393. L05, L04, L03, L02, L01, L00, \
  394. L10, L11, L12, L13, L14, L15, \
  395. L25, L24, L23, L22, L21, L20, \
  396. L30, L31, L32, L33, L34, L35, \
  397. L46, L45, L44, L43, L42, L41 \
  398. }
  399. /* RGB LED logical order map */
  400. /* Top->Bottom, Right->Left */
  401. #define RGBLIGHT_LED_MAP LED_LAYOUT( \
  402. 25, 20, 15, 10, 5, 0, \
  403. 26, 21, 16, 11, 6, 1, \
  404. 27, 22, 17, 12, 7, 2, \
  405. 28, 23, 18, 13, 8, 3, \
  406. 29, 24, 19, 14, 9, 4 )
  407. ```
  408. ## Clipping Range
  409. Using the `rgblight_set_clipping_range()` function, you can prepare more buffers than the actual number of LEDs, and output some of the buffers to the LEDs. This is useful if you want the split keyboard to treat left and right LEDs as logically contiguous.
  410. You can set the Clipping Range by executing the following code.
  411. ```c
  412. // some source
  413. rgblight_set_clipping_range(3, 4);
  414. ```
  415. <img src="https://user-images.githubusercontent.com/2170248/55743785-2bd82a00-5a6e-11e9-9d4b-1b4ffaf4932b.JPG" alt="clip direct" width="70%"/>
  416. In addition to setting the Clipping Range, you can use `RGBLIGHT_LED_MAP` together.
  417. ```c
  418. // config.h
  419. #define RGBLED_NUM 8
  420. #define RGBLIGHT_LED_MAP { 7, 6, 5, 4, 3, 2, 1, 0 }
  421. // some soruce
  422. rgblight_set_clipping_range(3, 4);
  423. ```
  424. <img src="https://user-images.githubusercontent.com/2170248/55743747-119e4c00-5a6e-11e9-91e5-013203ffae8a.JPG" alt="clip mapped" width="70%"/>
  425. ## Hardware Modification
  426. If your keyboard lacks onboard underglow LEDs, you may often be able to solder on an RGB LED strip yourself. You will need to find an unused pin to wire to the data pin of your LED strip. Some keyboards may break out unused pins from the MCU to make soldering easier. The other two pins, VCC and GND, must also be connected to the appropriate power pins.