Interfacing PS2 Keyboard with Arduino and Androwish

Difference between version 0 and 1 - Previous - Next
[JM] 2018/12/4 Using and stripped-down version of [A scale with Arduino and AndroWish] here is an interface to a PS2 keyboard.

The arduino is programmed with this:
https://github.com/PaulStoffregen/PS2Keyboard
======c
/*  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 3.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>

const int DataPin = 4;
const int IRQpin =  3;

PS2Keyboard keyboard;

void setup() {
  delay(1000);
  keyboard.begin(DataPin, IRQpin);
  Serial.begin(9600);
  Serial.println("OK");
}

void loop() {
  if (keyboard.available()) {
    
    // read the next key
    char c = keyboard.read();
    
    // check for some of the special keys
    if (c == PS2_ENTER) {
      Serial.println();
    } else if (c == PS2_TAB) {
      Serial.print("[Tab]");
    } else if (c == PS2_ESC) {
      Serial.print("[ESC]");
    } else if (c == PS2_PAGEDOWN) {
      Serial.print("[PgDn]");
    } else if (c == PS2_PAGEUP) {
      Serial.print("[PgUp]");
    } else if (c == PS2_LEFTARROW) {
      Serial.print("[Left]");
    } else if (c == PS2_RIGHTARROW) {
      Serial.print("[Right]");
    } else if (c == PS2_UPARROW) {
      Serial.print("[Up]");
    } else if (c == PS2_DOWNARROW) {
      Serial.print("[Down]");
    } else if (c == PS2_DELETE) {
      Serial.println("[Del]");
    } else {
      
      // otherwise, just print all normal characters
      Serial.println(c);
    }
  }
}
======

While Androwish (I actually used Undroidwish on a PC) is running this:

======
# AndroWish script to interface Arduino with PS2 Keyboard
#
# Wiring:
#
#  +--------------+       +-------------+   +-----------------+
#  | Android      |       |          +5V|---|        PS2      |
#  | Device       |--USB--| Arduino  GND|---|        Keyboard |
#  | w/ AndroWish |       |           D4|---|DATA             |
#  +--------------+       |           D3|---|CK               |
#                         +-------------+   +-----------------+
console show

wm attributes . -fullscreen 1
catch {borg screenorientation landscape}
sdltk screensaver 0

option add *Radiobutton.borderWidth 3
option add *Radiobutton.highlightThickness 0
option add *Button.borderWidth 3
option add *Button.highlightThickness 0

# Open/reopen serial line to Arduino
proc reopen {} {
    after cancel reopen
    if {[sdltk android]} {
        if {[catch {
            set dev [lindex [usbserial] 0]
            set ::SCALE(chan) [usbserial $dev]
            fconfigure $::SCALE(chan) -blocking 0 -translation binary \
                -mode 9600,n,8,1
            fileevent $::SCALE(chan) readable readv
            set ::SCALE(value) "online"
            puts -nonewline $::SCALE(chan) "S"
        } err]} {
            sdltk log verbose $err
            set ::SCALE(value) "offline"
            after 1000 reopen
            return
        }
    } else {
        if {[catch {
            set ::SCALE(chan) [open "COM4" {RDWR}]
            fconfigure $::SCALE(chan) -translation binary -mode 9600,n,8,1
            fileevent $::SCALE(chan) readable readv
            set ::SCALE(value) "online"
        } err]} {
            sdltk log verbose $err
            puts $err
            set ::SCALE(value) "off..line"
            after 1000 reopen
            return
        }
    }
    # sendc 5 S
}

# Read data from Arduino
proc readv {} {
    if {[eof $::SCALE(chan)]} {
        sdltk log verbose "EOF detected"
        puts "EOF detected"
        catch {close $::SCALE(chan)}
        set ::SCALE(value) "offline"
        after 1000 reopen
        return
    }
    
    if {[catch {gets $::SCALE(chan) v} err]} {
        sdltk log verbose $err
        puts $err
        catch {close $::SCALE(chan)}
        set ::SCALE(value) "offline"
        after 1000 reopen
        return
    }
    
    puts $v
    set ::SCALE(value) $v
}

# Main screen
label .weight -textvariable SCALE(value) -font {Courier -160}
grid .weight -row 0 -column 0 -columnspan 9 -sticky news
grid columnconfigure . 0 -uniform largebutton
grid rowconfigure . 0 -weight 12
grid columnconfigure . all -weight 1

reopen

======