Interface

Goal:

 

This week my goal was to build an interface that could be run on my RaspberryPi to read keystrokes and send them out over Serial. I need this functionality for my final project because I have a barcode-scanner that registers as a USB keyboard. My plan is to run this script in the background of the Pi when it boots and use it as a USB to serial converter to then send messages to my AVR to drive my robot. Is this the same as using a sledgehammer to drive a tack? Yes.

 

Python:

 

            I started with Python because I know it is popular in the Pi community and it was recommended and used a lot in class. I also knew that there was a PySerial library from the examples in the input week of class.

            I had previously had trouble with my python dendencies but found that if I run:

 

#export PYTHONPATH=${PYTHONPATH}:/usr/local/lib/python2.7/site-packages

 

At some point this stoped working, and I found that adding

 

import sys

sys.path.append('/usr/local/lib/python2.7/site-packages')

 

At the top of the file fixed that problem too.

 

In my terminal it fixes the problem. Not a permanent fix but a start.

 

After some googling I found PyNput library which handels keyboards and mice. I thought great, the assignments almost over, HA!

 

I coppied this from the python site.

 

 

from pynput.keyboard import Key, Listener

 

def on_press(key):

    print('{0} pressed'.format(

        key))

 

def on_release(key):

    print('{0} release'.format(

        key))

    if key == Key.esc:

        # Stop listener

        return False

 

# Collect events until released

with Listener(

        on_press=on_press,

        on_release=on_release) as listener:

    listener.join()

 

Error: something about indenting. I had copy pasted this, what could go wrong? After more googling I learned that Python uses the indentation space to the left of your command as syntax. Something must have gotten messed up in the paste, I fixed all the indenting and ran the script.

 

It would only register when I pressed control buttons but not letter or number buttons.

 

Description: Macintosh HD:Users:NedlamAdmin:Desktop:Screen Shot 2016-11-30 at 11.08.05 AM.png

 

The red bracket shows when I tried to enter letters. The blue arrow shows a strange line that showed up as a result of taking the screen capture (this is actually the second screen capture so that I could report the first).

 

My next guess was that this might be a security thing, against sniffing passwords etc. I ran it as root and no change.

 

I tried to figure this out for a few hours but with out much Python experience (or others in the lab with ideas) I felt like I needed to move on.

 

Processing:

 

In class Neil had recommended trying processing if you like the arduino IDE (which I do). I started by watching a tutorial video. The presenter was very animated but after I made my first rectangle I realized the videos were going way to slow for me.

 

I found an examples page on their website, very well organized with nice examples.

 

https://processing.org/examples/

 

after poking through a few of them I decided to combine

 

https://processing.org/examples/keyboardfunctions.html

 

&

 

https://www.processing.org/reference/libraries/serial/Serial.html

 

to get the interaction I wanted.

 

I wound up with this.

 

import processing.serial.*;

 

// The serial port:

Serial myPort = new Serial(this, "/dev/tty.usbserial-FTZ8A71D", 9600);   

 

// Open the port you are using at the rate you want:

//myPort = new Serial(this, "/dev/tty.usbserial-FTZ8A71D", 9600);

 

void draw() {

  if (keyPressed) {

    if (key == 'a') {

  myPort.write('a');

    }

    if (key == ' ') {

    myPort.write(' ');

    }

    if (key == 's') {

  myPort.write('s');

    }

    if (key == 'd') {

  myPort.write('d');

    }

    if (key == 'w') {

  myPort.write('w');

    }

}

}

 

And it worked!

 

https://drive.google.com/open?id=0Bwn9JkDLg-21NF9neFc5elpxTWs

 

While on the surface it does not look much different than controlling it through CoolTerm it is, because it will be much easier to have the processing program launch on startup with the port, baud rate, and ŇkeyboardÓ configured then tryin to launch coolterm or the equivelant and set all those things up. The one concern I have is making sure the little java window starts/stays active.

 

I ran this from my RPi over ssh and it worked as well.

 

Barcode Scanner:

            Once I had the processing running on my computer I was excited to finally controll the robot with the barcode scanner. I carefully attached all the aligator clips and plugged it into my mac. Launched the processing script andÉ FAIL!! I opened word to make sure the barcode scanner was writing to the computer and it was. I am not sure why it didnŐt work. Perhaps it handels external keyboard differently. I am going to get my hands on an external keyboard to confirm.

 

Description: Macintosh HD:Users:NedlamAdmin:Downloads:20161130_113427.jpg

 

 

Thoughts on processing:

            I only used it for a very specific probably not its main function task but I really liked it and can see how useful it could be for visualisation. When I have more time I will definetly play around with it.

 

Things I learned/you should look out for/ concerns/questions:

á      Python uses the indentation space to the left of your command as syntax.

á      The one concern I have is making sure the little java window starts/stays active.

á      Does the computer / processing see external keyboards differently?