From 8666c30a5274c7aebf8513479465303086206062 Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Mon, 17 Oct 2022 23:02:08 +0200 Subject: [PATCH] Improved mouse movement --- firmware/src/main.cpp | 98 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 78 insertions(+), 20 deletions(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index e0f6273..f199382 100755 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -36,21 +36,21 @@ * | Shift | Z | X | C | V | B | | N | M | , | . | - | Shift/Enter | * ----------------| M1/M2 | Alt | BSpc/Fn1 | | Spc/Fn1 | AltGr | Win |-------------------- * -------------------------- ------------------------- - * Layer 1 + * Layer 1 * ------------------------------------------ --------------------------------------------- * | Tab/Fn2 | F1 | F2 | F3 | F4 | F5 | | F6 | F7 | F8 | F9 | F10 | F11 | * | Ctrl/Esc | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 0 | + | * | Shift | § | Del | Esc | < | | | | ' | ¨ | ´ | | Shift/Enter | * ----------------| M1/M2 | Alt | BSpc/Fn1 | | Spc/Fn1 | AltGr | Win |-------------------- * -------------------------- ------------------------- - * Layer 2 + * Layer 2 * ------------------------------------------ --------------------------------------------- * | Tab/Fn2 | F12 | F13 | F14 | F15 | F16 | | ML | MD | MU | MR | | CapsLock | * | Ctrl/Esc | Prev| Play| Next| | | | Left| Down| Up |Right| | | * | Shift | | | | | | | Home| PgD | PgU | End | Ins | Shift/Enter | * ----------------| M1/M2 | Alt | BSpc/Fn1 | | Spc/Fn1 | AltGr | Win |-------------------- * -------------------------- ------------------------- - * Game mode + * Game mode * ------------------------------------------ --------------------------------------------- * | Tab | | | | | | | | | | | | | * | Ctrl | | | | | | | | | | | | | @@ -69,7 +69,7 @@ * - 0 LED off = Normal mode * - 1 LED constant on = Game mode * - 2 LED flashing = Caps Lock activated - * * Game mode: Replaces all layer keys with a "Game mode KEY". Configurable for each button + * * Game mode: Replaces all layer keys with a "Game mode KEY". Configurable for each button * * Tap/Hold functionality (only for layer0) * * Combo key (up to three simultaneous keys) support. Configurable for each button */ @@ -136,8 +136,8 @@ Keypad kp_keypad = Keypad(makeKeymap(kp_keys), kp_rowPins, kp_colPins, KP_ROWS, Button buttons[NBR_OF_BUTTONS] = { {1, KEY_FN2, KEY_TAB, NO_KEY, NO_KEY, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, - {2, KEY_Q, NO_KEY, KEY_F1, KEY_F12, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, - {3, KEY_W, NO_KEY, KEY_F2, KEY_F13, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, + {2, KEY_ML, NO_KEY, KEY_F1, KEY_F12, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, + {3, KEY_MR, NO_KEY, KEY_F2, KEY_F13, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, {4, KEY_E, NO_KEY, KEY_F3, KEY_F14, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, {5, KEY_R, NO_KEY, KEY_F4, KEY_F15, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, {6, KEY_T, NO_KEY, KEY_F5, KEY_F16, NO_KEY, true, {NO_KEY, NO_KEY, NO_KEY}, IDLE, false, 0, false, false, 0, 0, false}, @@ -242,6 +242,10 @@ unsigned long button_timestamp = 0; unsigned long mouse_wheel_timestamp = 0; unsigned long indicator_timestamp = 0; +bool mouse_l = false; +bool mouse_r = false; +bool mouse_u = false; +bool mouse_d = false; int mouse_x = 0; int mouse_y = 0; int mouse_wheel = 0; @@ -302,26 +306,40 @@ bool set_key(uint16_t keycode, uint8_t kstate) { if (kstate == RELEASED) { - mouse_x = 0; - mouse_y = 0; + if (keycode == KEY_ML) + { + mouse_l = false; + } + if (keycode == KEY_MR) + { + mouse_r = false; + } + if (keycode == KEY_MU) + { + mouse_u = false; + } + if (keycode == KEY_MD) + { + mouse_d = false; + } } else if (kstate == PRESSED) { if (keycode == KEY_MU) { - mouse_y = -10; + mouse_u = true; } else if (keycode == KEY_MD) { - mouse_y = 10; + mouse_d = true; } else if (keycode == KEY_MR) { - mouse_x = 10; + mouse_r = true; } else if (keycode == KEY_ML) { - mouse_x = -10; + mouse_l = true; } } } @@ -527,7 +545,7 @@ void scan_buttons() { if (buttons[i].gm_keycode == KEY_COMBO) { - /* Sending press command for all combo keys */ + /* Sending press command for all combo keys */ for (int j = 0; j < 3; j++) { set_key(buttons[i].combo_keycode[j], PRESSED); @@ -542,7 +560,7 @@ void scan_buttons() { if (buttons[i].fn1_keycode == KEY_COMBO) { - /* Sending press command for all combo keys */ + /* Sending press command for all combo keys */ for (int j = 0; j < 3; j++) { set_key(buttons[i].combo_keycode[j], PRESSED); @@ -557,7 +575,7 @@ void scan_buttons() { if (buttons[i].fn2_keycode == KEY_COMBO) { - /* Sending press command for all combo keys */ + /* Sending press command for all combo keys */ for (int j = 0; j < 3; j++) { set_key(buttons[i].combo_keycode[j], PRESSED); @@ -572,7 +590,7 @@ void scan_buttons() { if (buttons[i].keycode == KEY_COMBO) { - /* Sending press command for all combo keys */ + /* Sending press command for all combo keys */ for (int j = 0; j < 3; j++) { set_key(buttons[i].combo_keycode[j], PRESSED); @@ -616,7 +634,7 @@ void scan_buttons() { if (buttons[i].gm_keycode == KEY_COMBO) { - /* Sending release command for all combo keys */ + /* Sending release command for all combo keys */ for (int j = 0; j < 3; j++) { set_key(buttons[i].combo_keycode[j], RELEASED); @@ -629,13 +647,13 @@ void scan_buttons() } else { - /* Sending release command for all combo keys */ + /* Sending release command for all combo keys */ for (int j = 0; j < 3; j++) { set_key(buttons[i].combo_keycode[j], RELEASED); } - /* Sending release command for all other keycodes related to this button */ + /* Sending release command for all other keycodes related to this button */ set_key(buttons[i].fn2_keycode, RELEASED); set_key(buttons[i].fn1_keycode, RELEASED); set_key(buttons[i].keycode, RELEASED); @@ -719,9 +737,49 @@ void loop() } /* Update mouse 20ms */ - if (current_timestamp >= mouse_wheel_timestamp && mouse_wheel != 0) + if (current_timestamp >= mouse_wheel_timestamp && (mouse_wheel != 0 || mouse_x != 0 || mouse_y != 0 || mouse_d == true || mouse_l == true || mouse_r == true || mouse_u == true)) { + /* Stop movement when no buttons are pressed */ + if (mouse_d == false && mouse_u == false) + { + mouse_y = 0; + } + if (mouse_l == false && mouse_r == false) + { + mouse_x = 0; + } + + /* Stop movement when both up/down or left/right are pressed */ + if (mouse_d == true && mouse_u == true) + { + mouse_y = 0; + } + if (mouse_l == true && mouse_r == true) + { + mouse_x = 0; + } + + /* Move mouse cursor/wheel */ Mouse.move(mouse_x, mouse_y, mouse_wheel); + + /* Add mouse acceleration for next movement */ + if (mouse_r == true && mouse_x < 50) + { + mouse_x++; + } + if (mouse_l == true && mouse_x > -50) + { + mouse_x--; + } + if (mouse_d == true && mouse_y < 50) + { + mouse_y++; + } + if (mouse_u == true && mouse_y > -50) + { + mouse_y--; + } + mouse_wheel_timestamp = current_timestamp + 20; }