summaryrefslogtreecommitdiff
path: root/keyboards/crkbd/keymaps/rpbaptist/rgb.c
blob: 7f87d0a9eaa438d0f2066ac02cff22169820e17e (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2023 Your Name (@rpbaptist)
// SPDX-License-Identifier: GPL-2.0-or-later

#include "rpbaptist.h"

uint32_t transport_user_config = 0;

void user_config_sync(uint8_t initiator2target_buffer_size, const void* initiator2target_buffer, uint8_t target2initiator_buffer_size, void* target2initiator_buffer) {
    if (initiator2target_buffer_size == sizeof(transport_user_config)) {
        memcpy(&transport_user_config, initiator2target_buffer, initiator2target_buffer_size);
    }
}

#ifdef OLED_ENABLE
const char* rgb_matrix_anim_oled_text(uint8_t mode) {
    switch (mode) {
        case RGB_MATRIX_TYPING_HEATMAP:
            return PSTR("Heat ");
        case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
            return PSTR("Nexus");
        case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
            return PSTR("Ease ");
        case RGB_MATRIX_SOLID_COLOR:
            return PSTR("Solid");
        case RGB_MATRIX_BREATHING:
            return PSTR("Fade ");
        case RGB_MATRIX_CYCLE_ALL:
            return PSTR("Cycle");
        case RGB_MATRIX_RAINBOW_PINWHEELS:
            return PSTR("Wheel");
        case RGB_MATRIX_CYCLE_LEFT_RIGHT:
            return PSTR("Wave ");
        default:
            return PSTR("");
    }
}
#endif

extern led_config_t g_led_config;

void rgb_matrix_layer_helper(uint8_t hue, uint8_t sat, uint8_t val, uint8_t led_min, uint8_t led_max) {
    HSV hsv = {hue, sat, val};
    if (hsv.v > rgb_matrix_get_val()) {
        hsv.v = rgb_matrix_get_val();
    }

    RGB rgb = hsv_to_rgb(hsv);
    for (uint8_t i = 0; i < led_max; i++) {
        if (HAS_FLAGS(g_led_config.flags[i], LED_FLAG_UNDERGLOW)) {
            rgb_matrix_set_color(i, rgb.r, rgb.g, rgb.b);
        }
    }
}

extern user_config_t user_config;

bool rgb_matrix_in_idle(void) {
    return (user_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == user_config.rgb_matrix_idle_mode);
}

bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
    if (user_config.rgb_layer_indicator && !rgb_matrix_in_idle()) {
        switch (get_highest_layer(layer_state | default_layer_state)) {
            case _GAMING_EXT:
                rgb_matrix_layer_helper(HSV_PURPLE, led_min, led_max);
                break;
            case _SYM:
                rgb_matrix_layer_helper(HSV_YELLOW, led_min, led_max);
                break;
            case _NAV:
                rgb_matrix_layer_helper(HSV_SPRINGGREEN, led_min, led_max);
                break;
            case _UTIL:
                rgb_matrix_layer_helper(HSV_PINK, led_min, led_max);
                break;
            case _NUMPAD:
                rgb_matrix_layer_helper(HSV_CORAL, led_min, led_max);
                break;
            case _GAMING:
            case _WASD:
                rgb_matrix_layer_helper(HSV_RED, led_min, led_max);
                break;
            default:
                rgb_matrix_layer_helper(THEME_HSV, led_min, led_max);
                break;
        }
    }
    return false;
}

void rgb_matrix_turn_off_underglow(void) {
    rgb_matrix_layer_helper(0, 0, 0, 0, 54);
}

uint8_t rgb_matrix_speed_for_mode(uint8_t mode) {
    switch (mode) {
        case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
        case RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS:
            return RGB_MATRIX_ANIMATION_SPEED_FAST;
        case RGB_MATRIX_BREATHING:
        case RGB_MATRIX_CYCLE_LEFT_RIGHT:
        case RGB_MATRIX_RAINBOW_PINWHEELS:
            return RGB_MATRIX_ANIMATION_SPEED_SLOW;
        case RGB_MATRIX_CYCLE_ALL:
            return RGB_MATRIX_ANIMATION_SPEED_SLOWER;
        default:
            return RGB_MATRIX_ANIMATION_SPEED_MEDIUM;
    }
}

