SHARE
TWEET

Untitled

a guest May 5th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ps2dev.h" // to emulate a PS/2 device
  2.  
  3. // Orange = 2
  4. // Blue = 3
  5. // Red = 5V (3 in)
  6. // Black = GND (4 in)
  7. // EXT Power, USB for COM only
  8.  
  9. PS2dev keyboard(2,3); // PS2dev object (2:data, 3:clock)
  10. int enabled = 0; // pseudo variable for state of "keyboard"
  11. boolean serialConnected = false;
  12. int incomingByte = 0;
  13.  
  14. void ack() {
  15.   //acknowledge commands
  16.   while(keyboard.write(0xFA));
  17. }
  18.  
  19. int kbdCmd(int command) {
  20.   unsigned char val;
  21.   switch (command) {
  22.   case 0xFF: //reset
  23.     ack();
  24.     //the while loop lets us wait for the host to be ready
  25.     while(keyboard.write(0xAA)!=0);
  26.     break;
  27.   case 0xFE: //resend
  28.     ack();
  29.     break;
  30.   case 0xF6: //set defaults
  31.     //enter stream mode
  32.     ack();
  33.     break;
  34.   case 0xF5: //disable data reporting
  35.     //FM
  36.     enabled = 0;
  37.     ack();
  38.     break;
  39.   case 0xF4: //enable data reporting
  40.     //FM
  41.     enabled = 1;
  42.     ack();
  43.     break;
  44.   case 0xF3: //set typematic rate
  45.     ack();
  46.     keyboard.read(&val); //do nothing with the rate
  47.     ack();
  48.     break;
  49.   case 0xF2: //get device id
  50.     ack();
  51.     keyboard.write(0xAB);
  52.     keyboard.write(0x83);
  53.     break;
  54.   case 0xF0: //set scan code set
  55.     ack();
  56.     keyboard.read(&val); //do nothing with the rate
  57.     ack();
  58.     break;
  59.   case 0xEE: //echo
  60.     //ack();
  61.     keyboard.write(0xEE);
  62.     break;
  63.   case 0xED: //set/reset LEDs
  64.     ack();
  65.     keyboard.read(&val); //do nothing with the rate
  66.     ack();
  67.     break;
  68.   }
  69. }
  70.  
  71. void connectHost() {
  72.   while (Serial.available() <= 0) {
  73.     Serial.print('A');   // send a capital A
  74.     delay(300);
  75.   }
  76. }
  77.  
  78. void setup() {
  79.   pinMode(13, OUTPUT);
  80.   //establish serial connection with host
  81.   Serial.begin(9600);
  82.   // establish ps/2 connection with target
  83.   while(keyboard.write(0xAA)!=0){
  84.     digitalWrite(13, HIGH);
  85.     delay(500);
  86.     digitalWrite(13, LOW);
  87.     delay(500);
  88.   }
  89.   delay(100);  
  90.  
  91.   connectHost();
  92.   Serial.println("\nSerial Host Connected");
  93.   Serial.flush();
  94. }
  95.  
  96. void loop() {
  97.   unsigned char c;
  98.   if( (digitalRead(2)==LOW) || (digitalRead(3) == LOW)) {
  99.     if(digitalRead(2)==LOW){
  100.       Serial.println("pin 2  is LOW");
  101.     } else {
  102.       Serial.println("pin 3 is LOW");
  103.     }
  104.     while(keyboard.read(&c));
  105.     kbdCmd(c);
  106.     Serial.print("Target: 0x");
  107.     Serial.println(c, HEX);
  108.   }  
  109.   else {//if host device wants to send a command:
  110.     //echo ASCII code from terminal and write to ps/2
  111.     if(Serial.available() > 0) {
  112.       incomingByte = Serial.read();
  113.       keyboard.write(incomingByte);      
  114.       Serial.print("Host: 0x");
  115.       Serial.print(incomingByte, HEX);
  116.       Serial.print(" ");
  117.       Serial.print(incomingByte);
  118.       Serial.print(" ");
  119.       Serial.println(incomingByte, BIN);
  120.     }
  121.   }
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top