Jump to content
Sign in to follow this  
spudw2k

Wiimote LED Simulator

Recommended Posts

spudw2k

I've been playing around with my Wiimote and GlovePIE. I wanted to create some fun LED animations (to be written in GlovePIE) and needed a testbed when I was away from my Wiimote. Useless I know, but hey... Suggestions for improving the dec to bin stuff (_lights func), I'm all ears. nm. I got it. :D

edit: added more silly animations and routines

$Form1 = GUICreate("WiiLEDTestBed", 106, 80, -1, -1)
    $Checkbox0 = GUICtrlCreateCheckbox("", 10, 40, 17, 17)
    $Checkbox1 = GUICtrlCreateCheckbox("", 35, 40, 17, 17)
    $Checkbox2 = GUICtrlCreateCheckbox("", 58, 40, 17, 17)
    $Checkbox3 = GUICtrlCreateCheckbox("", 82, 40, 17, 17)

HotKeySet("{esc}","_Exit")

GUISetState(@SW_SHOW)

_Lights(0)
sleep(1500)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case -3
            Exit
    EndSwitch
    $x = Random(1,5,1)
    Execute("_Routine" & $x & "()")
WEnd

Func _Routine1()
    _AniFadeRight()
    _AniWipeRight()
    _Lights(0)
    _AniWipeLeft()
    _AniFadeLeft()
    _Lights(0)
EndFunc

Func _Routine2()
    _AniCrawlRight()
    _Lights(0)
    _AniCrawlLeft()
    _Lights(0)
    _AniCrawlRight()
    _Lights(0)
    _AniCrawlLeft()
    _Lights(0)
EndFunc

Func _Routine3()
    _AniSneakLeft()
    _Lights(0)
    _AniSneakLeft()
    _Lights(0)
    _AniSneakLeft()
    _AniSneakRight()
    _Lights(0)
    _AniSneakRight()
    _Lights(0)
    _AniSneakRight()
    _Lights(0)
EndFunc

Func _Routine4()
    _AniChaseLeft()
    _AniChaseLeft()
    _AniChaseLeft()
    _AniChaseLeft()
    _AniChaseLeft()
    _AniChaseLeft()
    _Lights(0)
    _AniChaseRight()
    _AniChaseRight()
    _AniChaseRight()
    _AniChaseRight()
    _AniChaseRight()
    _AniChaseRight()
    _Lights(0)
EndFunc

Func _Routine5()
    _AniBlackHoleIn()
    _Lights(0)
    _AniBlackHoleIn()
    _Lights(0)
    _AniBlackHoleIn()
    _Lights(0)
    _AniBlackHoleIn()
    _AniBlackHoleOut()
    _Lights(0)
    _AniBlackHoleOut()
    _Lights(0)
    _AniBlackHoleOut()
    _Lights(0)
    _AniBlackHoleOut()
EndFunc

Func _AniBlackHoleIn()
    _Lights(9)
    _Lights(15)
    _Lights(6)
    _Lights(0)
EndFunc

Func _AniBlackHoleOut()
    _Lights(0)
    _Lights(6)
    _Lights(15)
    _Lights(9)
EndFunc

Func _AniChaseRight()
    _Lights(9)
    _Lights(2)
    _Lights(4)
EndFunc

Func _AniChaseLeft()
    _Lights(9)
    _Lights(4)
    _Lights(2)
EndFunc

Func _AniSneakRight()
    $varA = 1
    _Lights($varA)
    For $x = 1 to 3
        _Lights(0)
        $varA *= 2
        _Lights($varA)
    Next
EndFunc

Func _AniSneakLeft()
    $varA = 8
    _Lights($varA)
    For $x = 1 to 3
        _Lights(0)
        $varA /= 2
        _Lights($varA)
    Next
EndFunc

Func _AniCrawlRight()
    $varA = 1
    $varB = 2
    $varC = 1
    _Lights($varA)
    For $x = 1 to 6
        If $varC = 1 Then
            $varA += $varB
            $varC = 0
        Else
            $varA -= $varB/2
            $varB *= 2
            $varC = 1
        EndIf
        _Lights($varA)
    Next
EndFunc

