Skip to content

Instantly share code, notes, and snippets.

@muppetjones
Last active April 9, 2024 23:53
Show Gist options
  • Save muppetjones/3dff33c414311feed57a479cee6b574f to your computer and use it in GitHub Desktop.
Save muppetjones/3dff33c414311feed57a479cee6b574f to your computer and use it in GitHub Desktop.
QMK Dynamic Layer Lighting
/* (Optional) Define RGB settings in your config.h */
#ifdef RGBLIGHT_ENABLE
# define RGBLIGHT_ANIMATIONS
# define RGBLIGHT_HUE_STEP 8
# define RGBLIGHT_SAT_STEP 8
# define RGBLIGHT_VAL_STEP 8
# define RGBLIGHT_LIMIT_VAL 150
# define RGBLIGHT_SLEEP
// # define RGBLIGHT_LAYERS // We do NOT need the layers!
#endif
/* Enable dynamic layer lighting.
* - Changes hue based on your current color.
* - Uses your current saturation and brigtness settings.
*/
/* Add these declarations to the top of your keymap.c
*/
#ifdef RGBLIGHT_ENABLE
static rgblight_config_t home_rgb;
void set_rgb_home(void);
void set_rgb_by_layer(layer_state_t);
#endif
/* Be sure to add the following keycodes to your map!
* "Adjust" is a good layer choice.
* - RGB_SAI,
* - RGB_HUI,
* - RGB_VAI
*/
/* Add the rgb blocks to the following functions
* - keyboard_post_init
* - layer_state_set_user
* - process_record_user
*/
void keyboard_post_init_user(void) {
// Call the keymap level matrix init.
#ifdef RGBLIGHT_ENABLE
set_rgb_home();
#endif
}
layer_state_t layer_state_set_user(layer_state_t state) {
#ifdef RGBLIGHT_ENABLE
set_rgb_by_layer(state);
#endif
}
void post_process_record_user(uint16_t keycode, keyrecord_t *record) {
// Regular user keycode case statement
switch (keycode) {
#ifdef RGBLIGHT_ENABLE
case RGB_HUD:
case RGB_HUI:
case RGB_SAD:
case RGB_SAI:
case RGB_VAD:
case RGB_VAI:
set_rgb_home();
break;
#endif
default:
break;
}
}
/* Somewhere near the end of your keymap.c
*
* This block sets up the layer state and colors.
*/
#ifdef RGBLIGHT_ENABLE
void set_rgb_home(void) {
home_rgb.raw = eeconfig_read_rgblight();
}
void set_rgb_by_layer(layer_state_t state) {
if (!rgblight_is_enabled()) {
return; // lighting not enabled
}
uint8_t offset = 0;
switch (get_highest_layer(state)) {
case _RAISE:
offset = 2 * RGBLIGHT_HUE_STEP;
break;
case _LOWER:
offset = -2 * RGBLIGHT_HUE_STEP;
break;
case _NAV:
offset = 1 * RGBLIGHT_HUE_STEP;
break;
// case _ADJUST: // layer color not recommended on layer w/ rgb keys
// offset = -96;
// break;
default: // for any other layers, or the default layer
break;
}
rgblight_sethsv_noeeprom((home_rgb.hue + offset) % 255, home_rgb.sat, home_rgb.val);
}
}
#endif
/* Turn on RGB underglow */
RGBLIGHT_ENABLE = yes # Enable keyboard RGB underglow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment