Created
January 9, 2020 10:06
-
-
Save simonjenny/458a7057c2d4dbe861e287b0cb5a725b to your computer and use it in GitHub Desktop.
WiiMote Remote for ExplorerHat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import explorerhat, time, random | |
import RPi.GPIO as GPIO | |
import cwiid | |
import os | |
import sys | |
speed = 50 | |
wii_connected = False | |
def connect_wii(): | |
global wii_connected | |
global wii | |
explorerhat.output.one.on() | |
print "Wii not connected please connect now (Press 1 + 2)" | |
time.sleep(1) | |
try: | |
wii=cwiid.Wiimote() | |
except RuntimeError: | |
print "No Wii found.. try reconnect again.." | |
return False | |
explorerhat.output.one.off() | |
print "Wii found.." | |
wii.rumble = True | |
time.sleep(0.5) | |
wii.rumble = False | |
wii_connected = True | |
time.sleep(3) | |
wii.rpt_mode = cwiid.RPT_BTN | |
def left(): | |
explorerhat.motor.one.forward() | |
explorerhat.motor.two.backward() | |
def right(): | |
explorerhat.motor.two.forward() | |
explorerhat.motor.one.backward() | |
def read_command(): | |
states = "" | |
buttons = wii.state['buttons'] | |
if buttons & cwiid.BTN_A: | |
states = "A" | |
if buttons & cwiid.BTN_B: | |
states = "B" | |
if buttons & cwiid.BTN_LEFT: | |
states = "L" | |
if buttons & cwiid.BTN_RIGHT: | |
states= "R" | |
if buttons & cwiid.BTN_UP: | |
states = "U" | |
if buttons & cwiid.BTN_DOWN: | |
states = "D" | |
if buttons & cwiid.BTN_HOME: | |
states = "HOME" | |
return states | |
def wiimode(): | |
if(wii_connected is False): | |
connect_wii() | |
if(wii_connected is True): | |
if (read_command() == "L"): | |
right() | |
if (read_command() == "R"): | |
left() | |
if (read_command() == "U"): | |
explorerhat.motor.forward(speed) | |
if (read_command() == "D"): | |
explorerhat.motor.backwards(speed) | |
if (read_command() == "B"): | |
explorerhat.output.one.toggle() | |
if (read_command() == "HOME"): | |
explorerhat.output.one.on() | |
wii.rumble(0.50) | |
os.system("shutdown -h 0"); | |
sys.exit() | |
time.sleep(0.25) | |
explorerhat.motor.stop() | |
try: | |
while True: | |
wiimode() | |
## Catches control-c. | |
except KeyboardInterrupt: | |
pass | |
## Catches any other exceptions. | |
except Exception: | |
pass | |
## Clean up GPIO before exit. | |
finally: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment