Mercurial > code > home > repos > light9
comparison light8/io.py @ 81:70bd142d72c2
added a SerialPot io object and made the IO code much more elegant (in io.py)
author | drewp |
---|---|
date | Fri, 12 Jul 2002 10:42:24 +0000 |
parents | 609cb9ae53b1 |
children | ae2ed47a5321 |
comparison
equal
deleted
inserted
replaced
80:5f0c6bc8e9de | 81:70bd142d72c2 |
---|---|
1 from parport import * | |
2 | 1 |
3 class ParportDMX: | 2 class BaseIO: |
4 def __init__(self, dummy=1, dimmers=68): | 3 def __init__(self): |
4 self.dummy=1 | |
5 self.__name__ = 'BaseIO' | |
6 # please override and set __name__ to your class name | |
7 | |
8 def golive(self): | |
9 """call this if you want to promote the dummy object becomes a live object""" | |
10 print "IO: %s is going live" % self.__name__ | |
11 self.dummy=0 | |
12 # you'd override with additional startup stuff here, | |
13 # perhaps even loading a module and saving it to a class | |
14 # attr so the subclass-specific functions can use it | |
15 | |
16 def isdummy(self): | |
17 return self.dummy | |
18 | |
19 def __repr__(self): | |
20 if self.dummy: | |
21 return "<dummy %s instance>" % self.__name__ | |
22 else: | |
23 return "<live %s instance>" % self.__name__ | |
24 | |
25 # the derived class will have more methods to do whatever it does, | |
26 # and they should return dummy values if self.dummy==1. | |
27 | |
28 class ParportDMX(BaseIO): | |
29 def __init__(self, dimmers=68): | |
30 BaseIO.__init__(self) | |
31 self.__name__='ParportDMX' | |
5 self.dimmers = dimmers | 32 self.dimmers = dimmers |
6 self.dummy = dummy | 33 |
7 if not dummy: | 34 def golive(self): |
8 getparport() | 35 BaseIO.golive(self) |
36 import parport | |
37 self.parport = parport | |
38 self.parport.getparport() | |
39 | |
9 def sendlevels(self, levels): | 40 def sendlevels(self, levels): |
10 if self.dummy: return | 41 if self.dummy: |
42 return | |
43 | |
11 levels = list(levels) + [0] | 44 levels = list(levels) + [0] |
12 # if levels[14] > 0: levels[14] = 100 # non-dim | 45 # if levels[14] > 0: levels[14] = 100 # non-dim |
13 outstart() | 46 self.parport.outstart() |
14 for p in range(1, self.dimmers + 2): | 47 for p in range(1, self.dimmers + 2): |
15 outbyte(levels[p-1]*255 / 100) | 48 self.parport.outbyte(levels[p-1]*255 / 100) |
49 | |
50 class SerialPots(BaseIO): | |
51 """ | |
52 this is a dummy object (that returns zeros forever) until you call startup() | |
53 which makes it bind to the port, etc | |
54 | |
55 """ | |
56 def __init__(self): | |
57 # no init here- call getport() to actually initialize | |
58 self.dummy=1 | |
59 self.__name__='SerialPots' # i thought this was automatic! | |
60 | |
61 def startup(self): | |
62 """ | |
63 ls -l /dev/i2c-0 | |
64 crw-rw-rw- 1 root root 89, 0 Jul 11 12:27 /dev/i2c-0 | |
65 """ | |
66 import serport | |
67 self.serport = serport | |
68 | |
69 self.f = open("/dev/i2c-0","rw") | |
70 | |
71 # this is for a chip with A0,A1,A2 lines all low: | |
72 port = 72 | |
73 | |
74 ioctl(self.f,I2C_SLAVE,port) | |
75 self.dummy=0 | |
76 | |
77 def getlevels(self): | |
78 if self.dummy: | |
79 return (0,0,0,0) | |
80 else: | |
81 return self.serport.read_all_adc(self.f.fileno()) | |
82 | |
83 | |
84 if __name__=='__main__': | |
85 | |
86 """ tester program that just dumps levels for a while """ | |
87 from time import sleep | |
88 from serport import * | |
89 | |
90 i=0 | |
91 while i<100: | |
92 sleep(.033) | |
93 i=i+1 | |
94 | |
95 print read_all_adc(f.fileno()) | |
96 |