## Sunday, April 26, 2009

### Elbow power: Things going *bump* in the night

Whenever some form of hardware doesn't work, many people bash it or elbow it, hoping and praying that this desperate measure might solve the problem. So, I figured, what if you could harness this form of kinetic input to control your computer

What if you could set the computer up so that when you slam it with your elbow, and like yours was the elbow of the Fonze, it kills Firefox (porn surfers rejoice), or renews your network connection, switches keyboard layout, switches desktop background, the possibilities are limited only by your imagination.

Disclaimer: Your computer will stand up to some degree of slapping and bashing about, it does have a breaking point. Use this at your own peril. I do not take responsibility for whatever damage you inflict upon your computer with this program.

Instead of buying expensive gyroscopes, accelerometers or hacking your wiimote, there's a very cheap way of making this a reality. The poor man's accelerometer is nothing but a computer mouse. Place it on top of your computer (make a casing for it if you want it to look neat). Now, the problem is how to read the data from the mouse. In Linux, this is a trivial matter. All hardware in Linux is a file, and you can simply open the mouse like any other old file.

Poor man's accelerometer.

What protocol the mouse device uses is a different pony. There's several mouse protocols, and they are all slightly different. The basic PS/2 protocol has 3 byte packets, where the first byte is buttons and some flags, and the second is relative motion in the X direction, and the third is relative motion in the Y direction. Older 2 button mice with no scroll wheel is generally PS/2 mice. Newer mice allow for longer packets, but they are generally backwards compatible with PS/2. There is more information on the protocol on Wikipedia if anyone is interested.

Great. So let's write a program that we can use to track jolts. The algorithm is going to be like this:

1. Open the device.
2. Read data from the file descriptor.
3. If the magnitude of the relative motion exceeds some threshold value, return from the program.

Add some red carpet code to allow customization, and you'll end up with this:

kinput.c
Makefile

Compile with make, and you're (hopefully) go.

Now that this helper program is done, what you need to do is write a shell script that performs some action when it detects a bump.

#!/bin/bash# DISCLAIMER! Your computer will stand up to some degree of # slapping and bashing about, it does have a breaking # point. Use this at your own peril. I do not take# responsibility for whatever damage you inflict upon# your computer with this program.KINPUT=/path/to/kinput # Change this to where you put kinputMDEVICE=/dev/input/mouse1 # Change this to some appropriate deviceTHRESHOLD=10 # Increase this if it goes off too easilyTIMEOUT=1 # Change this to how long you want to sleep # before waiting for another motionWHAT="killall firefox-bin" # Change this to what you want performed on a bumpwhile [ 1 ]; do if "$KINPUT" -d "$MDEVICE" -t "$THRESHOLD"; then echo "Bump detected!" eval "$WHAT" else exit; fi # Exit if something has gone wrong sleep "\$TIMEOUT"; # Sleep a momentdone

... and that's it. Running this in the background will allow you to solve your problems with your elbow. Correctly configured, you should only need to nudge your computer slightly. Bashing your computer too hard may harm your hard disks and cause problems in the wiring, so you shouldn't do that.

You could conceivably also use this program to detect mouse motion for other purposes. Running it in the background in the login manager and tracking the regular mouse could allow you to set up your computer to grab snapshots with your webcam of everyone who tries to log into your computer (which may or may not be legal where you live/work) and then email them to you.

There are some problems that might arise with this though, the most likely is that you don't have permission to read from the mouse device. Instead of running the script as root (are you insane?), it's a lot better to allow the mouse device to be read by all users, or add trusted users to the same user group as the mouse.