James Williams
LinkedInMastodonGithub

Motej with Groovy

Tags: Groovy

In my presentation at GeeCon 2010, I showed how you could use Groovy to interact with the Nintendo Wiimote, here is the code and a little explanation from one of my demos.

  • Install OS specific native Bluetooth libraries
  • Grab the jars for the Java JSR 82 implementation. Either BlueCove or Aventa. Note that Aventa only has a 14day trial and BlueCove requires linking to an extra gpl jar if you use it on Linux. Mac users on Snow Leopard will have to compile Bluecove from source.
  • Grab slf4j-api-1.5.8.jar and slf4j-simple-1.5.8.jar.

Open a Groovy console and add all the jars (bluecove-2.1.0.jar, bluecove-gpl-2.1.0.jar [if on Linux] and the slf4j jars) to the classpath using Script -> Add Jar to classpath.

Copy the following code into the console.

import motej.*
import motej.event.*

def listener = [moteFound:{Mote mote ->
      println("Found mote: " + mote.getBluetoothAddress())
      mote.setPlayerLeds([false, true, false, true] as boolean[])
      mote.rumble(2000l)
      Thread.sleep(10000l)
      mote.disconnect()
   }
] as MoteFinderListener

MoteFinder finder = MoteFinder.getMoteFinder()
finder.addMoteFinderListener(listener)

finder.startDiscovery()
Thread.sleep(30000l)
finder.stopDiscovery()

The above code starts discovery for controllers, registers a listener that rumbles the wiimote and changes the LEDs, and disconnects and stops discovery after a delay. You can activate your Wiimote for discovery by pressing the 1 & 2 buttons at the same time.

You can also download the source and libs in a ready-made project here.