comparison light8/io.py @ 0:45b12307c695

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