Pages: [1]   Go Down
Author Topic: Altering Functionality of PS/2 Keyboard  (Read 4101 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So wiring up the keyboard and reading using the PS/2 Keyboard Library was dead simple, but now I'd like to do something a little different. As it is now, when you press down a key, it performs like a usual keyboard, printing it's respective letter. When you hold it down, it prints the letter over and over, like thisssssssssssss. I would like instead to simply read when the key is pressed and when it is released.

Here's what I've got right now (hacked from the example supplied with the library).
Code:
void loop() {
  if (keyboard.available()) { //tells if a key is pressed
   
    char c = keyboard.read(); //finds out which key
   
    //Serial.print(c);
    if (c == PS2_ESC) { //is it the escape key that is pressed?
      if (current_key != 1) { //have we already established that it is pressed?
        current_key = 1; //set key as "on"
        Serial.print("[ESC ON]");
      }
    }
  }else{
    if (current_key != 0) {
      current_key = 0; //set key as "off"
      Serial.print("[ESC OFF]");
    }
  }
}

But when I hold a key, it just repeats "[ESC ON][ESC OFF]" over and over again. Am I to deduce that keyboard.available() is only detecting a key press every other loop? If this is the case, is there a way to set up a delay so that they will be in sync? Or better yet, an easy way to fix this in the library files? Any thoughts would be much appreciated.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Figured it out, thanks to this post I found: http://nocommercialpotential.posterous.com/interfacing-the-ps2-keyboard
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for posting that link, added to the PS/2 playground page with credits to you.
« Last Edit: February 03, 2012, 05:32:11 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, no problem.

I actually have a different question now about ps/2 scan codes. When I set it up to print the scan code in hexadecimal like
Code:
Serial.print(key, HEX);

For certain keys, it gives me two numbers comma separated. For example, the up arrow gives me "E0,75"... I consulted the scan code set and this is indeed correct, but I can't find anything about how to work with this data. When I set it up to print scan codes in binary, it gives me 11100000, which is equal to E0. So I just figured it was either/or, and set up my code to check for 0xE0.

This SORT OF works, but unlike the rest of the keys which I have set up to only detect the keypress only once when held, if I hold this key down it detects a keypress every loop ("[up][up][up][up]etc.."). I've tried checking for 0x75 as well, with the same results.

So.... what am I missing here?
« Last Edit: February 03, 2012, 09:50:05 pm by cassettica » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The technique to parse is to "look one char ahead", so you must read one char, check if it indicates a special char, if so read another char

in pseudocode
Code:
byte ch = readCharPS2();
switch(ch)
{
  case 0xE0: // special char => 2 bytes
    ch = readCharPS2();
    processSpecial(ch);
    break;

  case 0xE1: // another 2 byte code (for example)
    ch = readCharPS2();
    processSpecial(ch);
    break; 

  default:  //normalchar
    process(ch);
    break;
}

In the same way you can process any special key.



Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahhhhh. I see. Thanks so much, I don't think I ever would have figured that out on my own.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Welcome, most of the problems you must start to tackle with pen and paper, not with programming smiley-wink
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do you connect the wires from a PS2 keyboard to the Arduino?

And will this interface be able to make LED displays like these work to say things like
"Merry Christmas!", "Happy Birthday!", etc.?

Here's my LED display I bought on eBay that I'm working on this project with.


And here's the terminal strip for additional help.
« Last Edit: August 30, 2012, 07:48:04 pm by tpirman1982 » Logged

USA
Offline Offline
God Member
*****
Karma: 14
Posts: 644
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How do you connect the wires from a PS2 keyboard to the Arduino?

The Playground page for PS/2 Keyboard (also linked in the original post) has a wiring diagram showing how to connect the contacts on the MIDI plug to the appropriate pins on an Arduino.

As far as the LED display, if you don't have some documentation on how to connect "Data A" and "Data B" and what voltage levels they require it would very helpful to find some.
Logged

Pages: [1]   Go Up
Jump to: