diff --git a/mac_plus_ps2/PS2Keyboard.cpp b/mac_plus_ps2/PS2Keyboard.cpp index c1b03b1..709d5ca 100644 --- a/mac_plus_ps2/PS2Keyboard.cpp +++ b/mac_plus_ps2/PS2Keyboard.cpp @@ -6,7 +6,32 @@ ** Mostly rewritten Paul Stoffregen 2010, 2011 ** Modified for use beginning with Arduino 13 by L. Abraham Smith, * ** Modified for easy interrup pin assignement on method begin(datapin,irq_pin). Cuningan ** - ** Custom version modifed by Tomasz Rękawek to only provide scancodes. + + 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 + + Version 2.4 (March 2013) + - Support Teensy 3.0, Arduino Due, Arduino Leonardo & other boards + - French keyboard layout, David Chochoi, tchoyyfr at yahoo dot fr + + Version 2.3 (October 2011) + - Minor bugs fixed + + Version 2.2 (August 2011) + - Support non-US keyboards - thanks to Rainer Bruch for a German keyboard :) + + Version 2.1 (May 2011) + - timeout to recover from misaligned input + - compatibility with Arduino "new-extension" branch + - TODO: send function, proposed by Scott Penrose, scooterda at me dot com + + Version 2.0 (June 2010) + - Buffering added, many scan codes can be captured without data loss + if your sketch is busy doing other work + - Shift keys supported, completely rewritten scan code to ascii + - Slow linear search replaced with fast indexed table lookups + - Support for Teensy, Arduino Mega, and Sanguino added This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -29,6 +54,9 @@ static volatile uint8_t buffer[BUFFER_SIZE]; static volatile uint8_t head, tail; static uint8_t DataPin; +static uint8_t CharBuffer=0; +static uint8_t UTF8next=0; +static const PS2Keymap_t *keymap=NULL; // The ISR for the external interrupt void ps2interrupt(void) @@ -63,7 +91,7 @@ void ps2interrupt(void) } } -static inline uint8_t getScanCode(void) +static inline uint8_t get_scan_code(void) { uint8_t c, i; @@ -76,14 +104,290 @@ static inline uint8_t getScanCode(void) return c; } +// http://www.quadibloc.com/comp/scan.htm +// http://www.computer-engineering.org/ps2keyboard/scancodes2.html + +// These arrays provide a simple key map, to turn scan codes into ISO8859 +// output. If a non-US keyboard is used, these may need to be modified +// for the desired output. +// + +const PROGMEM PS2Keymap_t PS2Keymap_US = { + // without shift + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '`', 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0, + 0, 0, 'z', 's', 'a', 'w', '2', 0, + 0, 'c', 'x', 'd', 'e', '4', '3', 0, + 0, ' ', 'v', 'f', 't', 'r', '5', 0, + 0, 'n', 'b', 'h', 'g', 'y', '6', 0, + 0, 0, 'm', 'j', 'u', '7', '8', 0, + 0, ',', 'k', 'i', 'o', '0', '9', 0, + 0, '.', '/', 'l', ';', 'p', '-', 0, + 0, 0, '\'', 0, '[', '=', 0, 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, ']', 0, '\\', 0, 0, + 0, 0, 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 }, + // with shift + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '~', 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0, + 0, 0, 'Z', 'S', 'A', 'W', '@', 0, + 0, 'C', 'X', 'D', 'E', '$', '#', 0, + 0, ' ', 'V', 'F', 'T', 'R', '%', 0, + 0, 'N', 'B', 'H', 'G', 'Y', '^', 0, + 0, 0, 'M', 'J', 'U', '&', '*', 0, + 0, '<', 'K', 'I', 'O', ')', '(', 0, + 0, '>', '?', 'L', ':', 'P', '_', 0, + 0, 0, '"', 0, '{', '+', 0, 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '}', 0, '|', 0, 0, + 0, 0, 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 }, + 0 +}; + + +const PROGMEM PS2Keymap_t PS2Keymap_German = { + // without shift + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, '^', 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'q', '1', 0, + 0, 0, 'y', 's', 'a', 'w', '2', 0, + 0, 'c', 'x', 'd', 'e', '4', '3', 0, + 0, ' ', 'v', 'f', 't', 'r', '5', 0, + 0, 'n', 'b', 'h', 'g', 'z', '6', 0, + 0, 0, 'm', 'j', 'u', '7', '8', 0, + 0, ',', 'k', 'i', 'o', '0', '9', 0, + 0, '.', '-', 'l', PS2_o_DIAERESIS, 'p', PS2_SHARP_S, 0, + 0, 0, PS2_a_DIAERESIS, 0, PS2_u_DIAERESIS, '\'', 0, 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '+', 0, '#', 0, 0, + 0, '<', 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 }, + // with shift + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, PS2_DEGREE_SIGN, 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'Q', '!', 0, + 0, 0, 'Y', 'S', 'A', 'W', '"', 0, + 0, 'C', 'X', 'D', 'E', '$', PS2_SECTION_SIGN, 0, + 0, ' ', 'V', 'F', 'T', 'R', '%', 0, + 0, 'N', 'B', 'H', 'G', 'Z', '&', 0, + 0, 0, 'M', 'J', 'U', '/', '(', 0, + 0, ';', 'K', 'I', 'O', '=', ')', 0, + 0, ':', '_', 'L', PS2_O_DIAERESIS, 'P', '?', 0, + 0, 0, PS2_A_DIAERESIS, 0, PS2_U_DIAERESIS, '`', 0, 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '*', 0, '\'', 0, 0, + 0, '>', 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 }, + 1, + // with altgr + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 0, 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, '@', 0, 0, + 0, 0, 0, 0, 0, 0, PS2_SUPERSCRIPT_TWO, 0, + 0, 0, 0, 0, PS2_CURRENCY_SIGN, 0, PS2_SUPERSCRIPT_THREE, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, PS2_MICRO_SIGN, 0, 0, '{', '[', 0, + 0, 0, 0, 0, 0, '}', ']', 0, + 0, 0, 0, 0, 0, 0, '\\', 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '~', 0, '#', 0, 0, + 0, '|', 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 } +}; + + +const PROGMEM PS2Keymap_t PS2Keymap_French = { + // without shift + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, PS2_SUPERSCRIPT_TWO, 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'a', '&', 0, + 0, 0, 'w', 's', 'q', 'z', PS2_e_ACUTE, 0, + 0, 'c', 'x', 'd', 'e', '\'', '"', 0, + 0, ' ', 'v', 'f', 't', 'r', '(', 0, + 0, 'n', 'b', 'h', 'g', 'y', '-', 0, + 0, 0, ',', 'j', 'u', PS2_e_GRAVE, '_', 0, + 0, ';', 'k', 'i', 'o', PS2_a_GRAVE, PS2_c_CEDILLA, 0, + 0, ':', '!', 'l', 'm', 'p', ')', 0, + 0, 0, PS2_u_GRAVE, 0, '^', '=', 0, 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '$', 0, '*', 0, 0, + 0, '<', 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 }, + // with shift + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 0, 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, 'A', '1', 0, + 0, 0, 'W', 'S', 'Q', 'Z', '2', 0, + 0, 'C', 'X', 'D', 'E', '4', '3', 0, + 0, ' ', 'V', 'F', 'T', 'R', '5', 0, + 0, 'N', 'B', 'H', 'G', 'Y', '6', 0, + 0, 0, '?', 'J', 'U', '7', '8', 0, + 0, '.', 'K', 'I', 'O', '0', '9', 0, + 0, '/', PS2_SECTION_SIGN, 'L', 'M', 'P', PS2_DEGREE_SIGN, 0, + 0, 0, '%', 0, PS2_DIAERESIS, '+', 0, 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, PS2_POUND_SIGN, 0, PS2_MICRO_SIGN, 0, 0, + 0, '>', 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 }, + 1, + // with altgr + {0, PS2_F9, 0, PS2_F5, PS2_F3, PS2_F1, PS2_F2, PS2_F12, + 0, PS2_F10, PS2_F8, PS2_F6, PS2_F4, PS2_TAB, 0, 0, + 0, 0 /*Lalt*/, 0 /*Lshift*/, 0, 0 /*Lctrl*/, '@', 0, 0, + 0, 0, 0, 0, 0, 0, '~', 0, + 0, 0, 0, 0, 0 /*PS2_EURO_SIGN*/, '{', '#', 0, + 0, 0, 0, 0, 0, 0, '[', 0, + 0, 0, 0, 0, 0, 0, '|', 0, + 0, 0, 0, 0, 0, '`', '\\', 0, + 0, 0, 0, 0, 0, '@', '^', 0, + 0, 0, 0, 0, 0, 0, ']', 0, + 0, 0, 0, 0, 0, 0, '}', 0, + 0 /*CapsLock*/, 0 /*Rshift*/, PS2_ENTER /*Enter*/, '¤', 0, '#', 0, 0, + 0, '|', 0, 0, 0, 0, PS2_BACKSPACE, 0, + 0, '1', 0, '4', '7', 0, 0, 0, + '0', '.', '2', '5', '6', '8', PS2_ESC, 0 /*NumLock*/, + PS2_F11, '+', '3', '-', '*', '9', PS2_SCROLL, 0, + 0, 0, 0, PS2_F7 } +}; + + +#define BREAK 0x01 +#define MODIFIER 0x02 +#define SHIFT_L 0x04 +#define SHIFT_R 0x08 +#define ALTGR 0x10 + +static char get_iso8859_code(void) +{ + static uint8_t state=0; + uint8_t s; + char c; + + while (1) { + s = get_scan_code(); + if (!s) return 0; + if (s == 0xF0) { + state |= BREAK; + } else if (s == 0xE0) { + state |= MODIFIER; + } else { + if (state & BREAK) { + if (s == 0x12) { + state &= ~SHIFT_L; + } else if (s == 0x59) { + state &= ~SHIFT_R; + } else if (s == 0x11 && (state & MODIFIER)) { + state &= ~ALTGR; + } + // CTRL, ALT & WIN keys could be added + // but is that really worth the overhead? + state &= ~(BREAK | MODIFIER); + continue; + } + if (s == 0x12) { + state |= SHIFT_L; + continue; + } else if (s == 0x59) { + state |= SHIFT_R; + continue; + } else if (s == 0x11 && (state & MODIFIER)) { + state |= ALTGR; + } + c = 0; + if (state & MODIFIER) { + switch (s) { + case 0x70: c = PS2_INSERT; break; + case 0x6C: c = PS2_HOME; break; + case 0x7D: c = PS2_PAGEUP; break; + case 0x71: c = PS2_DELETE; break; + case 0x69: c = PS2_END; break; + case 0x7A: c = PS2_PAGEDOWN; break; + case 0x75: c = PS2_UPARROW; break; + case 0x6B: c = PS2_LEFTARROW; break; + case 0x72: c = PS2_DOWNARROW; break; + case 0x74: c = PS2_RIGHTARROW; break; + case 0x4A: c = '/'; break; + case 0x5A: c = PS2_ENTER; break; + default: break; + } + } else if ((state & ALTGR) && keymap->uses_altgr) { + if (s < PS2_KEYMAP_SIZE) + c = pgm_read_byte(keymap->altgr + s); + } else if (state & (SHIFT_L | SHIFT_R)) { + if (s < PS2_KEYMAP_SIZE) + c = pgm_read_byte(keymap->shift + s); + } else { + if (s < PS2_KEYMAP_SIZE) + c = pgm_read_byte(keymap->noshift + s); + } + state &= ~(BREAK | MODIFIER); + if (c) return c; + } + } +} + +bool PS2Keyboard::available() { + if (CharBuffer || UTF8next) return true; + CharBuffer = get_iso8859_code(); + if (CharBuffer) return true; + return false; +} + +int PS2Keyboard::read() { + uint8_t result; + + result = UTF8next; + if (result) { + UTF8next = 0; + } else { + result = CharBuffer; + if (result) { + CharBuffer = 0; + } else { + result = get_iso8859_code(); + } + if (result >= 128) { + UTF8next = (result & 0x3F) | 0x80; + result = ((result >> 6) & 0x1F) | 0xC0; + } + } + if (!result) return -1; + return result; +} + +int PS2Keyboard::getScanCode() { + return get_scan_code(); +} + PS2Keyboard::PS2Keyboard() { // nothing to do here, begin() does it all } -void PS2Keyboard::begin(uint8_t data_pin, uint8_t irq_pin) { +void PS2Keyboard::begin(uint8_t data_pin, uint8_t irq_pin, const PS2Keymap_t &map) { uint8_t irq_num=255; DataPin = data_pin; + keymap = ↦ // initialize the pins #ifdef INPUT_PULLUP @@ -151,6 +455,4 @@ void PS2Keyboard::begin(uint8_t data_pin, uint8_t irq_pin) { } } -uint8_t PS2Keyboard::getScanCode() { - return getScanCode(); -} + diff --git a/mac_plus_ps2/PS2Keyboard.h b/mac_plus_ps2/PS2Keyboard.h index b3bca86..662d468 100644 --- a/mac_plus_ps2/PS2Keyboard.h +++ b/mac_plus_ps2/PS2Keyboard.h @@ -6,7 +6,6 @@ ** Mostly rewritten Paul Stoffregen , June 2010 ** Modified for use with Arduino 13 by L. Abraham Smith, * ** Modified for easy interrup pin assignement on method begin(datapin,irq_pin). Cuningan ** - ** Custom version modifed by Tomasz Rękawek to only provide scancodes. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -35,6 +34,149 @@ #include "utility/int_pins.h" +// Every call to read() returns a single byte for each +// keystroke. These configure what byte will be returned +// for each "special" key. To ignore a key, use zero. +#define PS2_TAB 9 +#define PS2_ENTER 13 +#define PS2_BACKSPACE 127 +#define PS2_ESC 27 +#define PS2_INSERT 0 +#define PS2_DELETE 127 +#define PS2_HOME 0 +#define PS2_END 0 +#define PS2_PAGEUP 25 +#define PS2_PAGEDOWN 26 +#define PS2_UPARROW 11 +#define PS2_LEFTARROW 8 +#define PS2_DOWNARROW 10 +#define PS2_RIGHTARROW 21 +#define PS2_F1 0 +#define PS2_F2 0 +#define PS2_F3 0 +#define PS2_F4 0 +#define PS2_F5 0 +#define PS2_F6 0 +#define PS2_F7 0 +#define PS2_F8 0 +#define PS2_F9 0 +#define PS2_F10 0 +#define PS2_F11 0 +#define PS2_F12 0 +#define PS2_SCROLL 0 + +#define PS2_INVERTED_EXCLAMATION 161 // ¡ +#define PS2_CENT_SIGN 162 // ¢ +#define PS2_POUND_SIGN 163 // £ +#define PS2_CURRENCY_SIGN 164 // ¤ +#define PS2_YEN_SIGN 165 // ¥ +#define PS2_BROKEN_BAR 166 // ¦ +#define PS2_SECTION_SIGN 167 // § +#define PS2_DIAERESIS 168 // ¨ +#define PS2_COPYRIGHT_SIGN 169 // © +#define PS2_FEMININE_ORDINAL 170 // ª +#define PS2_LEFT_DOUBLE_ANGLE_QUOTE 171 // « +#define PS2_NOT_SIGN 172 // ¬ +#define PS2_HYPHEN 173 +#define PS2_REGISTERED_SIGN 174 // ® +#define PS2_MACRON 175 // ¯ +#define PS2_DEGREE_SIGN 176 // ° +#define PS2_PLUS_MINUS_SIGN 177 // ± +#define PS2_SUPERSCRIPT_TWO 178 // ² +#define PS2_SUPERSCRIPT_THREE 179 // ³ +#define PS2_ACUTE_ACCENT 180 // ´ +#define PS2_MICRO_SIGN 181 // µ +#define PS2_PILCROW_SIGN 182 // ¶ +#define PS2_MIDDLE_DOT 183 // · +#define PS2_CEDILLA 184 // ¸ +#define PS2_SUPERSCRIPT_ONE 185 // ¹ +#define PS2_MASCULINE_ORDINAL 186 // º +#define PS2_RIGHT_DOUBLE_ANGLE_QUOTE 187 // » +#define PS2_FRACTION_ONE_QUARTER 188 // ¼ +#define PS2_FRACTION_ONE_HALF 189 // ½ +#define PS2_FRACTION_THREE_QUARTERS 190 // ¾ +#define PS2_INVERTED_QUESTION MARK 191 // ¿ +#define PS2_A_GRAVE 192 // À +#define PS2_A_ACUTE 193 // Á +#define PS2_A_CIRCUMFLEX 194 // Â +#define PS2_A_TILDE 195 // Ã +#define PS2_A_DIAERESIS 196 // Ä +#define PS2_A_RING_ABOVE 197 // Å +#define PS2_AE 198 // Æ +#define PS2_C_CEDILLA 199 // Ç +#define PS2_E_GRAVE 200 // È +#define PS2_E_ACUTE 201 // É +#define PS2_E_CIRCUMFLEX 202 // Ê +#define PS2_E_DIAERESIS 203 // Ë +#define PS2_I_GRAVE 204 // Ì +#define PS2_I_ACUTE 205 // Í +#define PS2_I_CIRCUMFLEX 206 // Î +#define PS2_I_DIAERESIS 207 // Ï +#define PS2_ETH 208 // Ð +#define PS2_N_TILDE 209 // Ñ +#define PS2_O_GRAVE 210 // Ò +#define PS2_O_ACUTE 211 // Ó +#define PS2_O_CIRCUMFLEX 212 // Ô +#define PS2_O_TILDE 213 // Õ +#define PS2_O_DIAERESIS 214 // Ö +#define PS2_MULTIPLICATION 215 // × +#define PS2_O_STROKE 216 // Ø +#define PS2_U_GRAVE 217 // Ù +#define PS2_U_ACUTE 218 // Ú +#define PS2_U_CIRCUMFLEX 219 // Û +#define PS2_U_DIAERESIS 220 // Ü +#define PS2_Y_ACUTE 221 // Ý +#define PS2_THORN 222 // Þ +#define PS2_SHARP_S 223 // ß +#define PS2_a_GRAVE 224 // à +#define PS2_a_ACUTE 225 // á +#define PS2_a_CIRCUMFLEX 226 // â +#define PS2_a_TILDE 227 // ã +#define PS2_a_DIAERESIS 228 // ä +#define PS2_a_RING_ABOVE 229 // å +#define PS2_ae 230 // æ +#define PS2_c_CEDILLA 231 // ç +#define PS2_e_GRAVE 232 // è +#define PS2_e_ACUTE 233 // é +#define PS2_e_CIRCUMFLEX 234 // ê +#define PS2_e_DIAERESIS 235 // ë +#define PS2_i_GRAVE 236 // ì +#define PS2_i_ACUTE 237 // í +#define PS2_i_CIRCUMFLEX 238 // î +#define PS2_i_DIAERESIS 239 // ï +#define PS2_eth 240 // ð +#define PS2_n_TILDE 241 // ñ +#define PS2_o_GRAVE 242 // ò +#define PS2_o_ACUTE 243 // ó +#define PS2_o_CIRCUMFLEX 244 // ô +#define PS2_o_TILDE 245 // õ +#define PS2_o_DIAERESIS 246 // ö +#define PS2_DIVISION 247 // ÷ +#define PS2_o_STROKE 248 // ø +#define PS2_u_GRAVE 249 // ù +#define PS2_u_ACUTE 250 // ú +#define PS2_u_CIRCUMFLEX 251 // û +#define PS2_u_DIAERESIS 252 // ü +#define PS2_y_ACUTE 253 // ý +#define PS2_thorn 254 // þ +#define PS2_y_DIAERESIS 255 // ÿ + + +#define PS2_KEYMAP_SIZE 136 + +typedef struct { + uint8_t noshift[PS2_KEYMAP_SIZE]; + uint8_t shift[PS2_KEYMAP_SIZE]; + uint8_t uses_altgr; + uint8_t altgr[PS2_KEYMAP_SIZE]; +} PS2Keymap_t; + + +extern const PROGMEM PS2Keymap_t PS2Keymap_US; +extern const PROGMEM PS2Keymap_t PS2Keymap_German; +extern const PROGMEM PS2Keymap_t PS2Keymap_French; + + /** * Purpose: Provides an easy access to PS2 keyboards * Author: Christian Weichel @@ -52,9 +194,20 @@ class PS2Keyboard { * setting the pin modes correctly and driving those needed to high. * The propably best place to call this method is in the setup routine. */ - static void begin(uint8_t dataPin, uint8_t irq_pin); + static void begin(uint8_t dataPin, uint8_t irq_pin, const PS2Keymap_t &map = PS2Keymap_US); - static uint8_t getScanCode(); + /** + * Returns true if there is a char to be read, false if not. + */ + static bool available(); + + /** + * Returns the char last read from the keyboard. + * If there is no char availble, -1 is returned. + */ + static int read(); + + static int getScanCode(); }; #endif diff --git a/mac_plus_ps2/keymap.ino b/mac_plus_ps2/keymap.ino index 74533c4..d60f567 100644 --- a/mac_plus_ps2/keymap.ino +++ b/mac_plus_ps2/keymap.ino @@ -1,103 +1,104 @@ + void initScancodes() { for (int i = 0; i < 256; i++) { scanCodesTable[i] = NULL_TRANSITION; extScanCodesTable[i] = NULL_TRANSITION; } - scanCodesTable[0x0e] = 0x32; // ~` - scanCodesTable[0x16] = 0x12; // 1! - scanCodesTable[0x1e] = 0x13; // 2@ - scanCodesTable[0x26] = 0x14; // 3# - scanCodesTable[0x25] = 0x15; // 4$ - scanCodesTable[0x2e] = 0x17; // 5% - scanCodesTable[0x36] = 0x16; // 6^ - scanCodesTable[0x3d] = 0x1a; // 7& - scanCodesTable[0x3e] = 0x1c; // 8* - scanCodesTable[0x46] = 0x19; // 9( - scanCodesTable[0x45] = 0x1d; // 0) - scanCodesTable[0x4e] = 0x1b; // -_ - scanCodesTable[0x55] = 0x18; // += - scanCodesTable[0x5d] = 0x2a; // |\ - scanCodesTable[0x66] = 0x33; // backspace + scanCodesTable[0x66] = 0x67; // backspace + scanCodesTable[0x0e] = 0x65; // ~` + scanCodesTable[0x16] = 0x25; // 1! + scanCodesTable[0x1e] = 0x27; // 2@ + scanCodesTable[0x26] = 0x29; // 3# + scanCodesTable[0x25] = 0x2b; // 4$ + scanCodesTable[0x2e] = 0x2f; // 5% + scanCodesTable[0x36] = 0x2d; // 6^ + scanCodesTable[0x3d] = 0x35; // 7& + scanCodesTable[0x3e] = 0x39; // 8* + scanCodesTable[0x46] = 0x33; // 9( + scanCodesTable[0x45] = 0x3b; // 0) + scanCodesTable[0x4e] = 0x37; // -_ + scanCodesTable[0x55] = 0x31; // += + scanCodesTable[0x5d] = 0x55; // |\ - scanCodesTable[0x0d] = 0x30; // tab - scanCodesTable[0x15] = 0x0c; // q - scanCodesTable[0x1d] = 0x0d; // w - scanCodesTable[0x24] = 0x0e; // e - scanCodesTable[0x2d] = 0x0f; // r - scanCodesTable[0x2c] = 0x10; // t - scanCodesTable[0x35] = 0x11; // y - scanCodesTable[0x3c] = 0x20; // u - scanCodesTable[0x43] = 0x22; // i - scanCodesTable[0x44] = 0x1f; // o - scanCodesTable[0x4d] = 0x23; // p - scanCodesTable[0x54] = 0x21; // [{ - scanCodesTable[0x5b] = 0x1e; // }] - scanCodesTable[0x5a] = 0x24; // enter + scanCodesTable[0x0d] = 0x61; // tab + scanCodesTable[0x15] = 0x19; // q + scanCodesTable[0x1d] = 0x1b; // w + scanCodesTable[0x24] = 0x1d; // e + scanCodesTable[0x2d] = 0x1f; // r + scanCodesTable[0x2c] = 0x23; // t + scanCodesTable[0x35] = 0x21; // y + scanCodesTable[0x3c] = 0x41; // u + scanCodesTable[0x43] = 0x45; // i + scanCodesTable[0x44] = 0x3f; // o + scanCodesTable[0x4d] = 0x47; // p + scanCodesTable[0x54] = 0x43; // [{ + scanCodesTable[0x5b] = 0x3d; // }] + scanCodesTable[0x5a] = 0x49; // enter - scanCodesTable[0x58] = 0x39; // caps lock - scanCodesTable[0x1c] = 0x00; // a - scanCodesTable[0x1b] = 0x01; // s - scanCodesTable[0x23] = 0x02; // d - scanCodesTable[0x2b] = 0x03; // f - scanCodesTable[0x34] = 0x05; // g - scanCodesTable[0x33] = 0x04; // h - scanCodesTable[0x3b] = 0x26; // j - scanCodesTable[0x42] = 0x28; // k - scanCodesTable[0x4b] = 0x25; // l - scanCodesTable[0x4c] = 0x29; // :; - scanCodesTable[0x52] = 0x27; // "' + scanCodesTable[0x58] = 0x73; // caps lock + scanCodesTable[0x1c] = 0x01; // a + scanCodesTable[0x1b] = 0x03; // s + scanCodesTable[0x23] = 0x05; // d + scanCodesTable[0x2b] = 0x07; // f + scanCodesTable[0x34] = 0x0b; // g + scanCodesTable[0x33] = 0x09; // h + scanCodesTable[0x3b] = 0x4d; // j + scanCodesTable[0x42] = 0x51; // k + scanCodesTable[0x4b] = 0x4b; // l + scanCodesTable[0x4c] = 0x53; // :; + scanCodesTable[0x52] = 0x4f; // "' - scanCodesTable[0x12] = 0x38; // left shift - scanCodesTable[0x1a] = 0x06; // z - scanCodesTable[0x22] = 0x07; // x - scanCodesTable[0x21] = 0x08; // c - scanCodesTable[0x2a] = 0x09; // v - scanCodesTable[0x32] = 0x0b; // b - scanCodesTable[0x31] = 0x2d; // n - scanCodesTable[0x3a] = 0x2e; // m - scanCodesTable[0x41] = 0x2b; // <, - scanCodesTable[0x49] = 0x2f; // >. - scanCodesTable[0x4a] = 0x2c; // ?/ - scanCodesTable[0x59] = 0x38; // right shift + scanCodesTable[0x12] = 0x71; // left shift + scanCodesTable[0x1a] = 0x0d; // z + scanCodesTable[0x22] = 0x0f; // x + scanCodesTable[0x21] = 0x11; // c + scanCodesTable[0x2a] = 0x13; // v + scanCodesTable[0x32] = 0x17; // b + scanCodesTable[0x31] = 0x5b; // n + scanCodesTable[0x3a] = 0x5d; // m + scanCodesTable[0x41] = 0x57; // <, + scanCodesTable[0x49] = 0x5f; // >. + scanCodesTable[0x4a] = 0x59; // ?/ + scanCodesTable[0x59] = 0x71; // right shift - scanCodesTable[0x14] = 0x3a; // left ctrl -> option - scanCodesTable[0x11] = 0x3a; // left alt -> option - scanCodesTable[0x29] = 0x31; // space + scanCodesTable[0x14] = 0x75; // left ctrl -> option + scanCodesTable[0x11] = 0x75; // left alt -> option + scanCodesTable[0x29] = 0x63; // space - extScanCodesTable[0x11] = 0x3a; // right alt -> option - extScanCodesTable[0x14] = 0x3a; // right ctrl -> option + extScanCodesTable[0x11] = 0x75; // right alt -> option + extScanCodesTable[0x14] = 0x75; // right ctrl -> option - extScanCodesTable[0x5b] = 0x37; // left windows -> cmd - extScanCodesTable[0x1f] = 0x37; // left windows -> cmd - extScanCodesTable[0x8b] = 0x37; // left windows -> cmd + extScanCodesTable[0x5b] = 0x6f; // left windows -> cmd + extScanCodesTable[0x1f] = 0x6f; // left windows -> cmd + extScanCodesTable[0x8b] = 0x6f; // left windows -> cmd - extScanCodesTable[0x5c] = 0x37; // right windows - extScanCodesTable[0x27] = 0x37; // right windows - extScanCodesTable[0x8c] = 0x37; // right windows + extScanCodesTable[0x5c] = 0x6f; // right windows + extScanCodesTable[0x27] = 0x6f; // right windows + extScanCodesTable[0x8c] = 0x6f; // right windows - extScanCodesTable[0x75] = 0x4d; // up arrow - extScanCodesTable[0x6b] = 0x46; // left arrow - extScanCodesTable[0x72] = 0x48; // down arrow - extScanCodesTable[0x74] = 0x42; // right arrow - - extScanCodesTable[0x77] = 0x47; // numlock -> numpad clear - extScanCodesTable[0x71] = 0x47; // delete -> numpad clear - extScanCodesTable[0x4a] = 0x6d; // numpad / - extScanCodesTable[0x5a] = 0x4c; // numpad enter - - scanCodesTable[0x7c] = 0x62; // numpad * - scanCodesTable[0x7b] = 0x4e; // numpad - - scanCodesTable[0x70] = 0x52; // numpad 0 - scanCodesTable[0x71] = 0x41; // numpad . - scanCodesTable[0x69] = 0x53; // numpad 1 - scanCodesTable[0x72] = 0x54; // numpad 2 - scanCodesTable[0x7a] = 0x55; // numpad 3 - scanCodesTable[0x6b] = 0x56; // numpad 4 - scanCodesTable[0x73] = 0x57; // numpad 5 - scanCodesTable[0x74] = 0x58; // numpad 6 - scanCodesTable[0x79] = 0x66; // numpad + - scanCodesTable[0x6c] = 0x59; // numpad 7 - scanCodesTable[0x75] = 0x5b; // numpad 8 - scanCodesTable[0x7d] = 0x5c; // numpad 9 + scanCodesTable[0x77] = 0x0f | NUMPAD; // numlock -> numpad clear + extScanCodesTable[0x71] = 0x0f | NUMPAD; // delete -> numpad clear + extScanCodesTable[0x75] = 0x1b | NUMPAD; // up arrow + extScanCodesTable[0x6b] = 0x0d | NUMPAD; // left arrow + extScanCodesTable[0x72] = 0x11 | NUMPAD; // down arrow + extScanCodesTable[0x74] = 0x05 | NUMPAD; // right arrow + + extScanCodesTable[0x4a] = 0x1b | NUMPAD2; // numpad / + extScanCodesTable[0x5a] = 0x19 | NUMPAD; // numpad enter + + scanCodesTable[0x7c] = 0x05 | NUMPAD2;// numpad * + scanCodesTable[0x7b] = 0x1d | NUMPAD; // numpad - + scanCodesTable[0x70] = 0x25 | NUMPAD; // numpad 0 + scanCodesTable[0x71] = 0x03 | NUMPAD; // numpad . + scanCodesTable[0x69] = 0x27 | NUMPAD; // numpad 1 + scanCodesTable[0x72] = 0x29 | NUMPAD; // numpad 2 + scanCodesTable[0x7a] = 0x2b | NUMPAD; // numpad 3 + scanCodesTable[0x6b] = 0x2d | NUMPAD; // numpad 4 + scanCodesTable[0x73] = 0x2f | NUMPAD; // numpad 5 + scanCodesTable[0x74] = 0x31 | NUMPAD; // numpad 6 + scanCodesTable[0x79] = 0x0d | NUMPAD2;// numpad + + scanCodesTable[0x6c] = 0x33 | NUMPAD; // numpad 7 + scanCodesTable[0x75] = 0x37 | NUMPAD; // numpad 8 + scanCodesTable[0x7d] = 0x39 | NUMPAD; // numpad 9 } diff --git a/mac_plus_ps2/mac_plus_ps2.ino b/mac_plus_ps2/mac_plus_ps2.ino index 05f00bc..d13fa12 100644 --- a/mac_plus_ps2/mac_plus_ps2.ino +++ b/mac_plus_ps2/mac_plus_ps2.ino @@ -1,7 +1,12 @@ #include "PS2Keyboard.h" -#define PS2_DATA_PIN 2 -#define PS2_CLOCK_PIN 3 +//#define SERIAL_DEBUG + +#define NUMPAD 0x0100 +#define NUMPAD2 0x0200 + +#define PS2_DATA_PIN 3 +#define PS2_CLOCK_PIN 2 #define MAC_DATA_PIN 5 #define MAC_CLOCK_PIN 6 @@ -9,18 +14,22 @@ #define NULL_TRANSITION 0x7b PS2Keyboard keyboard; -byte scanCodesTable[256]; -byte extScanCodesTable[256]; +unsigned int scanCodesTable[256]; +unsigned int extScanCodesTable[256]; void setup() { +#ifdef SERIAL_DEBUG + Serial.begin(9600); +#endif initScancodes(); keyboard.begin(PS2_DATA_PIN, PS2_CLOCK_PIN); + //debug(); pinMode(LED_BUILTIN, OUTPUT); pinMode(MAC_CLOCK_PIN, OUTPUT); pinMode(MAC_DATA_PIN, INPUT_PULLUP); - + waitForInitSignal(); delayMicroseconds(180); } @@ -38,21 +47,17 @@ void waitForInitSignal() { } void loop() { - digitalWrite(LED_BUILTIN, LOW); - byte cmd = readByte(); - digitalWrite(LED_BUILTIN, HIGH); - - switch (cmd) { + switch (readCmd()) { case 0x10: inquiry(); break; case 0x14: // instant - sendByte(getKeyTransition()); + sendKey(getKeyTransition()); break; case 0x16: // model number - sendByte(B00000011); + sendByte(0x0b); break; case 0x36: // test @@ -61,51 +66,86 @@ void loop() { } } +byte readCmd() { + digitalWrite(LED_BUILTIN, LOW); + pinMode(MAC_DATA_PIN, INPUT_PULLUP); + delayMicroseconds(20); + + while (digitalRead(MAC_DATA_PIN) != LOW); + delayMicroseconds(400); + byte cmd = readByte(); + while (digitalRead(MAC_DATA_PIN) != HIGH); + + digitalWrite(LED_BUILTIN, HIGH); + pinMode(MAC_DATA_PIN, OUTPUT); + delayMicroseconds(20); + return cmd; +} + void inquiry() { unsigned long start = millis(); while ((millis() - start) < 250) { - byte key = getKeyTransition(); + int key = getKeyTransition(); if (key != NULL_TRANSITION) { - sendByte(key); + sendKey(key); return; } } sendByte(NULL_TRANSITION); } +void sendKey(unsigned int key) { + if (key & NUMPAD) { + sendByte(0x79); readCmd(); + sendByte(key); + } else if (key & NUMPAD2) { + sendByte(0x71); readCmd(); + sendByte(0x79); readCmd(); + sendByte(key); + } else { + sendByte(key); + } +} + byte readByte() { - pinMode(MAC_DATA_PIN, INPUT_PULLUP); byte b = 0; for (byte i = 0; i < 8; i++) { digitalWrite(MAC_CLOCK_PIN, LOW); delayMicroseconds(180); digitalWrite(MAC_CLOCK_PIN, HIGH); - delayMicroseconds(40); + delayMicroseconds(80); b = (b << 1) | digitalRead(MAC_DATA_PIN); - delayMicroseconds(180); + delayMicroseconds(140); } - +#ifdef SERIAL_DEBUG + Serial.print(b, HEX); + Serial.print(" -> "); +#endif return b; } void sendByte(byte b) { - pinMode(MAC_DATA_PIN, OUTPUT); +#ifdef SERIAL_DEBUG + Serial.print(b, HEX); + Serial.println(); +#endif for (byte m = 128; m > 0; m >>= 1) { - digitalWrite(MAC_DATA_PIN, b & m == 0 ? HIGH : LOW); + digitalWrite(MAC_DATA_PIN, !(b & m) ? LOW : HIGH); delayMicroseconds(40); digitalWrite(MAC_CLOCK_PIN, LOW); - delayMicroseconds(160); + delayMicroseconds(120); digitalWrite(MAC_CLOCK_PIN, HIGH); - delayMicroseconds(130); + delayMicroseconds(170); } + digitalWrite(MAC_DATA_PIN, HIGH); } -byte getKeyTransition() { +unsigned int getKeyTransition() { byte c = keyboard.getScanCode(); if (c == 0) { return NULL_TRANSITION; } else if (c == 0xf0) { - return translate(keyboard.getScanCode(), false, true); + return translate(waitForScanCode(), false, true); } else if (c == 0xe0) { return getExtendedTransition(); } else { @@ -113,22 +153,32 @@ byte getKeyTransition() { } } -byte getExtendedTransition() { - byte c = keyboard.getScanCode(); +unsigned int getExtendedTransition() { + byte c = waitForScanCode(); if (c == 0xf0) { - return translate(keyboard.getScanCode(), true, true); + return translate(waitForScanCode(), true, true); } else { return translate(c, true, false); } } -byte translate(byte scanCode, boolean extended, boolean released) { - byte translated = extended ? extScanCodesTable[scanCode] : scanCodesTable[scanCode]; +unsigned int translate(byte scanCode, boolean extended, boolean released) { + unsigned int translated = extended ? extScanCodesTable[scanCode] : scanCodesTable[scanCode]; if (translated == NULL_TRANSITION) { return NULL_TRANSITION; } else if (released) { - return translated & 0x80; + return translated | 0x80; } else { return translated; } } + +byte waitForScanCode() { + while (true) { + byte s = keyboard.getScanCode(); + if (s) { + return s; + } + } +} +