Code cleanup

This commit is contained in:
Christoffer Martinsson 2023-06-02 20:10:05 +02:00
parent 0c5d3b179e
commit 78f7df49a6

View File

@ -15,51 +15,6 @@
* --- ----- --------------------- --------------------------------------------- * --- ----- --------------------- ---------------------------------------------
* ------------------------------------------------------------------------------------------------------- * -------------------------------------------------------------------------------------------------------
* ======================================================================================================= * =======================================================================================================
*
* Copyright 2022 Christoffer Martinsson <cm@cmtec.se>
*
* 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 <Arduino.h> #include <Arduino.h>
@ -160,11 +115,11 @@ Button buttons[NBR_OF_BUTTONS] =
{35, KEY_SLASH, KEY_MINUS, KEY_INSERT, IDLE, NO_KEY, false}, {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}, {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}, {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}, {38, KEY_FN1, NO_KEY, NO_KEY, IDLE, NO_KEY, false},
{39, KEY_SPACE, KEY_SPACE, KEY_LEFT_GUI, 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}, {40, KEY_SPACE, KEY_BACKSPACE, KEY_BACKSPACE, IDLE, NO_KEY, false},
{41, KEY_FN1, KEY_FN1, KEY_FN1, IDLE, NO_KEY, false}, {41, KEY_FN1, NO_KEY, NO_KEY, IDLE, NO_KEY, false},
{42, KEY_FN1, KEY_RIGHT_ALT, KEY_RIGHT_ALT, IDLE, NO_KEY, false}}; {42, KEY_FN1, KEY_RIGHT_ALT, NO_KEY, IDLE, NO_KEY, false}};
// clang-format on // clang-format on
/* End of keymap config ----------------------------------------------------------------------------------------------------------------------------- */ /* End of keymap config ----------------------------------------------------------------------------------------------------------------------------- */
@ -195,14 +150,13 @@ bool set_key(uint16_t keycode, uint8_t kstate)
return false; return false;
} }
else if (keycode == KEY_WIN_LATCH) if (keycode == KEY_WIN_LATCH)
{ {
win_latched = true; win_latched = true;
return true;
} }
/* Normal keyboard keys (HID keyboard) */ /* Normal keyboard keys (HID keyboard) */
else
{
if (kstate == RELEASED) if (kstate == RELEASED)
{ {
if (win_latched == true) if (win_latched == true)
@ -228,20 +182,12 @@ bool set_key(uint16_t keycode, uint8_t kstate)
Keyboard.press(keycode); Keyboard.press(keycode);
} }
} }
}
return true; return true;
} }
/**
Scan key matrix and perform processing for each key.
@return void. void process_data()
*/
void scan_buttons()
{
/* Scan keypad */
if (kp_keypad.getKeys())
{ {
/* Enter bootloader if all four corner-buttons is pressed together */ /* Enter bootloader if all four corner-buttons is pressed together */
int reboot = 0; int reboot = 0;
@ -277,7 +223,7 @@ void scan_buttons()
{ {
for (int j = 0; j < NBR_OF_BUTTONS; j++) for (int j = 0; j < NBR_OF_BUTTONS; j++)
{ {
if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar) 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)*/ /* Check if FN1 key are defined to this button (Layer 0 and first position in combo array)*/
if (buttons[j].keycode == KEY_FN1) if (buttons[j].keycode == KEY_FN1)
@ -323,11 +269,22 @@ void scan_buttons()
for (int i = 0; i < NBR_OF_BUTTONS; i++) for (int i = 0; i < NBR_OF_BUTTONS; i++)
{ {
/* Check if key should be processed */ /* Check if key should be processed */
if (buttons[i].run_keycode == true) 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 */ /* Check if key pressed or released */
if (buttons[i].kstate == PRESSED)
{
if (fn_mode == 0) if (fn_mode == 0)
{ {
set_key(buttons[i].keycode, PRESSED); set_key(buttons[i].keycode, PRESSED);
@ -344,15 +301,19 @@ void scan_buttons()
buttons[i].last_keycode = buttons[i].fn2_keycode; buttons[i].last_keycode = buttons[i].fn2_keycode;
} }
} }
else if (buttons[i].kstate == RELEASED) }
/**
Scan key matrix and perform processing for each key.
@return void.
*/
void scan_buttons()
{ {
/* Sending release command for last keycode related to this button */ /* Scan keypad */
set_key(buttons[i].last_keycode, RELEASED); if (kp_keypad.getKeys())
} {
/* Reset run_keycode flag */ process_data();
buttons[i].run_keycode = false;
}
}
} }
/* Set status indication */ /* Set status indication */
@ -418,3 +379,17 @@ void loop()
indicator_timestamp = current_timestamp + 200; indicator_timestamp = current_timestamp + 200;
} }
} }
/*
* Copyright 2022 Christoffer Martinsson <cm@cmtec.se>
*
* 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.
*/