[Submission] PS/2 Communication library

In connection with this thread

I've dug up an old Arduino project (quoted here) of mine and ported it for the Spark Core.
It provides a basic, interrupt driven framework to talk to PS/2 devices (keyboard, mouse, ...).
For the topic above there is a PS/2 mouse example of how to talk to such a mouse.

Dedicated device classes (e.g. PS2Mouse, PS2Keyboard, PS2Scanner, ...) could be built on top of this.
In connection with the firmware/feature/usb-hid-support branch this would be a way to place the Core as a "man-in-the-middle" that could do both: pass through the devices data (a lot of USB devices still support PS/2 communication as legacy feature) and also remote control a PC via mouse/keyboard input via the cloud.

Please let me know if you find any issues with it or if there was any call for one of the above classes.

Repo: GitHub - ScruffR/PS2Communication: Interrupt driven PS/2 communication library (with PS/2 mouse example)
Also on Web IDE

2 Likes

I’ve found some little bugs in connection with mouse button 4 & 5 in the sample and fixed them.

v0.0.2
fix for buttons causing scroll wheel readings to add 64/128 steps per reading

v0.0.3
fix for swapped buttons 4 & 5

v0.0.4
PS2Mouse demo app starts with WiFi off. Cloud connect with 3+ button mouse press left/middle/right buttons together.
Some more debug info and some cleanup
Added application.cpp as local build demo for the use of core-firmware/feature/usb-hid-support together with PS/2 mouse.
If anybody would be interested in testing the USB HID Mouse/Keyboard feature with or without PS/2 mouse support, I could provide a prebuilt bin.

2 Likes

Just tested this library with a 3-button scroll-wheel mouse AND a very old two-button mouse, both PS/2. The library works excellently for both. 10/10 !

1 Like

@ScruffR I’ve started using your PS/2 Communication library in my code and it’s been great so far! I have a Photon hooked up to a PS/2 keyboard and have been using the keyboard as an input for the Photon. I’ve been running into an issue though: Normally when I power on my Photon, and consequently, the keyboard, the keyboard will power up and send the bytes FA and AA. All key presses after this properly map to the right values. However, sometimes, when I just reset the Photon instead of power it all the way off and back on, the keyboard will send the F4 followed by [. All the keys after these two bytes are shifted, but not shifted linearly within the scan codes. Do you have any insights?

Could you try to add a ps2.reset() - e.g. manually triggered via a button to test if it helps?

I guess the timing gets muddled up during a system reset without power cut, so forcing the PS/2 device to reset too after the system stabilizes might help.

If this doesn’t help the 10ms delay in reset() might be too short for your keyboard

void PS2Communication::reset()
{
  PS2Communication::suspend();
  PS2Communication::setPin(_dataPin, LOW);
  PS2Communication::flush();
  delayMicroseconds(10000);             // long enough for a reset?
  PS2Communication::setPin(_dataPin, HIGH);
  PS2Communication::resume();
  PS2Communication::write(0xFF);
  delay(500);
}

So I tried adding a manual PS2->reset() as part of the main loop, with the condition that if the time elapsed since Photon reset was greater than 2 seconds and the device had only returned F4 and [, that it would reset, which I logged with Particle.publish on the dashboard. I saw the keyboard try to reset a few times, but it kept returning F4 instead of FA. Is is possible to extend the 10 ms delay in reset()? Alternatively, I might just power the PS2 keyboard with a spare digital pin and hard reset the keyboard whenever the Photon resets.

l could extend the reset time, but would like to confirm first that this would really help.

Would you be so kind and try this out by downloading my library from here?
https://github.com/ScruffR/PS2Communication/archive/master.zip

And directly include the .h/.cpp files in your project.
This way you can experiment with different times.
You’d need to change the include statement in your project from #include PS2Communication/PS2Communication.h" to only #include "PS2Communication.h".

I was finally able to try out extending the reset time this morning. Increasing the time to 50 ms from 10 ms solved the problem!

Thanks for testing :+1:
I’ll push an update then ASAP


Done!

1 Like