Mercurial > code > home > repos > light9
annotate light8/io.py @ 82:65e77dc74ad8
yippee
author | dmcc |
---|---|
date | Fri, 12 Jul 2002 10:45:05 +0000 |
parents | 70bd142d72c2 |
children | ae2ed47a5321 |
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 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
15 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
16 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
|
17 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
|
18 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
19 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
|
20 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
|
21 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
|
22 else: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
23 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
|
24 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
25 # 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
|
26 # 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
|
27 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
28 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
|
29 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
|
30 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
|
31 self.__name__='ParportDMX' |
53 | 32 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
|
33 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
34 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
|
35 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
|
36 import parport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
37 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
|
38 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
|
39 |
72 | 40 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
|
41 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
|
42 return |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
43 |
72 | 44 levels = list(levels) + [0] |
45 # 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
|
46 self.parport.outstart() |
72 | 47 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
|
48 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
|
49 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
50 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
|
51 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
52 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
|
53 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
|
54 |
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 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
|
57 # 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
|
58 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
|
59 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
|
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 startup(self): |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
62 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
63 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
|
64 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
|
65 """ |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
66 import serport |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
67 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
|
68 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
69 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
|
70 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
71 # 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
|
72 port = 72 |
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 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
|
75 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
|
76 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
77 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
|
78 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
|
79 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
|
80 else: |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
81 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
|
82 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
83 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
84 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
|
85 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
86 """ 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
|
87 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
|
88 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
|
89 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
90 i=0 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
91 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
|
92 sleep(.033) |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
93 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
|
94 |
70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
drewp
parents:
72
diff
changeset
|
95 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
|
96 |