Friday, August 6, 2010

Wiimotelib for Small Basic, v0.1

A week or two ago I started teaching my little brother to code using Small Basic. It works surprisingly well, especially as a beginner language. That said, it's fairly limited. Fortunately, it's possible to write extensions to the language using .Net. So last night, I hacked together a wrapper of sorts for Brian Peek's WiimoteLib.
Included in the package is the binary (place it in the lib folder in the Small Basic installation folder,) an example (below,) and the source (in C#.)
Currently, button values can only be read in real time, so you have to put the program in a constant loop. This is pretty common, but those who are just starting Small Basic may find it weird.

Sample code:

c = 0
While(c = 0)
  c = WiiRemote.Connect()
  TextWindow.WriteLine("Connecting!")
EndWhile
TextWindow.WriteLine("Connected!")

WiiRemote.SetLights("true", "false", "false", "true")

buttonA = 0
lastButtonA = 0

buttonB = 0
lastButtonB = 0

While(1 = 1)
  buttonA = WiiRemote.A
  buttonB = WiiRemote.B

  If ((buttonA = 1) And (lastButtonA = 0)) Then
    TextWindow.WriteLine("Button A pressed!")
  endif

  If ((buttonB = 1) And (lastButtonB = 0)) Then
    TextWindow.WriteLine("Button B pressed!")
  endif

  If WiiRemote.Home = 1 then
    Program.End()
  EndIf

  lastButtonA = buttonA
  lastButtonB = buttonB
EndWhile
Post a Comment