Saturday 21 November 2015

Building a retro joystick

I've been thinking about building a reto arcade machine for a while now but I keep getting stuck on the costs. Even doing it on the cheap will end up costing about 500 pounds which is a lot for a novelty. Last week I decided the best way to eat this elephant is one bite at a time and so bought a joystick, some buttons, a usb joystick interface and a cheap wooden box. Pair that with an excited 5 year old and you have the 2 hour project shown below.


Martin has been excidedly decorating the box with markers and stickers for the past few days
We use a 28mm hole drill to drill holes for the buttons and joystick. Obviously I did this although Martin did pretty much the rest of the project.
Wiring up the joystick and switches are simple as it's just a case of pushing on the spade connectors.
Now to test it out with a bit of R-Type

Here's a video of the button being assembled: assembly video

Here's a quick play-test video:


The joystick kit came from Arcade World UK, and cost 28 pounds.
The box cost about 4 pounds from Amazon.



Friday 8 May 2015

Installing oracle jdk 8 on Centos 7.1

I want to install the java 8 jdk and to use that with groovy. That should be simple enough - but downloading the rpm and updating /etc/alternatives does not work. The problem is that the rpm installs java to a non-standard location and so groovy gives this error:

$ groovy -v
/bin/build-classpath: Could not find ../../jvm/java/lib Java extension for this JVM
/bin/build-classpath: error: Some specified jars were not found
Groovy Version: 1.8.9 JVM: 1.8.0_45 Vendor: Oracle Corporation OS: Linux

The solution is to install the tar.gz file version. First download it from Oracle.
Then, untar it and move the resulting directory to /usr/lib/jvm
tar xzf jdk-8u45-linux-x64.tar.gz
sudo mv jdk1.8.0_45 /usr/lib/jvm

Next, we set up the alternatives:

sudo alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.8.0_45/jre/bin/java 20000
sudo alternatives --install /usr/bin/jar jar /usr/lib/jvm/jdk1.8.0_45/bin/jar 20000
sudo alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk1.8.0_45/bin/javac 20000
sudo alternatives --install /usr/bin/javaws javaws /usr/lib/jvm/jdk1.8.0_45/jre/bin/javaws 20000

Now, we can set the java version we want using update-alternatives:

sudo update-alternatives --config java
sudo update-alternatives --config jar
sudo update-alternatives --config javac
sudo update-alternatives --config javaws

Now groovy also expects to find the active java installation at /usr/jvm/java, so we need to link that to our active java. I don't really like this option as it breaks the alternatives structure we set-up earlier, but at the moment I don't have a better solution.

sudo mkdir /usr/jvm
sudo ln -s /usr/lib/jvm/jdk1.8.0_45 /usr/jvm/java

And now everything is groovy:

groovy -v Groovy Version: 1.8.9 JVM: 1.8.0_45 Vendor: Oracle Corporation OS: Linux

Wednesday 23 January 2013

Raspberry pi development part 2

Compiling a simple hello world program is all very well but as soon as we want to compile something interesting (for example something using GLES or x-windows) then we run into the problem that we don't have the required header files or libraries. We can't use the libraries from the host system of course because they are for an x86 PC and not an ARM based raspberry pi.
One solution is to copy the header and library files over to the host system. The directories we need are /usr/include, /usr/lib, /opt/vc/include and /opt/vc/lib

rsync to the rescue

As both system as linux, we can use rsync. rsync is a utility that is used to keep two directories in sync ... exactly what we want. To begin with we need to install rsync on the pi as it is not installed in the base image.
ssh to the pi and execute the following command to do that:

sudo apt-get install rsync

One done, execute the following commands on the host linux system (changing 192.168.0.114 to the address of your raspberry pi).

cd ~/rpi

mkdir root/opt
mkdir root/usr

rsync -e ssh -lr pi@192.168.0.114:/usr/includeroot/usr
rsync -e ssh -lr pi@192.168.0.114:/usr/lib root/usr

mkdir root/opt
mkdir root/opt/vc

rsync -e ssh -lr pi@192.168.0.114:/opt/vc/include root/opt/vc
rsync -e ssh -lr pi@192.168.0.114:/opt/vc/lib root/opt/vc


