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