aboutsummaryrefslogtreecommitdiff
path: root/qmk/keyboards/sofle_choc/keymaps/custom/lighting.c
blob: 525d0c44133319225b66516efb1d79e67e64c48d (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
#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 _) {
  HSV hsv = {h, s, RGB_MATRIX_MAXIMUM_BRIGHTNESS};
  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 > 100 ? 100: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;

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
        hsv_of_color(HSV_RED),   // 010
        hsv_of_color(HSV_ORANGE),// 011
        hsv_of_color(HSV_CYAN),  // 100
        hsv_of_color(HSV_GREEN), // 101
        hsv_of_color(HSV_PURPLE),// 110
        hsv_of_color(HSV_WHITE), // 111
    };

    uint8_t counter = 0;
    uint8_t flags = 0;
    if (get_mods() & MOD_MASK_CTRL) {
        flags |= 0b001;
        counter += 1;
    }
    if (get_mods() & MOD_MASK_ALT) {
        flags |= 0b010;
        counter += 1;
    }
    if (get_mods() & MOD_MASK_SHIFT) {
        flags |= 0b100;
        counter += 1;
    }

    if (flags == last_flags) {
        return;
    }
    last_flags = flags;

    if (get_mods()) {
        if (rgb_matrix_get_mode() != RGB_MATRIX_SOLID_COLOR) {
            rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_COLOR);
        }
        uint8_t v = colors[flags].v;
        if (counter >= 2)
            v *= 2;
        rgb_matrix_sethsv_noeeprom(colors[flags].h, colors[flags].s, v);
    } else if (rgb_matrix_get_mode() != RGB_MATRIX_SOLID_REACTIVE_SIMPLE) {
        rgb_matrix_sethsv_noeeprom(HSV_WHITE);
        rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_REACTIVE_SIMPLE);
    }
}

#define KEY_5      MT(MOD_LCTL, KC_P5)
#define KEY_DOWN   MT(MOD_RCTL, KC_DOWN)
bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
    // Activate effects depending of mods

    if (is_caps_word_on()) {
        if (rgb_matrix_get_mode() != RGB_MATRIX_BREATHING) {
            rgb_matrix_mode_noeeprom(RGB_MATRIX_BREATHING);
        }
        rgb_matrix_sethsv_noeeprom(HSV_SPRINGGREEN);
        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) {
                rgb_matrix_set_color(index, 100, 100, 0);
            }

            if (layer == 0)
                continue;


            switch (keycode) {
                case KC_F1 ... KC_F11:
                    rgb_matrix_set_color(index, 128, 128, 128);
                    break;
                case KC_P1 ... KC_P0:
                case KEY_5:
                    rgb_matrix_set_color(index, 100, 100, 0);
                    break;
                case KC_PDOT:
                    // Add a bit of red for the punction symbol
                    rgb_matrix_set_color(index, 150, 50, 0);
                    break;
                case KC_RIGHT:
                case KEY_DOWN:
                case KC_LEFT ... KC_UP:
                case KC_PGUP:
                case KC_PGDN:
                    rgb_matrix_set_color(index, 32, 32, 128);
                    break;
                case KC_HOME:
                case KC_END:
                    rgb_matrix_set_color(index, RGB_SPRINGGREEN);
                    break;
                case KC_BSPC:
                case AL_SPC:
                case AL_ENT:
                    set_color(index, HSV_BLUE);
                    break;

            }
        }
    }

    return false;
}