#include QMK_KEYBOARD_H uint8_t flags = 0; #include "keycodes.h" #include "lighting.h" // The variable is defined in star_key.h // The LED is on when the key is pressed. extern bool is_star; // Set the color at the given position, but limit the intensity void set_color(uint8_t index, uint8_t h, uint8_t s, uint8_t v) { if (v > 20 + rgb_matrix_get_val()) v = 20 + rgb_matrix_get_val(); HSV hsv = {h, s, v}; RGB rgb = hsv_to_rgb(hsv); rgb_matrix_set_color(index, rgb.r, rgb.g, rgb.b); } HSV hsv_of_color(uint8_t h, uint8_t s, uint8_t v) { return (HSV){h, s, v > rgb_matrix_get_val() ? rgb_matrix_get_val():v}; } void lighting_init(void) { rgb_matrix_sethsv_noeeprom(HSV_WHITE); rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_REACTIVE_SIMPLE); } static uint8_t last_flags; static uint8_t one_shot_flags = 0; // Build a bit set depending of the active mods. // The bit set is intended to match colors (see below) // // There is a special case for the caps_lock state, because the function can // be called either from the active mods on the keyboard, or from the active // oneshot mods and I do not want to report the capslock in the evaluation of // the oneshot flags. uint8_t get_flags(uint8_t mods, bool with_caps) { uint8_t flags = 0; if (mods & MOD_MASK_CTRL) { flags |= 0b001; } if (mods & MOD_MASK_ALT) { flags |= 0b010; } if ((mods & MOD_MASK_SHIFT) || (with_caps && host_keyboard_led_state().caps_lock)) { flags |= 0b100; } return flags; } void oneshot_mods_changed_lighting(uint8_t mods) { one_shot_flags = get_flags(mods, false); } void housekeeping_task_lighting(void) { // Colors for the mods HSV const colors[] = { hsv_of_color(HSV_BLACK), // 000 hsv_of_color(55, 255, 255),// 001 Ctrl hsv_of_color(HSV_RED), // 010 Alt hsv_of_color(HSV_ORANGE),// 011 hsv_of_color(HSV_CYAN), // 100 Shift hsv_of_color(HSV_GREEN), // 101 hsv_of_color(HSV_PURPLE),// 110 hsv_of_color(HSV_WHITE), // 111 }; uint8_t flags = get_flags(get_mods(), true) | one_shot_flags; uint8_t matrix_mode = rgb_matrix_get_mode(); if (matrix_mode != RGB_MATRIX_BREATHING && \ (host_keyboard_led_state().caps_lock || one_shot_flags)) { rgb_matrix_mode_noeeprom(RGB_MATRIX_BREATHING); } if (flags == last_flags) { return; } last_flags = flags; if (flags) { if (matrix_mode != RGB_MATRIX_SOLID_COLOR) { rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_COLOR); } rgb_matrix_sethsv_noeeprom(colors[flags].h, colors[flags].s, rgb_matrix_get_val()); } else if (matrix_mode != RGB_MATRIX_SOLID_REACTIVE_SIMPLE) { rgb_matrix_sethsv_noeeprom(0, 0, rgb_matrix_get_val()); rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_REACTIVE_SIMPLE); } } bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) { // Activate effects depending of mods // Check once if we need to activate the LED. This can be cause either a // layer, or a modifier. If the state is default, exit the function early. uint8_t flags = get_mods(); uint8_t current_layer = get_highest_layer(layer_state); // The value `layer_state` is global if (! (flags || current_layer || is_star || one_shot_flags || host_keyboard_led_state().caps_lock)) { return false; } for (uint8_t row = 0; row < MATRIX_ROWS; ++row) { for (uint8_t col = 0; col < MATRIX_COLS; ++col) { uint8_t index = g_led_config.matrix_co[row][col]; if (index < led_min || index > led_max || index == NO_LED) continue; uint8_t layer = layer_switch_get_layer((keypos_t){col,row}); uint16_t keycode = keymap_key_to_keycode(layer, (keypos_t){col,row}); if (keycode == KC_O && is_star) { set_color(index, 42, 255, 255); } switch (keycode) { case KC_F1 ... KC_F11: set_color(index, 0, 0, 128); break; case KC_P1 ... KC_P0: case KC_1 ... KC_0: case KEY_6: if (!is_star) { set_color(index, 42, 255, 255); } break; case KC_BACKSLASH: case KC_EQUAL: case KC_PDOT: case KC_KP_SLASH ... KC_KP_PLUS: // Add a bit of red for the punction symbol set_color(index, 20, 255, 255); break; case KC_RIGHT: case KEY_DOWN: case KC_LEFT ... KC_UP: case KC_PGUP: case KC_PGDN: case KEY_PGDN: set_color(index, 170, 255, 223); break; case KC_HOME: case KC_END: case KC_TAB: case KEY_INS: set_color(index, HSV_GREEN); break; case KC_LSFT: case KEY_LOK: set_color(index, HSV_CYAN); break; case KC_BSPC: case KC_DELETE: set_color(index, HSV_PURPLE); break; case AL_SPC: case AL_ENT: case TD_ENTR: set_color(index, HSV_BLUE); break; case QK_RGB_MATRIX_VALUE_UP: case QK_RGB_MATRIX_VALUE_DOWN: set_color(index, HSV_SPRINGGREEN); break; case OS_LCTL: case OS_RCTL: set_color(index, 55, 255, 255); break; } } } return false; }