Go Down

Topic: ps2 Keyboard emulator and waking pc from sleep (Read 196 times) previous topic - next topic

jmeile

Sep 18, 2014, 06:20 pm Last Edit: Oct 10, 2014, 05:17 pm by jmeile Reason: 1
Hi

This is not really a question, but sharing my experience on this. It took me long to figure out some things.
In my project I wanted to create a ps2 Keyboard emulator to send keystrokes while the computer was sleeping and so waking it up. My basic setup was:
1x Arduino Duemilanove
1x ps2 Keyboard cable with only the male connector
1x laptop connected to a docking station with a ps2 port for keyboard

So, my idea was to connect the male connector to the ps2 port on the docking station, then plug the cables in the other end to the arduino. Finally send the signals through the ps2 port. So, first I will begin with my problems:

1) I found a lot of code for doing keyboard emulators based on the ps2dev.h library which you can get from here: http://playground.arduino.cc/ComponentLib/Ps2mouse (The download is in the last section: Emulating a PS2 device, Attach:ps2dev.zip). That library is old, so, you will have to replace the #include "WProgram.h" on ps2dev.h by #include "arduino.h". Then delete that include from ps2dev.cpp. If you don't do this, you will get errors while compiling your code in the arduino gui.

The code on most of the examples I found was almost the same; however, it didn't worked always. I just figured out after finding one example, that you need to do small delays after sending each scan code to the keyboard. Otherwise, either you won't get some keys, or keys like the shift will be activated, but not deactivated. I tried different values and saw that 2 milliseconds will be enough; however, you may increase it to 20 milliseconds if it doesn't work for you.

I also realized that you need also a pause before and after initializing the ps2 keyboard (Two lines on the first "if" from the "loop" block). I was always writing debug messages, so, it worked, but as soon as I deleted the debug messages, it didn't work. The fact is that printing a message last some time, so that's how I figured out you also need a pause there.

2) Another issue was cabling the keyboard. It really took me long time to figure out that you only need to plug the pins 1 (data) and 5 (clock) from the ps2 cable to the arduino. I was also connecting the power (pin 4) and ground (pin 3) to it. Since the other end is being connected to the pc, it will supply the power and the ground connections. If you connect those pins to the arduino, the system will work; however, if your laptop goes to sleep, the fan will be turned on and also the lights on the back of your laptop, so, I guess this is somehow a short circuit since the computer and the arduino are both providing power and ground.

3) The final issue was that waking the laptop with a ps2 keyboard isn't supported by all laptops. I was always trying with my emulated keyboard to wake my laptop and it didn't worked. I though it was because of the code. After long testing and changing things, I tested with a normal ps2 keyboard and it didn't work either. I tried reinstalling the drivers, updating the bios, changing the power settings. Nothing worked.

Then I found that you need first to check if the device supports it. So, in Windows you can just issue the command:
powercfg -devicequery "wake_programmable"

If your device doesn't appear there, it means that you can't wake your computer by using it. Another check you can do is going to the device manager, look for your keyboard, double click on it, then on the "Details"-Tab select "Power data". There you have to see something like: "PDCAP_WAKE_FROM_D0_SUPPORTED", or "PDCAP_WAKE_FROM_D1_SUPPORTED", or "PDCAP_WAKE_FROM_D2_SUPPORTED", or , or "PDCAP_WAKE_FROM_D3_SUPPORTED". If you don't see this, your device can't wake your computer and you also won't see the "Power Management"-Tab on its properties.

So, on my laptop, the only way to wake it up is with the power button or with an usb keyboard or an usb mouse. Even the built-in keyboard and mouse isn't supported :-(

So, right now I will try to wake my computer via usb. I guess this should work with an arduino leonardo, which has built-in support for emulating an usb-keyboard.

For the ones looking for the code, its on the attachment.

Best regards
Josef

Go Up