You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
104 lines
2.9 KiB
Python
104 lines
2.9 KiB
Python
import argparse
|
|
import sys
|
|
import os
|
|
import six
|
|
from random import randint
|
|
from PIL import Image, ImageOps
|
|
import curses
|
|
import sys, termios, tty, os, time
|
|
|
|
|
|
class Printer():
|
|
ESC = b'\x1B'
|
|
CHANGE_EMULATION = ESC+b'\x7B'+b'\x65'
|
|
LINE_FEED = b'\n'
|
|
CARIDGE_RET = b'\x0D'
|
|
CHARWIDTH = 78
|
|
strokes = 0
|
|
debug = True
|
|
target = "/dev/usb/lp0"
|
|
if debug:
|
|
target = "/dev/tty"
|
|
|
|
def printchar(self,char):
|
|
with open(self.target, 'wb') as printer:
|
|
printer.write(self.ESC+b"\x7B"+b"\x41")
|
|
char=self.strokes*" "+char
|
|
printer.write(bytes(str(char), 'utf-8'))
|
|
printer.write(self.ESC+b"\x25"+b"\x46"+b"\x0F"+b"\x00"+b"\x04"+b"\x08"+b"\x00")
|
|
self.strokes += 1
|
|
if(self.strokes>self.CHARWIDTH):
|
|
printer.write(self.LINE_FEED)
|
|
self.strokes = 0
|
|
|
|
def printstring(self,string):
|
|
with open(self.target, 'wb') as printer:
|
|
printer.write(self.ESC+b"\x7B"+b"\x41")
|
|
printer.write(bytes(str(string), 'utf-8'))
|
|
printer.write(self.ESC+b"\x25"+b"\x46"+b"\x0F"+b"\x00"+b"\x04"+b"\x08"+b"\x00")
|
|
self.newline()
|
|
|
|
|
|
def newline(self):
|
|
global strokes
|
|
with open(self.target, 'wb') as printer:
|
|
printer.write(self.LINE_FEED)
|
|
self.strokes = 0
|
|
|
|
|
|
|
|
class Terminal():
|
|
lineinput = ""
|
|
commandMode = False
|
|
writeloop = True
|
|
|
|
def __init__(self,printer):
|
|
self.printer = printer
|
|
|
|
def waitforinput(self):
|
|
term.writeloop = True
|
|
while self.writeloop:
|
|
char = self.getch()
|
|
self.lineinput+=char
|
|
print(self.lineinput)
|
|
if (ord(char) == 27):
|
|
print("exit publication :-(")
|
|
exit(0)
|
|
if (ord(char) == 13):
|
|
self.printer.newline()
|
|
self.checkforcommand(self.lineinput)
|
|
self.lineinput = ""
|
|
else:
|
|
print(char)
|
|
self.printer.printchar(char)
|
|
|
|
def getch(self):
|
|
fd = sys.stdin.fileno()
|
|
old_settings = termios.tcgetattr(fd)
|
|
try:
|
|
tty.setraw(sys.stdin.fileno())
|
|
ch = sys.stdin.read(1)
|
|
|
|
finally:
|
|
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
|
|
return ch
|
|
|
|
def checkforcommand(self, command):
|
|
print("received command:"+command)
|
|
if command == "help":
|
|
|
|
#if the command is valid we want to execute the command and continue the story
|
|
#self.writeloop = False
|
|
|
|
|
|
printer = Printer()
|
|
term = Terminal(printer)
|
|
printer.printstring("W E L C O M E !")
|
|
printer.printstring("---------------")
|
|
printer.printstring("POETIC SOFTWARE")
|
|
printer.printstring("---------------")
|
|
printer.printstring("type help for help")
|
|
term.waitforinput()
|
|
printer.printstring("write a command")
|
|
term.waitforinput()
|