PS2

Overview

PS/2 device is a legacy human interface device.

It's easy to use for input device on micro controller.

http://mbed.org/media/uploads/shintamainjp/_scaled_dsc03063.jpg

http://mbed.org/media/uploads/shintamainjp/_scaled_dsc03413.jpg

So I decided to design the library.

The library use only 2 pins (CLK and DAT).

It use interrupt based receive, so your application codes will simplify.

Feautures

  • Supported a PS/2 keyboard and a mouse.
  • Interrupt based receiver.
  • Simple interfaces.

Circuits

Circuits

Projects

Library

Test program

How to use it?

PS2Keyboard class

#include "mbed.h"
#include "PS2Keyboard.h"

PS2Keyboard ps2kb(p12, p11); // CLK, DAT

int main() {
    PS2Keyboard::keyboard_event_t evt_kb;

    while (1) {
        if (ps2kb.processing(&evt_kb)) {
            printf("[%d]:", evt_kb.type);
            for (int i = 0; i < evt_kb.length; i++) {
                printf("%02x ", evt_kb.scancode[i]);
            }
            printf("\n");
        }
    }
}

PS2Mouse class

#include "mbed.h"
#include "PS2Mouse.h"

PS2Mouse ps2ms(p23, p22); // CLK, DAT

int main() {
    PS2Mouse::mouse_event_t evt_ms;
    while (1) {
        if (ps2ms.processing(&evt_ms)) {
            printf("%c%c%c:%4d,%4d,%2d\n",
                evt_ms.left ? 'L' : '.',
                evt_ms.center ? 'C' : '.',
                evt_ms.right ? 'R' : '.',
                evt_ms.x, evt_ms.y, evt_ms.z);
        }
    }
}

References

Limits

  • Some PS/2 mouses will transmit data high rates. In this case, the internal buffer will overflow.




6 comments:

04 Oct 2010

Sparkfun has a handy PS/2 breakout board.

/media/uploads/4180_1/_scaled_ps2-1-.jpg

28 Oct 2010

Dear Mr.Jim-san

Thank you for your post. :) It's great parts for test the library.

Regards Shin.

21 Feb 2011

Dear Prof Hamblen

I am working with the PS/2 breakout module. I need to know a method to convert these scancodes to actual data. I am trying something as simple as interfacing a keyboard and lcd with the mbed module. I need whatever typed on the keyboard to be displayed on the lcd in real time!

26 Mar 2011

Deepesh Nair wrote:

Dear Prof Hamblen

I am working with the PS/2 breakout module. I need to know a method to convert these scancodes to actual data. I am trying something as simple as interfacing a keyboard and lcd with the mbed module. I need whatever typed on the keyboard to be displayed on the lcd in real time!

Look over at some of the hobby OS websites. http://www.win.tue.nl/~aeb/linux/kbd/scancodes.html http://wiki.osdev.org/PS2_Keyboard and finally http://forum.osdev.org/viewtopic.php?f=1&p=174629#p174629 is a very simplified scancode-to-ASCII lookup table.

22 Oct 2011

Dear Anyone,

There is a example for converting scancode to windows virtual key. And one example function called "get_line" which is to read a line from keyboard. The function just handle CAP LOCK/shift only. Others will be implemented by yourself. ^v^. Any comment, welcome to feedback.

Note: There may be some problem. Since, 1. I have modified LCD library to show cursor and get location from LCD.

You may comment out the line if problem found.

/media/uploads/hwkit/keyboard.txt

26 Sep 2012

This appears to have a subtle bug. It has a variable in the PS2Keyboard class named `count` that is then used like `buffer[count]=...`. Count however is never initialized. For me, it was causing random seg faults on start up.

I did this to fix it:

PS2Keyboard::PS2Keyboard(PinName clk_pin, PinName dat_pin)
        : ps2kb_init(clk_pin, dat_pin), ps2kb(clk_pin, dat_pin) {
        count=0; //this line
}