-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSpaceNavigatorLEDs.py
More file actions
152 lines (124 loc) · 3.8 KB
/
SpaceNavigatorLEDs.py
File metadata and controls
152 lines (124 loc) · 3.8 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# See http://stackoverflow.com/questions/29345325/raspberry-pyusb-gets-resource-busy#29347455
# Run python2 as root (sudo /usr/bin/python2.7 /home/pi/pythondev/SpaceNavigatorLEDs.py)
import usb.core
import usb.util
import sys
from time import gmtime, strftime
import time
import RPi.GPIO as GPIO
def setAxisPins( negPin, posPin, val ):
"Set the pins for an axis"
# light up either -tive or +tive LED
if val < 0:
posVal = GPIO.LOW
negVal = val/2*2 == val # flashing may be more interesting
negVal = GPIO.HIGH # or more annoying
elif val > 0:
negVal = GPIO.LOW
posVal = val/2*2 == val
posVal = GPIO.HIGH
else:
posVal = GPIO.LOW
negVal = GPIO.LOW
GPIO.output(posPin, posVal)
GPIO.output(negPin, negVal)
return
# Look for SpaceNavigator
dev = usb.core.find(idVendor=0x46d, idProduct=0xc626)
if dev is None:
raise ValueError('SpaceNavigator not found');
else:
print ('SpaceNavigator found')
print dev
# Setup GPIO pins
TX_NEG_PIN = 11
TX_POS_PIN = 8
TY_NEG_PIN = 5
TY_POS_PIN = 7
TZ_NEG_PIN = 6
TZ_POS_PIN = 12
RX_NEG_PIN = 13
RX_POS_PIN = 16
RY_NEG_PIN = 19
RY_POS_PIN = 20
RZ_NEG_PIN = 26
RZ_POS_PIN = 21
GPIO.setmode(GPIO.BCM) # logical numbering
GPIO.setup(TX_NEG_PIN,GPIO.OUT)
GPIO.setup(TX_POS_PIN,GPIO.OUT)
GPIO.setup(TY_NEG_PIN,GPIO.OUT)
GPIO.setup(TY_POS_PIN,GPIO.OUT)
GPIO.setup(TZ_NEG_PIN,GPIO.OUT)
GPIO.setup(TZ_POS_PIN,GPIO.OUT)
GPIO.setup(RX_NEG_PIN,GPIO.OUT)
GPIO.setup(RX_POS_PIN,GPIO.OUT)
GPIO.setup(RY_NEG_PIN,GPIO.OUT)
GPIO.setup(RY_POS_PIN,GPIO.OUT)
GPIO.setup(RZ_NEG_PIN,GPIO.OUT)
GPIO.setup(RZ_POS_PIN,GPIO.OUT)
# Don't need all this but may want it for a full implementation
cfg = dev.get_active_configuration()
print 'cfg is ', cfg
intf = cfg[(0,0)]
print 'intf is ', intf
ep = usb.util.find_descriptor(intf, custom_match = lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
print 'ep is ', ep
reattach = False
if dev.is_kernel_driver_active(0):
reattach = True
dev.detach_kernel_driver(0)
ep_in = dev[0][(0,0)][0]
ep_out = dev[0][(0,0)][1]
print ''
print 'Exit by pressing any button on the SpaceNavigator'
print ''
run = True
while run:
try:
data = dev.read(ep_in.bEndpointAddress, ep_in.bLength, 0)
# raw data
# print data
# print it correctly T: x,y,z R: x,y,z
if data[0] == 1:
# translation packet
tx = data[1] + (data[2]*256)
ty = data[3] + (data[4]*256)
tz = data[5] + (data[6]*256)
if data[2] > 127:
tx -= 65536
if data[4] > 127:
ty -= 65536
if data[6] > 127:
tz -= 65536
print "T: ", tx, ty, tz
setAxisPins(TX_NEG_PIN, TX_POS_PIN, tx)
setAxisPins(TY_NEG_PIN, TY_POS_PIN, ty)
setAxisPins(TZ_NEG_PIN, TZ_POS_PIN, tz)
if data[0] == 2:
# rotation packet
rx = data[1] + (data[2]*256)
ry = data[3] + (data[4]*256)
rz = data[5] + (data[6]*256)
if data[2] > 127:
rx -= 65536
if data[4] > 127:
ry -= 65536
if data[6] > 127:
rz -= 65536
print "R: ", rx, ry, rz
setAxisPins(RX_NEG_PIN, RX_POS_PIN, rx)
setAxisPins(RY_NEG_PIN, RY_POS_PIN, ry)
setAxisPins(RZ_NEG_PIN, RZ_POS_PIN, rz)
if data[0] == 3 and data[1] == 0:
# button packet - exit on the release
run = False
except usb.core.USBError:
print("USBError")
# except:
# print("read failed")
# end while
# Cleanup GPIO pins
GPIO.cleanup()
usb.util.dispose_resources(dev)
if reattach:
dev.attach_kernel_driver(0)