From d4610906bf9dad0d9d81933d931bc079d0f67528 Mon Sep 17 00:00:00 2001 From: CoffeeIsLife <36961653+CoffeeIsLife87@users.noreply.github.com> Date: Fri, 7 Jul 2023 09:34:01 -0500 Subject: [PATCH] Add a turbo feature to coffee keymap (#21291) Co-authored-by: Fae --- .../bm40hsrgb/keymaps/coffee/config.h | 3 + .../bm40hsrgb/keymaps/coffee/keymap.c | 135 +++++++++++++++++- .../bm40hsrgb/keymaps/coffee/readme.md | 14 +- 3 files changed, 145 insertions(+), 7 deletions(-) diff --git a/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/config.h b/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/config.h index e8f6fac120..ca8a4c6ab4 100644 --- a/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/config.h +++ b/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/config.h @@ -5,6 +5,9 @@ #define LAYER_STATE_8BIT +#define TURBO_ENABLE +#define HOLD_ON_OTHER_KEY_PRESS + // RGB Matrix effects disable #ifdef RGB_MATRIX_ENABLE # define UNDERGLOW_DISABLE diff --git a/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/keymap.c b/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/keymap.c index ef7cfab5fe..3d5bcade20 100644 --- a/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/keymap.c +++ b/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/keymap.c @@ -50,6 +50,26 @@ void dynamic_macro_record_end_user(int8_t direction) { #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, @@ -67,7 +87,7 @@ const uint16_t PROGMEM keymaps[_END][MATRIX_ROWS][MATRIX_COLS] = { 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, XXXXXXX, XXXXXXX, TOG_MSE, _______, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX + XXXXXXX, XXXXXXX, XXXXXXX, TRBO_TI, TRBO_ST, TOG_MSE, _______, XXXXXXX, XXXXXXX, XXXXXXX, XXXXXXX ) #ifdef MOUSEKEY_ENABLE ,[_MSE] = LAYOUT_planck_mit( @@ -117,12 +137,68 @@ bool rgb_matrix_indicators_advanced_user(uint8_t led_min, uint8_t led_max) { 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;} @@ -145,6 +221,23 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { } #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: @@ -162,6 +255,46 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { 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 } diff --git a/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/readme.md b/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/readme.md index 2798567b5e..980f559d71 100644 --- a/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/readme.md +++ b/keyboards/kprepublic/bm40hsrgb/keymaps/coffee/readme.md @@ -21,12 +21,12 @@ The secondary layer ( `_SUB` ) is for syntax, F keys, and number keys | 4 | _ | _ | | _ | _ | _ | <- | _ | | | | | The control layer ( `_CTR` ) is for lighting, media, and macros -| row | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -|-----|---|-------|------|-------|------|---------|-----|------|------|------|----|---------| -| 1 | | SPD- | BRI+ | SPD+ | HUE+ | SAT+ | | | VOL+ | | | MCR_REC | -| 2 | | EFCT- | BRI- | EFCT+ | HUE- | SAT- | | PREV | PLAY | NEXT | | MCR_PLY | -| 3 | | | TOG | | | | | | VOL- | | | MCR_SWT | -| 4 | | | | | | MSE_TOG | <- | | | | | | +| row | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | +|-----|---|-------|------|---------|---------|---------|-----|------|------|------|----|---------| +| 1 | | SPD- | BRI+ | SPD+ | HUE+ | SAT+ | | | VOL+ | | | MCR_REC | +| 2 | | EFCT- | BRI- | EFCT+ | HUE- | SAT- | | PREV | PLAY | NEXT | | MCR_PLY | +| 3 | | | TOG | | | | | | VOL- | | | MCR_SWT | +| 4 | | | | TRBO_TI | TRBO_ST | MSE_TOG | <- | | | | | | The mouse layer ( `_MSE` ) is for making the keyboard function as a mouse as well | row | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | @@ -67,6 +67,8 @@ The mouse layer ( `_MSE` ) is for making the keyboard function as a mouse as wel | SCR_D | Mouse scroll down | | ACL_U | Raise mouse/scroll speed | | ACL_D | Lower mouse/scroll speed | +| TRBO_ST | Set turbo key (Hold), start/stop turbo (Tap) | +| TRBO_TI | Turbo speed +/- (Hold to switch direction) | ## Compiling