Func _AniCrawlLeft()
    $varA = 8
    $varB = 8
    $varC = 0
    _Lights($varA)
    For $x = 1 to 6
        If $varC = 0 Then
            $varA += $varB/2
            $varC = 1
        Else
            $varA -= $varB
            $varB /= 2
            $varC = 0
        EndIf
        _Lights($varA)
    Next
EndFunc

Func _AniFadeRight()
    $varA = 0
    For $x = 1 to 4
        $varB = 2 * $varA + 1
        _Lights($varB)
        $varA = $varB
    Next
EndFunc

Func _AniFadeLeft()
    $varA = 15
    For $x = 1 to 4
        _Lights($varA)
        $varA = ($varA-1)/2
    Next
EndFunc

Func _AniWipeRight()
    $varA = 15
    $varB = 1
    For $x = 1 to 4
        _Lights($varA)
        $varA -= $varB
        $varB *= 2
    Next
EndFunc

Func _AniWipeLeft()
    $varA = 8
    $varB = 4
    For $x = 1 to 4
        _Lights($varA)
        $varA += $varB
        $varB /= 2
    Next
EndFunc

Func _Exit()
    Exit
EndFunc

Func _Lights($var)
    $led1 = 4
    $led2 = 4
    $led3 = 4
    $led4 = 4
    If BitAnd($var,1) Then $led1 = 1
    If BitAnd($var,2) Then $led2 = 1
    If BitAnd($var,4) Then $led3 = 1
    If BitAnd($var,8) Then $led4 = 1
    Dim $leds[4]=[$led1,$led2,$led3,$led4]
    _LightCtrl($leds)
    sleep(100)
EndFunc

Func _LightCtrl($arr)
    For $x = 0 to UBound($arr)-1
        GUICtrlSetState(Execute("$Checkbox" & $x), $arr[$x])
    Next
EndFunc
Edited by spudw2k

Share this post


Link to post
Share on other sites
WideBoyDixon

Global $NUM_LEDS = InputBox("WiiLEDTestBed", "How many LEDS?", "4")
If @error <> 0 Then Exit

$Form1 = GUICreate("WiiLEDTestBed", ($NUM_LEDS - 1) * 25 + 31, 80, -1, -1)
Global $aCB[$NUM_LEDS]
For $x = 0 To $NUM_LEDS - 1
    $aCB[$x] = GUICtrlCreateCheckbox("", 10 + ($x * 25), 40, 17, 17)
Next

HotKeySet("{esc}","_Exit")

GUISetState(@SW_SHOW)

_Lights(0)
sleep(1500)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case -3
            Exit
    EndSwitch
    _Routine1()
WEnd

Func _Routine1()
    _AniFadeRight()
    _AniWipeRight()
    _Lights(0)
    _AniWipeLeft()
    _AniFadeLeft()
    _Lights(0)
EndFunc

Func _AniFadeRight()
    For $x = 0 to $NUM_LEDS - 1
        _Lights(BitShift(2, -$x) - 1)
    Next
EndFunc

Func _AniFadeLeft()
    For $x = $NUM_LEDS - 1 to 0 Step -1
        _Lights(BitShift(2, -$x) - 1)
    Next
EndFunc

Func _AniWipeRight()
    $varA = BitShift(1, -$NUM_LEDS) - 1
    $varB = 1
    For $x = 1 to $NUM_LEDS
        _Lights($varA)
        $varA -= $varB
        $varB *= 2
    Next
EndFunc

Func _AniWipeLeft()
    $varA = BitShift(1, 1 - $NUM_LEDS)
    $varB = BitShift(1, 2 - $NUM_LEDS)
    For $x = 1 to $NUM_LEDS
        _Lights($varA)
        $varA += $varB
        $varB /= 2
    Next
EndFunc

Func _Exit()
    Exit
EndFunc

Func _Lights($var)
    $var = Mod($var, BitShift(1, -$NUM_LEDS))
    Dim $leds[$NUM_LEDS]
    For $x = 0 To $NUM_LEDS - 1
        $y = BitShift(1, -$x)
        $leds[$x] = 4 - (BitAnd($var, $y) / $y * 3)
    Next
    _LightCtrl($leds)         
    sleep(100)
EndFunc

Func _LightCtrl($arr)
    For $x = 0 to UBound($arr) - 1
        GUICtrlSetState($aCB[$x], $arr[$x])
    Next
