Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Arduino PS/2 keyboard dvorak conversion
  2. //now with added LCD (which does almost nothing..)
  3. //moar infos: http://http://elektrickery.blogspot.com/
  4.  
  5.  
  6. //keyboard device code from the PS2Dev library
  7. //keyboard reading code credit goes to clackmannan from the old forums
  8.  
  9. #define VERSIONTEXT "SuperKeyboard1"
  10.  
  11.  
  12. //for windows
  13. #include "I:\Sketchbook\superkeyboard1\scancodes.h"
  14. #include "I:\Sketchbook\libraries\ps2dev\ps2dev.cpp"
  15.  
  16. //for linux
  17. //#include "/media/keyboard/sketchbook/libraries/ps2dev/ps2dev.cpp"
  18. //#include "/media/keyboard/Sketchbook/superkeyboard1/scancodes.h"
  19.  
  20. #include <LiquidCrystal.h>
  21.  
  22. LiquidCrystal lcd(10, 11, 12, 13, 14, 15);
  23. PS2dev kbd_pc(5,6); // 5 clock 6 data
  24.  
  25.  
  26. int kbd_power = 2;     // yellow wire - keyboard needs delayed powerup
  27. int kbd_clock = 3;     // white wire - interrupt 1
  28. int synchdata = 4;     // green wire - no interrupt
  29. int pc_clock = 5;      // white wire
  30. int pc_data = 6;       // green wire
  31. int button = 7;        // front pannel
  32. int toggle = 8;        // front pannel
  33. int led = 9;           // front pannel
  34. /*
  35.  * LCD RS pin to digital pin 10
  36.  * LCD Enable pin to digital pin 11
  37.  * LCD D4 pin to digital pin 12
  38.  * LCD D5 pin to digital pin 13
  39.  * LCD D6 pin to digital pin 14 //this is ain0
  40.  * LCD D7 pin to digital pin 15 //this is ain1
  41. */
  42.  
  43. volatile int kbd_data;
  44. volatile byte kbd_ptr;
  45. byte scan;
  46. byte scan_buffer[5];
  47.  
  48. void setup()
  49. {    
  50.   pinMode(kbd_clock,INPUT);
  51.   pinMode(synchdata,INPUT);
  52.   attachInterrupt(1,fallingclock,FALLING);
  53.   pinMode(toggle,INPUT);
  54.   digitalWrite(toggle, HIGH);
  55.   pinMode(button,INPUT);
  56.   digitalWrite(button, HIGH);
  57.   randomSeed(analogRead(0));
  58.  
  59.   Serial.begin(115200);
  60.   lcd.begin(16, 2);
  61.  
  62.   pinMode(led, OUTPUT);
  63.   pinMode(kbd_power, OUTPUT);
  64.   digitalWrite(kbd_power, HIGH);
  65.  
  66.   Serial.println(VERSIONTEXT);
  67.   lcd.print(VERSIONTEXT);
  68.   lcd.setCursor(0, 1);
  69.   lcd.print(__DATE__);
  70.  
  71. }
  72.  
  73.  
  74.  
  75. void loop()
  76. {
  77.  
  78.   kbd_ptr = 0;
  79.   kbd_data  = 0;
  80.   while(true)
  81.   {
  82.     if (digitalRead(toggle) == 0) digitalWrite(led, LOW);
  83.     else digitalWrite(led, HIGH);
  84.     //if we've captured a complete keystroke then send it to the host
  85.  
  86.     if (11 == kbd_ptr)  
  87.     {
  88.      
  89.       scan_buffer_advance();
  90.       scan = (kbd_data >> 1) & 0xFF;
  91.       scan_buffer[0] = scan;
  92.       if (digitalRead(toggle) == 0) dvorak();
  93.       Serial.println(scan,HEX);
  94.      
  95.       kbd_pc.write(scan);
  96.       kbd_ptr = 0;
  97.       kbd_data  = 0;
  98.     }
  99.    
  100. //    if (digitalRead(button) == 0)
  101.  
  102.    
  103.   }  
  104. }
  105.  
  106. void scan_buffer_clear()
  107. {
  108.   scan_buffer[0] = 0;
  109.   scan_buffer[1] = 0;
  110.   scan_buffer[2] = 0;
  111.   scan_buffer[3] = 0;
  112.   scan_buffer[4] = 0;
  113. }
  114.  
  115.  
  116. void scan_buffer_advance()
  117. { //there has got to be a better way of doing this
  118.   scan_buffer[4]=scan_buffer[3];
  119.   scan_buffer[3]=scan_buffer[2];
  120.   scan_buffer[2]=scan_buffer[1];
  121.   scan_buffer[1]=scan_buffer[0];
  122. }
  123.  
  124. void fallingclock()
  125. {
  126.   // clock line falling, so kbd->host data
  127.   kbd_data = kbd_data | (digitalRead(synchdata) << kbd_ptr);
  128.   kbd_ptr ++ ;
  129. }
  130.  
  131. void dvorak()
  132. {  
  133.   switch (scan)
  134.   {
  135.     case scan_q:
  136.       scan = scan_fslash;  // /
  137.       scan_buffer_clear();
  138.       break;
  139.  
  140.     case scan_w:
  141.       scan = scan_comma;  //,
  142.       scan_buffer_clear();
  143.       break;
  144.  
  145.     case scan_e:
  146.       scan = scan_fullstop;  //.
  147.       scan_buffer_clear();
  148.       break;
  149.  
  150.     case scan_r:    //R
  151.       scan = scan_p;  //P
  152.       scan_buffer_clear();
  153.       break;
  154.  
  155.     case scan_t:    //T
  156.       scan = scan_y;  //Y
  157.       scan_buffer_clear();
  158.       break;
  159.  
  160.     case scan_y:    //Y
  161.       scan = scan_f;  //F
  162.       scan_buffer_clear();
  163.       break;
  164.  
  165.     case scan_u:    //U
  166.       scan = scan_g;  //G
  167.       scan_buffer_clear();
  168.       break;
  169.  
  170.     case scan_i:    //I
  171.       scan = scan_c;  //C
  172.       scan_buffer_clear();
  173.       break;
  174.  
  175.     case scan_o:    //O
  176.       scan = scan_r;  //R
  177.       scan_buffer_clear();
  178.       break;
  179.  
  180.     case scan_p:    //P
  181.       scan = scan_l;  //L
  182.       scan_buffer_clear();
  183.       break;
  184.  
  185.     case scan_a:    //A
  186.       //scan = scan_a;  //A
  187.       scan_buffer_clear();
  188.        break;
  189.  
  190.     case scan_s:    //S
  191.       scan = scan_o;  //O
  192.       scan_buffer_clear();
  193.       break;
  194.  
  195.     case scan_d:    //D
  196.       scan = scan_e;  //E
  197.       scan_buffer_clear();
  198.       break;
  199.  
  200.     case scan_f:    //F
  201.       scan = scan_u;  //U
  202.       scan_buffer_clear();
  203.       break;
  204.  
  205.     case scan_g:    //G
  206.       scan = scan_i;  //I
  207.       scan_buffer_clear();
  208.       break;
  209.  
  210.     case scan_h:    //H
  211.       scan = scan_d;  //D
  212.       scan_buffer_clear();
  213.       break;
  214.  
  215.     case scan_j:    //J
  216.       scan = scan_h;  //H
  217.       scan_buffer_clear();
  218.       break;
  219.  
  220.     case scan_k:    //K
  221.       scan = scan_t;  //T
  222.       scan_buffer_clear();
  223.       break;
  224.  
  225.     case scan_l:    //L
  226.       scan = scan_n;  //N
  227.       scan_buffer_clear();
  228.       break;
  229.  
  230.     case scan_semic:    //;
  231.       scan = scan_s;  //S
  232.       scan_buffer_clear();
  233.       break;
  234.  
  235.     case scan_bslash:    // \
  236.       //scan = scan_bslash;  // \
  237.       scan_buffer_clear();
  238.        break;
  239.  
  240.     case scan_z:    //Z
  241.       scan = scan_semic;  //;
  242.       scan_buffer_clear();
  243.       break;
  244.  
  245.     case scan_x:    //X
  246.       scan = scan_q;  //Q
  247.       scan_buffer_clear();
  248.       break;
  249.  
  250.     case scan_c:    //C
  251.       scan = scan_j;  //J
  252.       scan_buffer_clear();
  253.       break;
  254.  
  255.     case scan_v:    //V
  256.       scan = scan_k;  //K
  257.       scan_buffer_clear();
  258.       break;
  259.  
  260.     case scan_b:    //B
  261.       scan = scan_x;  //X
  262.       scan_buffer_clear();
  263.       break;
  264.  
  265.     case scan_n:    //N
  266.       scan = scan_b;  //B
  267.       scan_buffer_clear();
  268.       break;
  269.  
  270.     case scan_m:    //M
  271.       //scan = scan_m;  //M
  272.       scan_buffer_clear();
  273.        break;
  274.  
  275.     case scan_comma:    //,
  276.       scan = scan_w;  //W
  277.       scan_buffer_clear();
  278.       break;
  279.  
  280.     case scan_fullstop:    //.
  281.       scan = scan_v;  //V
  282.       scan_buffer_clear();
  283.       break;
  284.  
  285.     case scan_fslash:    //  /  
  286.       if ((scan_buffer[1] != scan_extended)&&(scan_buffer[2] != scan_extended)) // to ignore num /
  287.       {
  288.         scan = scan_z;  //Z
  289.         scan_buffer_clear();
  290.         break;
  291.       }
  292.      
  293.     case scan_space:
  294.     case scan_ctrl:
  295.     case scan_alt:
  296.     case scan_lshift:
  297.     case scan_rshift:
  298.     case scan_caps:
  299.     case scan_tab:
  300.     case scan_return:
  301.     case scan_tilde:
  302.     case scan_1:
  303.     case scan_2:
  304.     case scan_3:
  305.     case scan_4:
  306.     case scan_5:
  307.     case scan_6:
  308.     case scan_7:
  309.     case scan_8:
  310.     case scan_9:
  311.     case scan_0:
  312.     case scan_minus:
  313.     case scan_equals:
  314.     case scan_backspace:
  315.     case scan_backslash:
  316.     case scan_f1:
  317.     case scan_f2:
  318.     case scan_f3:
  319.     case scan_f4:
  320.     case scan_f5:
  321.     case scan_f6:
  322.     case scan_f7:
  323.     case scan_f8:
  324.     case scan_f9:
  325.     case scan_f10:
  326.     case scan_f11:
  327.     case scan_f12:
  328.     case scan_scroll:
  329.     case scan_brk:
  330.     case scan_ins:
  331.     case scan_del:
  332.     case scan_home:
  333.     case scan_end:
  334.     case scan_pageup:
  335.     case scan_pagedown:
  336.     case scan_left:
  337.     case scan_right:
  338.     case scan_up:
  339.     case scan_down:
  340.     //case scan_numlock:
  341.     case scan_numstar:
  342.     case scan_numminus:
  343.     case scan_numplus:
  344.     //case scan_numenter:
  345.     //case scan_numpoint:
  346.     //case scan_num0:
  347.     //case scan_num1:
  348.     //case scan_num2:
  349.     //case scan_num3:
  350.     //case scan_num4:
  351.     case scan_num5:
  352.     //case scan_num6:
  353.     //case scan_num7:
  354.     //case scan_num8:
  355.     //case scan_num9:
  356.       scan_buffer_clear();
  357.       break;
  358.   }
  359.  
  360. }