summaryrefslogtreecommitdiff
path: root/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/keymap.c
blob: 3d5bcade20f6b79946648894869ce8aca8fb5f6d (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
// Copyright 2022 Fae Fankhauser (@CoffeeIsLife87)
// SPDX-License-Identifier: GPL-2.0-or-later

#include QMK_KEYBOARD_H

enum custom_keycodes {
    MCR_PLY = QK_USER, // Macro play
    MCR_REC,           // Macro record
    MCR_SWT,           // Swap active macro
    MS_ACL_U,          // Mouse speed up
    MS_ACL_D,          // Mouse speed down
};

enum layout_names {
    _MAIN = 0,       // Keys Layout: The main keyboard layout that has all the characters
    _SUB,            // Extension to Main
    _CTR,            // Macros, RGB, Audio controls, layer access. More or less the control center of my keyboard
    #ifdef MOUSEKEY_ENABLE
    _MSE,
    #endif // MOUSEKEY_ENABLE
    _END,
};

#ifdef MOUSEKEY_ENABLE
#include "action.h"
#define TOG_MSE TG(_MSE)
static int current_accel = 0;
#else
#define TOG_MSE XXXXXXX
#endif // MOUSEKEY_ENABLE

#ifdef DYNAMIC_MACRO_ENABLE
// Macro 1 is = 1, Macro 2 = -1, No macro = 0
static bool MACRO1 = true; // Determines whether or not we're using 1 or 2
static bool RECORDING = false;
static uint16_t REC = DM_REC1; // Record either macro 1 or 2. Or stop recording
static uint16_t PLY = DM_PLY1; // Play either macro 1 or macro 2

void dynamic_macro_record_start_user(int8_t direction) {
    REC = DM_RSTP;
    RECORDING = true;
}

void dynamic_macro_record_end_user(int8_t direction) {
    RECORDING = false;
}
#else
#define MCR_PLY KC_NO
#define MCR_REC KC_NO
#define MCR_SWT KC_NO
#endif // DYNAMIC_MACRO_ENABLE

#ifdef TURBO_ENABLE
static bool turbo_active = false; // Only changed by TRBO_ST
static uint16_t turbo_keycode = KC_NO; // turbo_active can't be true if this is KC_NO
static bool turbo_key_pressed = false;
static bool turbo_keycode_selecting = false; // Determine new keycode or delay mode switch
static bool turbo_ms_delay_direction = true; // True is raise the delay, false is lower it
static uint16_t turbo_ms_delay_direction_indicator = 0;
static uint16_t turbo_ms_delay = 100; // Start at sending keycodes 10 times a second
static uint16_t turbo_timer; // Changed semi automatically
/*
 * Because the KC's are wrapped in `LT()` they won't be interpreted as KC_NO or KC_TRNS
 * Instead they'll just be like any other custom KC which will let me use them how I want 
*/
#define TRBO_ST LT(_CTR, KC_NO  )
#define TRBO_TI LT(_CTR, KC_TRNS)
#else
#define TRBO_ST KC_NO
#define TRBO_TI KC_NO
#endif

const uint16_t PROGMEM keymaps[_END][MATRIX_ROWS][MATRIX_COLS] = {
    [_MAIN] = LAYOUT_planck_mit(
        KC_ESC , KC_Q   , KC_W   , KC_E   , KC_R   , KC_T   , KC_Y   , KC_U   , KC_I   , KC_O   , KC_P   , KC_BSPC,
        KC_TAB , KC_A   , KC_S   , KC_D   , KC_F   , KC_G   , KC_H   , KC_J   , KC_K   , KC_L   , KC_SCLN, KC_QUOT,
        KC_LSFT, KC_Z   , KC_X   , KC_C   , KC_V   , KC_B   , KC_N   , KC_M   , KC_COMM, KC_DOT , KC_SLSH, KC_ENT ,
        KC_LCTL, KC_LGUI, XXXXXXX,KC_LALT ,MO(_SUB),      KC_SPC     ,MO(_CTR), KC_LEFT, KC_DOWN, KC_UP  , KC_RGHT
    ),
    [_SUB] = LAYOUT_planck_mit(
        KC_GRV , KC_1   , KC_2   , KC_3   , KC_4   , KC_5   , KC_6   , KC_7   , KC_8   , KC_9   , KC_0   , KC_DEL,
        XXXXXXX, KC_F1  , KC_F2  , KC_F3  , KC_F4  , KC_F5  , KC_F6  , KC_MINS, KC_EQL , KC_LBRC, KC_RBRC, KC_PGUP,
        _______, KC_F7  , KC_F8  , KC_F9  , KC_F10 , KC_F11 , KC_F12 , XXXXXXX, KC_HOME, KC_END , KC_BSLS, KC_PGDN,
        _______, _______, XXXXXXX, _______, _______,      _______    , _______, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX
    ),
    [_CTR] = LAYOUT_planck_mit(
        XXXXXXX, RGB_SPD, RGB_VAI, RGB_SPI, RGB_HUI, RGB_SAI, XXXXXXX, XXXXXXX, KC_VOLU, XXXXXXX, XXXXXXX, MCR_REC,
        XXXXXXX, RGB_RMOD,RGB_VAD, RGB_MOD, RGB_HUD, RGB_SAD, XXXXXXX, KC_MPRV, KC_MPLY, KC_MNXT, XXXXXXX, MCR_PLY,
        XXXXXXX, XXXXXXX, RGB_TOG, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, KC_VOLD, XXXXXXX, XXXXXXX, MCR_SWT,
        XXXXXXX, XXXXXXX, XXXXXXX, TRBO_TI, TRBO_ST,      TOG_MSE,     _______, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX
    )
    #ifdef MOUSEKEY_ENABLE
    ,[_MSE] = LAYOUT_planck_mit(
        XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
        XXXXXXX, KC_MS_L, KC_MS_D, KC_MS_U, KC_MS_R, XXXXXXX, XXXXXXX, KC_BTN1, KC_WH_D, KC_WH_U, KC_BTN2, XXXXXXX,
        XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX,
        XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX, MS_ACL_D,      TOG_MSE,    MS_ACL_U,XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX
    )
    #endif // MOUSEKEY_ENABLE
};

#ifdef RGB_MATRIX_ENABLE

#define LAYER (get_highest_layer(layer_state))
#define LAYER_SIZE (MATRIX_ROWS * MATRIX_COLS)
#define BRIGHTNESS rgb_matrix_get_val()

#ifdef UNDERGLOW_DISABLE
void keyboard_pre_init_user(void) {
    for (int key_id = 0; key_id < RGB_MATRIX_LED_COUNT; key_id++ ) {
        if (g_led_config.flags[key_id] == LED_FLAG_UNDERGLOW) {
            g_led_config.flags[key_id] = LED_FLAG_NONE;
        }
    }
}
#endif // UNDERGLOW_DISABLE

bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) {
    if (LAYER == _MAIN) {return false;}

    for (uint8_t i = led_min; i <= led_max; i++) {
        uint8_t pos = ((uint8_t*)g_led_config.matrix_co)[i];
        if (!HAS_ANY_FLAGS(g_led_config.flags[pos], (LED_FLAG_MODIFIER | LED_FLAG_KEYLIGHT))) {continue;}
        uint16_t KC = pgm_read_word(&((uint16_t*)keymaps)[(LAYER_SIZE * LAYER) + i]);

        switch (KC) {
            case KC_NO:
                RGB_MATRIX_INDICATOR_SET_COLOR(pos, 0, 0, 0 );
                break;

            #ifdef DYNAMIC_MACRO_ENABLE
            case MCR_SWT:
                if (!MACRO1) { RGB_MATRIX_INDICATOR_SET_COLOR(pos, 0, BRIGHTNESS, BRIGHTNESS); }
                break;

            case MCR_REC:
                if (RECORDING) { RGB_MATRIX_INDICATOR_SET_COLOR(pos, BRIGHTNESS, 0, 0); }
                break;
            #endif // DYNAMIC_MACRO_ENABLE

            #ifdef TURBO_ENABLE
            case TRBO_ST:
                if (turbo_keycode_selecting) {
                    RGB_MATRIX_INDICATOR_SET_COLOR(pos, BRIGHTNESS, BRIGHTNESS, BRIGHTNESS);
                } else {
                    if (turbo_active) {
                        if (turbo_key_pressed) {
                            RGB_MATRIX_INDICATOR_SET_COLOR(pos, BRIGHTNESS, BRIGHTNESS, BRIGHTNESS);
                        } else {
                            RGB_MATRIX_INDICATOR_SET_COLOR(pos, 0, 0, 0);
                        }
                    }
                }
                break;

            case TRBO_TI:
                if (turbo_ms_delay_direction_indicator != 0) {
                    uint8_t time = timer_elapsed(turbo_ms_delay_direction_indicator) / 200;
                    if (time > 0) {
                        if ((time % 2) == 1) {
                            if (turbo_ms_delay_direction) {
                                RGB_MATRIX_INDICATOR_SET_COLOR(pos, 0, BRIGHTNESS, 0);
                            } else {
                                RGB_MATRIX_INDICATOR_SET_COLOR(pos, BRIGHTNESS, 0, 0);
                            }
                        } else {
                            RGB_MATRIX_INDICATOR_SET_COLOR(pos, 0, 0, 0);
                        }
                        if (time >= 6) {
                            turbo_ms_delay_direction_indicator = 0;
                        }
                    }
                }
                break;
            #endif

        }
    }
    return false;
}
#endif

