blob: 5c287cb1578ef52d54518fe9e226fbdd87d3c7b2 [file] [log] [blame]
cliechti88641262002-07-29 02:09:04 +00001#pyparallel driver for win32
2#see __init__.py
3#
4#(C) 2002 Chris Liechti <cliechti@gmx.net>
5# this is distributed under a free software license, see license.txt
6#
7# thanks to Dincer Aydin dinceraydin@altavista.net for his work on the
8# winioport module: www.geocities.com/dinceraydin/ the graphic below is
9# borrowed form him ;-)
10
11
12# LPT1 = 0x0378 or 0x03BC
13# LPT2 = 0x0278 or 0x0378
14# LPT3 = 0x0278
15#
16# Data Register (base + 0) ........ outputs
17#
18# 7 6 5 4 3 2 1 0
19# . . . . . . . * D0 ........... (pin 2), 1=High, 0=Low (true)
20# . . . . . . * . D1 ........... (pin 3), 1=High, 0=Low (true)
21# . . . . . * . . D2 ........... (pin 4), 1=High, 0=Low (true)
22# . . . . * . . . D3 ........... (pin 5), 1=High, 0=Low (true)
23# . . . * . . . . D4 ........... (pin 6), 1=High, 0=Low (true)
24# . . * . . . . . D5 ........... (pin 7), 1=High, 0=Low (true)
25# . * . . . . . . D6 ........... (pin 8), 1=High, 0=Low (true)
26# * . . . . . . . D7 ........... (pin 9), 1=High, 0=Low (true)
27#
28# Status Register (base + 1) ...... inputs
29#
30# 7 6 5 4 3 2 1 0
31# . . . . . * * * Undefined
32# . . . . * . . . Error ........ (pin 15), high=1, low=0 (true)
33# . . . * . . . . Selected ..... (pin 13), high=1, low=0 (true)
34# . . * . . . . . No paper ..... (pin 12), high=1, low=0 (true)
35# . * . . . . . . Ack .......... (pin 10), high=1, low=0 (true)
36# * . . . . . . . Busy ......... (pin 11), high=0, low=1 (inverted)
37#
38# ctrl Register (base + 2) ..... outputs
39#
40# 7 6 5 4 3 2 1 0
41# . . . . . . . * Strobe ....... (pin 1), 1=low, 0=high (inverted)
42# . . . . . . * . Auto Feed .... (pin 14), 1=low, 0=high (inverted)
cliechti619e4562002-07-31 00:53:06 +000043# . . . . . * . . Initialize ... (pin 16), 1=high,0=low (true)
cliechti88641262002-07-29 02:09:04 +000044# . . . . * . . . Select ....... (pin 17), 1=low, 0=high (inverted)
45# * * * * . . . . Unused
46
47LPT1 = 0
48LPT2 = 1
49
50LPT1_base = 0x0378
51LPT2_base = 0x0278
52
cliechtia7bafcb2005-01-27 00:47:15 +000053import ctypes
54import os
55#need to patch PATH so that the DLL can be found and loaded
56os.environ['PATH'] = os.environ['PATH'] + ';' + os.path.abspath(os.path.dirname(__file__))
57#fake module, names of the functions are the same as in the old _pyparallel
58#python extension in earlier versions of this modules
59_pyparallel = ctypes.windll.simpleio
60#need to initialize giveio on WinNT based systems
61_pyparallel.init()
62
cliechti88641262002-07-29 02:09:04 +000063
64class Parallel:
65 def __init__(self, port = LPT1):
66 if port == LPT1:
67 self.dataRegAdr = LPT1_base
cliechti619e4562002-07-31 00:53:06 +000068 elif port == LPT2:
cliechti88641262002-07-29 02:09:04 +000069 self.dataRegAdr = LPT2_base
70 else:
71 raise ValueError("No such port available - expecting a number")
72 self.statusRegAdr = self.dataRegAdr + 1
73 self.ctrlRegAdr = self.dataRegAdr + 2
cliechtie7d23fe2003-05-26 19:42:23 +000074 self.ctrlReg = _pyparallel.inp(self.ctrlRegAdr)
cliechti88641262002-07-29 02:09:04 +000075
76 def setData(self, value):
77 _pyparallel.outp(self.dataRegAdr, value)
78
79 # control register output functions
cliechti619e4562002-07-31 00:53:06 +000080 def setDataStrobe(self, level):
cliechti88641262002-07-29 02:09:04 +000081 """data strobe bit"""
cliechti619e4562002-07-31 00:53:06 +000082 if level:
cliechti88641262002-07-29 02:09:04 +000083 self.ctrlReg = self.ctrlReg & ~0x01
cliechti619e4562002-07-31 00:53:06 +000084 else:
85 self.ctrlReg = self.ctrlReg | 0x01
cliechti88641262002-07-29 02:09:04 +000086 _pyparallel.outp(self.ctrlRegAdr, self.ctrlReg)
87
cliechti619e4562002-07-31 00:53:06 +000088 def setAutoFeed(self, level):
cliechti88641262002-07-29 02:09:04 +000089 """auto feed bit"""
cliechti619e4562002-07-31 00:53:06 +000090 if level:
cliechti88641262002-07-29 02:09:04 +000091 self.ctrlReg = self.ctrlReg & ~0x02
cliechti619e4562002-07-31 00:53:06 +000092 else:
93 self.ctrlReg = self.ctrlReg | 0x02
cliechti88641262002-07-29 02:09:04 +000094 _pyparallel.outp(self.ctrlRegAdr, self.ctrlReg)
95
cliechti619e4562002-07-31 00:53:06 +000096 def setInitOut(self, level):
cliechti88641262002-07-29 02:09:04 +000097 """initialize bit"""
cliechti619e4562002-07-31 00:53:06 +000098 if level:
cliechti88641262002-07-29 02:09:04 +000099 self.ctrlReg = self.ctrlReg | 0x04
cliechti619e4562002-07-31 00:53:06 +0000100 else:
101 self.ctrlReg = self.ctrlReg & ~0x04
cliechti88641262002-07-29 02:09:04 +0000102 _pyparallel.outp(self.ctrlRegAdr, self.ctrlReg)
103
cliechti619e4562002-07-31 00:53:06 +0000104 def setSelect(self, level):
cliechti88641262002-07-29 02:09:04 +0000105 """select bit"""
cliechti619e4562002-07-31 00:53:06 +0000106 if level:
cliechti88641262002-07-29 02:09:04 +0000107 self.ctrlReg = self.ctrlReg & ~0x08
cliechti619e4562002-07-31 00:53:06 +0000108 else:
109 self.ctrlReg = self.ctrlReg | 0x08
cliechti88641262002-07-29 02:09:04 +0000110 _pyparallel.outp(self.ctrlRegAdr, self.ctrlReg)
111
112 def getInError(self):
113 """Error pin"""
114 return _pyparallel.inp(self.statusRegAdr) & 0x08 and 1
115
cliechtie7d23fe2003-05-26 19:42:23 +0000116 def getInSelected(self):
cliechti88641262002-07-29 02:09:04 +0000117 """select pin"""
118 return _pyparallel.inp(self.statusRegAdr) & 0x10 and 1
119
120 def getInPaperOut(self):
121 """paper out pin"""
122 return _pyparallel.inp(self.statusRegAdr) & 0x20 and 1
123
124 def getInAcknowledge(self):
125 """Acknowledge pin"""
126 return _pyparallel.inp(self.statusRegAdr) & 0x40 and 1
127
128 def getInBusy(self):
129 """input from busy pin"""
cliechti619e4562002-07-31 00:53:06 +0000130 return not (_pyparallel.inp(self.statusRegAdr) & 0x80)