annotate light8/io.py @ 116:9ddea0c614ee

much prettier stdout, including a clock (so you can tell the server's running) much prettier stdout, including a clock (so you can tell the server's running) and channel updates only when the levels change, and throttled to every 100 updates even then.
author drewp
date Fri, 13 Jun 2003 06:15:28 +0000
parents 0459cecf8145
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
89
0459cecf8145 added missing imports and constants to SerialPots
drewp
parents: 83
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
16
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
17 def godummy(self):
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
18 print "IO: %s is going dummy" % self.__name__
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
19 self.dummy=1
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
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
032b2b67bc10 result of July 7th on-site editing
dmcc
parents: 29
diff changeset
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
609cb9ae53b1 results of 7.10 rehearsal, rollback broken IO changes
dmcc
parents: 71
diff changeset
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
609cb9ae53b1 results of 7.10 rehearsal, rollback broken IO changes
dmcc
parents: 71
diff changeset
50 levels = list(levels) + [0]
609cb9ae53b1 results of 7.10 rehearsal, rollback broken IO changes
dmcc
parents: 71
diff changeset
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
609cb9ae53b1 results of 7.10 rehearsal, rollback broken IO changes
dmcc
parents: 71
diff changeset
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
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
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
0459cecf8145 added missing imports and constants to SerialPots
drewp
parents: 83
diff changeset
80 from fcntl import *
0459cecf8145 added missing imports and constants to SerialPots
drewp
parents: 83
diff changeset
81
0459cecf8145 added missing imports and constants to SerialPots
drewp
parents: 83
diff changeset
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
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
86 def godummy(self):
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
87 BaseIO.godummy(self)
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
88 self.f.close()
ae2ed47a5321 io can now return to being dummy (untested)
drewp
parents: 81
diff changeset
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