From fdb3f8e605d40f46ecf0097d80ffa95aad8fd918 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Tue, 14 Jan 2014 20:00:04 -0500 Subject: [PATCH] Factor out logical-keymap code from physical matrix code --- ArduinoKeyboard.ino | 52 +++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/ArduinoKeyboard.ino b/ArduinoKeyboard.ino index bc7f7e38..764a082e 100644 --- a/ArduinoKeyboard.ino +++ b/ArduinoKeyboard.ino @@ -214,6 +214,36 @@ void setup_matrix() } +void set_keymap_layer(Key keymapEntry, byte matrixStateEntry) { + if (keymapEntry.flags & SWITCH_TO_LAYER) { + + // this logic sucks. there is a better way TODO this + if (! (keymapEntry.flags ^ ( MOMENTARY | SWITCH_TO_LAYER))) { + if (key_is_pressed(matrixStateEntry)) { + + + if ( keymapEntry.rawKey == LAYER_NEXT) { + active_layer++; + } else if ( keymapEntry.rawKey == LAYER_PREVIOUS) { + active_layer--; + } else { + active_layer = keymapEntry.rawKey; + } + } + } else if (! (keymapEntry.flags ^ ( SWITCH_TO_LAYER))) { + // switch layer and stay there + if (key_toggled_on(matrixStateEntry)) { + current_layer = active_layer = keymapEntry.rawKey; + save_current_layer(current_layer); + } + } + } +} +void handle_immediate_action_during_matrix_scan(Key keymapEntry, byte matrixStateEntry) { + + set_keymap_layer(keymapEntry, matrixStateEntry); + +} void scan_matrix() { @@ -236,27 +266,7 @@ void scan_matrix() // that we should be looking at a seconary Keymap halfway through the matrix scan - - // this logic sucks. there is a better way TODO this - if (! (keymaps[active_layer][row][col].flags ^ ( MOMENTARY | SWITCH_TO_LAYER))) { - if (key_is_pressed(matrixState[row][col])) { - - - if ( keymaps[current_layer][row][col].rawKey == LAYER_NEXT) { - active_layer++; - } else if ( keymaps[current_layer][row][col].rawKey == LAYER_PREVIOUS) { - active_layer--; - } else { - active_layer = keymaps[current_layer][row][col].rawKey; - } - } - } else if (! (keymaps[active_layer][row][col].flags ^ ( SWITCH_TO_LAYER))) { - // switch layer and stay there - if (key_toggled_on(matrixState[row][col])) { - current_layer = active_layer = keymaps[current_layer][row][col].rawKey; - save_current_layer(current_layer); - } - } + handle_immediate_action_during_matrix_scan(keymaps[active_layer][row][col], matrixState[row][col]); } digitalWrite(rowPins[row], HIGH);