an adapter from any connected PS/2 keyboard to USB HID output
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /** 00002 * 00003 * 00004 * PS/2 Code from : https://os.mbed.com/cookbook/PS2 00005 * Conversion code from: https://gist.github.com/Slumber86/4439a6a5f2fcb5ece61a5c24b962535d 00006 * https://github.com/keyboardio/KeyboardioHID/blob/master/src/BootKeyboard/BootKeyboard.cpp 00007 * Explantion of PS/2 protocol: https://web.archive.org/web/20070321032851/https://www.beyondlogic.org/keyboard/keybrd.htm 00008 * Explanation of keycodes of different layouts: http://www.quadibloc.com/comp/scan.htm 00009 * Source with LED command on arduino: https://playground.arduino.cc/PS2Keyboard/Cpp/ 00010 */ 00011 00012 #include "mbed.h" 00013 #include "USBKeyboard.h" 00014 #include "PS2Keyboard.h" 00015 #include "Keymaps.h" 00016 00017 DigitalOut myled2(LED2); 00018 DigitalOut myled4(LED4); 00019 PS2Keyboard ps2kb(p22, p23); //PS/2 keyboard (Clock, Data) 00020 USBKeyboard keyboard; 00021 LAYOUTS layo = LO_DE; //Layout of connected PS/2 keyboard, important only when additional keys are used 00022 bool ext, brk; 00023 int skip; 00024 uint8_t report[9]; //array with data for USB HID report 00025 Keymaps Kmap(layo, false); //keymap for normal keys 00026 Keymaps KmapE(layo, true); //keymap for keys with E0 prefix 00027 uint8_t leds; 00028 bool send_leds; 00029 00030 00031 void report_add(uint8_t k); //add USB keycode to report array 00032 void report_remove(uint8_t k); //remove USB keycode from report array 00033 00034 int main() 00035 { 00036 myled2=1; 00037 PS2Keyboard::keyboard_event_t evt_kb; 00038 while (!keyboard.configured()) { // wait until keyboard is configured 00039 } 00040 wait(1.0); 00041 myled2=0; 00042 myled4=1; 00043 while (1) { 00044 if (ps2kb.processing(&evt_kb)) { 00045 uint8_t k = evt_kb.scancode; //scancode of PS/2 keyboard 00046 uint8_t k2 = 0; //translated keycode in USB format 00047 00048 if (k) { 00049 if (skip) { 00050 --skip; 00051 } else { 00052 if (k == 0xE0) { //special key, lookup in KE konversion table 00053 ext = true; 00054 } else if (k == 0xF0) { //breakcode, following scancode will be removed from report 00055 brk = true; 00056 } else if (k == 0xFA) { //acknowledgement from keyboard 00057 if (send_leds) { 00058 send_leds = false; 00059 //send_msg(leds); //send LED status to keyboard, not functional 00060 } 00061 } else { 00062 if (k == 0xE1) { //only one multibyte scancode uses this prefix 00063 k2 = 72; 00064 skip = 7; //following scancodes not nesessary for indentification, are skiped 00065 brk = true; //no longpress of this key possible 00066 report_add(k2); 00067 keyboard.scanCodeRep(report); 00068 } else { 00069 k2 = ext ? KmapE.K[k] : Kmap.K[k]; // konvesion from PS/2 to USB HID keycode 00070 } 00071 00072 if (k2) { 00073 if (brk) { 00074 report_remove(k2); 00075 if (k2 == 83 || k2 == 71 || k2 == 57) { //keys relevant for keyboard LEDs 00076 send_leds = true; 00077 if (k2 == 83) { 00078 leds ^= 2; 00079 } else if (k2 == 71) { 00080 leds ^= 1; 00081 } else if (k2 == 57) { 00082 leds ^= 4; 00083 } 00084 //send_msg(0xED); //send LED status to keyboard, not functional 00085 } 00086 } else { 00087 report_add(k2); 00088 } 00089 keyboard.scanCodeRep(report); 00090 brk = false; 00091 ext = false; 00092 } 00093 } 00094 } 00095 } 00096 } 00097 } 00098 } 00099 00100 00101 void report_remove(uint8_t k) 00102 { 00103 uint8_t i; 00104 if (k >= 224) { //is modifier key 00105 report[1] &= ~(1 << (k - 224)); 00106 } else { 00107 for (i = 3; i < 9; ++i) { //loop only normal key places 00108 if (report[i] == k) { //if key in report 00109 report[i] = 0; 00110 break; 00111 } 00112 } 00113 } 00114 } 00115 00116 void report_add(uint8_t k) 00117 { 00118 uint8_t i; 00119 if (k >= 224) { //is modifier key 00120 report[1] |= 1 << (k - 224); 00121 } else if (report[3] != k && report[4] != k && 00122 report[5] != k && report[6] != k && 00123 report[7] != k && report[8] != k) { //not already set in report 00124 for (i = 3; i < 9; ++i) { //loop only normal key 00125 if (report[i] == 0) { //first free place is set 00126 report[i] = k; 00127 break; 00128 } 00129 } 00130 } 00131 }
Generated on Fri Jul 15 2022 10:09:06 by
1.7.2