This repository has been archived by the owner on Nov 29, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jstick.py
65 lines (51 loc) · 1.78 KB
/
jstick.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
60
61
62
63
64
65
#!/usr/bin/python
import binascii
import struct
import threading
import time
import numpy as np
class Button:
def __init__(self, button_id):
self.button_id = button_id
self.value = False
def update(self, command):
if command['type'] == 1 and command['button'] == self.button_id:
self.value = command['value']
class Stick:
def __init__(self, x, y):
self.x_value = x
self.y_value = y
self.coords = np.array([0., 0.])
def update(self, command):
if command['type'] == 2:
if command['button'] == self.x_value:
self.coords[0] = command['value'] / 32767
elif command['button'] == self.y_value:
self.coords[1] = command['value'] / 32767
class Joystick:
jstick_file = open("/dev/input/js0", "rb")
buttons = {}
def __init__(self):
self.thread = threading.Thread(target=self.updateCoords)
self.thread.start()
self.buttons['stick1'] = Stick(0, 1)
self.buttons['stick2'] = Stick(3, 2)
self.buttons['a'] = Button(0)
self.buttons['b'] = Button(1)
self.buttons['x'] = Button(2)
self.buttons['y'] = Button(3)
self.buttons['l'] = Button(10)
self.buttons['r'] = Button(11)
def updateCoords(self):
while True:
buf = self.jstick_file.read(8)
print(binascii.hexlify(buf))
command = {}
command['button'] = buf[7]
command['type'] = buf[6]
command['value'] = struct.unpack('h', buf[4:6])[0]
for key, button in self.buttons.items():
button.update(command)
time.sleep(0)
def __del__(self):
file.close()