+program=wii_g
+objects=main.o
+CFLAGS=-I/usr/local/include/libcwiimote -D_ENABLE_TILT -D_ENABLE_FORCE
+LDFLAGS=-L/usr/local/lib -lcwiimote -lbluetooth -lm
+
+all: $(program)
+
+clean:
+ rm -f $(objects) $(program)
+
+$(program): $(objects)
+ $(CC) -o $@ $< $(LDFLAGS)
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c $< -o $@
+wii_g - A simple program for reading the accelerometers on a Wii Remote
+
+wii_g simply connects to a Wii Remote via Bluetooth and prints out a
+continuous log of accelerometer data. Additionally, wii_g uses the Wii
+Remote's LEDs to display the magnitude of acceleration on the Wii
+Remote. It was designed to work as a data logger for automotive
+purposes, but honestly, I haven't yet used it in this capacity. :-/
+
+The data logged to stdout has four fields: time in fractional seconds
+since the UNIX epoch, X acceleration, Y acceleration, and Z
+acceleration. This should be enough to plot a graph of acceleration
+over time in a program like gnuplot (though I guess you could use
+Excel if you're into that kind of thing).
+
+The Wii Remote's LEDs are used to display the magnitude of acceleration
+in two different modes. Mode 1 (default, and also selectable by
+pressing the 1 button) provides indication of acceleration in the X
+direction. That is, the side-to-side motion. Acceleration in the +X
+direction (to the right) lights up the two rightmost LEDs at 0.5g and
+1.0g, and likewise -X acceleration (to the left) lights up the leftmost
+LEDs. Mode 2 (selectable by pressing the 2 button) provides an absolute
+meter in the same fashion, lighting up the four LEDs in sequence at
+0.5g, 1.0g, 1.5g, and 2.0g. In this mode, it is normal for two LEDs to
+be lit up all the time. That's the acceleration due to gravity
+(typically 1g, but you may need to adjust the program if you live
+somewhere other than Earth).
+
+Wii_g requires libwiimote, which you can get at
+http://libwiimote.sourceforge.net/
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <sys/time.h>
+
+#include "wiimote.h"
+#include "wiimote_api.h"
+
+int wiimode = 0;
+
+int main(int argc, char *argv[]) {
+ wiimote_t wiimote = WIIMOTE_INIT;
+
+ if (argc < 2) {
+ fprintf(stderr, "Usage: wiig BDADDR\n");
+ exit(1);
+ }
+
+ fprintf(stderr, "Please press 1 and 2 on the wiimote to connect...\n");
+
+ if (wiimote_connect(&wiimote, argv[1]) < 0) {
+ fprintf(stderr, "Could not connect to wiimote: %s\n", wiimote_get_error());
+ exit(1);
+ }
+
+ /* Turn on the accelerometers */
+ wiimote.mode.acc = 1;
+
+ float accmag;
+ struct timeval t;
+ while(wiimote_is_open(&wiimote)) {
+ if (wiimote_update(&wiimote) < 0) {
+ wiimote_disconnect(&wiimote);
+ break;
+ }
+
+ gettimeofday(&t,NULL);
+
+ accmag = sqrt(wiimote.force.x*wiimote.force.x+wiimote.force.y*wiimote.force.y+wiimote.force.z*wiimote.force.z);
+
+ /* Switch LED monitoring modes */
+ if (wiimote.keys.one)
+ wiimode = 0;
+ if (wiimote.keys.two)
+ wiimode = 1;
+
+ wiimote.led.bits = 0;
+ switch(wiimode) {
+ case 0: /* L-R meter */
+ if (wiimote.force.x < -1.0)
+ wiimote.led.one = 1;
+ if (wiimote.force.x < -0.5)
+ wiimote.led.two = 1;
+ if (wiimote.force.x > 0.5)
+ wiimote.led.three = 1;
+ if (wiimote.force.x > 1.0)
+ wiimote.led.four = 1;
+ break;
+ case 1: /* absolute force meter */
+ if (accmag > 0.5)
+ wiimote.led.one = 1;
+ if (accmag > 1.0)
+ wiimote.led.two = 1;
+ if (accmag > 1.5)
+ wiimote.led.three = 1;
+ if (accmag > 2.0)
+ wiimote.led.four = 1;
+ break;
+ }
+
+ printf("%d.%d\t%f\t%f\t%f\n", t.tv_sec, t.tv_usec,
+ wiimote.force.x, wiimote.force.y,
+ wiimote.force.z);
+ }
+}