EndFunc

I know WiiMotes don't have more than 4 LEDs but I thought it was an interesting exercise :D

WBD

Share this post


Link to post
Share on other sites
spudw2k

I know WiiMotes don't have more than 4 LEDs but I thought it was an interesting exercise :o

WBD

Very neat, way to make this dynamic! :D

edit:

Actually, now that I think about it; You can link up to 4 Wiimotes at a time, so technically your design maybe useful afterall. :P I was kinda getting a hardon over the idea of animating across four wiimotes. :D Saldy I only have one wiimote to play :( with so I'll prob stick to the 4 LED design for now, but I will surely adapt to your ideas when I get another one. Also, I'm not sure if GlovePIE has a BitShifting method (I'm still learning) so that may be a challenge. Thanks for your input.

edit: modified ver of WideBoyDixon's script

Global $NUM_LEDS
$WIIMOTE_COUNT = InputBox("WiiLEDTestBed", "How many WiiMotes?", "1")
If @error <> 0 or $WIIMOTE_COUNT < 1 or $WIIMOTE_COUNT > 4 Then Exit

$NUM_LEDS = $WIIMOTE_COUNT * 4
$int = 0
Global $aCB[$NUM_LEDS]
Global $aWII[$WIIMOTE_COUNT]
For $x = 1 to $WIIMOTE_COUNT
    $aWII[$x-1] = GUICreate("Wiimote" & $x, 75 + 31, 90, (@DesktopWidth*.475) - (110*$WIIMOTE_COUNT) + (112*$x), -1,0x00800000,-1,WinGetHandle("Program Manager"))
    For $y = 0 To 3
        $aCB[$int] = GUICtrlCreateCheckbox("", 10 + ($y * 25), 40, 17, 17)
        $int+=1
    Next
Next

HotKeySet("{esc}","_Exit")

For $x = 0 to $WIIMOTE_COUNT-1
    GUISwitch($aWII[$x])
    GUISetState(@SW_SHOW)
Next

_Lights(0)
;sleep(1500)

While 1
    ;For $x = 0 To ($NUM_LEDS*$WIIMOTE_COUNT*4^$WIIMOTE_COUNT)-1
        ;_Lights($x)
    ;Next
    ;_Lights(Random(0,($NUM_LEDS*$WIIMOTE_COUNT*4^$WIIMOTE_COUNT)-1,1))
    _Routine1()
WEnd

Func _Routine1()
    _AniFadeRight()
    _AniWipeRight()
    _Lights(0)
    _AniWipeLeft()
    _AniFadeLeft()
    _Lights(0)
EndFunc

Func _AniFadeRight()
    For $x = 0 to $NUM_LEDS - 1
        _Lights(BitShift(2, -$x) - 1)
    Next
EndFunc

Func _AniFadeLeft()
    For $x = $NUM_LEDS - 1 to 0 Step -1
        _Lights(BitShift(2, -$x) - 1)
    Next
EndFunc

Func _AniWipeRight()
    $varA = BitShift(1, -$NUM_LEDS) - 1
    $varB = 1
    For $x = 1 to $NUM_LEDS
        _Lights($varA)
        $varA -= $varB
        $varB *= 2
    Next
EndFunc

Func _AniWipeLeft()
    $varA = BitShift(1, 1 - $NUM_LEDS)
    $varB = BitShift(1, 2 - $NUM_LEDS)
    For $x = 1 to $NUM_LEDS
        _Lights($varA)
        $varA += $varB
        $varB /= 2
    Next
EndFunc

Func _Exit()
    Exit
EndFunc

Func _Lights($var)
    $var = Mod($var, BitShift(1, -$NUM_LEDS))
    Dim $leds[$NUM_LEDS]
    For $x = 0 To $NUM_LEDS - 1
        $y = BitShift(1, -$x)
        $leds[$x] = 4 - (BitAnd($var, $y) / $y * 3)
    Next
    _LightCtrl($leds)         
    sleep(100)
EndFunc

Func _LightCtrl($arr)
    For $x = 0 to UBound($arr) - 1
        GUICtrlSetState($aCB[$x], $arr[$x])
    Next
EndFunc
Edited by spudw2k

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.