1
0
mirror of https://github.com/rzzzwilson/pymlac.git synced 2025-06-10 09:32:41 +00:00

New amalgamated PtrPtp device

This commit is contained in:
Ross Wilson 2016-01-19 13:19:17 +07:00
parent dd21fb6167
commit 2bcbab9028
2 changed files with 59 additions and 51 deletions

View File

@ -55,7 +55,6 @@ class PtrPtp(object):
self.device_use = None
self.device_motor_on = False
self.device_open_file = None
self.device_open_file = None
self.device_filename = None
self.device_ready = False
@ -93,15 +92,10 @@ class PtrPtp(object):
if self.device_use == self.InUsePTP:
raise RuntimeError("ptr_dismount: Can't dismount PTR file, being used as PTP")
self.device_use = None
self.device_motor_on = False
self.device_ready = False
if self.device_filename:
self.device_open_file.close()
self.device_open_file = None
self.device_filename = None
self.ptr_at_eof = True
self.ptr_value = self.PtrEOF
self.reset()
def start(self):
"""Turn papertape reader motor on."""
@ -196,14 +190,10 @@ class PtrPtp(object):
if self.device_use == self.InUsePTR:
raise RuntimeError("ptp_dismount: Can't dismount PTP file, being used as PTR")
self.device_motor_on = False
self.device_ready = False
if self.ptp_filename:
self.device_open_file.close()
self.device_open_file = None
self.ptp_filename = None
self.ptp_at_eof = True
self.ptp_value = self.PtrEOF
self.reset()
def punch(self, value):
"""Write byte to papertape file.
@ -216,7 +206,7 @@ class PtrPtp(object):
self.device_ready = False
self.device_cycle_count = self.PtpNotReadyCycles
self.device_open_file.write(struct.pack('1B', value))
self.device_open_file.write(value)
def ptp_tick(self, cycles):
"""Called to push PTP state along.

View File

@ -15,14 +15,22 @@ where <options> is zero or more of:
# 2. writing test *.ptp files which we mount and read
import Ptr
import Ptp
import os
import PtrPtp
# module global constants
PtrFilename = '_#PTR#_.ptp'
PtpFilename = '_#PTP#_.ptp'
Logfile = 'test_PTR_PTP.log'
def logger(*args):
msg = ' '.join(args)
with open(Logfile, 'ab') as fd:
fd.write(msg + '\n')
def read_no_tape(ptr):
"""Read from device with no tape mounted."""
@ -31,22 +39,23 @@ def read_no_tape(ptr):
byte = ptr.read()
if byte != 0377:
print('Error')
ptr.tick(1000000) # wait a long time
ptr.ptr_tick(1000000) # wait a long time
byte = ptr.read()
if byte != 0377:
print('Error')
# turn device on, still no tape
ptr.start()
ptr.tick(1000000) # wait a long time
ptr.ptr_tick(1000000) # wait a long time
byte = ptr.read()
if byte != 0377:
print('Error')
ptr.tick(1000000) # wait a long time
ptr.ptr_tick(1000000) # wait a long time
byte = ptr.read()
if byte != 0377:
print('Error')
ptr.stop()
ptr.ptr_dismount()
def create_papertape(filename):
"""Create a PTP file."""
@ -67,41 +76,38 @@ def create_papertape(filename):
def create_papertape_ptp(ptp, filename):
"""Create a PTP file using the Ptp device."""
ptp.mount(filename)
ptp.start()
# ptp.reset()
ptp.ptp_mount(filename)
# leader
for _ in range(128):
while not ptp.ready():
ptp.tick(1)
ptp.write(chr(0))
ptp.ptp_tick(1)
ptp.punch(chr(0))
while ptp.ready():
ptp.tick(1)
ptp.ptp_tick(1)
# body
for v in range(1, 256):
while not ptp.ready():
ptp.tick(1)
ptp.write(chr(v))
while ptp.ready():
ptp.tick(1)
ptp.ptp_tick(1)
ptp.punch(chr(v))
# trailer
for _ in range(128):
while not ptp.ready():
ptp.tick(1)
ptp.write(chr(0))
while ptp.ready():
ptp.tick(1)
ptp.ptp_tick(1)
ptp.punch(chr(0))
ptp.stop()
ptp.dismount()
# ptp.stop()
ptp.ptp_dismount()
def read_tape(ptr, filename):
"""Create tape and read it."""
# now mount and read tape
ptr.mount(filename)
# ptr.reset()
ptr.ptr_mount(filename)
ptr.start()
# read leader
@ -109,10 +115,10 @@ def read_tape(ptr, filename):
count = 0
while True:
while not ptr.ready():
ptr.tick(1)
ptr.ptr_tick(1)
byte = ptr.read()
while ptr.ready():
ptr.tick(1)
while ptr.ready():
ptr.ptr_tick(1)
if byte != 0:
break
count += 1
@ -124,10 +130,10 @@ def read_tape(ptr, filename):
count = 1
while True:
while not ptr.ready():
ptr.tick(1)
ptr.ptr_tick(1)
byte = ptr.read()
while ptr.ready():
ptr.tick(1)
while ptr.ready():
ptr.ptr_tick(1)
if byte == 0:
break
count += 1
@ -139,29 +145,41 @@ def read_tape(ptr, filename):
count = 1
while True:
while not ptr.ready():
ptr.tick(1)
ptr.ptr_tick(1)
byte = ptr.read()
if byte != 0:
break
count += 1
while ptr.ready():
ptr.tick(1)
while ptr.ready():
ptr.ptr_tick(1)
ptr.stop()
print('%d bytes of trailer' % count)
ptr.ptr_dismount()
def main():
"""Test the papertape reader."""
ptr = Ptr.Ptr()
ptp = Ptp.Ptp()
try:
os.remove(Logfile)
except OSError:
pass # ignore 'file not there'
read_no_tape(ptr)
ptrptp = PtrPtp.PtrPtp()
logger('created reader/punch device')
read_no_tape(ptrptp)
logger('After read_no_tape')
create_papertape(PtrFilename)
read_tape(ptr, PtrFilename)
create_papertape_ptp(ptp, PtpFilename)
read_tape(ptr, PtpFilename)
logger('After create_papertape')
read_tape(ptrptp, PtrFilename)
logger('After read_tape')
create_papertape_ptp(ptrptp, PtpFilename)
logger('After create_papertape_ptp')
read_tape(ptrptp, PtpFilename)
logger('After read_tape')