Saturday, 31 May 2008

Silly Mii

My talk for barcamplondon4 was "Silly Mii". It's a few ideas I had for silly things to do with the wii-mote. The program includes a simple persistence of vision LED message bar and an 'invisible instrument'. The invisible instrument supports up to four IR sources to play sounds.
Presentation
Program

Wednesday, 21 May 2008

Ruby: Debugging backticks

I needed to knock up a Ruby script recently that orchestrated some external processes using backticks. However something was going wrong and I need to temporarily peek into the inputs and outputs of the external programs. Ruby bend-ability to the rescue:
module Kernel
  alias_method :real_backticks, :'`'

  def `(cmd) 
    puts "`#{cmd}` => #{output = real_backticks(cmd)}"
    output
  end
end

Wednesday, 23 April 2008

Four pixel high font

I needed a font that was four pixels high for a project I'm working on and this is what I came up with. Capital B and E and lower case s and z are really difficult but it's just about readable. Free for all commercial or non-commercial use but I'd appreciate a credit if you find it helpful.

Wednesday, 9 April 2008

HOWTO: Get wii-mote data into Android

In the previous post I demonstrated live wii-mote data going into Google Android. Here is how to replicate this for yourself.

Overview

A C program communicates with the wii-mote over bluetooth and outputs the accelerometer readings to a file. This is then read by a patched version of the OpenIntents sensor simulator and relayed into Android.

The bits

There are four main parts you'll need to get this going:

  1. http://dalethatcher.com/android-wii/wiiacc.tar.bz2 A small home grown C app that uses the CWiid library V0.6.
  2. http://dalethatcher.com/android-wii/wiimote-sensor-simulator.jar A patched version of the OpenIntents sensor simulator program that supports input from wiiacc.
  3. http://openintents.googlecode.com/files/openintents-binary-0.1.5.zip The OpenIntents binaries, which includes the sensors demo program.
  4. http://code.google.com/android/download.html The Android emulator.

Setup

Setting up each of the parts.

wiiacc

  1. Get cwiid-0.6.00.tgz if you need to and install it.
  2. Download wiiacc.tar.bz2 and unpack it.
  3. Run bootstrap.sh and then follow the instructions in the INSTALL file to build and install it.
  4. Run the program with the output filename as an argument. I use 'wiiacc wii' in my home directory.
  5. Follow the instructions from the program and if all is well you should start to see wii accelerometer data in the output file.

Patched Sensor Simulator

  1. Download wiimote-sensor-simulator.jar or build from the source if you prefer.
  2. Run "java -jar wiimote-sensor-simulator.jar"
  3. Scroll down to the bottom of the settings window to the "Real sensor bridge" section.
  4. Enter the path to the output file in the text field (I use '/home/dale/wii') and select "Use Wii-mote accelerometer".
  5. If all is well you should see the image of the mobile phone in the top left mostly match the movement of the wii-mote. I say mostly because the wii-mote works out it's orientation by gravity which means that it doesn't measure yaw. There is a good explanation here.

OpenIntents and Android

There are some instructions on the OpenIntents wiki here. However this is the short version:

  1. Unpack android and run "tools/emulator"
  2. Unpack the open intents binary package and install the OpenGL viewer by running: android_install/tools/adb install openintents-binary-0.1.5/OpenIntents.apk android_install/tools/adb install openintents-binary-0.1.5/samples/OpenGLSensors.apk
  3. When the emulator starts launch the OpenGL Sensors program and go to the settings menu, enter the most plausible IP address found under 'Possible IP addresses'.
  4. You can then test it's working by going to the "Testing" tab and clicking connect, you should see the wii accelerometer data.
  5. For some reason I usually find at this point you'll need to come out of the OpenGL Sensors app and go back in. However once I've done this selecting 'connect' in the menu works.

Wii-mote accelerometer into Android

I successfully hooked up my wii-mote to the OpenIntents sensor simulator and then into Android. More information is available here.

Friday, 4 April 2008

MyHelpa New Front Page

We've revamped the front page on MyHelpa today. Have a look and let me know what you think: http://myhelpa.com/.

Friday, 28 March 2008

Rails: Don't override initialize on ActiveRecord objects

ActiveRecord::Base doesn't always use new to create objects, so initialize might not be called. I wanted to use a Hash on an ActiveRecord::Base subclass to store some calculated values, so I naively did this:
class User < ActiveRecord::Base
  def initialize(args = nil)
    super
    @my_cache = {}
  end
end
However I quickly ran into some "You have a nil object when you didn't expect it!" issues. Some debugger investigation revealed that the @my_cache variable wasn't being set when I called find_or_create_ if the object already existed in the database. Digging in the source revealed that the instantiate method in active_record/base.rb uses allocate to create classes rather than new. This means the initialize method is being neatly sidestepped when creating objects from the database. The solution is to use the 'after_initialize' callback:
class User < ActiveRecord::Base
  def after_initialize
    @my_cache = {}
  end
end
One further note of caution, When passing parameters into a new or create method the after_initialize is called after the parameters have been set. So you can't rely on the initialization being done before overridden accessors are called.