# Monthly Archives: June 2015

## Gauss gun – part 1

At the end of the last semester of Monaghan Coder Dojo, I promised the students we’d do something cool for the next series of classes. We’re going to build a Gauss gun.

A Gauss gun is a rail, which a metal projectile travels along. It has a series of electro-magnets on it. As the projectile approaches each magnet, the magnet turns on, accelerating the projectile in towards the center of the magnet.

As the projectile reaches the center, the magnet turns off, so the projectile travels through it, and on towards the next magnet.

The same trick is done a few times, accelerating the projectile more and more each time, until it finally reaches the end of the track.

The first thing I had to do was design a circuit which you can turn on electronically that will stay on, and which you can then turn off electronically. I mean, the circuit should not involve a switch that requires physical effort to turn on and off, as that may slow down the projectile.

So, the solution I came up with was:
1. a circuit which uses a transistor to turn on. This way it can be enabled by shoving a little bit of power through the transistor’s base.
2. the circuit, once completed, will feed a little bit of its output electricity back into the transistor’s base by using a capacitor to give a smooth and continuous power line.
3. to turn it off, we will short-circuit the capacitor.

I did a quick “proof of concept” with an LED.

In the next article, I’ll show how to adapt this so that the “switches” are photoelectric cells, so you can turn the circuit on by disrupting one light beam, and turn it off by disrupting the next.

## Short story:

Github repository for kbarcode – the JavaScript part of the solution. You can use it on its own, without needing Cordova at all.

Demo of kbarcode finding a barcode and then the barcode parameters being printed out onto the image that the barcode was found in.

## Long story:

There are already a few barcode readers for Cordova. The most popular one is the official Phonegap barcode plugin, which is based on the amazingly comprehensive ZXing library of algorithms.

At FieldMotion, we were using the official plugin, but it had a few short-comings that meant we had to look for a better solution:

• When looking for a barcode, the plugin opens up an external camera application. This means that your own application stops, the external app is started, and when you find your barcode, yur app is started up again. This process is very jarring, and noticeably slow.
• You have absolutely no say over the look of the barcode scanner.
• If you want multiple barcodes, you are out of luck – you’re just going to have to go through the selection process manually for every one of them.

What we wanted was:

• A small camera view to appear when we press to select a barcode.
• To be able to style this ourselves in whatever way we want.
• To optionally keep the scanner open after it has found a barcode, so that it can keep scanning for others if need be.
• It must feel natural and fast.

So, we went looking.

The nearest thing to a solution that we found was a combination of two plugins – Moonware’s CameraPlus plugin, which allows the camera to be opened in the background and its photos returned to a JavaScript callback for you to handle however you wish, and Eddie Larsson’s JOB (JavaScript-only Barcode Reader).

In combination, these appear to be perfect – we could get images via CameraPlus, display them in a popup UI that could be used by the user to center the barcode, and then use JOB to scan the image and retrieve the code.

Unfortunately, this method is SLOW.

I identified two main reasons for this:

1. Streaming images to JavaScript via a Java bridge is very slow, because the images need to be encrypted in Base64 (increasing their size), and the images also need to be in high resolution in order to give the barcode reader the best chance it can get.
2. The method that Eddie’s algorithm uses is to find the barcode in the image, no matter where it is, which involves reading the entire image. In JavaScript. Brilliant, but slow.

After some wracking of the brain, I came up with this solution:

• Tweak the CameraPlus plugin so it returns just a small image to be displayed, and also a 1px high gray-scale strip from the center of a higher-resolution image (in byte array format).
• Write a barcode decryption algorithm that will find a barcode in a 1D array of gray-scale values, instead of a 2D image.

This worked wonderfully. We now have a very usable barcode reader that is not very laggy, and finds the barcodes incredibly quickly. We’re also only interested in the EAN-13 encoding, so we don’t need to check for other encodings.

The reason we chose to use a 1D strip instead of the entire image, is that if you have a UI which has a marker displayed where you want the user to put the barcode, they are psychologically inclined to do so, so you really only need to consider that single central strip, and can safely ignore the rest of the image.

It’s a Worker, so it runs in a separate thread to the rest of your code. No need to include it in your HTML file – just correct the reference to the file in the code example below.

Example usage:

```var kbarcode=new Worker('kbarcode.js');