#ifdef TURBO_ENABLE
void housekeeping_task_user(void) {
    if (turbo_active) {
        if (turbo_key_pressed) {
            if (timer_elapsed(turbo_timer) > turbo_ms_delay) {
                unregister_code(turbo_keycode);
                turbo_key_pressed = false;
                turbo_timer = timer_read();
            }
        } else {
            if (timer_elapsed(turbo_timer) > (turbo_ms_delay / 2)) {
                register_code(turbo_keycode);
                turbo_key_pressed = true;
            }
        }
    }
}
#endif

bool process_record_user(uint16_t keycode, keyrecord_t *record) {
    #ifdef DYNAMIC_MACRO_ENABLE
    if (keycode == MCR_REC) {keycode = REC;}
    if (keycode == MCR_PLY) {keycode = PLY;}
    if (!process_dynamic_macro(keycode, record)) {
        REC = (MACRO1 ? DM_REC1 : DM_REC2);
        return false;
    }
    #endif // DYNAMIC_MACRO_ENABLE

    #ifdef MOUSEKEY_ENABLE
    if ((keycode == MS_ACL_U) || (keycode == MS_ACL_D)) {
        if (record->event.pressed) {
            if ( (keycode == MS_ACL_U) && (current_accel < 2) ) { current_accel += 1; }
            if ( (keycode == MS_ACL_D) && (current_accel > 0) ) { current_accel -= 1; }
        }
        keycode = KC_ACL0 + current_accel;
        action_t mousekey_action = action_for_keycode(keycode);
        process_action(record, mousekey_action);
    }
    #endif // MOUSEKEY_ENABLE

    #ifdef TURBO_ENABLE
    if (record->event.pressed) {
        if (turbo_keycode_selecting) {
            if (IS_BASIC_KEYCODE(keycode)    || 
                IS_MODIFIER_KEYCODE(keycode) ||
                IS_JOYSTICK_KEYCODE(keycode) ||
                IS_MOUSE_KEYCODE(keycode))
            {
                unregister_code(turbo_keycode);
                turbo_key_pressed = false;
                turbo_keycode = keycode;
                turbo_keycode_selecting = false;
            }
        }
    }
    #endif

    switch (keycode) {
        #ifdef DYNAMIC_MACRO_ENABLE
        case MCR_SWT:
            if (!RECORDING && record->event.pressed) {
                // if the button is pressed and we're not recording
                MACRO1 = !MACRO1;
                if (MACRO1) {
                    REC = DM_REC1;
                    PLY = DM_PLY1;
                } else {
                    REC = DM_REC2;
                    PLY = DM_PLY2;
                }
            }
            return false;
        #endif // DYNAMIC_MACRO_ENABLE

        #ifdef TURBO_ENABLE
        case TRBO_ST:
            if (record->event.pressed) {
                if (!record->tap.count) {
                    turbo_keycode_selecting = !turbo_keycode_selecting;
                } else {
                    if (turbo_keycode != KC_NO) {
                        if (!turbo_active) {
                            turbo_keycode_selecting = false;
                            turbo_active = true;
                        } else {
                            unregister_code(turbo_keycode);
                            turbo_key_pressed = false;
                            turbo_active = false;
                        }
                    }
                }
            }
            return false;

        case TRBO_TI:
            if (record->event.pressed) {
                if (!record->tap.count) {
                    turbo_ms_delay_direction = !turbo_ms_delay_direction;
                    turbo_ms_delay_direction_indicator = timer_read();
                } else {
                    if (turbo_ms_delay_direction)
                        turbo_ms_delay += 20;
                    else
                        turbo_ms_delay -= 20;

                    if (turbo_ms_delay > 500) // half a second
                        turbo_ms_delay = 500;
                    if (turbo_ms_delay < 60) // Around 16 times a second
                        turbo_ms_delay = 60;
                }
            }
            return false;
        #endif

        default:
            return true; //Process all other keycodes normally
    }
}