Updated comments

This commit is contained in:
Christoffer Martinsson 2022-10-16 10:01:26 +02:00
parent 01a6a7c028
commit 20dd6914dc

View File

@ -29,12 +29,23 @@
* Keyboard/Mouse based on standard teensy "Keypad" library for button scanning, standard teensy * Keyboard/Mouse based on standard teensy "Keypad" library for button scanning, standard teensy
* "usb_keyboard" library for HID keyboard/mouse usb data communication. * "usb_keyboard" library for HID keyboard/mouse usb data communication.
* *
* ---------------------------------------------------------------------------------------------
* | Tab/Fn2 | Q | W | E | R | T | | Y | U | I | O | P | Å |
* | Ctrl/Esc | A | S | D | F | G | | H | J | K | L | Ö | Ä |
* | Shift/Del | Z | X | C | V | B | | N | M | , | . | - | Shift/Enter |
* -----------------| M1/M2 | Alt | BSpc/Fn1 | | Spc/Fn1 | AltGr | Win |--------------------
* -------------------------- -------------------------
* Features: * Features:
* *
* * 42 keys "Split" keyboard layout. 36 finger keys and 6 thumb keys. * * 42 keys "Split" keyboard layout. 36 finger keys and 6 thumb keys.
* * Function buttons with total of four key-layer support (Primary + 2fn layers + Game mode). * * Function buttons with total of four key-layer support (Primary + 2fn layers + Game mode).
* * Mouse movement, wheel up, wheel down, left button, right button and middle button support * * Mouse movement, wheel up, wheel down, left button, right button and middle button support
* * Caps Lock warning indication (if activated) * * Status indication -
* - 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 key
*
*/ */
#include <Arduino.h> #include <Arduino.h>
@ -125,7 +136,8 @@ Button buttons[NBR_OF_BUTTONS] =
// byte kp_colPins[KP_COLS] = {12, 11, 10, 9, 8, 7, 26, 25, 24, 23, 22, 21}; // byte kp_colPins[KP_COLS] = {12, 11, 10, 9, 8, 7, 26, 25, 24, 23, 22, 21};
// // @formatter:off // // @formatter:off
// Key ID corresponding with the physical design of the actual keyboard. // /* Keymap config -----------------------------------------------------------------------------------------------------------------------------------------------------
// Key ID corresponding with the physical design of the actual keyboard. */
// char kp_keys[KP_ROWS][KP_COLS] = { // char kp_keys[KP_ROWS][KP_COLS] = {
// {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, // {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
// {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, // {13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24},
@ -133,14 +145,12 @@ Button buttons[NBR_OF_BUTTONS] =
// {0, 0, 0, 37, 38, 39, 40, 41, 42, 0, 0, 0}}; // {0, 0, 0, 37, 38, 39, 40, 41, 42, 0, 0, 0}};
// Keypad kp_keypad = Keypad(makeKeymap(kp_keys), kp_rowPins, kp_colPins, KP_ROWS, KP_COLS); // Keypad kp_keypad = Keypad(makeKeymap(kp_keys), kp_rowPins, kp_colPins, KP_ROWS, KP_COLS);
// /* Valid "Fn0 (hold) key" when using tap mode are: KEY_LEFT_SHIFT, KEY_RIGHT_SHIFT, KEY_LEFT_CTRL, KEY_RIGHT_CTRL, KEY_RIGHT_ALT, KEY_LEFT_GUI, KEY_RIGHT_GUI, KEY_FN1, KEY_FN2
// // Button keymap. // Fn1 and Fn2 keys are N/A when using tap mode and should me defined as NO_KEY.
// // Valid "Fn0 (hold) key" when using tap mode are: KEY_LEFT_SHIFT, KEY_RIGHT_SHIFT, KEY_LEFT_CTRL, KEY_RIGHT_CTRL, KEY_RIGHT_ALT, KEY_LEFT_GUI, KEY_RIGHT_GUI, KEY_FN1, KEY_FN2 // "GM replace key" will override all layer keys (Fn0, Fn1, Fn2) both tap and hold while game mode are active. */
// // Fn1 and Fn2 keys are N/A when using tap mode and should me defined as NO_KEY.
// // "GM replace key" will override all layer keys (Fn0, Fn1, Fn2) both tap and hold while game mode are active.
// Button buttons[NBR_OF_BUTTONS] = // Button buttons[NBR_OF_BUTTONS] =
// { // {
// // key ID Fn0 (hold) key Fn0 tap key Fn1 key Fn2 key GM replace key tap enable // /* key ID Fn0 (hold) key Fn0 tap key Fn1 key Fn2 key GM replace key tap enable */
// {1, KEY_FN2, KEY_TAB, NO_KEY, NO_KEY, NO_KEY, true, IDLE, false, 0, false, false, 0, 0, false}, // {1, KEY_FN2, KEY_TAB, NO_KEY, NO_KEY, NO_KEY, true, IDLE, false, 0, false, false, 0, 0, false},
// {2, KEY_Q, NO_KEY, KEY_F1, KEY_F12, NO_KEY, false, IDLE, false, 0, false, false, 0, 0, false}, // {2, KEY_Q, NO_KEY, KEY_F1, KEY_F12, NO_KEY, false, IDLE, false, 0, false, false, 0, 0, false},
// {3, KEY_W, NO_KEY, KEY_F2, KEY_F13, NO_KEY, false, IDLE, false, 0, false, false, 0, 0, false}, // {3, KEY_W, NO_KEY, KEY_F2, KEY_F13, NO_KEY, false, IDLE, false, 0, false, false, 0, 0, false},
@ -201,15 +211,21 @@ int mouse_wheel = 0;
bool game_mode = false; bool game_mode = false;
bool key_pressed = false; bool key_pressed = false;
/**
Converts a color code to its numerical value.
@param colorCode color code to convert.
@return the numerical value of the color code.
*/
bool set_key(uint16_t keycode, uint8_t kstate) bool set_key(uint16_t keycode, uint8_t kstate)
{ {
// Abort if keycode is invalid /* Abort if keycode is invalid */
if (keycode == NO_KEY || keycode == KEY_FN1 || keycode == KEY_FN2) if (keycode == NO_KEY || keycode == KEY_FN1 || keycode == KEY_FN2)
{ {
return false; return false;
} }
// Mouse buttons /* Mouse buttons */
if (keycode >= KEY_M1 && keycode <= KEY_M3) if (keycode >= KEY_M1 && keycode <= KEY_M3)
{ {
if (kstate == RELEASED) if (kstate == RELEASED)
@ -222,20 +238,7 @@ bool set_key(uint16_t keycode, uint8_t kstate)
} }
} }
// Toggle game mode /* Mouse wheel */
else if (keycode == KEY_GM)
{
if (kstate == PRESSED && game_mode == true)
{
game_mode = false;
}
else if (kstate == PRESSED && game_mode == false)
{
game_mode = true;
}
}
// Mouse wheel
else if ((keycode == KEY_MWU || keycode == KEY_MWD)) else if ((keycode == KEY_MWU || keycode == KEY_MWD))
{ {
if (kstate == RELEASED) if (kstate == RELEASED)
@ -255,7 +258,20 @@ bool set_key(uint16_t keycode, uint8_t kstate)
} }
} }
// Normal keyboard keys /* Toggle game mode */
else if (keycode == KEY_GM)
{
if (kstate == PRESSED && game_mode == true)
{
game_mode = false;
}
else if (kstate == PRESSED && game_mode == false)
{
game_mode = true;
}
}
/* Normal keyboard keys */
else else
{ {
if (kstate == RELEASED) if (kstate == RELEASED)
@ -271,13 +287,19 @@ bool set_key(uint16_t keycode, uint8_t kstate)
return true; return true;
} }
/**
Converts a color code to its numerical value.
@param colorCode color code to convert.
@return the numerical value of the color code.
*/
void scan_buttons() void scan_buttons()
{ {
// Scan keypad /* Scan keypad */
if (kp_keypad.getKeys()) 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;
for (int i = 0; i < LIST_MAX; i++) for (int i = 0; i < LIST_MAX; i++)
{ {
@ -303,7 +325,7 @@ void scan_buttons()
_reboot_Teensyduino_(); _reboot_Teensyduino_();
} }
// Check for FN mode /* Check for FN mode */
int fn_mode = 0; int fn_mode = 0;
for (int i = 0; i < LIST_MAX; i++) for (int i = 0; i < LIST_MAX; i++)
{ {
@ -327,7 +349,7 @@ void scan_buttons()
} }
} }
// Process key press /* Process key press */
for (int i = 0; i < LIST_MAX; i++) for (int i = 0; i < LIST_MAX; i++)
{ {
if (kp_keypad.key[i].kstate == PRESSED) if (kp_keypad.key[i].kstate == PRESSED)
@ -336,11 +358,11 @@ void scan_buttons()
{ {
if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true) if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true)
{ {
// Tap state: /* Tap state:
// 0 = idle (not pressed for a while) 0 = idle (not pressed for a while)
// 1 = pressed 1 = pressed
// 2 = released within timeout, pressing tap key 2 = released within timeout, pressing tap key
// 3 = pressed again within timeout, holding tap key 3 = pressed again within timeout, holding tap key */
if (buttons[j].tap_enable && buttons[j].tap_state == 0 && (game_mode == false || buttons[j].gm_keycode == NO_KEY)) if (buttons[j].tap_enable && buttons[j].tap_state == 0 && (game_mode == false || buttons[j].gm_keycode == NO_KEY))
{ {
buttons[j].tap_timeout_timestamp = current_timestamp + TAP_TIMEOUT; buttons[j].tap_timeout_timestamp = current_timestamp + TAP_TIMEOUT;
@ -368,11 +390,11 @@ void scan_buttons()
{ {
if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true) if (buttons[j].keypad_kchar == kp_keypad.key[i].kchar && kp_keypad.key[i].stateChanged == true)
{ {
// Tap state: /* Tap state:
// 0 = idle (not pressed for a while) 0 = idle (not pressed for a while)
// 1 = pressed 1 = pressed
// 2 = released within timeout, pressing tap key 2 = released within timeout, pressing tap key
// 3 = pressed again within timeout, holding tap key 3 = pressed again within timeout, holding tap key */
if (buttons[j].tap_enable && buttons[j].tap_state == 1 && (game_mode == false || buttons[j].gm_keycode == NO_KEY)) if (buttons[j].tap_enable && buttons[j].tap_state == 1 && (game_mode == false || buttons[j].gm_keycode == NO_KEY))
{ {
buttons[j].tap_release_timestamp = current_timestamp + TAP_TIMEOUT + 10; buttons[j].tap_release_timestamp = current_timestamp + TAP_TIMEOUT + 10;
@ -391,7 +413,7 @@ void scan_buttons()
} }
} }
// Check if any "non tap keys" has been pressed /* Check if any "non tap keys" has been pressed */
for (int i = 0; i < NBR_OF_BUTTONS; i++) for (int i = 0; i < NBR_OF_BUTTONS; i++)
{ {
if (buttons[i].run_keycode == true && buttons[i].tap_enable == false && buttons[i].kstate == PRESSED) if (buttons[i].run_keycode == true && buttons[i].tap_enable == false && buttons[i].kstate == PRESSED)
@ -406,27 +428,27 @@ void scan_buttons()
} }
} }
// Execute key commands /* Execute key commands */
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 == true)
{ {
// Check if key pressed or released /* Check if key pressed or released */
if (buttons[i].kstate == PRESSED) if (buttons[i].kstate == PRESSED)
{ {
// Check if key is in tap mode and not in game mode /* Check if key is in tap mode and not in game mode */
if (buttons[i].tap_enable == true && (game_mode == false || buttons[i].gm_keycode == NO_KEY)) if (buttons[i].tap_enable == true && (game_mode == false || buttons[i].gm_keycode == NO_KEY))
{ {
if (buttons[i].tap_state == 1) if (buttons[i].tap_state == 1)
{ {
if (buttons[i].hold_direct == true) if (buttons[i].hold_direct == true)
{ {
// Press hold key if "hold direct" is enabled (tap state = 1) /* Press hold key if "hold direct" is enabled (tap state = 1) */
set_key(buttons[i].keycode, PRESSED); set_key(buttons[i].keycode, PRESSED);
} }
// Reset tap inhibit flag /* Reset tap inhibit flag */
buttons[i].tap_inhibit = false; buttons[i].tap_inhibit = false;
} }
} }
@ -452,12 +474,12 @@ void scan_buttons()
} }
else if (buttons[i].kstate == RELEASED) else if (buttons[i].kstate == RELEASED)
{ {
// Check if key is in tap mode /* Check if key is in tap mode */
if (buttons[i].tap_enable == true && (game_mode == false || buttons[i].gm_keycode == NO_KEY)) if (buttons[i].tap_enable == true && (game_mode == false || buttons[i].gm_keycode == NO_KEY))
{ {
if (buttons[i].tap_state == 2) if (buttons[i].tap_state == 2)
{ {
// Press tap key if no other keys are pressed (tap state = 2) /* Press tap key if no other keys are pressed (tap state = 2) */
set_key(buttons[i].tap_keycode, RELEASED); // Fix for not send press and hold for the tap key set_key(buttons[i].tap_keycode, RELEASED); // Fix for not send press and hold for the tap key
set_key(buttons[i].keycode, RELEASED); set_key(buttons[i].keycode, RELEASED);
if (buttons[i].tap_inhibit == false) if (buttons[i].tap_inhibit == false)
@ -467,7 +489,7 @@ void scan_buttons()
} }
else else
{ {
// Release all keys (tap state = 0,1,3) /* Release all keys (tap state = 0,1,3) */
set_key(buttons[i].keycode, RELEASED); set_key(buttons[i].keycode, RELEASED);
set_key(buttons[i].tap_keycode, RELEASED); set_key(buttons[i].tap_keycode, RELEASED);
buttons[i].tap_state = 0; buttons[i].tap_state = 0;
@ -487,16 +509,16 @@ void scan_buttons()
} }
} }
} }
// Reset run_keycode flag /* Reset run_keycode flag */
buttons[i].run_keycode = false; buttons[i].run_keycode = false;
} }
} }
} }
// Status indication /* Status indication
// 0 = off (normal) 0 = off (normal)
// 1 = constant on (Game mode) 1 = constant on (Game mode)
// 2 = blinking (CapsLock) 2 = blinking (CapsLock) */
if (keyboard_leds & (1 << USB_LED_CAPS_LOCK)) if (keyboard_leds & (1 << USB_LED_CAPS_LOCK))
{ {
status_led_mode = 2; status_led_mode = 2;
@ -522,18 +544,18 @@ void loop()
current_timestamp = millis(); current_timestamp = millis();
// ---------------------------------------------------------- /* ----------------------------------------------------------
// Scan buttons 5ms Scan buttons 5ms
// ---------------------------------------------------------- ---------------------------------------------------------- */
if (current_timestamp >= button_timestamp) if (current_timestamp >= button_timestamp)
{ {
button_timestamp = current_timestamp + 5; button_timestamp = current_timestamp + 5;
scan_buttons(); scan_buttons();
} }
// ---------------------------------------------------------- /* ----------------------------------------------------------
// Fn tap timeout TAP_TIMEOUT Fn tap timeout TAP_TIMEOUT
// ---------------------------------------------------------- ---------------------------------------------------------- */
for (int i = 0; i < NBR_OF_BUTTONS; i++) for (int i = 0; i < NBR_OF_BUTTONS; i++)
{ {
if (current_timestamp >= buttons[i].tap_timeout_timestamp && buttons[i].tap_timeout_enable) if (current_timestamp >= buttons[i].tap_timeout_timestamp && buttons[i].tap_timeout_enable)
@ -554,9 +576,9 @@ void loop()
} }
} }
// ---------------------------------------------------------- /* ----------------------------------------------------------
// Fn tap release TAP_TIMEOUT + 10ms Fn tap release TAP_TIMEOUT + 10ms
// ---------------------------------------------------------- ---------------------------------------------------------- */
for (int i = 0; i < NBR_OF_BUTTONS; i++) for (int i = 0; i < NBR_OF_BUTTONS; i++)
{ {
if (current_timestamp >= buttons[i].tap_release_timestamp && buttons[i].tap_release_enable) if (current_timestamp >= buttons[i].tap_release_timestamp && buttons[i].tap_release_enable)
@ -567,18 +589,18 @@ void loop()
} }
} }
// ---------------------------------------------------------- /* ----------------------------------------------------------
// Update mouse wheel 20ms Update mouse wheel 20ms
// ---------------------------------------------------------- ---------------------------------------------------------- */
if (current_timestamp >= mouse_wheel_timestamp && mouse_wheel != 0) if (current_timestamp >= mouse_wheel_timestamp && mouse_wheel != 0)
{ {
Mouse.move(0, 0, mouse_wheel); Mouse.move(0, 0, mouse_wheel);
mouse_wheel_timestamp = current_timestamp + 20; mouse_wheel_timestamp = current_timestamp + 20;
} }
// ---------------------------------------------------------- /* ----------------------------------------------------------
// Update indicator 200ms Update indicator 200ms
// ---------------------------------------------------------- ---------------------------------------------------------- */
if (current_timestamp >= indicator_timestamp) if (current_timestamp >= indicator_timestamp)
{ {
if (status_led_mode == 2 && status_led_on == false) if (status_led_mode == 2 && status_led_on == false)