Saturday, December 13, 2014

Arduino Retro Computer: PS/2 Keyboard

Now that I had a video output, I needed some method of input to the computer. I have an extra PS/2 keyboard, and Adafruit sells a very handy PS/2 Wired Connector (Part #804)!


The wires coming from the PS/2 connector are flexible, so I soldered some rigid pins that can plug into the Arduino's GPIO slots.

Wiring from the PS/2 connector to Arduino Mega:
Black to GND
Green to 5V
Yellow to 2 (PWM, IRQ pin)
Brown to 30 (Digital, Data pin)
White - not used
Red - not used


Next up was the code. The Arduino PS/2 library and more information on connecting a keyboard can be found at: http://playground.arduino.cc/Main/PS2Keyboard

The examples in the library are very good, but here is a code snippet to show how simple it is:

#include <PS2Keyboard.h>

const int DataPin = 30;
const int IRQpin =  2;

PS2Keyboard keyboard;

void setup()
{
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
}

void loop()
{
  if (keyboard.available())
  {
    char inputKey = keyboard.read();

    if (inputKey == PS2_ENTER)
    {
      Serial.println();
    }
    else if (inputKey == PS2_TAB)
    {
      Serial.print("[Tab]");
    }
    else if (inputKey == PS2_ESC)
    {
      Serial.print("[ESC]");
    }
    else if (inputKey == PS2_LEFTARROW)
    {
      Serial.print("[Left]");
    }
    else if (inputKey == PS2_RIGHTARROW)
    {
      Serial.print("[Right]");
    }
    else if (inputKey == PS2_UPARROW)
    {
      Serial.print("[Up]");
    }
    else if (inputKey == PS2_DOWNARROW)
    {
      Serial.print("[Down]");
    }
    else if (inputKey == PS2_DELETE)
    {
      Serial.print("[Del]");
    }
    else if (inputKey == PS2_PAGEDOWN)
    {
      Serial.print("[PgDn]");
    }
    else if (inputKey == PS2_PAGEUP)
    {
      Serial.print("[PgUp]");
    }
    else if (inputKey >= 32 && inputKey <= 126)
    {
      // Normal characters.
      Serial.print(inputKey);
    }
  }
}

I've been writing an operating system for the Arduino computer that captures those key inputs to do various commands. The OS is a major work in progress, but I will be making postings about it as well.


No comments:

Post a Comment