Mercurial > code > home > repos > light9
annotate light9/io/__init__.py @ 1287:1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Ignore-this: 9495a02620509bbd03271b47f9ab3282
author | Drew Perttula <drewp@bigasterisk.com> |
---|---|
date | Sat, 14 May 2016 23:56:02 +0000 |
parents | e263c4bd73f9 |
children | 37cbb245d93c |
rev | line source |
---|---|
333
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
1 from __future__ import division |
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
2 import sys |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
3 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
4 class BaseIO: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
5 def __init__(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
6 self.dummy=1 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
7 self.__name__ = 'BaseIO' |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
8 # please override and set __name__ to your class name |
0 | 9 |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
10 def golive(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
11 """call this if you want to promote the dummy object becomes a live object""" |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
12 print "IO: %s is going live" % self.__name__ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
13 self.dummy=0 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
14 # you'd override with additional startup stuff here, |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
15 # perhaps even loading a module and saving it to a class |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
16 # attr so the subclass-specific functions can use it |
83 | 17 |
18 def godummy(self): | |
19 print "IO: %s is going dummy" % self.__name__ | |
20 self.dummy=1 | |
21 # you might override this to close ports, etc | |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
22 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
23 def isdummy(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
24 return self.dummy |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
25 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
26 def __repr__(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
27 if self.dummy: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
28 return "<dummy %s instance>" % self.__name__ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
29 else: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
30 return "<live %s instance>" % self.__name__ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
31 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
32 # the derived class will have more methods to do whatever it does, |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
33 # and they should return dummy values if self.dummy==1. |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
34 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
35 class ParportDMX(BaseIO): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
36 def __init__(self, dimmers=68): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
37 BaseIO.__init__(self) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
38 self.__name__='ParportDMX' |
53 | 39 self.dimmers = dimmers |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
40 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
41 def golive(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
42 BaseIO.golive(self) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
43 import parport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
44 self.parport = parport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
45 self.parport.getparport() |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
46 |
72 | 47 def sendlevels(self, levels): |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
48 if self.dummy: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
49 return |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
50 |
72 | 51 levels = list(levels) + [0] |
52 # if levels[14] > 0: levels[14] = 100 # non-dim | |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
53 self.parport.outstart() |
72 | 54 for p in range(1, self.dimmers + 2): |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
55 self.parport.outbyte(levels[p-1]*255 / 100) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
56 |
333
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
57 class UsbDMX(BaseIO): |
876
e263c4bd73f9
pass dmx choice into dmxserver
Drew Perttula <drewp@bigasterisk.com>
parents:
752
diff
changeset
|
58 def __init__(self, dimmers=72, port='/dev/dmx0'): |
333
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
59 BaseIO.__init__(self) |
358
5ee1de9ddc9d
fix usbdmx live mode (probably breaking dummy mode)
Drew Perttula <drewp@bigasterisk.com>
parents:
335
diff
changeset
|
60 self.__name__ = "UsbDMX" |
876
e263c4bd73f9
pass dmx choice into dmxserver
Drew Perttula <drewp@bigasterisk.com>
parents:
752
diff
changeset
|
61 self.port = port |
358
5ee1de9ddc9d
fix usbdmx live mode (probably breaking dummy mode)
Drew Perttula <drewp@bigasterisk.com>
parents:
335
diff
changeset
|
62 self.out = None |
426
cd75a12ad66d
adjust dmx dimmer count back to PHS theater levels
drewp@bigasterisk.com
parents:
364
diff
changeset
|
63 self.dimmers = dimmers |
358
5ee1de9ddc9d
fix usbdmx live mode (probably breaking dummy mode)
Drew Perttula <drewp@bigasterisk.com>
parents:
335
diff
changeset
|
64 |
5ee1de9ddc9d
fix usbdmx live mode (probably breaking dummy mode)
Drew Perttula <drewp@bigasterisk.com>
parents:
335
diff
changeset
|
65 def _dmx(self): |
5ee1de9ddc9d
fix usbdmx live mode (probably breaking dummy mode)
Drew Perttula <drewp@bigasterisk.com>
parents:
335
diff
changeset
|
66 if self.out is None: |
1287
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
67 if self.port == 'udmx': |
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
68 from udmx import Udmx |
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
69 self.out = Udmx() |
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
70 self.out.write = self.out.SendDMX |
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
71 else: |
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
72 sys.path.append("dmx_usb_module") |
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
73 from dmx import Dmx |
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
74 self.out = Dmx(self.port) |
358
5ee1de9ddc9d
fix usbdmx live mode (probably breaking dummy mode)
Drew Perttula <drewp@bigasterisk.com>
parents:
335
diff
changeset
|
75 return self.out |
333
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
76 |
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
77 def sendlevels(self, levels): |
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
78 if self.dummy: |
44189a37a876
change dmxserver to use enttec opendmx
drewp@bigasterisk.com
parents:
209
diff
changeset
|
79 return |
364
9a03605121b7
fix usb dmx levels off by one
Drew Perttula <drewp@bigasterisk.com>
parents:
358
diff
changeset
|
80 # I was outputting on 76 and it was turning on the light at |
9a03605121b7
fix usb dmx levels off by one
Drew Perttula <drewp@bigasterisk.com>
parents:
358
diff
changeset
|
81 # dmx75. So I added the 0 byte. |
9a03605121b7
fix usb dmx levels off by one
Drew Perttula <drewp@bigasterisk.com>
parents:
358
diff
changeset
|
82 packet = '\x00' + ''.join([chr(int(lev * 255 / 100)) |
9a03605121b7
fix usb dmx levels off by one
Drew Perttula <drewp@bigasterisk.com>
parents:
358
diff
changeset
|
83 for lev in levels]) + "\x55" |
358
5ee1de9ddc9d
fix usbdmx live mode (probably breaking dummy mode)
Drew Perttula <drewp@bigasterisk.com>
parents:
335
diff
changeset
|
84 self._dmx().write(packet) |
1287
1382cb43a3ca
dmxserver -d udmx to talk to the first udmx usb adapter found
Drew Perttula <drewp@bigasterisk.com>
parents:
876
diff
changeset
|
85 |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
86 class SerialPots(BaseIO): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
87 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
88 this is a dummy object (that returns zeros forever) until you call startup() |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
89 which makes it bind to the port, etc |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
90 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
91 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
92 def __init__(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
93 # no init here- call getport() to actually initialize |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
94 self.dummy=1 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
95 self.__name__='SerialPots' # i thought this was automatic! |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
96 |
83 | 97 def golive(self): |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
98 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
99 ls -l /dev/i2c-0 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
100 crw-rw-rw- 1 root root 89, 0 Jul 11 12:27 /dev/i2c-0 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
101 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
102 import serport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
103 self.serport = serport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
104 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
105 self.f = open("/dev/i2c-0","rw") |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
106 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
107 # this is for a chip with A0,A1,A2 lines all low: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
108 port = 72 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
109 |
89 | 110 from fcntl import * |
111 | |
112 I2C_SLAVE = 0x0703 #/* Change slave address */ | |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
113 ioctl(self.f,I2C_SLAVE,port) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
114 self.dummy=0 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
115 |
83 | 116 def godummy(self): |
117 BaseIO.godummy(self) | |
118 self.f.close() | |
119 | |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
120 def getlevels(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
121 if self.dummy: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
122 return (0,0,0,0) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
123 else: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
124 return self.serport.read_all_adc(self.f.fileno()) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
125 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
126 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
127 if __name__=='__main__': |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
128 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
129 """ tester program that just dumps levels for a while """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
130 from time import sleep |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
131 from serport import * |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
132 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
133 i=0 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
134 while i<100: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
135 sleep(.033) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
136 i=i+1 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
137 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
138 print read_all_adc(f.fileno()) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
139 |