LAK132

XT Keyboard test

Sep 28th, 2020 (edited)
1,371
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *
  3.  * TEENSY KEYBOARD CONTROLLER by Lucas Kleiss
  4.  *
  5.  * This software defines two types of modifiers:
  6.  *    * Modifier Keys eg. MODIFIERKEY_SHIFT
  7.  *    * Modifier Scan Codes eg. 96
  8.  * These different types of modifiers serve different purposes and great care should be taken not to get them mixed up
  9.  *
  10.  * This software was specifically designed for the Focus FK-2001 XT Mechanical Keyboard
  11.  *
  12.  */
  13.  
  14. #define systemRequest 84
  15. #define modCode 96
  16. #define altModCode 97
  17.  
  18. #define clock digitalRead(clockPin)
  19. #define data digitalRead(dataPin)
  20.  
  21. int clockPin = 3;
  22. int dataPin = 4;
  23. int switchPin = 22;
  24. int keyTemp = 0;
  25. int keyState[104];
  26. int outKeyState[6] = {0, 0, 0, 0, 0, 0};
  27. int outModState[4] = {0, 0, 0, 0};
  28. int keyboardCode[] = {    
  29.   0x00,    // Position 00 Unused
  30.   0x29,    // Position 01 Esc
  31.   0x1E,    // Position 02 1 !
  32.   0x1F,    // Position 03 2 @
  33.   0x20,    // Position 04 3 #
  34.   0x21,    // Position 05 4 $
  35.   0x22,    // Position 06 5 %
  36.   0x23,    // Position 07 6 ^
  37.   0x24,    // Position 08 7 &
  38.   0x25,    // Position 09 8 *
  39.   0x26,    // Position 10 9 (
  40.   0x27,    // Position 11 0 )
  41.   0x2D,    // Position 12 - _
  42.   0x2E,    // Position 13 = +
  43.   0x2A,    // Position 14 Backspace
  44.   0x2B,    // Position 15 Tab
  45.   0x14,    // Position 16 Q
  46.   0x1A,    // Position 17 W
  47.   0x08,    // Position 18 E
  48.   0x15,    // Position 19 R
  49.   0x17,    // Position 20 T
  50.   0x1C,    // Position 21 Y
  51.   0x18,    // Position 22 U
  52.   0x0C,    // Position 23 I
  53.   0x12,    // Position 24 O
  54.   0x13,    // Position 25 P
  55.   0x2F,    // Position 26 [ {
  56.   0x30,    // Position 27 ] }
  57.   0x28,    // Position 28 Return
  58.   0xE0,    // Position 29 Ctrl
  59.   0x04,    // Position 30 A
  60.   0x16,    // Position 31 S
  61.   0x07,    // Position 32 D
  62.   0x09,    // Position 33 F
  63.   0x0A,    // Position 34 G
  64.   0x0B,    // Position 35 H
  65.   0x0D,    // Position 36 J
  66.   0x0E,    // Position 37 K
  67.   0x0F,    // Position 38 L
  68.   0x33,    // Position 39 ; :
  69.   0x34,    // Position 40 ' "
  70.   0x35,    // Position 41 ` ~
  71.   0xE1,    // Position 42 Left Shift
  72.   0x31,    // Position 43 \ |
  73.   0x1D,    // Position 44 Z
  74.   0x1B,    // Position 45 X
  75.   0x06,    // Position 46 C
  76.   0x19,    // Position 47 V
  77.   0x05,    // Position 48 B
  78.   0x11,    // Position 49 N
  79.   0x10,    // Position 50 M
  80.   0x36,    // Position 51 , <
  81.   0x37,    // Position 52 . >
  82.   0x38,    // Position 53 / ?
  83.   0xE5,    // Position 54 Right Shift
  84.   0x46,    // Position 55 PrtSc
  85.   0xE2,    // Position 56 Alt
  86.   0x2C,    // Position 57 Space
  87.   0x39,    // Position 58 Caps Lock
  88.   0x3A,    // Position 59 F1
  89.   0x3B,    // Position 60 F2
  90.   0x3C,    // Position 61 F3
  91.   0x3D,    // Position 62 F4
  92.   0x3E,    // Position 63 F5
  93.   0x3F,    // Position 64 F6
  94.   0x40,    // Position 65 F7
  95.   0x41,    // Position 66 F8
  96.   0x42,    // Position 67 F9
  97.   0x43,    // Position 68 F10
  98.   0x53,    // Position 69 Num Lock
  99.   0x47,    // Position 70 Scroll Lock
  100.   0x5F,    // Position 71 Home 7
  101.   0x60,    // Position 72 Up 8
  102.   0x61,    // Position 73 PgUp 9
  103.   0x56,    // Position 74 -
  104.   0x5C,    // Position 75 Left 4
  105.   0x5D,    // Position 76 5
  106.   0x5E,    // Position 77 Right 6
  107.   0x57,    // Position 78 +
  108.   0x59,    // Position 79 End 1
  109.   0x5A,    // Position 80 Down 2
  110.   0x5B,    // Position 81 PgDn 3
  111.   0x62,    // Position 82 Ins 0
  112.   0x63     // Position 83 Del .
  113. } ;
  114. #define altKeyboardCode keyboardCode
  115.  
  116. boolean modCodeAct = false;
  117. boolean altModCodeAct = false;
  118.  
  119. void setup()
  120. {
  121.   Serial.begin(9600);
  122.   Serial.println("Connected");
  123.   pinMode(clockPin, INPUT);
  124.   pinMode(dataPin, INPUT);
  125.   // pinMode(switchPin, OUTPUT);
  126.   // digitalWrite(switchPin, HIGH);
  127.   // delay(1000);
  128.   // digitalWrite(switchPin, LOW); // Needed to start the Focus FK-2001 in AT mode before switching to XT mode
  129.   // Keyboard.set_modifier(0);
  130.   // Keyboard.set_key1(0);
  131.   // Keyboard.set_key2(0);
  132.   // Keyboard.set_key3(0);
  133.   // Keyboard.set_key4(0);
  134.   // Keyboard.set_key5(0);
  135.   // Keyboard.set_key6(0);
  136.   // Keyboard.send_now();
  137. }
  138.  
  139. void loop()
  140. {
  141.   int x = 0;
  142.   if (clock == LOW)
  143.   {
  144.     while (clock == LOW){}
  145.     while (x < 8)
  146.     {
  147.       if (clock == HIGH)
  148.       {
  149.         if (x != 7)
  150.         {
  151.           keyTemp += (data << x);
  152.         }
  153.         else
  154.         {
  155.           keyState[keyTemp] = data;
  156.         }
  157.         x++;
  158.         while (clock == HIGH){}
  159.       }
  160.       while (clock == LOW){}
  161.     }
  162.     //Code that requires the current key code goes here
  163.     if (keyTemp == modCode || keyTemp == altModCode) // Allows the mod codes to register correctly
  164.     {
  165.       delayMicroseconds(100);
  166.     }
  167.     keyRegister (keyTemp, !keyState[keyTemp]); //Sends out the key presses every time it is run
  168.     keyTemp = 0;
  169.   }
  170. }
  171.  
  172. // Registers all key presses
  173.  
  174. void keyRegister (int key, boolean pressed)
  175. {
  176.   if (modCodeAct)
  177.   {
  178.     if (altKeyboardCode[key] == MODIFIERKEY_CTRL || altKeyboardCode[key] == MODIFIERKEY_SHIFT || altKeyboardCode[key] == MODIFIERKEY_ALT || altKeyboardCode[key] == MODIFIERKEY_GUI)
  179.     {
  180.       setModKey (key, pressed, true);
  181.     }  
  182.     else
  183.     {
  184.       setNormKey (key, pressed, true);
  185.     }
  186.     modCodeAct = false;
  187.   }
  188.   else if (altModCodeAct)
  189.   {
  190.     if (altKeyboardCode[key] == MODIFIERKEY_CTRL || altKeyboardCode[key] == MODIFIERKEY_SHIFT || altKeyboardCode[key] == MODIFIERKEY_ALT || altKeyboardCode[key] == MODIFIERKEY_GUI)
  191.     {
  192.       setModKey (key, pressed, true);
  193.     }
  194.     else
  195.     {
  196.       setNormKey (key, pressed, true);
  197.     }
  198.     modCodeAct = true;
  199.     altModCodeAct = false;
  200.   }
  201.   else if (key == modCode)
  202.   {
  203.     modCodeAct = true;
  204.   }
  205.   else if (key == altModCode)
  206.   {
  207.     altModCodeAct = true;
  208.   }
  209.   else if (keyboardCode[key] == MODIFIERKEY_CTRL || keyboardCode[key] == MODIFIERKEY_SHIFT || keyboardCode[key] == MODIFIERKEY_ALT || keyboardCode[key] == MODIFIERKEY_GUI)
  210.   {
  211.     setModKey (key, pressed, false);
  212.   }
  213.   else
  214.   {
  215.     setNormKey (key, pressed, false);
  216.   }
  217. }
  218.  
  219. // Sets all normal key presses
  220.  
  221. void setNormKey (int key, boolean pressed, boolean mod)
  222. {
  223.   boolean set = false;
  224.   int x = 0;
  225.   int i = 0;
  226.   while (i < 6 && !set)
  227.   {
  228.     if (outKeyState[i] == keyboardCode[key])
  229.     {
  230.       x = i; //Skips x to the point where the key is already pressed to prevent key from being registered twice (ghosting bug)
  231.       set = true;
  232.     }
  233.     i++;
  234.   }
  235.   set = false;
  236.   while (x < 6 && !set)
  237.   {
  238.     if ((outKeyState[x] == keyboardCode[key] && pressed && !mod) || (outKeyState[x] == altKeyboardCode[key] && pressed && mod))
  239.     {
  240.       set = true;
  241.     }
  242.     else if (((((outKeyState[x] == 0) && pressed) || ((outKeyState[x] == keyboardCode[key]) && !pressed)) && !mod) || ((((outKeyState[x] == 0) && pressed) || ((outKeyState[x] == altKeyboardCode[key]) && !pressed)) && mod))
  243.     {
  244.       if (pressed && !mod)
  245.       {
  246.         outKeyState[x] = keyboardCode[key];
  247.       }
  248.       else if (pressed && mod)
  249.       {
  250.         outKeyState[x] = altKeyboardCode[key];
  251.       }
  252.       else
  253.       {
  254.         outKeyState[x] = 0;
  255.       }
  256.       set = true;
  257.     }
  258.     Serial.println(outKeyState[x]);
  259.     x++;
  260.   }
  261.   // Keyboard.set_key1(outKeyState[0]);
  262.   // Keyboard.set_key2(outKeyState[1]);
  263.   // Keyboard.set_key3(outKeyState[2]);
  264.   // Keyboard.set_key4(outKeyState[3]);
  265.   // Keyboard.set_key5(outKeyState[4]);
  266.   // Keyboard.set_key6(outKeyState[5]);
  267.   // Keyboard.send_now();
  268. }
  269.  
  270. // Sets all modifier key presses
  271.  
  272. void setModKey (int key, boolean pressed, int mod)
  273. {
  274.   boolean set = false;
  275.   int x = 0;
  276.   int i = 0;
  277.   while (i < 4 && !set)
  278.   {
  279.     if (outKeyState[i] == keyboardCode[key])
  280.     {
  281.       x = i; //Skips x to the point where the key is already pressed to prevent key from being registered twice (ghosting bug)
  282.       set = true;
  283.     }
  284.     i++;
  285.   }
  286.   set = false;
  287.   while (x < 4 && !set)
  288.   {
  289.     if ((outModState[x] == keyboardCode[key] && pressed && !mod) || (outModState[x] == altKeyboardCode[key] && pressed && mod))
  290.     {
  291.       set = true;
  292.     }
  293.     else if (((((outModState[x] == 0) && pressed) || ((outModState[x] == keyboardCode[key]) && !pressed)) && !mod) || ((((outModState[x] == 0) && pressed) || ((outModState[x] == altKeyboardCode[key]) && !pressed)) && mod))
  294.     {
  295.       if (pressed && !mod)
  296.       {
  297.         outModState[x] = keyboardCode[key];
  298.       }
  299.       else if (pressed && mod)
  300.       {
  301.         outModState[x] = altKeyboardCode[key];
  302.       }
  303.       else
  304.       {
  305.         outModState[x] = 0;
  306.       }
  307.       set = true;
  308.     }
  309.     x++;
  310.   }
  311.   Serial.println(outModState[0]);
  312.   // Keyboard.set_modifier(outModState[0] | outModState[1] | outModState[2] | outModState[3]);
  313.   // Keyboard.send_now();
  314. }
  315.  
RAW Paste Data