aboutsummaryrefslogtreecommitdiff
path: root/qmk/keyboards/sofle_choc/keymaps/custom/lighting.c
blob: df729ccdd0db10f0a067a50a908bbe9965275fc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#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;
}