author | Tero Marttila <terom@paivola.fi> |
Mon, 02 Jun 2014 18:26:41 +0300 | |
changeset 87 | 2179a4e28aba |
parent 83 | 136e210fce82 |
child 90 | 13c2deb919d1 |
permissions | -rw-r--r-- |
74 | 1 |
import collections |
2 |
import logging; log = logging.getLogger('qmsk.dmx.control') |
|
3 |
import serial |
|
4 |
||
83
136e210fce82
qmsk.dmx: new Head-based model/view; output/updates not yet implemented
Tero Marttila <terom@paivola.fi>
parents:
74
diff
changeset
|
5 |
""" |
136e210fce82
qmsk.dmx: new Head-based model/view; output/updates not yet implemented
Tero Marttila <terom@paivola.fi>
parents:
74
diff
changeset
|
6 |
Low-level DMX channel output. |
136e210fce82
qmsk.dmx: new Head-based model/view; output/updates not yet implemented
Tero Marttila <terom@paivola.fi>
parents:
74
diff
changeset
|
7 |
""" |
136e210fce82
qmsk.dmx: new Head-based model/view; output/updates not yet implemented
Tero Marttila <terom@paivola.fi>
parents:
74
diff
changeset
|
8 |
|
74 | 9 |
class DMXError (Exception) : |
10 |
def __init__ (self, **kwargs) : |
|
11 |
self.kwargs = kwargs |
|
12 |
||
13 |
def __str__ (self) : |
|
14 |
return self.__doc__.strip().format(**self.kwargs) |
|
15 |
||
16 |
class DMXCommandError (DMXError) : |
|
17 |
""" |
|
18 |
Command {cmd!r} failed: {out!r} |
|
19 |
""" |
|
20 |
||
21 |
class DMXUnknownCommandError (DMXError) : |
|
22 |
""" |
|
23 |
Unknown command: {cmd!r} |
|
24 |
""" |
|
25 |
||
26 |
class DMX (object) : |
|
27 |
""" |
|
28 |
Arudino-based DMX controller using src/hello-dmx.c over the serial port. |
|
29 |
""" |
|
30 |
||
31 |
SERIAL = '/dev/arduino' |
|
32 |
SERIAL_BAUD = 9600 |
|
33 |
SERIAL_TIMEOUT = 1.0 |
|
34 |
||
35 |
@classmethod |
|
36 |
def open (cls, path, baud=SERIAL_BAUD, timeout=SERIAL_TIMEOUT) : |
|
37 |
return cls(serial.Serial(path, baud, timeout=timeout)) |
|
38 |
||
39 |
def __init__ (self, io) : |
|
40 |
self.io = io |
|
41 |
||
42 |
# XXX: bug |
|
43 |
self.io.write('\r') |
|
44 |
self.io.flush() |
|
45 |
self.io.read(1) |
|
46 |
||
47 |
def _arg (self, arg) : |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
48 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
49 |
Format given value as a protocol argument. |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
50 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
51 |
|
74 | 52 |
if isinstance(arg, str) : |
53 |
value, = arg |
|
54 |
value = ord(value) |
|
55 |
elif isinstance(arg, int) : |
|
56 |
value = arg |
|
57 |
else : |
|
58 |
raise ValueError(arg) |
|
59 |
||
60 |
if 0 <= value <= 255 : |
|
61 |
return str(value) |
|
62 |
else : |
|
63 |
raise ValueError(value) |
|
64 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
65 |
def __iter__ (self) : |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
66 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
67 |
Read command responses back. |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
68 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
69 |
|
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
70 |
while True: |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
71 |
yield self.io.readline() |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
72 |
|
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
73 |
def __call__ (self, cmd, *args, **opts) : |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
74 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
75 |
|
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
76 |
<chr> [<int> [...]] |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
77 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
78 |
|
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
79 |
# XXX: |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
80 |
poll = opts.pop('poll', True) |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
81 |
|
74 | 82 |
out = cmd + ' ' + ' '.join(self._arg(arg) for arg in args) + '\r' |
83 |
||
84 |
log.info("%s", out) |
|
85 |
||
86 |
self.io.write(out) |
|
87 |
self.io.flush() |
|
88 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
89 |
if poll: |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
90 |
for ret in self: |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
91 |
break |
74 | 92 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
93 |
if '!' in ret : |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
94 |
raise DMXCommandError(cmd=out, out=ret) |
74 | 95 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
96 |
elif '?' in ret : |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
97 |
raise DMXUnknownCommandError(cmd=cmd) |
74 | 98 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
99 |
def clear (self, **opts) : |
74 | 100 |
""" |
101 |
Set dmx = [ ] |
|
102 |
||
103 |
i.e. start transmitting zero-length DMX packets. |
|
104 |
For most lights, this seems to be equivalent to losing the DMX signal, and they retain their old state. |
|
105 |
""" |
|
106 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
107 |
self('c', **opts) |
74 | 108 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
109 |
def zero (self, **opts) : |
74 | 110 |
""" |
111 |
Set dmx = [0, ...] |
|
112 |
||
113 |
Uses the maximum DMX packet length available. |
|
114 |
""" |
|
115 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
116 |
self('z', **opts) |
74 | 117 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
118 |
def out (self, *values, **opts) : |
74 | 119 |
""" |
120 |
Set dmx = (value, ...) |
|
121 |
""" |
|
122 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
123 |
self('o', *values, **opts) |
74 | 124 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
125 |
def set (self, start, *values, **opts) : |
74 | 126 |
""" |
127 |
Set dmx[start:] = value |
|
128 |
""" |
|
129 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
130 |
self('s', start, *values, **opts) |
74 | 131 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
132 |
def fill (self, start, end, *values, **opts) : |
74 | 133 |
""" |
134 |
Set dmx[start:end] to repetitions of (value, ...) |
|
135 |
""" |
|
136 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
137 |
self('f', start, end, *values, **opts) |
74 | 138 |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
139 |
def range (self, start, stop, step, value, **opts) : |
74 | 140 |
""" |
141 |
Set dmx[start:end:step] = value |
|
142 |
""" |
|
143 |
||
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
144 |
self('r', start, stop, step, value, **opts) |
74 | 145 |
|
146 |
def __setitem__ (self, index, value) : |
|
87
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
147 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
148 |
Magic indexing. |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
149 |
""" |
2179a4e28aba
qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts
Tero Marttila <terom@paivola.fi>
parents:
83
diff
changeset
|
150 |
|
74 | 151 |
if isinstance(value, collections.Sequence) : |
152 |
values = tuple(value) |
|
153 |
else : |
|
154 |
values = (value, ) |
|
155 |
||
156 |
if isinstance(index, slice) : |
|
157 |
if index.start and index.stop and index.step : |
|
158 |
# XXX: single |
|
159 |
self.range(index.start, index.stop, index.step, value) |
|
160 |
||
161 |
elif index.start and index.stop : |
|
162 |
self.fill(index.start, index.stop, *values) |
|
163 |
||
164 |
elif index.start : |
|
165 |
self.set(index.start, *values) |
|
166 |
||
167 |
else : |
|
168 |
raise IndexError("invalid slice: %s" % (index, )) |
|
169 |
||
170 |
else : |
|
171 |
# simple set |
|
172 |
self.set(index, *values) |
|
173 |