#!/usr/bin/python | |
# | |
# === pi-Racey === | |
# | |
# wii_connect.py | |
# Connect a Nintendo Wii Remote via Bluetooth | |
# to drive a remote control car through GPIO | |
# | |
# Project URL : | |
# http://ed-george.github.io/piRacey/ | |
# | |
# Author(s) : Ed George | |
# Original : Matt Hawkins (Raspberry Pi Spy) | |
# Date : 19/04/2014 | |
# ----------------------- | |
# Import required Python libraries | |
# ----------------------- | |
import RPi.GPIO as GPIO | |
import cwiid | |
import time | |
button_delay = 0.1 | |
#Define blink | |
def blink(pin): | |
GPIO.output(pin,GPIO.HIGH) | |
time.sleep(1) | |
GPIO.output(pin,GPIO.LOW) | |
time.sleep(1) | |
return | |
#Define rumble | |
def rumble(duration): | |
wii.rumble = 1 | |
time.sleep(duration) | |
wii.rumble = 0 | |
return | |
print 'Press 1 + 2 on your Wii Remote now ...' | |
time.sleep(1) | |
# Attempt connection to the Wii Remote | |
try: | |
wii = cwiid.Wiimote() | |
except RuntimeError: | |
print "Error creating Wiimote connection" | |
quit() | |
print 'Wii Remote connected successfully\n' | |
rumble(1) | |
print 'Press PLUS and MINUS together to disconnect and quit.\n' | |
wii.rpt_mode = cwiid.RPT_BTN | |
GPIO.setmode(GPIO.BOARD) | |
# Setup GPIO Pins as Output | |
GPIO.setup(12, GPIO.OUT) | |
GPIO.setup(18, GPIO.OUT) | |
while True: | |
buttons = wii.state['buttons'] | |
# If Plus and Minus buttons pressed | |
# together then rumble and quit. | |
if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0): | |
print '\nClosing connection ...' | |
GPIO.cleanup() | |
rumble(2) | |
exit(wii) | |
# Check if other buttons are pressed by | |
# doing a bitwise AND of the buttons number | |
# and the predefined constant for that button. | |
if (buttons & cwiid.BTN_LEFT): | |
print 'Left pressed' | |
time.sleep(button_delay) | |
if(buttons & cwiid.BTN_RIGHT): | |
print 'Right pressed' | |
time.sleep(button_delay) | |
if (buttons & cwiid.BTN_UP): | |
print 'Up pressed' | |
time.sleep(button_delay) | |
if (buttons & cwiid.BTN_DOWN): | |
print 'Down pressed' | |
time.sleep(button_delay) | |
if (buttons & cwiid.BTN_1): | |
print 'Button 1 pressed' | |
time.sleep(button_delay) | |
if (buttons & cwiid.BTN_2): | |
print 'Button 2 pressed' | |
time.sleep(button_delay) | |
if (buttons & cwiid.BTN_A): | |
print 'Button A pressed' | |
time.sleep(button_delay) | |
blink(12) | |
if (buttons & cwiid.BTN_B): | |
print 'Button B pressed' | |
time.sleep(button_delay) | |
blink(18) | |
if (buttons & cwiid.BTN_HOME): | |
print 'Home Button pressed' | |
time.sleep(button_delay) | |
if (buttons & cwiid.BTN_MINUS): | |
print 'Minus Button pressed' | |
time.sleep(button_delay) | |
if (buttons & cwiid.BTN_PLUS): | |
print 'Plus Button pressed' | |
time.sleep(button_delay) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment