PS2Keyboardのサンプルをベースに、とりあえず動くベータ版を実装してみた。すごーく面倒でした。
Simple_Test.ino
/* PS2Keyboard library example PS2Keyboard now requries both pins specified for begin() keyboard.begin(data_pin, irq_pin); Valid irq pins: Arduino Uno: 2, 3 Arduino Due: All pins, except 13 (LED) Arduino Mega: 2, 3, 18, 19, 20, 21 Teensy 2.0: All pins, except 13 (LED) Teensy 2.0: 5, 6, 7, 8 Teensy 1.0: 0, 1, 2, 3, 4, 6, 7, 16 Teensy++ 2.0: 0, 1, 2, 3, 18, 19, 36, 37 Teensy++ 1.0: 0, 1, 2, 3, 18, 19, 36, 37 Sanguino: 2, 10, 11 for more information you can read the original wiki in arduino.cc at http://www.arduino.cc/playground/Main/PS2Keyboard or http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html Like the Original library and example this is under LGPL license. Modified by Cuninganreset@gmail.com on 2010-03-22 Modified by Paul Stoffregen <paul@pjrc.com> June 2010 */ #include <PS2Keyboard.h> #include <SoftwareSerial.h> SoftwareSerial mySerial(6, 7); // RX, TX PS2Keyboard keyboard; const int DataPin = 4; const int IRQpin = 3; uint8_t state=0; boolean KANA=false; boolean CAPS=false; boolean NUML=false; boolean SHIFT=false; boolean CTRL=false; boolean ALT=false; uint8_t ROWL0=0xf0; uint8_t ROWL1=0xf1; uint8_t ROWL2=0xf2; uint8_t ROWL3=0xf3; uint8_t ROWL4=0xf4; uint8_t ROWL5=0xf5; uint8_t ROWL6=0xf6; uint8_t ROWL7=0xf7; uint8_t ROWL8=0xf8; uint8_t ROWL9=0xf9; uint8_t ROWLA=0xfa; uint8_t ROWLB=0xfb; uint8_t ROWLC=0xfc; uint8_t ROWLD=0xfd; uint8_t ROWLE=0xfe; uint8_t ROWH0=0xff; uint8_t ROWH1=0xff; uint8_t ROWH2=0xff; uint8_t ROWH3=0xff; uint8_t ROWH4=0xff; uint8_t ROWH5=0xff; uint8_t ROWH6=0xff; uint8_t ROWH7=0xff; uint8_t ROWH8=0xff; uint8_t ROWH9=0xff; uint8_t ROWHA=0xff; uint8_t ROWHB=0xff; uint8_t ROWHC=0xff; uint8_t ROWHD=0xff; uint8_t ROWHE=0xf7; void setup() { delay(1000); keyboard.begin(DataPin, IRQpin); Serial.begin(9600); Serial.println("Keyboard Test:"); mySerial.begin(19200); } #define BREAK 0x01 #define MODIFIER 0x02 void loop() { if (keyboard.available2()) { uint8_t s = keyboard.read2(); if (s == 0xF0) { state |= BREAK; } else if (s == 0xE0) { state |= MODIFIER; Serial.print("M:"); } else if (s == 0xE1) { Serial.print("P:"); } else { if (state & BREAK) { Serial.print("U"); Serial.println(s); if (NUML) { if (s==112 || s==105 || s==114 || s==122 || s==107 || s==115 || s==116 || s==108) { if (s==112) ROWL0|=0x10; // 11100000 11111111 [0] if (s==105) ROWL0|=0x20; // 11010000 11111111 [1] if (s==114) ROWL0|=0x40; // 10110000 11111111 [2] if (s==122) ROWL0|=0x80; // 01110000 11111111 [3] if (s==107) ROWH0|=0x01; // 11110000 11111110 [4] if (s==115) ROWH0|=0x02; // 11110000 11111101 [5] if (s==116) ROWH0|=0x04; // 11110000 11111011 [6] if (s==108) ROWH0|=0x08; // 11110000 11110111 [7] mySerial.write2(ROWL0,ROWH0); } if (s==117 || s==125 || s==124 || s==121 || s==113 || s==90) { if (s==117) ROWL1|=0x10; // 11100001 11101111 [8] if (s==125) ROWL1|=0x20; // 11010001 11101111 [9] if (s==124) ROWL1|=0x40; // 10110001 11101111 [*] if (s==121) ROWL1|=0x80; // 01110001 11101111 [+] // if (s==) ROWH1|=0x01; // 11110001 11101110 [=] // if (s==) ROWH1|=0x02; // 11110001 11101101 [,] if (s==113) ROWH1|=0x04; // 11110001 11101011 [.] if (s==90 ) ROWH1|=0x08; // 11110001 11100111 [ENTER] mySerial.write2(ROWL1,ROWH1); } } else { if (s==90 ) { ROWH1|=0x08; // 11110001 11100111 [ENTER] mySerial.write2(ROWL1,ROWH1); } if (s==108 || s==117 || s==116 || s==102) { if (s==108) ROWL8|=0x10; // 11101000 11101111 [HOME] if (s==117) ROWL8|=0x20; // 11011000 11101111 [UP] if (s==116) ROWL8|=0x40; // 10111000 11101111 [RIGHT] if (s==102) ROWL8|=0x80; // 01111000 11101111 [BS] mySerial.write2(ROWL8,ROWH8); } if (s==13 || s==114 || s==107 || s==9 || s==120) { if (s==13 ) ROWLA|=0x10; // 11101010 11111111 [TAB] if (s==114) ROWLA|=0x20; // 11011010 11111111 [DOWN] if (s==107) ROWLA|=0x40; // 10111010 11111111 [LEFT] if (s==9 ) ROWLA|=0x80; // 01111010 11111111 [F11] if (s==120) ROWHA|=0x01; // 11111010 11111110 [F10] // if (s==78 ) ROWHA|=0x02; // 11111010 11111101 [-] // if (s==74 ) ROWHA|=0x04; // 11111010 11111011 [/] // if (s==88 ) ROWHA|=0x08; // 11111010 11110111 [CAPS] mySerial.write2(ROWLA,ROWHA); } if (s==125 || s==122) { if (s==125) ROWLB|=0x10; // 11101011 11101111 [PAGEUP] if (s==122) ROWLB|=0x20; // 11011011 11101111 [PAGEDOWN] mySerial.write2(ROWLB,ROWHB); } if (s==39) { ROWLD|=0x40; // 10111101 11101111 [田] mySerial.write2(ROWLD,ROWHD); } } if (s==84 || s==28 || s==50 || s==33 || s==35 || s==36 || s==43 || s==52) { if (s==84 ) ROWL2|=0x10; // 11100010 11101111 [@] if (s==28 ) ROWL2|=0x20; // 11010010 11101111 [a] if (s==50 ) ROWL2|=0x40; // 10110010 11101111 [b] if (s==33 ) ROWL2|=0x80; // 01110010 11101111 [c] if (s==35 ) ROWH2|=0x01; // 11110010 11101110 [d] if (s==36 ) ROWH2|=0x02; // 11110010 11101101 [e] if (s==43 ) ROWH2|=0x04; // 11110010 11101011 [f] if (s==52 ) ROWH2|=0x08; // 11110010 11100111 [g] mySerial.write2(ROWL2,ROWH2); } if (s==51 || s==67 || s==59 || s==66 || s==75 || s==58 || s==49 || s==68) { if (s==51 ) ROWL3|=0x10; // 11100011 11111111 [h] if (s==67 ) ROWL3|=0x20; // 11010011 11111111 [i] if (s==59 ) ROWL3|=0x40; // 10110011 11111111 [j] if (s==66 ) ROWL3|=0x80; // 01110011 11111111 [k] if (s==75 ) ROWH3|=0x01; // 11110011 11111110 [l] if (s==58 ) ROWH3|=0x02; // 11110011 11111101 [m] if (s==49 ) ROWH3|=0x04; // 11110011 11111011 [n] if (s==68 ) ROWH3|=0x08; // 11110011 11110111 [o] mySerial.write2(ROWL3,ROWH3); } if (s==77 || s==21 || s==45 || s==27 || s==44 || s==60 || s==42 || s==29) { if (s==77 ) ROWL4|=0x10; // 11100100 11101111 [p] if (s==21 ) ROWL4|=0x20; // 11010100 11101111 [q] if (s==45 ) ROWL4|=0x40; // 10110100 11101111 [r] if (s==27 ) ROWL4|=0x80; // 01110100 11101111 [s] if (s==44 ) ROWH4|=0x01; // 11110100 11101110 [t] if (s==60 ) ROWH4|=0x02; // 11110100 11101101 [u] if (s==42 ) ROWH4|=0x04; // 11110100 11101011 [v] if (s==29 ) ROWH4|=0x08; // 11110100 11100111 [w] mySerial.write2(ROWL4,ROWH4); } if (s==34 || s==53 || s==26 || s==91 || s==106 || s==93 || s==85 || s==78 || s==123) { if (s==34 ) ROWL5|=0x10; // 11100101 11111111 [x] if (s==53 ) ROWL5|=0x20; // 11010101 11111111 [y] if (s==26 ) ROWL5|=0x40; // 10110101 11111111 [z] if (s==91 ) ROWL5|=0x80; // 01110101 11111111 [[] if (s==106) ROWH5|=0x01; // 11110101 11111110 [\] if (s==93 ) ROWH5|=0x02; // 11110101 11111101 []] if (s==85 ) ROWH5|=0x04; // 11110101 11111011 [^] if (s==78 ) ROWH5|=0x08; // 11110101 11110111 [-] if (s==123) ROWH5|=0x08; // 11110101 11110111 [-] mySerial.write2(ROWL5,ROWH5); } if (s==69 || s==22 || s==30 || s==38 || s==37 || s==46 || s==54 || s==61) { if (s==69 ) ROWL6|=0x10; // 11100110 11111111 [0] if (s==22 ) ROWL6|=0x20; // 11010110 11111111 [1] if (s==30 ) ROWL6|=0x40; // 10110110 11111111 [2] if (s==38 ) ROWL6|=0x80; // 01110110 11111111 [3] if (s==37 ) ROWH6|=0x01; // 11110110 11111110 [4] if (s==46 ) ROWH6|=0x02; // 11110110 11111101 [5] if (s==54 ) ROWH6|=0x04; // 11110110 11111011 [6] if (s==61 ) ROWH6|=0x08; // 11110110 11110111 [7] mySerial.write2(ROWL6,ROWH6); } if (s==62 || s==70 || s==82 || s==76 || s==65 || s==73 || s==74 || s==81) { if (s==62 ) ROWL7|=0x10; // 11100111 11101111 [8] if (s==70 ) ROWL7|=0x20; // 11010111 11101111 [9] if (s==82 ) ROWL7|=0x40; // 10110111 11101111 [:] if (s==76 ) ROWL7|=0x80; // 01110111 11101111 [;] if (s==65 ) ROWH7|=0x01; // 11110111 11101110 [,] if (s==73 ) ROWH7|=0x02; // 11110111 11101101 [.] if (s==74 ) ROWH7|=0x04; // 11110111 11101011 [/] if (s==81 ) ROWH7|=0x08; // 11110111 11100111 [_] mySerial.write2(ROWL7,ROWH7); } if (s==7 || s==5 || s==6 || s==4 || s==12 || s==3 || s==41 || s==118) { if (s==7 ) ROWL9|=0x10; // 11101001 11111111 [F12] if (s==5 ) ROWL9|=0x20; // 11011001 11111111 [F1] if (s==6 ) ROWL9|=0x40; // 10111001 11111111 [F2] if (s==4 ) ROWL9|=0x80; // 01111001 11111111 [F3] if (s==12 ) ROWH9|=0x01; // 11111001 11111110 [F4] if (s==3 ) ROWH9|=0x02; // 11111001 11111101 [F5] if (s==41 ) ROWH9|=0x04; // 11111001 11111011 [SPACE] if (s==118) ROWH9|=0x08; // 11111001 11110111 [ESC] mySerial.write2(ROWL9,ROWH9); } if (s==19 ) KANA=!KANA; if (!KANA) { ROWH8|=0x02; // 11111000 11111101 [KANA] mySerial.write2(ROWL8,ROWH8); } if (s == 88) CAPS=!CAPS; if (!CAPS) { ROWHA|=0x08; // 11111000 11100111 [CAPS] mySerial.write2(ROWLA,ROWHA); } if (s==18 || s==89) SHIFT=false; if (!SHIFT) { ROWH8|=0x04; // 11111000 11111011 [SHIFT] mySerial.write2(ROWL8,ROWH8); } if (s==20 ) CTRL=false; if (!CTRL) { ROWH8|=0x08; // 11111000 11110111 [CTRL] mySerial.write2(ROWL8,ROWH8); } if (s==17 ) ALT=false; if (!ALT) { ROWH8|=0x01; // 11111000 11111110 [ALT] mySerial.write2(ROWL8,ROWH8); } if (s==119 ) NUML=!NUML; if (s==31) { // [田] while (keyboard.available2()) keyboard.read2(); state=0; KANA=false; CAPS=false; NUML=false; SHIFT=false; ROWL0=0xf0; ROWL1=0xf1; ROWL2=0xf2; ROWL3=0xf3; ROWL4=0xf4; ROWL5=0xf5; ROWL6=0xf6; ROWL7=0xf7; ROWL8=0xf8; ROWL9=0xf9; ROWLA=0xfa; ROWLB=0xfb; ROWLC=0xfc; ROWLD=0xfd; ROWLE=0xfe; ROWH0=0xff; ROWH1=0xff; ROWH2=0xff; ROWH3=0xff; ROWH4=0xff; ROWH5=0xff; ROWH6=0xff; ROWH7=0xff; ROWH8=0xff; ROWH9=0xff; ROWHA=0xff; ROWHB=0xff; ROWHC=0xff; ROWHD=0xff; ROWHE=0xf7; for (uint8_t c = 0Xf0;c < 0Xff; c++) { mySerial.write2(c, 0xff); delay(30); } } state &= ~(BREAK | MODIFIER); } else { Serial.print("D"); Serial.println(s); if (KANA) { ROWL8=0xf8; ROWH8=0xfd; // 11111000 11111101 [KANA] mySerial.write2(ROWL8,ROWH8); } if (CAPS) { ROWLA=0xfa; ROWHA=0xf7; // 11111010 11100111 [CAPS] mySerial.write2(ROWLA,ROWHA); } if (s==18 && !(state & MODIFIER)) SHIFT=true; if (s==89) SHIFT=true; if (SHIFT) { Serial.println("SHIFT"); ROWH8&=0xfb; // 11111000 11111011 [SHIFT] mySerial.write2(ROWL8,ROWH8); } if (s==20 ) CTRL=true; if (CTRL) { Serial.println("CTRL"); ROWH8&=0xf7; // 11111000 11110111 [CTRL] mySerial.write2(ROWL8,ROWH8); } if (s==17 ) ALT=true; if (ALT) { Serial.println("ALT"); ROWH8&=0xfe; // 11111000 11111110 [ALT] mySerial.write2(ROWL8,ROWH8); } if (NUML) { if (s==112 || s==105 || s==114 || s==122 || s==107 || s==115 || s==116 || s==108) { if (s==112) ROWL0&=0xef; // 11100000 11111111 [0] if (s==105) ROWL0&=0xdf; // 11010000 11111111 [1] if (s==114) ROWL0&=0xbf; // 10110000 11111111 [2] if (s==122) ROWL0&=0x7f; // 01110000 11111111 [3] if (s==107) ROWH0&=0xfe; // 11110000 11111110 [4] if (s==115) ROWH0&=0xfd; // 11110000 11111101 [5] if (s==116) ROWH0&=0xfb; // 11110000 11111011 [6] if (s==108) ROWH0&=0xf7; // 11110000 11110111 [7] mySerial.write2(ROWL0,ROWH0); } if (s==117 || s==125 || s==124 || s==121 || s==113 || s==90) { if (s==117) ROWL1&=0xef; // 11100001 11101111 [8] if (s==125) ROWL1&=0xdf; // 11010001 11101111 [9] if (s==124) ROWL1&=0xbf; // 10110001 11101111 [*] if (s==121) ROWL1&=0x7f; // 01110001 11101111 [+] // if (s==) ROWH1&=0xfe; // 11110001 11101110 [=] // if (s==) ROWH1&=0xfd; // 11110001 11101101 [,] if (s==113) ROWH1&=0xfb; // 11110001 11101011 [.] if (s==90 ) ROWH1&=0xf7; // 11110001 11100111 [ENTER] mySerial.write2(ROWL1,ROWH1); } } else { if (s==90 ) { ROWH1&=0xf7; // 11110001 11100111 [ENTER] mySerial.write2(ROWL1,ROWH1); } if (s==108 || s==117 || s==116 || s==102) { if (s==108) ROWL8&=0xef; // 11101000 11101111 [HOME] if (s==117) ROWL8&=0xdf; // 11011000 11101111 [UP] if (s==116) ROWL8&=0xbf; // 10111000 11101111 [RIGHT] if (s==102) ROWL8&=0x7f; // 01111000 11101111 [BS] mySerial.write2(ROWL8,ROWH8); } if (s==13 || s==114 || s==107 || s==9 || s==120) { if (s==13 ) ROWLA&=0xef; // 11101010 11111111 [TAB] if (s==114) ROWLA&=0xdf; // 11011010 11111111 [DOWN] if (s==107) ROWLA&=0xbf; // 10111010 11111111 [LEFT] if (s==9 ) ROWLA&=0x7f; // 01111010 11111111 [F11] if (s==120) ROWHA&=0xfe; // 11111010 11111110 [F10] // if (s==78 ) ROWHA&=0xfd; // 11111010 11111101 [-] // if (s==74 ) ROWHA&=0xfb; // 11111010 11111011 [/] // if (s==88 ) ROWHA&=0xf7; // 11111010 11110111 [CAPS] mySerial.write2(ROWLA,ROWHA); } if (s==125 || s==122) { if (s==125) ROWLB&=0xef; // 11101011 11101111 [PAGEUP] if (s==122) ROWLB&=0xdf; // 11011011 11101111 [PAGEDOWN] mySerial.write2(ROWLB,ROWHB); } if (s==39) { ROWLD&=0xbf; // 10111101 11101111 [田] mySerial.write2(ROWLD,ROWHD); } } if (s==84 || s==28 || s==50 || s==33 || s==35 || s==36 || s==43 || s==52) { if (s==84 ) ROWL2&=0xef; // 11100010 11101111 [@] if (s==28 ) ROWL2&=0xdf; // 11010010 11101111 [a] if (s==50 ) ROWL2&=0xbf; // 10110010 11101111 [b] if (s==33 ) ROWL2&=0x7f; // 01110010 11101111 [c] if (s==35 ) ROWH2&=0xfe; // 11110010 11101110 [d] if (s==36 ) ROWH2&=0xfd; // 11110010 11101101 [e] if (s==43 ) ROWH2&=0xfb; // 11110010 11101011 [f] if (s==52 ) ROWH2&=0xf7; // 11110010 11100111 [g] mySerial.write2(ROWL2,ROWH2); } if (s==51 || s==67 || s==59 || s==66 || s==75 || s==58 || s==49 || s==68) { if (s==51 ) ROWL3&=0xef; // 11100011 11111111 [h] if (s==67 ) ROWL3&=0xdf; // 11010011 11111111 [i] if (s==59 ) ROWL3&=0xbf; // 10110011 11111111 [j] if (s==66 ) ROWL3&=0x7f; // 01110011 11111111 [k] if (s==75 ) ROWH3&=0xfe; // 11110011 11111110 [l] if (s==58 ) ROWH3&=0xfd; // 11110011 11111101 [m] if (s==49 ) ROWH3&=0xfb; // 11110011 11111011 [n] if (s==68 ) ROWH3&=0xf7; // 11110011 11110111 [o] mySerial.write2(ROWL3,ROWH3); } if (s==77 || s==21 || s==45 || s==27 || s==44 || s==60 || s==42 || s==29) { if (s==77 ) ROWL4&=0xef; // 11100100 11101111 [p] if (s==21 ) ROWL4&=0xdf; // 11010100 11101111 [q] if (s==45 ) ROWL4&=0xbf; // 10110100 11101111 [r] if (s==27 ) ROWL4&=0x7f; // 01110100 11101111 [s] if (s==44 ) ROWH4&=0xfe; // 11110100 11101110 [t] if (s==60 ) ROWH4&=0xfd; // 11110100 11101101 [u] if (s==42 ) ROWH4&=0xfb; // 11110100 11101011 [v] if (s==29 ) ROWH4&=0xf7; // 11110100 11100111 [w] mySerial.write2(ROWL4,ROWH4); } if (s==34 || s==53 || s==26 || s==91 || s==106 || s==93 || s==85 || s==78 || s==123) { if (s==34 ) ROWL5&=0xef; // 11100101 11111111 [x] if (s==53 ) ROWL5&=0xdf; // 11010101 11111111 [y] if (s==26 ) ROWL5&=0xbf; // 10110101 11111111 [z] if (s==91 ) ROWL5&=0x7f; // 01110101 11111111 [[] if (s==106) ROWH5&=0xfe; // 11110101 11111110 [\] if (s==93 ) ROWH5&=0xfd; // 11110101 11111101 []] if (s==85 ) ROWH5&=0xfb; // 11110101 11111011 [^] if (s==78 ) ROWH5&=0xf7; // 11110101 11110111 [-] if (s==123) ROWH5&=0xf7; // 11110101 11110111 [-] mySerial.write2(ROWL5,ROWH5); } if (s==69 || s==22 || s==30 || s==38 || s==37 || s==46 || s==54 || s==61) { if (s==69 ) ROWL6&=0xef; // 11100110 11111111 [0] if (s==22 ) ROWL6&=0xdf; // 11010110 11111111 [1] if (s==30 ) ROWL6&=0xbf; // 10110110 11111111 [2] if (s==38 ) ROWL6&=0x7f; // 01110110 11111111 [3] if (s==37 ) ROWH6&=0xfe; // 11110110 11111110 [4] if (s==46 ) ROWH6&=0xfd; // 11110110 11111101 [5] if (s==54 ) ROWH6&=0xfb; // 11110110 11111011 [6] if (s==61 ) ROWH6&=0xf7; // 11110110 11110111 [7] mySerial.write2(ROWL6,ROWH6); } if (s==62 || s==70 || s==82 || s==76 || s==65 || s==73 || s==74 || s==81) { if (s==62 ) ROWL7&=0xef; // 11100111 11101111 [8] if (s==70 ) ROWL7&=0xdf; // 11010111 11101111 [9] if (s==82 ) ROWL7&=0xbf; // 10110111 11101111 [:] if (s==76 ) ROWL7&=0x7f; // 01110111 11101111 [;] if (s==65 ) ROWH7&=0xfe; // 11110111 11101110 [,] if (s==73 ) ROWH7&=0xfd; // 11110111 11101101 [.] if (s==74 ) ROWH7&=0xfb; // 11110111 11101011 [/] if (s==81 ) ROWH7&=0xf7; // 11110111 11100111 [_] mySerial.write2(ROWL7,ROWH7); } if (s==7 || s==5 || s==6 || s==4 || s==12 || s==3 || s==41 || s==118) { if (s==7 ) ROWL9&=0xef; // 11101001 11111111 [F12] if (s==5 ) ROWL9&=0xdf; // 11011001 11111111 [F1] if (s==6 ) ROWL9&=0xbf; // 10111001 11111111 [F2] if (s==4 ) ROWL9&=0x7f; // 01111001 11111111 [F3] if (s==12 ) ROWH9&=0xfe; // 11111001 11111110 [F4] if (s==3 ) ROWH9&=0xfd; // 11111001 11111101 [F5] if (s==41 ) ROWH9&=0xfb; // 11111001 11111011 [SPACE] if (s==118) ROWH9&=0xf7; // 11111001 11110111 [ESC] mySerial.write2(ROWL9,ROWH9); } } } } }