SHARE
TWEET

Superkeyboard 16-08-11

spiderwebby Aug 16th, 2011 157 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 arduino 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. int lcd_power = 16;        //power for lcd on Ain2
  35. /*
  36.  * LCD RS pin to digital pin 10
  37.  * LCD Enable pin to digital pin 11
  38.  * LCD D4 pin to digital pin 12
  39.  * LCD D5 pin to digital pin 13
  40.  * LCD D6 pin to digital pin 14 //this is ain0
  41.  * LCD D7 pin to digital pin 15 //this is ain1
  42. */
  43.  
  44. volatile int kbd_data;
  45. volatile byte kbd_ptr;
  46. byte scan;
  47. byte scan_buffer[3];
  48. int keyboardmode = -1;
  49. int keycount = 0;
  50. int prev_scan = 0;
  51.  
  52. void setup()
  53. {
  54.   digitalWrite(lcd_power, HIGH);
  55.   delay(200);
  56.   lcd.print(VERSIONTEXT);
  57.   lcd.begin(16, 2);
  58.   lcd.setCursor(0, 1);
  59.   lcd.print(__DATE__);
  60.   pinMode(kbd_clock,INPUT);
  61.   pinMode(synchdata,INPUT);
  62.   attachInterrupt(1,fallingclock,FALLING);
  63.   pinMode(toggle,INPUT);
  64.   digitalWrite(toggle, HIGH);
  65.   pinMode(button,INPUT);
  66.   digitalWrite(button, HIGH);
  67. //  randomSeed(analogRead(0));
  68. //  Serial.begin(115200);
  69.  
  70.   pinMode(led, OUTPUT);
  71.   pinMode(kbd_power, OUTPUT);
  72.   digitalWrite(kbd_power, HIGH);
  73.  
  74. //  Serial.println(VERSIONTEXT);
  75.   delay(500);
  76.   lcd.setCursor(0, 1);
  77.   lcd.print("KeyCount:       ");
  78. }
  79.  
  80.  
  81. void loop()
  82. {
  83.  
  84.   kbd_ptr = 0;
  85.   kbd_data  = 0;
  86.   while(true)
  87.   {
  88.     if (digitalRead(toggle) == 0)
  89.     {
  90.       digitalWrite(led, LOW);
  91.       if (keyboardmode != 1)
  92.       {
  93.         lcd.setCursor(0, 0);
  94.         lcd.print("Dvorak          ");  
  95.         keyboardmode = 1;
  96.       }
  97.     }
  98.     else
  99.     {
  100.       digitalWrite(led, HIGH);  //make this PWM. LED is brighter than the sun.
  101.       if (keyboardmode != 0)
  102.       {
  103.         lcd.setCursor(0, 0);
  104.         lcd.print("Qwerty          ");  
  105.         keyboardmode = 0;
  106.       }
  107.     }
  108.    
  109.     if (digitalRead(button) == 0)
  110.     {
  111.       keycount=0;
  112.       lcd.setCursor(10, 1);
  113.       lcd.print("      ");
  114.       lcd.setCursor(10, 1);
  115.       lcd.print(keycount/2);
  116.     }
  117.    
  118.        
  119.    
  120.     //if we've captured a complete keystroke then send it to the host
  121.     if (11 == kbd_ptr)  
  122.     {
  123.      
  124.       scan_buffer_advance();
  125.       scan = (kbd_data >> 1) & 0xFF;
  126.       scan_buffer[0] = scan;
  127.       if (digitalRead(toggle) == 0) dvorak();
  128.  
  129.       //Serial.println(scan,HEX);
  130.      
  131.      
  132.       kbd_pc.write(scan);
  133.       kbd_ptr = 0;
  134.       kbd_data  = 0;
  135.      
  136.      
  137.       lcd.setCursor(10, 1);
  138.       lcd.print(keycount);
  139.      
  140.     }
  141.    
  142.  
  143.    
  144.   }  
  145. }
  146.  
  147. void scan_buffer_clear()
  148. {
  149.   int i;
  150.   for(i=0;i<=2;i++) scan_buffer[i] = 0;
  151. }
  152.  
  153. void scan_buffer_advance()
  154. { //there has got to be a better way of doing this
  155.   int i;
  156.   for(i=2;i>=1;i-- ) scan_buffer[i]=scan_buffer[i-1];
  157. }
  158.  
  159. void fallingclock()
  160. {
  161.   // clock line falling, so kbd->host data
  162.   kbd_data = kbd_data | (digitalRead(synchdata) << kbd_ptr);
  163.   kbd_ptr ++ ;
  164. }
  165.  
  166. void on_keypress()
  167. {
  168.   if(scan!=prev_scan)
  169.   {
  170.     keycount++;
  171.     prev_scan = scan;
  172.   }
  173.   scan_buffer_clear();
  174. }
  175.  
  176. void dvorak()
  177. {  
  178.   switch (scan)
  179.   {
  180.     case scan_q:
  181.       scan = scan_fslash;  // /
  182.       on_keypress();
  183.       break;
  184.  
  185.     case scan_w:
  186.       scan = scan_comma;  //,
  187.       on_keypress();
  188.       break;
  189.  
  190.     case scan_e:
  191.       scan = scan_fullstop;  //.
  192.       on_keypress();
  193.       break;
  194.  
  195.     case scan_r:    //R
  196.       scan = scan_p;  //P
  197.       on_keypress();
  198.       break;
  199.  
  200.     case scan_t:    //T
  201.       scan = scan_y;  //Y
  202.       on_keypress();
  203.       break;
  204.  
  205.     case scan_y:    //Y
  206.       scan = scan_f;  //F
  207.       on_keypress();
  208.       break;
  209.  
  210.     case scan_u:    //U
  211.       scan = scan_g;  //G
  212.       on_keypress();
  213.       break;
  214.  
  215.     case scan_i:    //I
  216.       scan = scan_c;  //C
  217.       on_keypress();
  218.       break;
  219.  
  220.     case scan_o:    //O
  221.       scan = scan_r;  //R
  222.       on_keypress();
  223.       break;
  224.  
  225.     case scan_p:    //P
  226.       scan = scan_l;  //L
  227.       on_keypress();
  228.       break;
  229.  
  230.     case scan_a:    //A
  231.       //scan = scan_a;  //A
  232.       on_keypress();
  233.        break;
  234.  
  235.     case scan_s:    //S
  236.       scan = scan_o;  //O
  237.       on_keypress();
  238.       break;
  239.  
  240.     case scan_d:    //D
  241.       scan = scan_e;  //E
  242.       on_keypress();
  243.       break;
  244.  
  245.     case scan_f:    //F
  246.       scan = scan_u;  //U
  247.       on_keypress();
  248.       break;
  249.  
  250.     case scan_g:    //G
  251.       scan = scan_i;  //I
  252.       on_keypress();
  253.       break;
  254.  
  255.     case scan_h:    //H
  256.       scan = scan_d;  //D
  257.       on_keypress();
  258.       break;
  259.  
  260.     case scan_j:    //J
  261.       scan = scan_h;  //H
  262.       on_keypress();
  263.       break;
  264.  
  265.     case scan_k:    //K
  266.       scan = scan_t;  //T
  267.       on_keypress();
  268.       break;
  269.  
  270.     case scan_l:    //L
  271.       scan = scan_n;  //N
  272.       on_keypress();
  273.       break;
  274.  
  275.     case scan_semic:    //;
  276.       scan = scan_s;  //S
  277.       on_keypress();
  278.       break;
  279.  
  280.     case scan_bslash:    // \
  281.       //scan = scan_bslash;  // \
  282.        on_keypress();
  283.        break;
  284.  
  285.     case scan_z:    //Z
  286.       scan = scan_semic;  //;
  287.       on_keypress();
  288.       break;
  289.  
  290.     case scan_x:    //X
  291.       scan = scan_q;  //Q
  292.       on_keypress();
  293.       break;
  294.  
  295.     case scan_c:    //C
  296.       scan = scan_j;  //J
  297.       on_keypress();
  298.       break;
  299.  
  300.     case scan_v:    //V
  301.       scan = scan_k;  //K
  302.       on_keypress();
  303.       break;
  304.  
  305.     case scan_b:    //B
  306.       scan = scan_x;  //X
  307.       on_keypress();
  308.       break;
  309.  
  310.     case scan_n:    //N
  311.       scan = scan_b;  //B
  312.       on_keypress();
  313.       break;
  314.  
  315.     case scan_m:    //M
  316.       //scan = scan_m;  //M
  317.       on_keypress();
  318.        break;
  319.  
  320.     case scan_comma:    //,
  321.       scan = scan_w;  //W
  322.       on_keypress();
  323.       break;
  324.  
  325.     case scan_fullstop:    //.
  326.       scan = scan_v;  //V
  327.       on_keypress();
  328.       break;
  329.  
  330.     case scan_fslash:    //  /  
  331.       if ((scan_buffer[1] != scan_extended)&&(scan_buffer[2] != scan_extended)) // to ignore num /
  332.       {
  333.         scan = scan_z;  //Z
  334.         on_keypress();
  335.         break;
  336.       }
  337.          
  338.     case scan_space:
  339.     case scan_tab:
  340.     case scan_return:
  341.     case scan_tilde:
  342.     case scan_1:
  343.     case scan_2:
  344.     case scan_3:
  345.     case scan_4:
  346.     case scan_5:
  347.     case scan_6:
  348.     case scan_7:
  349.     case scan_8:
  350.     case scan_9:
  351.     case scan_0:
  352.     case scan_minus:
  353.     case scan_equals:
  354.     case scan_backslash:
  355.     case scan_f1:
  356.     case scan_f2:
  357.     case scan_f3:
  358.     case scan_f4:
  359.     case scan_f5:
  360.     case scan_f6:
  361.     case scan_f7:
  362.     case scan_f8:
  363.     case scan_f9:
  364.     case scan_f10:
  365.     case scan_f11:
  366.     case scan_f12:
  367.     case scan_scroll:
  368.     case scan_brk:
  369.     case scan_ins:
  370.     case scan_del:
  371.     case scan_home:
  372.     case scan_end:
  373.     case scan_pageup:
  374.     case scan_pagedown:
  375.     case scan_left:
  376.     case scan_right:
  377.     case scan_up:
  378.     case scan_down:
  379.     //case scan_numlock:
  380.     case scan_numstar:
  381.     case scan_numminus:
  382.     case scan_numplus:
  383.     //case scan_numenter:
  384.     //case scan_numpoint:
  385.     //case scan_num0:
  386.     //case scan_num1:
  387.     //case scan_num2:
  388.     //case scan_num3:
  389.     //case scan_num4:
  390.     case scan_num5:
  391.     //case scan_num6:
  392.     //case scan_num7:
  393.     //case scan_num8:
  394.     //case scan_num9:
  395.     on_keypress();
  396.    
  397.     case scan_ctrl:
  398.     case scan_alt:
  399.     case scan_lshift:
  400.     case scan_rshift:
  401.     case scan_caps:
  402.     case scan_backspace:
  403.    
  404.    
  405.           scan_buffer_clear();
  406.          
  407.           break;
  408.   }
  409.  
  410. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 60% OFF on Pastebin PRO accounts!
Top