28 Arudino-based DMX controller using src/hello-dmx.c over the serial port. |
28 Arudino-based DMX controller using src/hello-dmx.c over the serial port. |
29 """ |
29 """ |
30 |
30 |
31 SERIAL = '/dev/arduino' |
31 SERIAL = '/dev/arduino' |
32 SERIAL_BAUD = 9600 |
32 SERIAL_BAUD = 9600 |
33 SERIAL_TIMEOUT = 1.0 |
|
34 |
33 |
35 @classmethod |
34 @classmethod |
36 def open (cls, path, baud=SERIAL_BAUD, timeout=SERIAL_TIMEOUT) : |
35 def open (cls, path, baud=SERIAL_BAUD) : |
37 return cls(serial.Serial(path, baud, timeout=timeout)) |
36 return cls(serial.Serial(path, baud)) |
38 |
37 |
39 def __init__ (self, io) : |
38 def __init__ (self, io) : |
40 self.io = io |
39 self.io = io |
|
40 |
|
41 # XXX: sync initial line |
|
42 self.sync() |
|
43 self.sync() |
41 |
44 |
42 # XXX: bug |
45 def sync (self) : |
43 self.io.write('\r') |
46 """ |
|
47 XXX: startup sync |
|
48 """ |
|
49 |
|
50 self.io.write(b'\r') |
44 self.io.flush() |
51 self.io.flush() |
45 self.io.read(1) |
52 self.io.read(1) |
46 |
53 |
47 def _arg (self, arg) : |
54 def _arg (self, arg) : |
48 """ |
55 """ |
76 <chr> [<int> [...]] |
83 <chr> [<int> [...]] |
77 """ |
84 """ |
78 |
85 |
79 # XXX: |
86 # XXX: |
80 poll = opts.pop('poll', True) |
87 poll = opts.pop('poll', True) |
81 |
88 |
82 out = cmd + ' ' + ' '.join(self._arg(arg) for arg in args) + '\r' |
89 out = cmd + b' ' + b' '.join(self._arg(arg) for arg in args) |
83 |
90 |
84 log.info("%s", out) |
91 log.info("%s", out) |
85 |
92 |
86 self.io.write(out) |
93 self.io.write(out) |
|
94 self.io.write(b'\r') |
87 self.io.flush() |
95 self.io.flush() |
88 |
96 |
89 if poll: |
97 if poll: |
90 for ret in self: |
98 for ret in self: |
91 break |
99 break |
92 |
100 |
93 if '!' in ret : |
101 if b'!' in ret : |
94 raise DMXCommandError(cmd=out, out=ret) |
102 raise DMXCommandError(cmd=out, out=ret) |
95 |
103 |
96 elif '?' in ret : |
104 elif b'?' in ret : |
97 raise DMXUnknownCommandError(cmd=cmd) |
105 raise DMXUnknownCommandError(cmd=cmd) |
98 |
106 |
99 def clear (self, **opts) : |
107 def clear (self, **opts) : |
100 """ |
108 """ |
101 Set dmx = [ ] |
109 Set dmx = [ ] |
102 |
110 |
103 i.e. start transmitting zero-length DMX packets. |
111 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. |
112 For most lights, this seems to be equivalent to losing the DMX signal, and they retain their old state. |
105 """ |
113 """ |
106 |
114 |
107 self('c', **opts) |
115 self(b'c', **opts) |
108 |
116 |
109 def zero (self, **opts) : |
117 def zero (self, **opts) : |
110 """ |
118 """ |
111 Set dmx = [0, ...] |
119 Set dmx = [0, ...] |
112 |
120 |
113 Uses the maximum DMX packet length available. |
121 Uses the maximum DMX packet length available. |
114 """ |
122 """ |
115 |
123 |
116 self('z', **opts) |
124 self(b'z', **opts) |
117 |
125 |
118 def out (self, *values, **opts) : |
126 def out (self, *values, **opts) : |
119 """ |
127 """ |
120 Set dmx = (value, ...) |
128 Set dmx = (value, ...) |
121 """ |
129 """ |
122 |
130 |
123 self('o', *values, **opts) |
131 self(b'o', *values, **opts) |
124 |
132 |
125 def set (self, start, *values, **opts) : |
133 def set (self, start, *values, **opts) : |
126 """ |
134 """ |
127 Set dmx[start:] = value |
135 Set dmx[start:] = value |
128 """ |
136 """ |
129 |
137 |
130 self('s', start, *values, **opts) |
138 self(b's', start, *values, **opts) |
131 |
139 |
132 def fill (self, start, end, *values, **opts) : |
140 def fill (self, start, end, *values, **opts) : |
133 """ |
141 """ |
134 Set dmx[start:end] to repetitions of (value, ...) |
142 Set dmx[start:end] to repetitions of (value, ...) |
135 """ |
143 """ |
136 |
144 |
137 self('f', start, end, *values, **opts) |
145 self(b'f', start, end, *values, **opts) |
138 |
146 |
139 def range (self, start, stop, step, value, **opts) : |
147 def range (self, start, stop, step, value, **opts) : |
140 """ |
148 """ |
141 Set dmx[start:end:step] = value |
149 Set dmx[start:end:step] = value |
142 """ |
150 """ |
143 |
151 |
144 self('r', start, stop, step, value, **opts) |
152 self(b'r', start, stop, step, value, **opts) |
145 |
153 |
146 def __setitem__ (self, index, value) : |
154 def __setitem__ (self, index, value) : |
147 """ |
155 """ |
148 Magic indexing. |
156 Magic indexing. |
149 """ |
157 """ |