From 63ea54289c2611b77cb2efc6379f612f9983b1bf Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 14 Jan 2014 22:32:23 -0500 Subject: [PATCH] refactoring --- ArduinoKeyboard.h | 25 ++++ ArduinoKeyboard.ino | 331 ++++++++++++++++++++++---------------------- 2 files changed, 193 insertions(+), 163 deletions(-) diff --git a/ArduinoKeyboard.h b/ArduinoKeyboard.h index 25ab1ae1..bc429b71 100644 --- a/ArduinoKeyboard.h +++ b/ArduinoKeyboard.h @@ -48,5 +48,30 @@ void report_matrix(); double mouse_accel (double cycles); void handle_mouse_movement( char x, char y); + +// hardware layer interaction +void setup_pins(); +void scan_matrix(); + +// key matrix +void setup_matrix(); +void reset_matrix(); +void handle_immediate_action_during_matrix_scan(Key keymapEntry, byte matrixStateEntry); + +// keymaps +void set_keymap_layer(Key keymapEntry, byte matrixStateEntry); + +// sending events to the computer +void record_key_being_pressed(byte character); +void release_keys_not_being_pressed(); +void reset_key_report(); +void handle_synthetic_key_press(byte switchState, Key mappedKey); +void handle_mouse_key_press(byte switchState, Key mappedKey, char &x, char &y); +void send_key_events(); + + + + + //Do not add code below this line #endif /* KeyboardIO_H_ */ diff --git a/ArduinoKeyboard.ino b/ArduinoKeyboard.ino index 5b7c1f7a..16b7fce6 100644 --- a/ArduinoKeyboard.ino +++ b/ArduinoKeyboard.ino @@ -24,9 +24,6 @@ #include "keymaps_generated.h" #include // Don't need this for CLI compilation, but do need it in the IDE -#include "KeyboardEEPROM.h" -#include "KeyboardMouse.h" -#include "KeyboardDebugging.h" //extern int usbMaxPower; @@ -47,47 +44,15 @@ float carriedOverX = 0; float carriedOverY = 0; -void release_keys_not_being_pressed() -{ - - // we use charsReportedLastTime to figure out what we might not be holding anymore and can now release. this is destructive to charsReportedLastTime - - for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { - // for each key we were holding as of the end of the last cycle - // see if we're still holding it - // if we're not, call an explicit Release - - if (charsReportedLastTime[i] != 0x00) { - // if there _was_ a character in this slot, go check the - // currently held characters - for (byte j = 0; j < KEYS_HELD_BUFFER; j++) { - if (charsReportedLastTime[i] == charsBeingReported[j]) { - // if's still held, we don't need to do anything. - charsReportedLastTime[i] = 0x00; - break; - } - } - - } - } - for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { - if (charsReportedLastTime[i] != 0x00) { - Keyboard.release(charsReportedLastTime[i]); - } - } -} - -void record_key_being_pressed(byte character) +void setup_matrix() { - for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { - // todo - deal with overflowing the 12 key buffer here - if (charsBeingReported[i] == 0x00) { - charsBeingReported[i] = character; - break; + //blank out the matrix. + for (byte col = 0; col < COLS; col++) { + for (byte row = 0; row < ROWS; row++) { + matrixState[row][col] = 0; } } } - void reset_matrix() { for (byte col = 0; col < COLS; col++) { @@ -95,127 +60,9 @@ void reset_matrix() matrixState[row][col] <<= 1; } } - for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { - charsReportedLastTime[i] = charsBeingReported[i]; - charsBeingReported[i] = 0x00; - } -} - - - - -void handle_synthetic_key_press(byte switchState, Key mappedKey) { - if (mappedKey.flags & IS_CONSUMER) { - if (key_toggled_on (switchState)) { - Keyboard.consumerControl(mappedKey.rawKey); - } - } - else if (mappedKey.flags & IS_SYSCTL) { - if (key_toggled_on (switchState)) { - Keyboard.systemControl(mappedKey.rawKey); - } - } - else if (mappedKey.flags & IS_MACRO) { - if (key_toggled_on (switchState)) { - if (mappedKey.rawKey == 1) { - Keyboard.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); - } else if (key_is_pressed(switchState)) { - } else if (Mouse.isPressed(mappedKey.rawKey) ) { - Mouse.release(mappedKey.rawKey); - } - } } -void handle_mouse_key_press(byte switchState, Key mappedKey, char &x, char &y) { - - if (key_is_pressed(switchState)) { - if (mappedKey.rawKey & MOUSE_UP) { - y -= 1; - } - if (mappedKey.rawKey & MOUSE_DN) { - y += 1; - } - if (mappedKey.rawKey & MOUSE_L) { - x -= 1; - } - if (mappedKey.rawKey & MOUSE_R) { - x += 1 ; - } - } -} -void send_key_events(byte layer) -{ - //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 - - - // TODO:switch to sending raw HID packets - - // really, these are signed small ints - char x = 0; - char y = 0; - - for (byte row = 0; row < ROWS; row++) { - - for (byte col = 0; col < COLS; col++) { - byte switchState = matrixState[row][col]; - Key mappedKey = keymaps[layer][row][col]; - if (mappedKey.flags & MOUSE_KEY ) { - handle_mouse_key_press(matrixState[row][col], keymaps[layer][row][col], x, y); - - - } else if (mappedKey.flags & SYNTHETIC_KEY) { - handle_synthetic_key_press(matrixState[row][col], keymaps[layer][row][col]); - } - else { - if (key_is_pressed(switchState)) { - record_key_being_pressed(mappedKey.rawKey); - if (key_toggled_on (switchState)) { - Keyboard.press(mappedKey.rawKey); - } - } else if (key_toggled_off (switchState)) { - Keyboard.release(mappedKey.rawKey); - } - } - } - } - handle_mouse_movement(x, y); - release_keys_not_being_pressed(); -} -void setup_pins() -{ - //set up the row pins as outputs - for (byte row = 0; row < ROWS; row++) { - pinMode(rowPins[row], OUTPUT); - digitalWrite(rowPins[row], HIGH); - } - - for (byte col = 0; col < COLS; col++) { - pinMode(colPins[col], INPUT); - digitalWrite(colPins[col], HIGH); - //drive em high by default s it seems to be more reliable than driving em low - - } -} - -void setup_matrix() -{ - //blank out the matrix. - for (byte col = 0; col < COLS; col++) { - for (byte row = 0; row < ROWS; row++) { - matrixState[row][col] = 0; - } - } - -} void set_keymap_layer(Key keymapEntry, byte matrixStateEntry) { if (keymapEntry.flags & SWITCH_TO_LAYER) { @@ -247,10 +94,10 @@ void handle_immediate_action_during_matrix_scan(Key keymapEntry, byte matrixStat set_keymap_layer(keymapEntry, matrixStateEntry); } + void scan_matrix() { - active_layer = current_layer; //scan the Keyboard matrix looking for connections for (byte row = 0; row < ROWS; row++) { @@ -283,19 +130,17 @@ void setup() Serial.begin(115200); Keyboard.begin(); Mouse.begin(); - //#ifdef DEBUG_SERIAL - //#endif setup_matrix(); setup_pins(); - Serial.println("loaded the matrix"); current_layer = load_current_layer(); active_layer = current_layer; } void loop() { + active_layer = current_layer; scan_matrix(); - send_key_events(active_layer); + send_key_events(); reset_matrix(); } @@ -489,3 +334,163 @@ void handle_mouse_movement( char x, char y) } +// +// Key Reports +// +void release_keys_not_being_pressed() +{ + // we use charsReportedLastTime to figure out what we might + // not be holding anymore and can now release. this is + // destructive to charsReportedLastTime + + for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { + // for each key we were holding as of the end of the last cycle + // see if we're still holding it + // if we're not, call an explicit Release + + if (charsReportedLastTime[i] != 0x00) { + // if there _was_ a character in this slot, go check the + // currently held characters + for (byte j = 0; j < KEYS_HELD_BUFFER; j++) { + if (charsReportedLastTime[i] == charsBeingReported[j]) { + // if's still held, we don't need to do anything. + charsReportedLastTime[i] = 0x00; + break; + } + } + } + } + for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { + if (charsReportedLastTime[i] != 0x00) { + Keyboard.release(charsReportedLastTime[i]); + } + } +} + +void record_key_being_pressed(byte character) +{ + for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { + // todo - deal with overflowing the 12 key buffer here + if (charsBeingReported[i] == 0x00) { + charsBeingReported[i] = character; + break; + } + } +} + +void reset_key_report() +{ + for (byte i = 0; i < KEYS_HELD_BUFFER; i++) { + charsReportedLastTime[i] = charsBeingReported[i]; + charsBeingReported[i] = 0x00; + } +} + + +// Sending events to the usb host + +void handle_synthetic_key_press(byte switchState, Key mappedKey) { + if (mappedKey.flags & IS_CONSUMER) { + if (key_toggled_on (switchState)) { + Keyboard.consumerControl(mappedKey.rawKey); + } + } + else if (mappedKey.flags & IS_SYSCTL) { + if (key_toggled_on (switchState)) { + Keyboard.systemControl(mappedKey.rawKey); + } + } + else if (mappedKey.flags & IS_MACRO) { + if (key_toggled_on (switchState)) { + if (mappedKey.rawKey == 1) { + Keyboard.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); + } else if (key_is_pressed(switchState)) { + } else if (Mouse.isPressed(mappedKey.rawKey) ) { + Mouse.release(mappedKey.rawKey); + } + } +} +void handle_mouse_key_press(byte switchState, Key mappedKey, char &x, char &y) { + + if (key_is_pressed(switchState)) { + if (mappedKey.rawKey & MOUSE_UP) { + y -= 1; + } + if (mappedKey.rawKey & MOUSE_DN) { + y += 1; + } + if (mappedKey.rawKey & MOUSE_L) { + x -= 1; + } + + if (mappedKey.rawKey & MOUSE_R) { + x += 1 ; + } + } +} + +void send_key_events() +{ + //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 + + + // TODO:switch to sending raw HID packets + + // really, these are signed small ints + char x = 0; + char y = 0; + + for (byte row = 0; row < ROWS; row++) { + + for (byte col = 0; col < COLS; col++) { + byte switchState = matrixState[row][col]; + Key mappedKey = keymaps[active_layer][row][col]; + if (mappedKey.flags & MOUSE_KEY ) { + handle_mouse_key_press(matrixState[row][col], keymaps[active_layer][row][col], x, y); + + + } else if (mappedKey.flags & SYNTHETIC_KEY) { + handle_synthetic_key_press(matrixState[row][col], keymaps[active_layer][row][col]); + } + else { + if (key_is_pressed(switchState)) { + record_key_being_pressed(mappedKey.rawKey); + if (key_toggled_on (switchState)) { + Keyboard.press(mappedKey.rawKey); + } + } else if (key_toggled_off (switchState)) { + Keyboard.release(mappedKey.rawKey); + } + } + } + } + handle_mouse_movement(x, y); + release_keys_not_being_pressed(); +} + + +// Hardware initialization +void setup_pins() +{ + //set up the row pins as outputs + for (byte row = 0; row < ROWS; row++) { + pinMode(rowPins[row], OUTPUT); + digitalWrite(rowPins[row], HIGH); + } + + for (byte col = 0; col < COLS; col++) { + pinMode(colPins[col], INPUT); + digitalWrite(colPins[col], HIGH); + //drive em high by default s it seems to be more reliable than driving em low + + } +} +