From 78f7df49a6120d59f4eb13159a713c4074025f4c Mon Sep 17 00:00:00 2001 From: Christoffer Martinsson Date: Fri, 2 Jun 2023 20:10:05 +0200 Subject: [PATCH] Code cleanup --- firmware/src/main.cpp | 339 +++++++++++++++++++----------------------- 1 file changed, 157 insertions(+), 182 deletions(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index ebc3d76..0fef638 100755 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -15,51 +15,6 @@ * --- ----- --------------------- --------------------------------------------- * ------------------------------------------------------------------------------------------------------- * ======================================================================================================= - * - * Copyright 2022 Christoffer Martinsson - * - * CMtec CMDR Keyboard 42 can be redistributed and/or modified under the terms of the GNU General - * Public License (Version 2), as published by the Free Software Foundation. - * A copy of the license can be found online at www.gnu.o urg/licenses. - * - * CMtec CMDR Keyboard 42 is distributed in the hope that it will be useful, but WITHOUT ANY - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - * A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * Keyboard/Mouse based on standard teensy "Keypad" library for button scanning, standard teensy - * "usb_keyboard" library for HID keyboard/mouse usb data communication. - * - * Layer 0 - * --------------------------------------- --------------------------------------- - * | Tab | Q | W | E | R | T | | Y | U | I | O | P | Å | - * | LCtrl | A | S | D | F | G | | H | J | K | L | Ö | Ä | - * | Shift | Z | X | C | V | B | | N | M | , | . | - | Shift | - * --------------------| Alt | Fn1 | Spc | | Spc | Fn1 | Win |-------------------- - * ------------------- ------------------- - * Layer 1 (Fn1) - * --------------------------------------- --------------------------------------- - * | Esc | F1 | F2 | F3 | F4 | F5 | | F6 | F7 | F8 | F9 | F10 | BSpc | - * | LCtrl | 1 | 2 | 3 | 4 | 5 | | 6 | 7 | 8 | 9 | 0 | Enter | - * | Shift | 6 | 7 | 8 | 9 | 0 | | < | ´ | ' | ¨ | + | Shift | - * --------------------| Alt | Fn1 | Spc | | BSpc| Fn1 |AlrGr|-------------------- - * ------------------- ------------------- - * Layer 2 (Fn1 + Fn1) - * --------------------------------------- --------------------------------------- - * | F11 | F12 | F13 | F14 | F15 | F16 | | § | | | | CpLk| BSpc | - * | LCtrl | Play| Next| F17 | F18 | F19 | | Left| Down| Up |Right| Del | Enter | - * | Shift | F20 | F21 | F22 | F23 | F24 | | Home| PgD | PgU | End | Ins | Shift | - * --------------------| Alt | Fn1 | Win | | BSpc| Fn1 | Win |-------------------- - * ------------------- ------------------- - * Features: - * - * * 42 keys "Split" keyboard layout. 36 finger buttons and 6 thumb buttons. - * * Extreme low profile (only one pcb). - * * Cost efficient solution with one pcb and one 3D printed cover. - * * Function keys with total of three layer support (Primary + 2fn layers). - * * Mouse movement, wheel up, wheel down, left button, right button and middle button support - * * Status indication - - * - LED off = Normal mode - * - LED flashing = Caps Lock activated */ #include @@ -160,11 +115,11 @@ Button buttons[NBR_OF_BUTTONS] = {35, KEY_SLASH, KEY_MINUS, KEY_INSERT, IDLE, NO_KEY, false}, {36, KEY_RIGHT_SHIFT, KEY_RIGHT_SHIFT, KEY_RIGHT_SHIFT, IDLE, NO_KEY, false}, {37, KEY_LEFT_ALT, KEY_LEFT_ALT, KEY_LEFT_ALT, IDLE, NO_KEY, false}, - {38, KEY_FN1, KEY_FN1, KEY_FN1, IDLE, NO_KEY, false}, - {39, KEY_SPACE, KEY_SPACE, KEY_LEFT_GUI, IDLE, NO_KEY, false}, + {38, KEY_FN1, NO_KEY, NO_KEY, IDLE, NO_KEY, false}, + {39, KEY_SPACE, KEY_BACKSPACE, KEY_LEFT_GUI, IDLE, NO_KEY, false}, {40, KEY_SPACE, KEY_BACKSPACE, KEY_BACKSPACE, IDLE, NO_KEY, false}, - {41, KEY_FN1, KEY_FN1, KEY_FN1, IDLE, NO_KEY, false}, - {42, KEY_FN1, KEY_RIGHT_ALT, KEY_RIGHT_ALT, IDLE, NO_KEY, false}}; + {41, KEY_FN1, NO_KEY, NO_KEY, IDLE, NO_KEY, false}, + {42, KEY_FN1, KEY_RIGHT_ALT, NO_KEY, IDLE, NO_KEY, false}}; // clang-format on /* End of keymap config ----------------------------------------------------------------------------------------------------------------------------- */ @@ -195,44 +150,159 @@ bool set_key(uint16_t keycode, uint8_t kstate) return false; } - else if (keycode == KEY_WIN_LATCH) + if (keycode == KEY_WIN_LATCH) { win_latched = true; + return true; } /* Normal keyboard keys (HID keyboard) */ - else + if (kstate == RELEASED) { - if (kstate == RELEASED) + if (win_latched == true) { - if (win_latched == true) - { - Keyboard.release(keycode); - Keyboard.release(KEY_LEFT_GUI); - win_latched = false; - } - else - { - Keyboard.release(keycode); - } + Keyboard.release(keycode); + Keyboard.release(KEY_LEFT_GUI); + win_latched = false; } - else if (kstate == PRESSED) + else { - if (win_latched == true) - { - Keyboard.press(KEY_LEFT_GUI); - Keyboard.press(keycode); - } - else - { - Keyboard.press(keycode); - } + Keyboard.release(keycode); + } + } + else if (kstate == PRESSED) + { + if (win_latched == true) + { + Keyboard.press(KEY_LEFT_GUI); + Keyboard.press(keycode); + } + else + { + Keyboard.press(keycode); } } return true; } + +void process_data() +{ + /* Enter bootloader if all four corner-buttons is pressed together */ + int reboot = 0; + for (int i = 0; i < LIST_MAX; i++) + { + if ((kp_keypad.key[i].kchar == 1) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) + { + reboot += 1; + } + if ((kp_keypad.key[i].kchar == 25) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) + { + reboot += 1; + } + if ((kp_keypad.key[i].kchar == 12) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) + { + reboot += 1; + } + if ((kp_keypad.key[i].kchar == 36) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) + { + reboot += 1; + } + } + if (reboot == 4) + { + _reboot_Teensyduino_(); + } + + /* Check for Fn1 mode */ + int fn_mode = 0; + for (int i = 0; i < LIST_MAX; i++) + { + if (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD) + { + for (int j = 0; j < NBR_OF_BUTTONS; j++) + { + if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && buttons[j].keycode == KEY_FN1) + { + /* Check if FN1 key are defined to this button (Layer 0 and first position in combo array)*/ + if (buttons[j].keycode == KEY_FN1) + { + fn_mode++; + } + break; + } + } + } + } + + /* Process key press/release */ + for (int i = 0; i < LIST_MAX; i++) + { + if (kp_keypad.key[i].kstate == PRESSED) + { + for (int j = 0; j < NBR_OF_BUTTONS; j++) + { + if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true) + { + buttons[j].run_keycode = true; + buttons[j].kstate = PRESSED; + break; + } + } + } + else if (kp_keypad.key[i].kstate == RELEASED) + { + for (int j = 0; j < NBR_OF_BUTTONS; j++) + { + if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true) + { + buttons[j].run_keycode = true; + buttons[j].kstate = RELEASED; + break; + } + } + } + } + + /* Execute key commands */ + for (int i = 0; i < NBR_OF_BUTTONS; i++) + { + /* Check if key should be processed */ + if (buttons[i].run_keycode == false) + { + continue; + } + + /* Reset run_keycode flag */ + buttons[i].run_keycode = false; + + if (buttons[i].kstate == RELEASED) + { + /* Sending release command for last keycode related to this button */ + set_key(buttons[i].last_keycode, RELEASED); + continue; + } + + /* Check if key pressed or released */ + if (fn_mode == 0) + { + set_key(buttons[i].keycode, PRESSED); + buttons[i].last_keycode = buttons[i].keycode; + } + else if (fn_mode == 1) + { + set_key(buttons[i].fn1_keycode, PRESSED); + buttons[i].last_keycode = buttons[i].fn1_keycode; + } + else if (fn_mode == 2) + { + set_key(buttons[i].fn2_keycode, PRESSED); + buttons[i].last_keycode = buttons[i].fn2_keycode; + } + } +} + /** Scan key matrix and perform processing for each key. @@ -243,116 +313,7 @@ void scan_buttons() /* Scan keypad */ if (kp_keypad.getKeys()) { - /* Enter bootloader if all four corner-buttons is pressed together */ - int reboot = 0; - for (int i = 0; i < LIST_MAX; i++) - { - if ((kp_keypad.key[i].kchar == 1) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) - { - reboot += 1; - } - if ((kp_keypad.key[i].kchar == 25) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) - { - reboot += 1; - } - if ((kp_keypad.key[i].kchar == 12) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) - { - reboot += 1; - } - if ((kp_keypad.key[i].kchar == 36) && (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD)) - { - reboot += 1; - } - } - if (reboot == 4) - { - _reboot_Teensyduino_(); - } - - /* Check for Fn1 mode */ - int fn_mode = 0; - for (int i = 0; i < LIST_MAX; i++) - { - if (kp_keypad.key[i].kstate == PRESSED || kp_keypad.key[i].kstate == HOLD) - { - for (int j = 0; j < NBR_OF_BUTTONS; j++) - { - if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar) - { - /* Check if FN1 key are defined to this button (Layer 0 and first position in combo array)*/ - if (buttons[j].keycode == KEY_FN1) - { - fn_mode++; - } - break; - } - } - } - } - - /* Process key press/release */ - for (int i = 0; i < LIST_MAX; i++) - { - if (kp_keypad.key[i].kstate == PRESSED) - { - for (int j = 0; j < NBR_OF_BUTTONS; j++) - { - if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true) - { - buttons[j].run_keycode = true; - buttons[j].kstate = PRESSED; - break; - } - } - } - else if (kp_keypad.key[i].kstate == RELEASED) - { - for (int j = 0; j < NBR_OF_BUTTONS; j++) - { - if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true) - { - buttons[j].run_keycode = true; - buttons[j].kstate = RELEASED; - break; - } - } - } - } - - /* Execute key commands */ - for (int i = 0; i < NBR_OF_BUTTONS; i++) - { - /* Check if key should be processed */ - if (buttons[i].run_keycode == true) - { - /* Check if key pressed or released */ - if (buttons[i].kstate == PRESSED) - { - if (fn_mode == 0) - { - set_key(buttons[i].keycode, PRESSED); - buttons[i].last_keycode = buttons[i].keycode; - } - else if (fn_mode == 1) - { - set_key(buttons[i].fn1_keycode, PRESSED); - buttons[i].last_keycode = buttons[i].fn1_keycode; - } - else if (fn_mode == 2) - { - set_key(buttons[i].fn2_keycode, PRESSED); - buttons[i].last_keycode = buttons[i].fn2_keycode; - } - } - else if (buttons[i].kstate == RELEASED) - { - /* Sending release command for last keycode related to this button */ - set_key(buttons[i].last_keycode, RELEASED); - } - /* Reset run_keycode flag */ - buttons[i].run_keycode = false; - } - } + process_data(); } /* Set status indication */ @@ -418,3 +379,17 @@ void loop() indicator_timestamp = current_timestamp + 200; } } +/* + * Copyright 2022 Christoffer Martinsson + * + * CMtec CMDR Keyboard 42 can be redistributed and/or modified under the terms of the GNU General + * Public License (Version 2), as published by the Free Software Foundation. + * A copy of the license can be found online at www.gnu.o urg/licenses. + * + * CMtec CMDR Keyboard 42 is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * Keyboard/Mouse based on standard teensy "Keypad" library for button scanning, standard teensy + * "usb_keyboard" library for HID keyboard/mouse usb data communication. + */