bool rgb_matrix_mode_active(uint8_t mode) {
    return (mode == RGB_MATRIX_SOLID_REACTIVE_SIMPLE || mode == RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS || mode == RGB_MATRIX_TYPING_HEATMAP);
}

void rgb_matrix_update_current_mode(uint8_t mode) {
    rgb_matrix_config.speed = rgb_matrix_speed_for_mode(mode);
    rgb_matrix_mode_noeeprom(mode);
}

void rgb_matrix_update_dynamic_mode(uint8_t mode) {
    uint8_t speed;
    speed = rgb_matrix_speed_for_mode(mode);

    if (rgb_matrix_mode_active(mode)) {
        user_config.rgb_matrix_active_speed = speed;
        user_config.rgb_matrix_active_mode  = mode;
    } else {
        user_config.rgb_matrix_idle_speed = speed;
        user_config.rgb_matrix_idle_mode  = mode;
    }
}

void rgb_matrix_update_mode(uint8_t mode) {
    if (user_config.rgb_matrix_idle_anim) {
        rgb_matrix_update_dynamic_mode(mode);
    }
    if (rgb_matrix_mode_active(mode) || !user_config.rgb_matrix_idle_anim) {
        rgb_matrix_update_current_mode(mode);
    }
}

uint8_t get_rgb_matrix_active_mode(void) {
    if (user_config.rgb_matrix_idle_anim) {
        return user_config.rgb_matrix_active_mode;
    } else {
        return rgb_matrix_get_mode();
    }
}

void rgb_matrix_toggle_active_mode(void) {
    switch (get_rgb_matrix_active_mode()) {
        case RGB_MATRIX_SOLID_REACTIVE_SIMPLE:
            rgb_matrix_update_mode(RGB_MATRIX_TYPING_HEATMAP);
            break;
        case RGB_MATRIX_TYPING_HEATMAP:
            rgb_matrix_update_mode(RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS);
            break;
        default:
            rgb_matrix_update_mode(RGB_MATRIX_SOLID_REACTIVE_SIMPLE);
            break;
    }
}

uint8_t get_rgb_matrix_idle_mode(void) {
    if (user_config.rgb_matrix_idle_anim) {
        return user_config.rgb_matrix_idle_mode;
    } else {
        return rgb_matrix_get_mode();
    }
}

void rgb_matrix_toggle_simple_passive_mode(void) {
    switch (get_rgb_matrix_idle_mode()) {
        case RGB_MATRIX_SOLID_COLOR:
            rgb_matrix_update_mode(RGB_MATRIX_BREATHING);
            break;
        case RGB_MATRIX_BREATHING:
            rgb_matrix_update_mode(RGB_MATRIX_CYCLE_ALL);
            break;
        default:
            rgb_matrix_update_mode(RGB_MATRIX_SOLID_COLOR);
            break;
    }
}

void rgb_matrix_toggle_color_passive_mode(void) {
    switch (get_rgb_matrix_idle_mode()) {
        case RGB_MATRIX_RAINBOW_PINWHEELS:
            rgb_matrix_update_mode(RGB_MATRIX_CYCLE_LEFT_RIGHT);
            break;
        default:
            rgb_matrix_update_mode(RGB_MATRIX_RAINBOW_PINWHEELS);
            break;
    }
}

void rgb_matrix_toggle_underglow_layer_indicator(void) {
    user_config.rgb_layer_indicator ^= 1;
    if (user_config.rgb_layer_indicator) {
        layer_state_set(layer_state); // This is needed to immediately set the layer color (looks better)
    } else {
        rgb_matrix_turn_off_underglow();
    }
}

void rgb_matrix_toggle_idle_animation_change(void) {
    user_config.rgb_matrix_idle_anim ^= 1;
    if (user_config.rgb_matrix_idle_anim) {
        rgb_matrix_update_mode(user_config.rgb_matrix_active_mode);
    } else {
        rgb_matrix_update_current_mode(user_config.rgb_matrix_idle_mode);
    }
}

void rgb_matrix_set_gaming_defaults(void) {
    if (!user_config.rgb_layer_indicator) {
        user_config.rgb_layer_indicator = true;
    }
    user_config.rgb_matrix_idle_timeout = GAMING_IDLE_TIMEOUT;
    rgb_matrix_update_mode(RGB_MATRIX_RAINBOW_PINWHEELS);
    rgb_matrix_update_mode(RGB_MATRIX_SOLID_REACTIVE_MULTINEXUS);
}

