Then build and upload the Arduino sketch. I use inotool for that
ino build && ino upload
from serial import Serial
from gamepad import Gamepad
serial = Serial('/dev/ttyUSB0')
gamepad = Gamepad(serial)
Create the callback function
def move(self, event):
print 'move', event.state.get_axes()
Attach the callback to an event
gamepad.on('move', move)
gamepad.listen()
move
move-x
move-y
move-right
move-left
move-up
move-down
move-center
button
button-press
button-release
switch
switch-press
switch-release
The move-right
, move-left
, move-up
and move-down
events are holding when were fired and unholding when the move-center
event is fired.
This is useful if you want to run some code by a timer. For example, imagine you want to implement pacman like game. If you move left the pacman will continue move left until it hit the next wall.
def move(event):
while gamepad.is_holding(event):
print 'move',
if event.is_move_left():
print 'left'
elif event.is_move_right():
print 'right'
elif event.is_move_up():
print 'up'
elif event.is_move_down():
print 'down'
time.sleep(1)
def move_center(event):
print "center", event.state.get_axes()
gamepad.on('move', move)
gamepad.on('move-center', move_center)
gamepad.listen()
This is an example using another project: arduino-matrix-rgb