Go Down

Topic: PS2 Keyboard Emulator (Read 10349 times) previous topic - next topic

TardisX

Yes, I thought as much :-)

Hardware wise, can I basically hook the port straight up to the arduino? Do I need to do something smarter? Finding lots of stuff about hooking up a keyboard to an arduino, but not the reverse....

luke123

ive given up on the usb hid , as it wont compile.

so ive gone down the ps/2 route.

Ive also considered using serial ,  i only want to run the exe and dont want to install software or .net framework.



Just wondering if anyone has been able to act as a ps/2 keyboard yet ?

If so can you post your code that would be a big help.

cheer
luke


Slasy

#17
Aug 18, 2010, 03:24 pm Last Edit: Aug 18, 2010, 03:56 pm by Slasy Reason: 1
My example of using ps2dev as a keyboard:

edit 'ps2dev.h'
Code: [Select]
comment out #include "WConstants.h"
and add a line #include <Wprogram.h>
...
//#include "WConstants.h"
#include <Wprogram.h>
...

here is code:
I used royboy's code http://siliconrepublic.blogspot.com
Code: [Select]
#include "ps2dev.h" // to emulate a PS/2 device

PS2dev keyboard(3,2); // PS2dev object (2:data, 3:clock)
int enabled = 0; // pseudo variable for state of "keyboard"

void ack() {
 //acknowledge commands
 while(keyboard.write(0xFA));
}

int keyboardcommand(int command) {
 unsigned char val;
 switch (command) {
 case 0xFF: //reset
   ack();
   //the while loop lets us wait for the host to be ready
   while(keyboard.write(0xAA)!=0);
   break;
 case 0xFE: //resend
   ack();
   break;
 case 0xF6: //set defaults
   //enter stream mode
   ack();
   break;
 case 0xF5: //disable data reporting
   //FM
   enabled = 0;
   ack();
   break;
 case 0xF4: //enable data reporting
   //FM
   enabled = 1;
   ack();
   break;
 case 0xF3: //set typematic rate
   ack();
   keyboard.read(&val); //do nothing with the rate
   ack();
   break;
 case 0xF2: //get device id
   ack();
   keyboard.write(0xAB);
   keyboard.write(0x83);
   break;
 case 0xF0: //set scan code set
   ack();
   keyboard.read(&val); //do nothing with the rate
   ack();
   break;
 case 0xEE: //echo
   //ack();
   keyboard.write(0xEE);
   break;
 case 0xED: //set/reset LEDs
   ack();
   keyboard.read(&val); //do nothing with the rate
   ack();
   break;
 }
}

void setup() {
 // send the keyboard start up
 while(keyboard.write(0xAA)!=0);
 delay(10);
}

void loop() {
 unsigned char c;
 //if host device wants to send a command:
 if( (digitalRead(3)==LOW) || (digitalRead(2) == LOW)) {
   while(keyboard.read(&c)) ;
   keyboardcommand(c);
 }
 else{ //send keypresses accordingly using scancodes
 // secancodes: http://www.computer-engineering.org/ps2keyboard/scancodes2.html
 keyboard.write(0x1C); // \
 keyboard.write(0xF0); //  |- send 'a'
 keyboard.write(0x1C); // /
 delay (1000); // wait 1 second
 }
}

Go Up