void rgb_matrix_set_typing_defaults(void) {
    user_config.rgb_matrix_idle_timeout = IDLE_TIMEOUT;
    rgb_matrix_update_mode(RGB_MATRIX_TYPING_PASSIVE);
    rgb_matrix_update_mode(RGB_MATRIX_TYPING_ACTIVE);
}

void rgb_matrix_set_defaults(void) {
    rgb_matrix_enable_noeeprom();
    rgb_matrix_sethsv_noeeprom(THEME_HSV);

    user_config.rgb_layer_indicator  = true;
    user_config.rgb_matrix_idle_anim = true;

    rgb_matrix_set_typing_defaults();

    rgb_matrix_mode_noeeprom(RGB_MATRIX_SOLID_REACTIVE_SIMPLE);
}

void matrix_scan_rgb(void) {
    if (user_config.rgb_matrix_idle_anim && rgb_matrix_get_mode() == user_config.rgb_matrix_active_mode && last_input_activity_elapsed() > user_config.rgb_matrix_idle_timeout) {
        if (user_config.rgb_layer_indicator) {
            rgb_matrix_turn_off_underglow();
        }
        rgb_matrix_update_current_mode(user_config.rgb_matrix_idle_mode);
    }
}

void user_transport_sync(void) {
    // Keep track of the last state, so that we can tell if we need to propagate to slave
    static uint32_t last_config = 0, last_sync = 0;

    // Check if the state values are different
    // or if sync timer elapsed
    if (memcmp(&user_config, &last_config, sizeof(transport_user_config)) || (timer_elapsed32(last_sync) > 250)) {
        memcpy(&last_config, &user_config, sizeof(transport_user_config));

        if (transaction_rpc_send(USER_CONFIG_SYNC, sizeof(transport_user_config), &transport_user_config)) {
            last_sync = timer_read32();
        }
    }
}

void user_transport_update(void) {
    if (is_keyboard_master()) {
        transport_user_config = user_config.raw;
        user_transport_sync();
    } else {
        user_config.raw = transport_user_config;
    }
}

void housekeeping_task_user(void) {
    static bool has_ran_yet;
    if (!has_ran_yet) {
        has_ran_yet = true;
        startup_user();
    }
    matrix_scan_rgb();

    // Update config to slave
    user_transport_update();
}

void eeconfig_init_user(void) {
    user_config.raw                  = 0;
    user_config.rgb_layer_indicator  = true;
    user_config.rgb_matrix_idle_anim = true;

    rgb_matrix_enable();
    rgb_matrix_sethsv(THEME_HSV);
    rgb_matrix_mode(RGB_MATRIX_SOLID_REACTIVE_SIMPLE);

    eeconfig_update_user(user_config.raw);
}

void keyboard_post_init_user(void) {
    set_single_persistent_default_layer(_COLEMAKDH);
    rgb_matrix_set_defaults();

    // Register user config sync
    transaction_register_rpc(USER_CONFIG_SYNC, user_config_sync);
}

bool process_record_user_rgb_matrix(uint16_t keycode, keyrecord_t* record) {
    if (user_config.rgb_matrix_idle_anim) {
        if (rgb_matrix_get_mode() == user_config.rgb_matrix_idle_mode) {
            rgb_matrix_update_current_mode(user_config.rgb_matrix_active_mode);
            if (!user_config.rgb_layer_indicator) {
                rgb_matrix_turn_off_underglow();
            }
        }
    }

    switch (keycode) {
        case RGB_RST:
            if (record->event.pressed) {
                rgb_matrix_set_defaults();
            }
            break;
        case RGB_UND: // Toggle separate underglow status
            if (record->event.pressed) {
                rgb_matrix_toggle_underglow_layer_indicator();
            }
            break;
        case RGB_IDL: // Toggle idle/heatmap animation
            if (record->event.pressed) {
                rgb_matrix_toggle_idle_animation_change();
            }
            break;
        case RGB_ATG:
            if (record->event.pressed) {
                rgb_matrix_toggle_active_mode();
            }
            break;
        case RGB_PST:
            if (record->event.pressed) {
                rgb_matrix_toggle_simple_passive_mode();
            }
            break;
        case RGB_PCT:
            if (record->event.pressed) {
                rgb_matrix_toggle_color_passive_mode();
            }
            break;
    }
    return true;
}