brotherax_teletype/tools/printfile.py

88 lines
2.6 KiB
Python
Raw Normal View History

2024-10-08 11:23:51 +00:00
#! /usr/bin/env python3
import serial
import codecs
import argparse
serialdevice='/dev/ttyUSB0'
2024-10-18 09:46:09 +00:00
chars=" qwertzuiopü+asdfghjklöä#yxcvbnm,.-QWERTZUIOPÜ*ASDFGHJKLÖÄ'´`YXCVBNM;:_1234567890ß!\"§$%&/()=?°²³|"
2024-10-08 11:23:51 +00:00
def main():
parser = argparse.ArgumentParser(description='Send Textfile over serial')
parser.add_argument('filename', help="file to print")
parser.add_argument('-d', '--device', nargs='?', help="serial device name")
parser.add_argument('-l', '--length', nargs='?', type=int, help="maximum line length")
2024-10-18 09:46:09 +00:00
parser.add_argument('-w', '--wait', nargs='?', type=int, help="wait after x characters")
2024-10-08 11:23:51 +00:00
parser.add_argument('-x','--dryrun', action="store_true", help="do not send anything to printer")
args = parser.parse_args()
charsperline=65
if args.length is not None:
charsperline=int(args.length)
print("args.length: "+str(args.length))
print("Checking for maximum line length: "+str(charsperline))
2024-10-18 09:46:09 +00:00
waitafter=0
if args.wait is not None:
waitafter=int(args.wait)
print("Waiting after every "+str(charsperline)+" characters sent")
2024-10-08 11:23:51 +00:00
filename=args.filename
if args.device is not None:
serialdevice=args.device
lines=[]
with codecs.open(filename, encoding='utf-8') as file:
lines=file.readlines()
lines=[x.rstrip() for x in lines] # remove whitespaces and linebreaks
2024-10-18 09:46:09 +00:00
lines=[x.replace("´", "´ ") for x in lines] # add space to chars where printer is not advancing
lines=[x.replace("`", "` ") for x in lines] # add space to chars where printer is not advancing
2024-10-08 11:23:51 +00:00
for iline,line in enumerate(lines):
2024-10-08 12:43:13 +00:00
if not set(line).issubset(chars+'\\'):
2024-10-08 11:23:51 +00:00
print(str(iline)+":"+str(line))
print("Contains non printable characters")
exit()
if len(line)>charsperline:
print(str(iline)+":"+str(line))
print("Line is too long." +str(len(line))+">"+str(charsperline))
exit()
if (args.dryrun):
2024-10-18 09:46:09 +00:00
print("Dry Run enabled")
2024-10-08 11:23:51 +00:00
with serial.Serial(serialdevice, 9600, timeout=1) as ser:
2024-10-18 09:46:09 +00:00
charssent=0
for iline,line in enumerate(lines):
if waitafter>0 and charssent>waitafter:
input("Press Enter to continue...")
charssent=0
print(str(iline)+"/"+str(len(lines))+" ("+str(charssent)+")"+":"+str(line))
charssent+=len(line)
if (not args.dryrun):
ser.write(line.encode())
ser.write(b'\n')
2024-10-08 11:23:51 +00:00
if __name__ == "__main__":
main()