Go Down

Topic: PS2Keyboard lib works with Due, module for <1$ (Read 1 time) previous topic - next topic

HermannSW

Apr 12, 2016, 05:24 pm Last Edit: Apr 12, 2016, 05:31 pm by HermannSW
Hi,

searched the forum and did not find a thread in Due forum about PS2Keyboard library.
First I tried last version (2.3) from Arduino Playground:
http://playground.arduino.cc/Main/PS2Keyboard

That does not compile for Arduino Due. Then I found the project wiki page,
https://www.pjrc.com/teensy/td_libs_PS2Keyboard.html

downloaded v2.4 and ran "PS2Keyboard" Simple_Test example and it immediately worked:


On the right side you see the 0,94$ PS2 module for Arduino I used to connect the keyboard:
http://www.aliexpress.com/item/PS2-Module-PS-2-Mouse-Keyboard-Socket-Compatible-for-Arduino/32586612372.html

The only problem left for me is that my keyboard is German ...  :)
(z in Anyway and > where : should have been)

Code: [Select]
/*  PS2Keyboard library example
 
  PS2Keyboard now requries both pins specified for begin()

  keyboard.begin(data_pin, irq_pin);
 
  Valid irq pins:
     Arduino Uno:  2, 3
     Arduino Due:  All pins, except 13 (LED)
     Arduino Mega: 2, 3, 18, 19, 20, 21
     Teensy 2.0:   All pins, except 13 (LED)
     Teensy 2.0:   5, 6, 7, 8
     Teensy 1.0:   0, 1, 2, 3, 4, 6, 7, 16
     Teensy++ 2.0: 0, 1, 2, 3, 18, 19, 36, 37
     Teensy++ 1.0: 0, 1, 2, 3, 18, 19, 36, 37
     Sanguino:     2, 10, 11
 
  for more information you can read the original wiki in arduino.cc
  at http://www.arduino.cc/playground/Main/PS2Keyboard
  or http://www.pjrc.com/teensy/td_libs_PS2Keyboard.html
 
  Like the Original library and example this is under LGPL license.
 
  Modified by Cuninganreset@gmail.com on 2010-03-22
  Modified by Paul Stoffregen <paul@pjrc.com> June 2010
*/
   
#include <PS2Keyboard.h>
...


Hermann.
stepper PT camera system:
https://forum.arduino.cc/index.php?topic=647703.0
high precision linear actuator:
https://forum.arduino.cc/index.php?topic=645745.0
http://stamm-wilbrandt.de/en/Raspberry_camera.html

HermannSW

I wanted a standalone Due system with PS2Keyboard, and therefore DueVGA library came to my mind.

This is the hardware, very few cables and resistors needed:



This is the end picture of an editor demo video:



This is the youtube video:
https://youtu.be/xlLvWif0Dng


And this is the sketch (with bug in cursor mode):
Code: [Select]
#include <VGA.h>

#include <PS2Keyboard.h>

const int DataPin = 8;
const int IRQpin =  5;

PS2Keyboard keyboard;

#define w 1024
#define h 675
#define P "(with PS2Keyboard lib, v2.4)"
#define S "Sierpinski triangle"
#define T "1024 x 675  DueVGA lib"

int x,y,state;
long last;

void setup() {
  unsigned char L[2][h];
  int n,k,o=0;
 
  //keyboard.begin(DataPin, IRQpin, PS2Keymap_US);
  keyboard.begin(DataPin, IRQpin, PS2Keymap_German);
  //keyboard.begin(DataPin, IRQpin, PS2Keymap_French);

  x=y=0;
 
  VGA.begin(w,h);
 
  for(n=0; n<h; ++n, o=1-o) {
    L[1-o][0]=L[1-o][n]=1;
    for(k=1; k<n; ++k) { L[1-o][k]=L[o][k-1]+L[o][k]; }
    for(k=0; k<=n; ++k) {
      VGA.drawPixel(w/2-n+2*k,n,(L[1-o][k]%2)!=0);
    }
  }
 
  drawText32(P,w/2-16*strlen(P),h-128);
  drawText32(T,w/2-16*strlen(T),h-64);
  drawText32(S,w/2-16*strlen(S),h-32);

  last=millis(); state=0;
  drawCursor32(x,y,state);   
}

void loop() {
  if (keyboard.available()) {
    char c = keyboard.read();
    if (c == PS2_ENTER) {
      if (state)
        drawCursor32(x,y,state);
      y+=32; x=0;
    } else if (c == PS2_BACKSPACE) {
      if (x > 0) {
        char s[]=" ";
        if (state)
          drawCursor32(x,y,state);
        x-=32;
        drawText32(s,x,y);
      } 
    } else if (c == PS2_LEFTARROW) {
      if (x > 0) {
        if (state)
          drawCursor32(x,y,state);
        x-=32;
      } 
    } else if (c == PS2_RIGHTARROW) {
      if (x < 992) {
        if (state)
          drawCursor32(x,y,state);
        x+=32;
      } 
    } else if (c == PS2_UPARROW) {
      if (y > 0) {
        if (state)
          drawCursor32(x,y,state);
        y-=32;
      } 
    } else if (c == PS2_DOWNARROW) {
      if (y < 640) {
        if (state)
          drawCursor32(x,y,state);
        y+=32;
      } 
    } else {
      char s[]={c,0};
      drawText32(s,x,y);
      x+=32;
    }
  }
  else
  {
    if (millis()-last > 500)
    {
      drawCursor32(x,y,state);
      last=millis();
    } 
  }
}

void drawCursor32(int x, int y, int& s)
{
  int i,j;
  s = 1-s;
  for(i=x; i<x+32; ++i)
    for(j=y; j<y+32; ++j)
      VGA.putPPixelFast(i,j,1-VGA.getPPixelFast(i,j));
}

void drawText32(char *text, int x, int y)
{
  uint8_t t;
  while(t=(uint8_t)*text++){
    for(int j=0;j<8;j++)for(int i=0;i<8;i++){
      bool p=(_vga_font8x8[8*t+j]&(128>>i))!=0;
      for(int a=0; a<4; ++a)for(int b=0; b<4; ++b) {
        VGA.drawPixel(x+4*i+a,y+4*j+b,p);
      }
    }
    x+=32; 
  }
}



I used 32x32 font only for being able to still read the text in the video.
Was a long fight with my Android phone camera to get video/photos that were not too bright.

This is how to use a German keyboard:
Code: [Select]
keyboard.begin(DataPin, IRQpin, PS2Keymap_German);

I was not sure on 5V pin of PS2 connector, so I connected it to 3.3V Arduino pin and all worked fine.


Hermann.
stepper PT camera system:
https://forum.arduino.cc/index.php?topic=647703.0
high precision linear actuator:
https://forum.arduino.cc/index.php?topic=645745.0
http://stamm-wilbrandt.de/en/Raspberry_camera.html

Go Up