This will synchronise the /usr/include and /usr/lib directories from the raspberry pi over to ~/rpi/root on the host system. Whenever you update the raspberry pi or add new packages, run the rsync commands again to make sure that the host is up to date.


Saturday 19 January 2013

plugging in the pi

One of my Christmas gifts was a raspberry pi - it's a little single chip computer which costs about 30 pounds. It's aimed at encouraging school kids to start programming and so comes with a complete python programming environment. It also has an external connector which can be used to control external devices such as stepper motors.
I'm interested in compiling some C++ apps for it and I'll keep my notes on the process here. It's mainly for my benefit as I will only get a few hours here and there to play with it and without a log I will likely forget what I did the previous time.

Setup

Nothing special. Download Raspian wheezy http://www.raspberrypi.org/downloads and copy it onto a SD card. Pop it into the pi, connect a keyboard, display and ethernet connection and power it up. In the config menu, make sure ssh is enabled as we'll be mainly accessing via a shell from another computer.
The pi gets an IP address automatically using DHCP - generally that won't change too often, but I would prefer it not to change at all. To ensure that is the case, just use the DHCP settings in the router admin page and add a reservation for the pi. That means a given address will be reserved for a given MAC address.

cross-compiler setup

The pi is a fully fledged linux environment and is capable of compiling c++ code ... but it's not a very fast device and the access to the SD card is quite slow so compiling anything of note is likely to be painful. The answer is to compile the code on a more powerful PC and then transfer the resulting binaries to the pi. This is called cross compiling because the compiled binaries will not run on the system that compiled them (in this case, the binaries will be arm6 machine code whilst the PC runs Intel x86 code.)
My main machine runs windows 8 ... but setting up a cross compiling environment under windows is a pain, so instead I chose to set-up a lubuntu linux system running under vmware player. I gave the VM 2Gb RAM and a 20Gb disk which seems plenty. The host is an intel i7 with 24Gb RAM, so it can easily run this vm. I chose lubuntu because it needs less resources than fully fledged ubuntu and so the same VM should run happily on my laptop too.

The good thing about using linux on a PC as a host cross-compile system is that somebody else has done the hard work of making a compiler that runs on an x86 PC platform but generates code to run on an ARM6 platform. This is stored in a git repository, so we need to install git and then clone the repository.

First, from the command line (LXTerminal under Applications) , install git by:

sudo apt-get install git
once this is complete, we're going to make a directory called rpi and get the tools into that directory. Use the following commands

mkdir rpi 
cd rpi
git clone git://github.com/raspberrypi/tools.git
A quick sanity check that this has worked is to run the following:

./tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc --version

which should produce output that looks like this:


arm-linux-gnueabihf-gcc (crosstool-NG linaro-1.13.1+bzr2458 - Linaro GCC 2012.08) 4.7.2 20120731 (prerelease)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

That's just running the cross compiler and asking it to display its version. If you get the above result then we're ready to go.

Simple hello world test

We should test the compiler works using the simplest program that we can and it would be rude not to run a time honoured hello world test. I'm going to make a directory called src in the rpi directory which will contain all my projects, so first I will make that directory plus one for hello world.

mkdir src
cd src
mkdir helloworld
cd helloworld

now, I'm going to open a text editor (I use vi but everyone has their favourite) and create the file helloworld.cpp with the following content:

#include <stdio.h>

int main(int argc, char* argv[])
{
    printf("hello world\n");
    return 0;
}

Now, use the following command to compile the file using the cross compiler:
../../tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ helloworld.cpp

This will result in a file named a.out being created.

Transfer via ssh/scp

We need to copy the file over to the rpi. We could do this by copying it onto the SD card but copying via ssh is much easier. First ssh to the pi in a new console window and accept the identity. My pi is at address 192.168.0.114, yours will be different. Use the following command (with the ip address of your pi of course):
ssh pi@192.168.0.114
by default the password is raspberry.

Now, back in you original console window, use the following command to transfer the a.out file to the pi (again, change the ip address):
scp a.out pi@192.168.0.114:a.out

Now, on the pi, execute the program with
./a.out

and if all has gone well, you should see 'hello world' printed on the screen. For reference, the output from my terminal is as follows.

pi@raspberrypi ~ $ ./a.out

hello world
pi@raspberrypi ~ $