diff --git a/arduino_micro_code/arduino_micro_code.ino b/arduino_micro_code/arduino_micro_code.ino index 3d70867..26378e3 100644 --- a/arduino_micro_code/arduino_micro_code.ino +++ b/arduino_micro_code/arduino_micro_code.ino @@ -1,40 +1,169 @@ - +#include #include +// Setup --------------------------------------------- + int SoftSerialRX = 8; int SoftSerialTX = 9; -int lastRead = 0; +// Types --------------------------------------------- + +#define KEY_F13 0xF0 // 0x68 + 0x88 +#define KEY_F14 0xF1 // 0x69 + 0x88 +#define KEY_F15 0xF2 // 0x6A + 0x88 +#define KEY_F16 0xF3 // 0x6B + 0x88 +#define KEY_F17 0xF4 // 0x6C + 0x88 +#define KEY_F18 0xF5 // 0x6D + 0x88 + +enum parse_mode{ + none, + regular_key, + special_key, + trackball +}; + +// Globals ------------------------------------------- + +parse_mode ParseMode; SoftwareSerial KeyboardSerial(SoftSerialRX, SoftSerialTX); +unsigned long time_since_last_loop; +bool shift; + +// Functions ----------------------------------------- void setup() { + digitalWrite(SoftSerialTX, HIGH); pinMode(SoftSerialRX, INPUT); pinMode(SoftSerialTX, OUTPUT); Serial.begin(2400); + Keyboard.begin(); + ParseMode = parse_mode::none; KeyboardSerial.begin(2400); while (!Serial) { ; } + time_since_last_loop = millis(); + shift = false; +} + +void parseRegularKey(unsigned long v) +{ + char result; + bool parsed = false; + if ((v & 32) == 32) + { + shift = true; + } + else + { + shift = false; + } + if (v <= 126) + { // main keyboard + result = v; + parsed = true; + } + // A few keys that need some adjustments to match their label. + if (v == 13) result = '\n'; + if (v == 94) result = '^'; + if (v == 126) result = '~'; + // tab + if (v == 129){ result = 9; shift = false; parsed = true; } + // shift+tab + if (v == 130) + { + Keyboard.press(KEY_LEFT_SHIFT); + Keyboard.press(KEY_TAB); + parsed = false; + } + if (parsed == true) + { + Keyboard.print(result); + } +} + +void parseSpecialKey(unsigned long v) +{ + switch(v) + { + case 49: + Keyboard.press(KEY_HOME); + break; + case 50: // insert line + Keyboard.write(KEY_HOME); + Keyboard.write(KEY_RETURN); + Keyboard.write(KEY_UP_ARROW); + break; + case 51: // delete line (using the vim way for this) + // non-vim people might want to do: + // END-SHIFT-HOME-BS or something... + Keyboard.write(KEY_ESC); + Keyboard.write('d'); + Keyboard.write('d'); + Keyboard.write('i'); + break; + case 53: // insert char - using this as regular insert. + Keyboard.write(KEY_INSERT); + break; + case 54: // delete char - using this as regular delete. + Keyboard.write(KEY_DELETE); + break; + case 48: // up / down arrow (they send the same keycode?!) + Keyboard.write(KEY_UP_ARROW); + break; + case 120: + Keyboard.write(KEY_RIGHT_ARROW); + break; + case 126: + Keyboard.write(KEY_LEFT_ARROW); + break; + } + // F1..F12 + if ((v >= 86) && (v <= 97)) + { + Keyboard.write(v + 108); + } + if (v == 98) Keyboard.write(KEY_F13); + if (v == 99) Keyboard.write(KEY_F14); + if (v == 100) Keyboard.write(KEY_F14); + // the 'cancel' key we'll use as ESC key, since there + // is no actual key labeled escape. + if (v == 101) Keyboard.write(KEY_ESC); } void loop() { - + KeyboardSerial.listen(); if (KeyboardSerial.available()) { - Serial.println(KeyboardSerial.read(), BIN); + unsigned long v = KeyboardSerial.read(); + switch (v) + { + case 0xA8: + ParseMode = parse_mode::regular_key; + Serial.println("Regular key"); + break; + case 0xA1: + ParseMode = parse_mode::special_key; + Serial.println("Special key"); + break; + default: + if (ParseMode == parse_mode::regular_key) + { + parseRegularKey(v); + } + else if (ParseMode == parse_mode::special_key) + { + parseSpecialKey(v); + } + break; + } + Serial.println(v, BIN); + Serial.println(v, DEC); } - if (Serial.available()) + else { - Serial.println("O"); - KeyboardSerial.write(Serial.read()); + Keyboard.releaseAll(); } - - /* - int v = digitalRead(SoftSerialRX); - if (v != lastRead){ - Serial.write(v == 0 ? "0" : "1"); - lastRead = v; - } - */ + delay(20); }