Mercurial > code > home > repos > light9
annotate light8/io.py @ 169:2794ad9a8fd8
out with the old, in with the new:
out with the old, in with the new:
dolly NSI subs and cues added
old dance show subs removed
author | dmcc |
---|---|
date | Wed, 09 Jul 2003 05:47:37 +0000 |
parents | 0459cecf8145 |
children |
rev | line source |
---|---|
89 | 1 |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
2 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
3 class BaseIO: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
4 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
|
5 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
|
6 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
|
7 # please override and set __name__ to your class name |
0 | 8 |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
9 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
|
10 """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
|
11 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
|
12 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
|
13 # 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
|
14 # 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
|
15 # attr so the subclass-specific functions can use it |
83 | 16 |
17 def godummy(self): | |
18 print "IO: %s is going dummy" % self.__name__ | |
19 self.dummy=1 | |
20 # 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
|
21 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
22 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
|
23 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
|
24 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
25 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
|
26 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
|
27 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
|
28 else: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
29 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
|
30 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
31 # 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
|
32 # 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
|
33 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
34 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
|
35 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
|
36 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
|
37 self.__name__='ParportDMX' |
53 | 38 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
|
39 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
40 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
|
41 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
|
42 import parport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
43 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
|
44 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
|
45 |
72 | 46 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
|
47 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
|
48 return |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
49 |
72 | 50 levels = list(levels) + [0] |
51 # 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
|
52 self.parport.outstart() |
72 | 53 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
|
54 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
|
55 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
56 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
|
57 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
58 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
|
59 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
|
60 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
61 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
62 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
|
63 # 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
|
64 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
|
65 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
|
66 |
83 | 67 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
|
68 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
69 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
|
70 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
|
71 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
72 import serport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
73 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
|
74 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
75 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
|
76 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
77 # 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
|
78 port = 72 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
79 |
89 | 80 from fcntl import * |
81 | |
82 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
|
83 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
|
84 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
|
85 |
83 | 86 def godummy(self): |
87 BaseIO.godummy(self) | |
88 self.f.close() | |
89 | |
81
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
90 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
|
91 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
|
92 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
|
93 else: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
94 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
|
95 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
96 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
97 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
|
98 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
99 """ 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
|
100 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
|
101 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
|
102 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
103 i=0 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
104 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
|
105 sleep(.033) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
106 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
|
107 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
108 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
|
109 |