-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
65 lines (47 loc) · 1.3 KB
/
control.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/env python
import usb.core
import usb.util
import time
import RPi.GPIO as GPIO
USB_VENDOR = 0x0c40 # Rii
USB_PRODUCT = 0x7a11 # Mini Wireless Keyboard
USB_IF = 0 # Interface
USB_TIMEOUT = 5 # Timeout in MS
BTN_LEFT = 80
BTN_RIGHT = 79
BTN_DOWN = 81
BTN_UP = 82
BTN_STOP = 44 # Space
BTN_EXIT = 41 # ESC
LEFT = 24
RIGHT = 25
GPIO.setmode(GPIO.BCM)
GPIO.setup(LEFT, GPIO.OUT)
GPIO.setup(RIGHT, GPIO.OUT)
dev = usb.core.find(idVendor=USB_VENDOR, idProduct=USB_PRODUCT)
endpoint = dev[0][(0,0)][0]
if dev.is_kernel_driver_active(USB_IF) is True:
dev.detach_kernel_driver(USB_IF)
usb.util.claim_interface(dev, USB_IF)
while True:
control = None
try:
control = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT)
except:
pass
if control != None:
if BTN_DOWN in control:
pass
elif BTN_UP in control:
GPIO.output(LEFT, True)
GPIO.output(RIGHT, True)
elif BTN_LEFT in control:
GPIO.output(LEFT, True)
GPIO.output(RIGHT, False)
elif BTN_RIGHT in control:
GPIO.output(RIGHT, True)
GPIO.output(LEFT, False)
else:
GPIO.output(LEFT, False)
GPIO.output(RIGHT, False)
time.sleep(0.02)