forked from MikeMakes/Phoebe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (52 loc) · 1.99 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#This script has to be executed in the laptop
import tcp
from ps3map import *
from evdev import InputDevice,list_devices, ecodes, KeyEvent, categorize
#Next, we define some functions to be able to use the PS3 controller
def search_devices(name='Sony PLAYSTATION(R)3 Controller'): #This will search 'name' through all the devices
print("Searching:")
print(repr(name))
devices = [InputDevice(fn) for fn in list_devices()]
for device in devices:
print(device.fn, device.name, device.phys)
if device.name == name:
return device #Return the device with name 'name'
def key_press(key): #Return True when key is pressed
if event.type == ecodes.EV_KEY: #Check if it is a key related event
keyevent = categorize(event)
if event.code == key and keyevent.keystate == KeyEvent.key_down: #If 'key' pressed:
return True
else:
return False
def key_release(key): #Return True when key is released
if event.type == ecodes.EV_KEY:
keyevent = categorize(event)
if event.code == key and keyevent.keystate == KeyEvent.key_up: #If 'key' released:
return True
else:
return False
def axe_move(axe): #Return axe's value when its move
if event.type == ecodes.EV_ABS: #Check if it is an absolute axis related event
if event.code == axe: #If 'axe' moved:
return event.value
#This is the main part of the program. It connects to a TCP network created by Raspberry.
#From the PS3 controller, it detects the axes R2 and L2, then send the values to Raspberry and
#and finally it shows in screen which axe has been pressed and the value sent to Raspberry
try:
client = tcp.mysocket()
client.connect("192.168.1.192",4200)
gamepad1 = search_devices('Sony PLAYSTATION(R)3 Controller')
for event in gamepad1.read_loop():
axe = axe_move(r2_axe)
if axe is not None:
client.send("RIGHT")
client.send(str(axe))
print "RIGHT: ", axe
axe = axe_move(l2_axe)
if axe is not None:
client.send("LEFT")
client.send(str(axe))
print "LEFT: ", axe
finally:
client.send("EXIT")
client.closesocket()