diff --git a/ArduinoKeyboard.h b/ArduinoKeyboard.h index 59474005..2b4bf5d9 100644 --- a/ArduinoKeyboard.h +++ b/ArduinoKeyboard.h @@ -16,8 +16,8 @@ void setup(); //add your function definitions for the project KeyboardIO here typedef struct { - byte flags; - byte rawKey; + byte flags; + byte rawKey; } Key; @@ -26,60 +26,60 @@ typedef struct { boolean key_was_pressed (byte keyState) { - if ( byte((keyState >> 4)) ^ B00001111 ) { - return false; - } else { - return true; - } + if ( byte((keyState >> 4)) ^ B00001111 ) { + return false; + } else { + return true; + } } boolean key_was_not_pressed (byte keyState) { - if ( byte((keyState >> 4)) ^ B00000000 ) { - return false; - } else { - return true; - } + if ( byte((keyState >> 4)) ^ B00000000 ) { + return false; + } else { + return true; + } } boolean key_is_pressed (byte keyState) { - if ( byte((keyState << 4)) ^ B11110000 ) { - return false; - } else { - return true; - } + if ( byte((keyState << 4)) ^ B11110000 ) { + return false; + } else { + return true; + } } boolean key_is_not_pressed (byte keyState) { - if ( byte((keyState << 4)) ^ B00000000 ) { - return false; - } else { - return true; - } + if ( byte((keyState << 4)) ^ B00000000 ) { + return false; + } else { + return true; + } } boolean key_toggled_on(byte keyState) { - if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) { - return true; - } else { - return false; - } + if (key_is_pressed(keyState) && key_was_not_pressed(keyState)) { + return true; + } else { + return false; + } } boolean key_toggled_off(byte keyState) { - if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) { - return true; - } else { - return false; - } + if (key_was_pressed(keyState) && key_is_not_pressed(keyState)) { + return true; + } else { + return false; + } } diff --git a/ArduinoKeyboard.ino b/ArduinoKeyboard.ino index e777b934..d54e65aa 100644 --- a/ArduinoKeyboard.ino +++ b/ArduinoKeyboard.ino @@ -46,237 +46,237 @@ byte current_layer = 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; iJ, + + if (digitalRead(colPins[col])) { + matrixState[row][col] |= 0; // noop. just here for clarity + } else { + matrixState[row][col] |= 1; // noop. just here for clarity + } + // while we're inspecting the electrical matrix, we look + // to see if the Key being held is a firmware level + // metakey, so we can act on it, lest we only discover + // that we should be looking at a seconary Keymap halfway through the matrix scan + - //scan the Keyboard matrix looking for connections - for (byte row = 0; row < ROWS; row++) { - digitalWrite(rowPins[row], LOW); - for (byte col = 0; col < COLS; col++) { - //If we see an electrical connection on I->J, - - if (digitalRead(colPins[col])) { - matrixState[row][col] |= 0; // noop. just here for clarity - } else { - matrixState[row][col] |= 1; // noop. just here for clarity - } - // while we're inspecting the electrical matrix, we look - // to see if the Key being held is a firmware level - // metakey, so we can act on it, lest we only discover - // 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); - } - } + // 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; + } } - digitalWrite(rowPins[row], HIGH); + } 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); + } + } + } - send_key_events(active_layer); + digitalWrite(rowPins[row], HIGH); + } + send_key_events(active_layer); } void setup() { - //usbMaxPower = 100; - Serial.begin(115200); - Keyboard.begin(); - Mouse.begin(); -//#ifdef DEBUG_SERIAL -//#endif - setup_matrix(); - Serial.println("loaded the matrix"); - current_layer = load_current_layer(); + //usbMaxPower = 100; + Serial.begin(115200); + Keyboard.begin(); + Mouse.begin(); + //#ifdef DEBUG_SERIAL + //#endif + setup_matrix(); + Serial.println("loaded the matrix"); + current_layer = load_current_layer(); } void loop() { - scan_matrix(); - // report_matrix(); - reset_matrix(); + scan_matrix(); + // report_matrix(); + reset_matrix(); } diff --git a/KeyboardDebugging.h b/KeyboardDebugging.h index a25fb466..ef17c4b3 100644 --- a/KeyboardDebugging.h +++ b/KeyboardDebugging.h @@ -3,30 +3,30 @@ void report_matrix() { #ifdef DEBUG_SERIAL - if (reporting_counter++ %100 == 0 ) { - for (byte row = 0; row < ROWS; row++) { - for (byte col = 0; col < COLS; col++) { - Serial.print(matrixState[row][col],HEX); - Serial.print(", "); + if (reporting_counter++ % 100 == 0 ) { + for (byte row = 0; row < ROWS; row++) { + for (byte col = 0; col < COLS; col++) { + Serial.print(matrixState[row][col], HEX); + Serial.print(", "); - } - Serial.println(""); - } - Serial.println(""); + } + Serial.println(""); } + Serial.println(""); + } #endif } void report(byte row, byte col, boolean value) { #ifdef DEBUG_SERIAL - Serial.print("Detected a change on "); - Serial.print(col); - Serial.print(" "); - Serial.print(row); - Serial.print(" to "); - Serial.print(value); - Serial.println("."); + Serial.print("Detected a change on "); + Serial.print(col); + Serial.print(" "); + Serial.print(row); + Serial.print(" to "); + Serial.print(value); + Serial.println("."); #endif } diff --git a/KeyboardEEPROM.h b/KeyboardEEPROM.h index 3dfea22c..c8ffd421 100644 --- a/KeyboardEEPROM.h +++ b/KeyboardEEPROM.h @@ -3,16 +3,16 @@ void save_current_layer(byte layer) { - EEPROM.write(EEPROM_LAYER_LOCATION, layer); + EEPROM.write(EEPROM_LAYER_LOCATION, layer); } byte load_current_layer() { - byte layer = EEPROM.read(EEPROM_LAYER_LOCATION); - if (layer >= LAYERS ) { - return 0; // undefined positions get saved as 255 - } - return layer; + byte layer = EEPROM.read(EEPROM_LAYER_LOCATION); + if (layer >= LAYERS ) { + return 0; // undefined positions get saved as 255 + } + return layer; } diff --git a/KeyboardMouse.h b/KeyboardMouse.h index 08833901..fbcb9dd2 100644 --- a/KeyboardMouse.h +++ b/KeyboardMouse.h @@ -1,59 +1,59 @@ double mouseActiveForCycles = 0; -float carriedOverX =0; -float carriedOverY =0; +float carriedOverX = 0; +float carriedOverY = 0; double mouse_accel (double cycles) { - double accel = atan((cycles/50)-5); - accel += 1.5707963267944; // we want the whole s curve, not just the bit that's usually above the x and y axes; - accel = accel *0.85; - if (accel<0.25) { - accel =0.25; - } - return accel; + double accel = atan((cycles / 50) - 5); + accel += 1.5707963267944; // we want the whole s curve, not just the bit that's usually above the x and y axes; + accel = accel * 0.85; + if (accel < 0.25) { + accel = 0.25; + } + return accel; } void handle_mouse_movement( char x, char y) { - if (x!=0 || y!=0) { - mouseActiveForCycles++; - double accel = (double) mouse_accel(mouseActiveForCycles); - float moveX=0; - float moveY=0; - if (x>0) { - moveX = (x*accel) + carriedOverX; - carriedOverX = moveX - floor(moveX); - } else if(x<0) { - moveX = (x*accel) - carriedOverX; - carriedOverX = ceil(moveX) - moveX; - } + if (x != 0 || y != 0) { + mouseActiveForCycles++; + double accel = (double) mouse_accel(mouseActiveForCycles); + float moveX = 0; + float moveY = 0; + if (x > 0) { + moveX = (x * accel) + carriedOverX; + carriedOverX = moveX - floor(moveX); + } else if (x < 0) { + moveX = (x * accel) - carriedOverX; + carriedOverX = ceil(moveX) - moveX; + } - if (y >0) { - moveY = (y*accel) + carriedOverY; - carriedOverY = moveY - floor(moveY); - } else if (y<0) { - moveY = (y*accel) - carriedOverY; - carriedOverY = ceil(moveY) - moveY; - } + if (y > 0) { + moveY = (y * accel) + carriedOverY; + carriedOverY = moveY - floor(moveY); + } else if (y < 0) { + moveY = (y * accel) - carriedOverY; + carriedOverY = ceil(moveY) - moveY; + } #ifdef DEBUG_SERIAL - Serial.println(); - Serial.print("cycles: "); - Serial.println(mouseActiveForCycles); - Serial.print("Accel: "); - Serial.print(accel); - Serial.print(" moveX is "); - Serial.print(moveX); - Serial.print(" moveY is "); - Serial.print(moveY); - Serial.print(" carriedoverx is "); - Serial.print(carriedOverX); - Serial.print(" carriedOverY is "); - Serial.println(carriedOverY); + Serial.println(); + Serial.print("cycles: "); + Serial.println(mouseActiveForCycles); + Serial.print("Accel: "); + Serial.print(accel); + Serial.print(" moveX is "); + Serial.print(moveX); + Serial.print(" moveY is "); + Serial.print(moveY); + Serial.print(" carriedoverx is "); + Serial.print(carriedOverX); + Serial.print(" carriedOverY is "); + Serial.println(carriedOverY); #endif - Mouse.move(moveX,moveY, 0); - } else { - mouseActiveForCycles=0; - } + Mouse.move(moveX, moveY, 0); + } else { + mouseActiveForCycles = 0; + } }