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