Mercurial > code > home > repos > homeauto
annotate service/shuttlepro/shuttlepro.py @ 331:a94f2a522d41
build and import updates for rdfdb, etc
Ignore-this: 233cb2b31f03be51695f0fff40eecca7
author | drewp@bigasterisk.com |
---|---|
date | Mon, 19 Feb 2018 04:21:28 -0800 |
parents | 57ae7dc0f417 |
children |
rev | line source |
---|---|
43 | 1 #!/usr/bin/env python |
2 # | |
3 # Copyright 2005 Free Software Foundation, Inc. | |
4 # | |
5 # This file is part of GNU Radio | |
6 # | |
7 # GNU Radio is free software; you can redistribute it and/or modify | |
8 # it under the terms of the GNU General Public License as published by | |
9 # the Free Software Foundation; either version 3, or (at your option) | |
10 # any later version. | |
11 # | |
12 # GNU Radio is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU General Public License for more details. | |
16 # | |
17 # You should have received a copy of the GNU General Public License | |
18 # along with GNU Radio; see the file COPYING. If not, write to | |
19 # the Free Software Foundation, Inc., 51 Franklin Street, | |
20 # Boston, MA 02110-1301, USA. | |
21 # | |
22 | |
23 """ | |
24 Handler for Griffin PowerMate, Contour ShuttlePro & ShuttleXpress USB knobs | |
25 | |
26 modified by drewp@bigasterisk.com | |
27 """ | |
126
a8e19321d63f
rework shuttlepro's output statements
drewp@bigasterisk.com
parents:
125
diff
changeset
|
28 import os, time, logging |
43 | 29 import sys |
30 import struct | |
31 import exceptions | |
32 import threading | |
33 | |
126
a8e19321d63f
rework shuttlepro's output statements
drewp@bigasterisk.com
parents:
125
diff
changeset
|
34 logging.basicConfig(level=logging.INFO) |
a8e19321d63f
rework shuttlepro's output statements
drewp@bigasterisk.com
parents:
125
diff
changeset
|
35 log = logging.getLogger() |
43 | 36 |
37 def hexint(mask): | |
38 """ | |
39 Convert unsigned masks into signed ints. | |
40 | |
41 This allows us to use hex constants like 0xf0f0f0f2 when talking to | |
42 our hardware and not get screwed by them getting treated as python | |
43 longs. | |
44 """ | |
45 if mask >= 2**31: | |
46 return int(mask-2**32) | |
47 return mask | |
48 imported_ok = True | |
49 | |
50 try: | |
51 import select | |
52 import fcntl | |
53 except ImportError: | |
54 imported_ok = False | |
55 | |
56 | |
57 # First a little bit of background: | |
58 # | |
59 # The Griffin PowerMate has | |
60 # * a single knob which rotates | |
61 # * a single button (pressing the knob) | |
62 # | |
63 # The Contour ShuttleXpress (aka SpaceShuttle) has | |
64 # * "Jog Wheel" -- the knob (rotary encoder) on the inside | |
65 # * "Shuttle Ring" -- the spring loaded rubber covered ring | |
66 # * 5 buttons | |
67 # | |
68 # The Contour ShuttlePro has | |
69 # * "Jog Wheel" -- the knob (rotary encoder) on the inside | |
70 # * "Shuttle Ring" -- the spring loaded rubber covered ring | |
71 # * 13 buttons | |
72 # | |
73 # The Contour ShuttlePro V2 has | |
74 # *"Jog Wheel" -- the knob (rotary encoder) on the inside | |
75 # * "Shuttle Ring" -- the spring loaded rubber covered ring | |
76 # * 15 buttons | |
77 | |
78 # We remap all the buttons on the devices so that they start at zero. | |
79 | |
80 # For the ShuttleXpress the buttons are 0 to 4 (left to right) | |
81 | |
82 # For the ShuttlePro, we number the buttons immediately above | |
83 # the ring 0 to 4 (left to right) so that they match our numbering | |
84 # on the ShuttleXpress. The top row is 5, 6, 7, 8. The first row below | |
85 # the ring is 9, 10, and the bottom row is 11, 12. | |
86 | |
87 # For the ShuttlePro V2, buttons 13 & 14 are to the | |
88 # left and right of the wheel respectively. | |
89 | |
90 # We generate 3 kinds of events: | |
91 # | |
92 # button press/release (button_number, press/release) | |
93 # knob rotation (relative_clicks) # typically -1, +1 | |
94 # shuttle position (absolute_position) # -7,-6,...,0,...,6,7 | |
95 | |
96 # ---------------------------------------------------------------- | |
97 # Our ID's for the devices: | |
98 # Not to be confused with anything related to magic hardware numbers. | |
99 | |
100 ID_POWERMATE = 'powermate' | |
101 ID_SHUTTLE_XPRESS = 'shuttle xpress' | |
102 ID_SHUTTLE_PRO = 'shuttle pro' | |
103 ID_SHUTTLE_PRO_V2 = 'shuttle pro v2' | |
104 | |
105 # ------------------------------------------------------------------------ | |
106 # format of messages that we read from /dev/input/event* | |
107 # See /usr/include/linux/input.h for more info | |
108 # | |
109 #struct input_event { | |
110 # struct timeval time; = {long seconds, long microseconds} | |
111 # unsigned short type; | |
112 # unsigned short code; | |
113 # unsigned int value; | |
114 #}; | |
115 | |
116 input_event_struct = "@llHHi" | |
117 input_event_size = struct.calcsize(input_event_struct) | |
118 | |
119 # ------------------------------------------------------------------------ | |
120 # input_event types | |
121 # ------------------------------------------------------------------------ | |
122 | |
123 IET_SYN = 0x00 # aka RESET | |
124 IET_KEY = 0x01 # key or button press/release | |
125 IET_REL = 0x02 # relative movement (knob rotation) | |
126 IET_ABS = 0x03 # absolute position (graphics pad, etc) | |
127 IET_MSC = 0x04 | |
128 IET_LED = 0x11 | |
129 IET_SND = 0x12 | |
130 IET_REP = 0x14 | |
131 IET_FF = 0x15 | |
132 IET_PWR = 0x16 | |
133 IET_FF_STATUS = 0x17 | |
134 IET_MAX = 0x1f | |
135 | |
136 # ------------------------------------------------------------------------ | |
137 # input_event codes (there are a zillion of them, we only define a few) | |
138 # ------------------------------------------------------------------------ | |
139 | |
140 # these are valid for IET_KEY | |
141 | |
142 IEC_BTN_0 = 0x100 | |
143 IEC_BTN_1 = 0x101 | |
144 IEC_BTN_2 = 0x102 | |
145 IEC_BTN_3 = 0x103 | |
146 IEC_BTN_4 = 0x104 | |
147 IEC_BTN_5 = 0x105 | |
148 IEC_BTN_6 = 0x106 | |
149 IEC_BTN_7 = 0x107 | |
150 IEC_BTN_8 = 0x108 | |
151 IEC_BTN_9 = 0x109 | |
152 IEC_BTN_10 = 0x10a | |
153 IEC_BTN_11 = 0x10b | |
154 IEC_BTN_12 = 0x10c | |
155 IEC_BTN_13 = 0x10d | |
156 IEC_BTN_14 = 0x10e | |
157 IEC_BTN_15 = 0x10f | |
158 | |
159 # these are valid for IET_REL (Relative axes) | |
160 | |
161 IEC_REL_X = 0x00 | |
162 IEC_REL_Y = 0x01 | |
163 IEC_REL_Z = 0x02 | |
164 IEC_REL_HWHEEL = 0x06 | |
165 IEC_REL_DIAL = 0x07 # rotating the knob | |
166 IEC_REL_WHEEL = 0x08 # moving the shuttle ring | |
167 IEC_REL_MISC = 0x09 | |
168 IEC_REL_MAX = 0x0f | |
169 | |
170 # ------------------------------------------------------------------------ | |
171 | |
172 class powermate(object): | |
173 """ | |
174 Interface to Griffin PowerMate and Contour Shuttles | |
175 """ | |
176 def __init__(self, filename=None, on_event=lambda ev: None): | |
177 self.on_event = on_event | |
178 self.handle = -1 | |
179 if not imported_ok: | |
180 raise exceptions.RuntimeError, 'powermate not supported on this platform' | |
181 | |
182 if filename: | |
183 if not self._open_device(filename): | |
184 raise exceptions.RuntimeError, 'Unable to find powermate' | |
185 else: | |
186 ok = False | |
187 for d in range(0, 16): | |
188 if self._open_device("/dev/input/event%d" % d): | |
189 ok = True | |
190 break | |
191 if not ok: | |
192 raise exceptions.RuntimeError, 'Unable to find powermate' | |
193 | |
194 def __del__(self): | |
195 self.keep_running = False | |
196 if self.handle >= 0: | |
197 os.close(self.handle) | |
198 self.handle = -1 | |
199 | |
200 def _open_device(self, filename): | |
201 try: | |
202 self.handle = os.open(filename, os.O_RDWR) | |
203 if self.handle < 0: | |
204 print "can't open file" | |
205 return False | |
206 | |
207 # read event device name | |
208 name = fcntl.ioctl(self.handle, hexint(0x80ff4506), chr(0) * 256) | |
209 name = name.replace(chr(0), '') | |
210 print "%s name is %s" % (filename, name) | |
211 # do we see anything we recognize? | |
212 if name == 'Griffin PowerMate' or name == 'Griffin SoundKnob': | |
213 self.id = ID_POWERMATE | |
214 self.mapper = _powermate_remapper() | |
215 elif name == 'CAVS SpaceShuttle A/V' or name == 'Contour Design ShuttleXpress': | |
216 self.id = ID_SHUTTLE_XPRESS | |
217 self.mapper = _contour_remapper() | |
218 elif name == 'Contour Design ShuttlePRO': | |
219 self.id = ID_SHUTTLE_PRO | |
220 self.mapper = _contour_remapper() | |
221 elif name == 'Contour Design ShuttlePRO v2': | |
222 self.id = ID_SHUTTLE_PRO_V2 | |
223 self.mapper = _contour_remapper() | |
224 else: | |
225 os.close(self.handle) | |
226 self.handle = -1 | |
227 return False | |
228 | |
229 # get exclusive control of the device, using ioctl EVIOCGRAB | |
230 # there may be an issue with this on non x86 platforms and if | |
231 # the _IOW,_IOC,... macros in <asm/ioctl.h> are changed | |
232 fcntl.ioctl(self.handle,hexint(0x40044590), 1) | |
233 return True | |
234 except exceptions.OSError: | |
235 return False | |
236 | |
237 | |
238 def set_event_receiver(self, obj): | |
239 self.event_receiver = obj | |
240 | |
241 | |
242 def set_led_state(self, static_brightness, pulse_speed=0, | |
243 pulse_table=0, pulse_on_sleep=0, pulse_on_wake=0): | |
244 """ | |
245 What do these magic values mean... | |
246 """ | |
247 if self.id != ID_POWERMATE: | |
248 return False | |
249 | |
250 static_brightness &= 0xff; | |
251 if pulse_speed < 0: | |
252 pulse_speed = 0 | |
253 if pulse_speed > 510: | |
254 pulse_speed = 510 | |
255 if pulse_table < 0: | |
256 pulse_table = 0 | |
257 if pulse_table > 2: | |
258 pulse_table = 2 | |
259 pulse_on_sleep = not not pulse_on_sleep # not not = convert to 0/1 | |
260 pulse_on_wake = not not pulse_on_wake | |
261 magic = (static_brightness | |
262 | (pulse_speed << 8) | |
263 | (pulse_table << 17) | |
264 | (pulse_on_sleep << 19) | |
265 | (pulse_on_wake << 20)) | |
266 data = struct.pack(input_event_struct, 0, 0, 0x04, 0x01, magic) | |
267 os.write(self.handle, data) | |
268 return True | |
269 | |
270 def read_next(self): | |
271 s = os.read (self.handle, input_event_size) | |
272 if not s: | |
273 return | |
274 | |
275 raw_input_event = struct.unpack(input_event_struct,s) | |
276 sec, usec, type, code, val = self.mapper(raw_input_event) | |
277 | |
278 if type == IET_SYN: # ignore | |
279 pass | |
280 elif type == IET_MSC: # ignore (seems to be PowerMate reporting led brightness) | |
281 pass | |
282 elif type == IET_REL and code == IEC_REL_DIAL: | |
283 self.on_event({"dial":val}) | |
284 elif type == IET_REL and code == IEC_REL_WHEEL: | |
285 self.on_event({"shuttle":val}) | |
286 elif type == IET_KEY: | |
287 self.on_event({"key":{"button":code - IEC_BTN_0, "press":val}}) | |
288 else: | |
289 print "powermate: unrecognized event: type = 0x%x code = 0x%x val = %d" % (type, code, val) | |
290 | |
291 | |
292 class _powermate_remapper(object): | |
293 def __init__(self): | |
294 pass | |
295 def __call__(self, event): | |
296 """ | |
297 Notice how nice and simple this is... | |
298 """ | |
299 return event | |
300 | |
301 class _contour_remapper(object): | |
302 def __init__(self): | |
303 self.prev = None | |
304 def __call__(self, event): | |
305 """ | |
306 ...and how screwed up this is | |
307 """ | |
308 sec, usec, type, code, val = event | |
309 if type == IET_REL and code == IEC_REL_WHEEL: | |
310 # === Shuttle ring === | |
311 # First off, this really ought to be IET_ABS, not IET_REL! | |
312 # They never generate a zero value so you can't | |
313 # tell when the shuttle ring is back in the center. | |
314 # We kludge around this by calling both -1 and 1 zero. | |
315 if val == -1 or val == 1: | |
316 return (sec, usec, type, code, 0) | |
317 return event | |
318 | |
319 if type == IET_REL and code == IEC_REL_DIAL: | |
320 # === Jog knob (rotary encoder) === | |
321 # Dim wits got it wrong again! This one should return a | |
322 # a relative value, e.g., -1, +1. Instead they return | |
323 # a total that runs modulo 256 (almost!). For some | |
324 # reason they count like this 253, 254, 255, 1, 2, 3 | |
325 | |
326 if self.prev is None: # first time call | |
327 self.prev = val | |
328 return (sec, usec, IET_SYN, 0, 0) # will be ignored above | |
329 | |
330 diff = val - self.prev | |
331 if diff == 0: # sometimes it just sends stuff... | |
332 return (sec, usec, IET_SYN, 0, 0) # will be ignored above | |
333 | |
334 if abs(diff) > 100: # crossed into the twilight zone | |
335 if self.prev > val: # we've wrapped going forward | |
336 self.prev = val | |
337 return (sec, usec, type, code, +1) | |
338 else: # we've wrapped going backward | |
339 self.prev = val | |
340 return (sec, usec, type, code, -1) | |
341 | |
342 self.prev = val | |
343 return (sec, usec, type, code, diff) | |
344 | |
345 if type == IET_KEY: | |
346 # remap keys so that all 3 gadgets have buttons 0 to 4 in common | |
347 return (sec, usec, type, | |
348 (IEC_BTN_5, IEC_BTN_6, IEC_BTN_7, IEC_BTN_8, | |
349 IEC_BTN_0, IEC_BTN_1, IEC_BTN_2, IEC_BTN_3, IEC_BTN_4, | |
350 IEC_BTN_9, IEC_BTN_10, | |
351 IEC_BTN_11, IEC_BTN_12, | |
352 IEC_BTN_13, IEC_BTN_14)[code - IEC_BTN_0], val) | |
353 | |
354 return event | |
355 | |
89 | 356 |
43 | 357 if __name__ == '__main__': |
358 def ev(what): | |
359 print 'ev', what | |
125
bcf20b7ede56
shuttlepro post your events to reasoning service
drewp@bigasterisk.com
parents:
89
diff
changeset
|
360 |
43 | 361 p = powermate("/dev/input/by-id/usb-Contour_Design_ShuttlePRO-event-if00", ev) |
362 while True: | |
363 p.read_next() |