From 5545283db124b3f7344975434b31ad534da7ace3 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Wed, 27 Jan 2016 01:15:46 -0800 Subject: [PATCH] First pass at extracting key event functions to their own source files --- KeyboardioFirmware.h | 26 ++++---- KeyboardioFirmware.ino | 131 +++-------------------------------------- key_events.cpp | 126 +++++++++++++++++++++++++++++++++++++++ key_events.h | 25 ++++++++ 4 files changed, 169 insertions(+), 139 deletions(-) create mode 100644 key_events.cpp create mode 100644 key_events.h diff --git a/KeyboardioFirmware.h b/KeyboardioFirmware.h index 9e9fd2c4..cc55b330 100644 --- a/KeyboardioFirmware.h +++ b/KeyboardioFirmware.h @@ -23,28 +23,22 @@ void setup(); #include #include -#include "key_defs.h" #include "KeyboardConfig.h" -#include "led_control.h" #include "generated/keymaps.h" -#include "matrix_state.h" -#include "mouse_movement.h" -#include "storage.h" +#include "key_events.h" -uint8_t matrixState[ROWS][COLS] = {0}; -static const Key keymaps[KEYMAPS][ROWS][COLS] = { KEYMAP_LIST }; -uint8_t primary_keymap = 0; -uint8_t temporary_keymap = 0; +extern uint8_t matrixState[ROWS][COLS]; +extern const Key keymaps[KEYMAPS][ROWS][COLS]; +extern uint8_t primary_keymap; +extern uint8_t temporary_keymap; + +extern KeyboardStorage Storage; +extern LEDControl LEDs; -void scan_matrix(); -// sending events to the computer -void handle_synthetic_key_event(byte switchState, Key mappedKey); -void handle_key_event(byte row, byte col); -void press_key(Key mappedKey); -void handle_keymap_key_event(byte switchState, Key keymapEntry); -void handle_mouse_key_event(byte switchState, Key mappedKey); + +void scan_matrix(); #ifndef VERSION #define VERSION "locally-built" diff --git a/KeyboardioFirmware.ino b/KeyboardioFirmware.ino index d7581318..3968f9f0 100644 --- a/KeyboardioFirmware.ino +++ b/KeyboardioFirmware.ino @@ -6,8 +6,14 @@ #include "KeyboardioFirmware.h" #include "HID-Project.h" -KeyboardStorage Storage; -LEDControl LEDs; + + uint8_t matrixState[ROWS][COLS] = {0}; + const Key keymaps[KEYMAPS][ROWS][COLS] = { KEYMAP_LIST }; + uint8_t primary_keymap = 0; + uint8_t temporary_keymap = 0; + + KeyboardStorage Storage; + LEDControl LEDs; @@ -49,124 +55,3 @@ void loop() { } -void handle_synthetic_key_event(byte switchState, Key mappedKey) { - if (mappedKey.flags & IS_MOUSE_KEY ) { - if (mappedKey.rawKey & MOUSE_WARP) { - if (key_toggled_on(switchState)) { - warp_mouse(mappedKey); - } - } else { - handle_mouse_key_event(switchState, mappedKey); - } - } else if (mappedKey.flags & IS_CONSUMER) { - if (key_toggled_on (switchState)) { - ConsumerControl.press(mappedKey.rawKey); - } - } else if (mappedKey.flags & IS_INTERNAL) { - if (key_toggled_on (switchState)) { - if (mappedKey.rawKey == LED_TOGGLE) { - LEDs.next_mode(); - } - } - } else if (mappedKey.flags & IS_SYSCTL) { - if (key_toggled_on (switchState)) { - SystemControl.press(mappedKey.rawKey); - } - } else if (mappedKey.flags & IS_MACRO) { - if (key_toggled_on (switchState)) { - if (mappedKey.rawKey == 1) { - Serial.print("Keyboard.IO keyboard driver v0.00"); - } - } - } else if (mappedKey.rawKey == KEY_MOUSE_BTN_L - || mappedKey.rawKey == KEY_MOUSE_BTN_M - || mappedKey.rawKey == KEY_MOUSE_BTN_R) { - if (key_toggled_on (switchState)) { - Mouse.press(mappedKey.rawKey); - end_warping(); - } else if (key_is_pressed(switchState)) { - } else if (Mouse.isPressed(mappedKey.rawKey) ) { - Mouse.release(mappedKey.rawKey); - } - } -} - -void handle_key_event(byte row, byte col) { - //for every newly pressed button, figure out what logical key it is and send a key down event - // for every newly released button, figure out what logical key it is and send a key up event - - // really, these are signed small ints - - byte switchState = matrixState[row][col]; - Key mappedKey = keymaps[temporary_keymap][row][col]; - - if (keymaps[primary_keymap][row][col].flags & SWITCH_TO_KEYMAP) { - handle_keymap_key_event(switchState, keymaps[primary_keymap][row][col]); - } - if (mappedKey.flags & SYNTHETIC_KEY) { - handle_synthetic_key_event(switchState, mappedKey); - } else { - if (key_is_pressed(switchState)) { - press_key(mappedKey); - } - } -} - -void press_key(Key mappedKey) { - if (mappedKey.flags & SHIFT_HELD) { - Keyboard.press(Key_LShift.rawKey); - } - if (mappedKey.flags & CTRL_HELD) { - Keyboard.press(Key_LCtrl.rawKey); - } - if (mappedKey.flags & ALT_HELD) { - Keyboard.press(Key_RAlt.rawKey); - } - if (mappedKey.flags & GUI_HELD) { - Keyboard.press(Key_LGUI.rawKey); - } - Keyboard.press(mappedKey.rawKey); -} - - -void handle_keymap_key_event(byte switchState, Key keymapEntry) { - if (keymapEntry.flags & MOMENTARY ) { - if (key_toggled_on(switchState)) { - if ( keymapEntry.rawKey == KEYMAP_NEXT) { - temporary_keymap++; - } else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) { - temporary_keymap--; - } else { - temporary_keymap = keymapEntry.rawKey; - } - } - if (key_toggled_off(switchState)) { - temporary_keymap = primary_keymap; - } - - } else { - // switch keymap and stay there - if (key_toggled_on(switchState)) { - temporary_keymap = primary_keymap = keymapEntry.rawKey; - Storage.save_primary_keymap(primary_keymap); - } - } -} - -void handle_mouse_key_event(byte switchState, Key mappedKey) { - if (key_is_pressed(switchState)) { - if (mappedKey.rawKey & MOUSE_UP) { - move_mouse(0,-1); - } - if (mappedKey.rawKey & MOUSE_DN) { - move_mouse(0,1); - } - if (mappedKey.rawKey & MOUSE_L) { - move_mouse(-1,0); - } - if (mappedKey.rawKey & MOUSE_R) { - move_mouse(1,0); - } - } -} - diff --git a/key_events.cpp b/key_events.cpp new file mode 100644 index 00000000..23f7000b --- /dev/null +++ b/key_events.cpp @@ -0,0 +1,126 @@ +#include "key_events.h" + + + + +void handle_synthetic_key_event(byte switchState, Key mappedKey) { + if (mappedKey.flags & IS_MOUSE_KEY ) { + if (mappedKey.rawKey & MOUSE_WARP) { + if (key_toggled_on(switchState)) { + warp_mouse(mappedKey); + } + } else { + handle_mouse_key_event(switchState, mappedKey); + } + } else if (mappedKey.flags & IS_CONSUMER) { + if (key_toggled_on (switchState)) { + ConsumerControl.press(mappedKey.rawKey); + } + } else if (mappedKey.flags & IS_INTERNAL) { + if (key_toggled_on (switchState)) { + if (mappedKey.rawKey == LED_TOGGLE) { + LEDs.next_mode(); + } + } + } else if (mappedKey.flags & IS_SYSCTL) { + if (key_toggled_on (switchState)) { + SystemControl.press(mappedKey.rawKey); + } + } else if (mappedKey.flags & IS_MACRO) { + if (key_toggled_on (switchState)) { + if (mappedKey.rawKey == 1) { + Serial.print("Keyboard.IO keyboard driver v0.00"); + } + } + } else if (mappedKey.rawKey == KEY_MOUSE_BTN_L + || mappedKey.rawKey == KEY_MOUSE_BTN_M + || mappedKey.rawKey == KEY_MOUSE_BTN_R) { + if (key_toggled_on (switchState)) { + Mouse.press(mappedKey.rawKey); + end_warping(); + } else if (key_is_pressed(switchState)) { + } else if (Mouse.isPressed(mappedKey.rawKey) ) { + Mouse.release(mappedKey.rawKey); + } + } +} + +void handle_key_event(byte row, byte col) { + //for every newly pressed button, figure out what logical key it is and send a key down event + // for every newly released button, figure out what logical key it is and send a key up event + + // really, these are signed small ints + + byte switchState = matrixState[row][col]; + Key mappedKey = keymaps[temporary_keymap][row][col]; + + if (keymaps[primary_keymap][row][col].flags & SWITCH_TO_KEYMAP) { + handle_keymap_key_event(switchState, keymaps[primary_keymap][row][col]); + } + if (mappedKey.flags & SYNTHETIC_KEY) { + handle_synthetic_key_event(switchState, mappedKey); + } else { + if (key_is_pressed(switchState)) { + press_key(mappedKey); + } + } +} + +void press_key(Key mappedKey) { + if (mappedKey.flags & SHIFT_HELD) { + Keyboard.press(Key_LShift.rawKey); + } + if (mappedKey.flags & CTRL_HELD) { + Keyboard.press(Key_LCtrl.rawKey); + } + if (mappedKey.flags & ALT_HELD) { + Keyboard.press(Key_RAlt.rawKey); + } + if (mappedKey.flags & GUI_HELD) { + Keyboard.press(Key_LGUI.rawKey); + } + Keyboard.press(mappedKey.rawKey); +} + + +void handle_keymap_key_event(byte switchState, Key keymapEntry) { + if (keymapEntry.flags & MOMENTARY ) { + if (key_toggled_on(switchState)) { + if ( keymapEntry.rawKey == KEYMAP_NEXT) { + temporary_keymap++; + } else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) { + temporary_keymap--; + } else { + temporary_keymap = keymapEntry.rawKey; + } + } + if (key_toggled_off(switchState)) { + temporary_keymap = primary_keymap; + } + + } else { + // switch keymap and stay there + if (key_toggled_on(switchState)) { + temporary_keymap = primary_keymap = keymapEntry.rawKey; + Storage.save_primary_keymap(primary_keymap); + } + } +} + +void handle_mouse_key_event(byte switchState, Key mappedKey) { + if (key_is_pressed(switchState)) { + if (mappedKey.rawKey & MOUSE_UP) { + move_mouse(0,-1); + } + if (mappedKey.rawKey & MOUSE_DN) { + move_mouse(0,1); + } + if (mappedKey.rawKey & MOUSE_L) { + move_mouse(-1,0); + } + if (mappedKey.rawKey & MOUSE_R) { + move_mouse(1,0); + } + } +} + diff --git a/key_events.h b/key_events.h new file mode 100644 index 00000000..05f6628d --- /dev/null +++ b/key_events.h @@ -0,0 +1,25 @@ +#pragma once +#include + +#include "key_defs.h" +#include "matrix_state.h" +#include "mouse_movement.h" +#include "led_control.h" +#include "storage.h" + + +extern uint8_t matrixState[ROWS][COLS]; +extern const Key keymaps[KEYMAPS][ROWS][COLS]; +extern uint8_t primary_keymap; +extern uint8_t temporary_keymap; + +extern KeyboardStorage Storage; +extern LEDControl LEDs; + +// sending events to the computer +void handle_synthetic_key_event(byte switchState, Key mappedKey); +void handle_key_event(byte row, byte col); +void press_key(Key mappedKey); +void handle_keymap_key_event(byte switchState, Key keymapEntry); +void handle_mouse_key_event(byte switchState, Key mappedKey); +