2020-10-17 18:46:57 +00:00
|
|
|
/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-23 18:57:24 +00:00
|
|
|
#ifdef RGBLIGHT_ENABLE
|
|
|
|
|
|
|
|
# include "drashna.h"
|
|
|
|
# include "rgb_stuff.h"
|
|
|
|
# include "eeprom.h"
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2021-02-23 18:57:24 +00:00
|
|
|
bool has_initialized;
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2020-06-15 10:41:28 +00:00
|
|
|
void rgblight_sethsv_default_helper(uint8_t index) { rgblight_sethsv_at(rgblight_get_hue(), rgblight_get_sat(), rgblight_get_val(), index); }
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2021-02-23 18:57:24 +00:00
|
|
|
# ifdef RGBLIGHT_TWINKLE
|
2018-06-01 05:11:06 +00:00
|
|
|
static rgblight_fadeout lights[RGBLED_NUM];
|
|
|
|
|
2019-09-07 15:57:30 +00:00
|
|
|
/* Handler for fading/twinkling effect */
|
2019-07-23 03:22:33 +00:00
|
|
|
void scan_rgblight_fadeout(void) { // Don't effing change this function .... rgblight_sethsv is supppppper intensive
|
2019-05-07 05:34:09 +00:00
|
|
|
bool litup = false;
|
2020-01-06 09:49:49 +00:00
|
|
|
|
2019-07-23 03:22:33 +00:00
|
|
|
for (uint8_t light_index = 0; light_index < RGBLED_NUM; ++light_index) {
|
2021-05-25 16:24:01 +00:00
|
|
|
if (lights[light_index].enabled && sync_timer_elapsed(lights[light_index].timer) > 10) {
|
2019-07-23 03:22:33 +00:00
|
|
|
rgblight_fadeout *light = &lights[light_index];
|
|
|
|
litup = true;
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2019-07-23 03:22:33 +00:00
|
|
|
if (light->life) {
|
|
|
|
light->life -= 1;
|
2019-11-20 20:37:24 +00:00
|
|
|
if (get_highest_layer(layer_state) == 0) {
|
2019-07-23 03:22:33 +00:00
|
|
|
sethsv(light->hue + rand() % 0xF, 255, light->life, (LED_TYPE *)&led[light_index]);
|
|
|
|
}
|
2021-05-25 16:24:01 +00:00
|
|
|
light->timer = sync_timer_read();
|
2019-07-23 03:22:33 +00:00
|
|
|
} else {
|
2019-11-20 20:37:24 +00:00
|
|
|
if (light->enabled && get_highest_layer(layer_state) == 0) {
|
2019-07-23 03:22:33 +00:00
|
|
|
rgblight_sethsv_default_helper(light_index);
|
|
|
|
}
|
|
|
|
litup = light->enabled = false;
|
2019-05-07 05:34:09 +00:00
|
|
|
}
|
2018-06-01 05:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-20 20:37:24 +00:00
|
|
|
if (litup && get_highest_layer(layer_state) == 0) {
|
2019-05-07 05:34:09 +00:00
|
|
|
rgblight_set();
|
|
|
|
}
|
2018-06-01 05:11:06 +00:00
|
|
|
}
|
|
|
|
|
2019-09-07 15:57:30 +00:00
|
|
|
/* Triggers a LED to fade/twinkle.
|
|
|
|
* This function handles the selection of the LED and prepres for it to be used.
|
|
|
|
*/
|
2018-06-01 05:11:06 +00:00
|
|
|
void start_rgb_light(void) {
|
|
|
|
uint8_t indices[RGBLED_NUM];
|
2019-07-23 03:22:33 +00:00
|
|
|
uint8_t indices_count = 0;
|
|
|
|
uint8_t min_life = 0xFF;
|
2018-06-01 05:11:06 +00:00
|
|
|
uint8_t min_life_index = -1;
|
2020-01-06 09:49:49 +00:00
|
|
|
|
2019-07-23 03:22:33 +00:00
|
|
|
for (uint8_t index = 0; index < RGBLED_NUM; ++index) {
|
|
|
|
if (lights[index].enabled) {
|
|
|
|
if (min_life_index == -1 || lights[index].life < min_life) {
|
|
|
|
min_life = lights[index].life;
|
|
|
|
min_life_index = index;
|
|
|
|
}
|
|
|
|
continue;
|
2018-06-01 05:11:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 03:22:33 +00:00
|
|
|
indices[indices_count] = index;
|
|
|
|
++indices_count;
|
2018-06-01 05:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t light_index;
|
|
|
|
if (!indices_count) {
|
|
|
|
light_index = min_life_index;
|
2019-07-23 03:22:33 +00:00
|
|
|
} else {
|
|
|
|
light_index = indices[rand() % indices_count];
|
2018-06-01 05:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rgblight_fadeout *light = &lights[light_index];
|
2019-07-23 03:22:33 +00:00
|
|
|
light->enabled = true;
|
2021-05-25 16:24:01 +00:00
|
|
|
light->timer = sync_timer_read();
|
2019-07-23 03:22:33 +00:00
|
|
|
light->life = 0xC0 + rand() % 0x40;
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2020-06-15 10:41:28 +00:00
|
|
|
light->hue = rgblight_get_hue() + (rand() % 0xB4) - 0x54;
|
2018-06-01 05:11:06 +00:00
|
|
|
|
|
|
|
rgblight_sethsv_at(light->hue, 255, light->life, light_index);
|
|
|
|
}
|
2021-02-23 18:57:24 +00:00
|
|
|
# endif
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2020-03-31 23:26:43 +00:00
|
|
|
bool process_record_user_rgb_light(uint16_t keycode, keyrecord_t *record) {
|
2019-09-07 15:57:30 +00:00
|
|
|
uint16_t temp_keycode = keycode;
|
|
|
|
// Filter out the actual keycode from MT and LT keys.
|
2019-05-07 05:34:09 +00:00
|
|
|
if ((keycode >= QK_MOD_TAP && keycode <= QK_MOD_TAP_MAX) || (keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX)) {
|
2019-09-07 15:57:30 +00:00
|
|
|
temp_keycode &= 0xFF;
|
2019-05-07 05:34:09 +00:00
|
|
|
}
|
2019-09-07 15:57:30 +00:00
|
|
|
|
|
|
|
switch (temp_keycode) {
|
2021-02-23 18:57:24 +00:00
|
|
|
# ifdef RGBLIGHT_TWINKLE
|
2019-05-07 05:34:09 +00:00
|
|
|
case KC_A ... KC_SLASH:
|
|
|
|
case KC_F1 ... KC_F12:
|
|
|
|
case KC_INSERT ... KC_UP:
|
|
|
|
case KC_KP_SLASH ... KC_KP_DOT:
|
|
|
|
case KC_F13 ... KC_F24:
|
|
|
|
case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
|
2019-07-23 03:22:33 +00:00
|
|
|
if (record->event.pressed) {
|
|
|
|
start_rgb_light();
|
|
|
|
}
|
|
|
|
break;
|
2021-02-23 18:57:24 +00:00
|
|
|
# endif // RGBLIGHT_TWINKLE
|
2019-07-23 03:22:33 +00:00
|
|
|
}
|
2018-06-01 05:11:06 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-23 18:57:24 +00:00
|
|
|
# if defined(RGBLIGHT_STARTUP_ANIMATION)
|
|
|
|
static bool is_enabled;
|
|
|
|
static bool is_rgblight_startup;
|
|
|
|
static uint8_t old_hue;
|
2020-06-15 10:41:28 +00:00
|
|
|
static uint16_t rgblight_startup_loop_timer;
|
2021-02-23 18:57:24 +00:00
|
|
|
# endif
|
2020-06-15 10:41:28 +00:00
|
|
|
|
2020-03-31 23:26:43 +00:00
|
|
|
void keyboard_post_init_rgb_light(void) {
|
2021-02-23 18:57:24 +00:00
|
|
|
# if defined(RGBLIGHT_STARTUP_ANIMATION)
|
2020-06-15 10:41:28 +00:00
|
|
|
is_enabled = rgblight_is_enabled();
|
2019-07-23 03:22:33 +00:00
|
|
|
if (userspace_config.rgb_layer_change) {
|
|
|
|
rgblight_enable_noeeprom();
|
|
|
|
}
|
2020-06-15 10:41:28 +00:00
|
|
|
if (rgblight_is_enabled()) {
|
2020-03-31 23:26:43 +00:00
|
|
|
layer_state_set_rgb_light(layer_state);
|
2020-06-15 10:41:28 +00:00
|
|
|
old_hue = rgblight_get_hue();
|
2019-05-07 05:34:09 +00:00
|
|
|
rgblight_mode_noeeprom(RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-06-15 10:41:28 +00:00
|
|
|
is_rgblight_startup = true;
|
2019-04-22 18:55:55 +00:00
|
|
|
}
|
2021-02-23 18:57:24 +00:00
|
|
|
# endif
|
2020-03-31 23:26:43 +00:00
|
|
|
layer_state_set_rgb_light(layer_state);
|
2019-02-17 02:28:04 +00:00
|
|
|
}
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2020-03-31 23:26:43 +00:00
|
|
|
void matrix_scan_rgb_light(void) {
|
2019-09-07 15:57:30 +00:00
|
|
|
# ifdef RGBLIGHT_TWINKLE
|
2019-05-07 05:34:09 +00:00
|
|
|
scan_rgblight_fadeout();
|
2019-09-07 15:57:30 +00:00
|
|
|
# endif // RGBLIGHT_ENABLE
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2021-02-23 18:57:24 +00:00
|
|
|
# if defined(RGBLIGHT_STARTUP_ANIMATION)
|
2020-06-15 10:41:28 +00:00
|
|
|
if (is_rgblight_startup && is_keyboard_master()) {
|
2021-05-25 16:24:01 +00:00
|
|
|
if (sync_timer_elapsed(rgblight_startup_loop_timer) > 10) {
|
2020-06-15 10:41:28 +00:00
|
|
|
static uint8_t counter;
|
|
|
|
counter++;
|
|
|
|
rgblight_sethsv_noeeprom((counter + old_hue) % 255, 255, 255);
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_startup_loop_timer = sync_timer_read();
|
2020-06-15 10:41:28 +00:00
|
|
|
if (counter == 255) {
|
|
|
|
is_rgblight_startup = false;
|
|
|
|
if (!is_enabled) {
|
|
|
|
rgblight_disable_noeeprom();
|
|
|
|
}
|
|
|
|
if (userspace_config.rgb_layer_change) {
|
|
|
|
layer_state_set_rgb_light(layer_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-23 18:57:24 +00:00
|
|
|
# endif
|
2018-06-01 05:11:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:37:24 +00:00
|
|
|
void rgblight_set_hsv_and_mode(uint8_t hue, uint8_t sat, uint8_t val, uint8_t mode) {
|
|
|
|
rgblight_sethsv_noeeprom(hue, sat, val);
|
2020-10-17 18:46:57 +00:00
|
|
|
// wait_us(175); // Add a slight delay between color and mode to ensure it's processed correctly
|
2019-11-20 20:37:24 +00:00
|
|
|
rgblight_mode_noeeprom(mode);
|
|
|
|
}
|
|
|
|
|
2020-03-31 23:26:43 +00:00
|
|
|
layer_state_t layer_state_set_rgb_light(layer_state_t state) {
|
2021-02-23 18:57:24 +00:00
|
|
|
# ifdef RGBLIGHT_ENABLE
|
2019-05-07 05:34:09 +00:00
|
|
|
if (userspace_config.rgb_layer_change) {
|
2021-02-23 18:57:24 +00:00
|
|
|
switch (get_highest_layer(state | default_layer_state)) {
|
2021-05-25 16:24:01 +00:00
|
|
|
case _MACROS: // mouse
|
|
|
|
if (!layer_state_cmp(state, _GAMEPAD) && !layer_state_cmp(state, _DIABLO)) {
|
|
|
|
rgblight_set_hsv_and_mode(HSV_CHARTREUSE, RGBLIGHT_MODE_BREATHING + 3);
|
|
|
|
}
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
|
|
|
case _MEDIA:
|
2019-11-20 20:37:24 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_CHARTREUSE, RGBLIGHT_MODE_KNIGHT + 1);
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
|
|
|
case _GAMEPAD:
|
2019-11-20 20:37:24 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_ORANGE, RGBLIGHT_MODE_SNAKE + 2);
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
|
|
|
case _DIABLO:
|
2019-11-20 20:37:24 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_BREATHING + 3);
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
|
|
|
case _RAISE:
|
2019-11-20 20:37:24 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_YELLOW, RGBLIGHT_MODE_BREATHING + 3);
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
|
|
|
case _LOWER:
|
2019-11-20 20:37:24 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_GREEN, RGBLIGHT_MODE_BREATHING + 3);
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
|
|
|
case _ADJUST:
|
2019-11-20 20:37:24 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_RED, RGBLIGHT_MODE_KNIGHT + 2);
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
2020-10-17 18:46:57 +00:00
|
|
|
case _COLEMAK:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_MAGENTA, RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-10-17 18:46:57 +00:00
|
|
|
break;
|
|
|
|
case _DVORAK:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_SPRINGGREEN, RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-10-17 18:46:57 +00:00
|
|
|
break;
|
|
|
|
case _WORKMAN:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_GOLDENROD, RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-10-17 18:46:57 +00:00
|
|
|
break;
|
|
|
|
case _NORMAN:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_CORAL, RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-10-17 18:46:57 +00:00
|
|
|
break;
|
|
|
|
case _MALTRON:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_YELLOW, RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-10-17 18:46:57 +00:00
|
|
|
break;
|
|
|
|
case _EUCALYN:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_PINK, RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-10-17 18:46:57 +00:00
|
|
|
break;
|
|
|
|
case _CARPLAX:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_BLUE, RGBLIGHT_MODE_STATIC_LIGHT);
|
2020-10-17 18:46:57 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-05-25 16:24:01 +00:00
|
|
|
rgblight_set_hsv_and_mode(HSV_CYAN, RGBLIGHT_MODE_STATIC_LIGHT);
|
2019-05-07 05:34:09 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-06-01 05:11:06 +00:00
|
|
|
}
|
2021-02-23 18:57:24 +00:00
|
|
|
# endif // RGBLIGHT_ENABLE
|
2018-06-01 05:11:06 +00:00
|
|
|
|
2019-05-07 05:34:09 +00:00
|
|
|
return state;
|
2018-11-27 00:45:24 +00:00
|
|
|
}
|
2021-02-23 18:57:24 +00:00
|
|
|
|
|
|
|
#endif
|