Last active
August 29, 2015 14:13
-
-
Save sloev/9deef7f8cf82184a1ce2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Created on Apr 15, 2014 | |
@author: johannes | |
inspired by: | |
github.com/patriciogonzalezvivo/ofxThermalPrinter | |
and | |
Python library for the Adafruit Thermal Printer | |
todo: | |
initialize density and setup | |
print line | |
''' | |
import time | |
class SimpleThermalPrinter(): | |
''' | |
classdocs | |
''' | |
def __init__(self): | |
''' | |
Constructor | |
''' | |
baudrate = 19200 | |
self.BYTE_TIME =(12.0) / float(baudrate) | |
self.LINE_TIME=self.BYTE_TIME*10 | |
self.resumeTime = 0.0 | |
self.dotPrintTime = 0.80#vigtigt | |
self.dotFeedTime = 0.060 | |
self.printer = "/dev/usb/lp0" | |
time.sleep(1) | |
self.reset() | |
def reset(self): | |
#self.timeoutWait() | |
command=[12]#flush | |
self.writeLine(command) | |
time.sleep(0.5) | |
command=[27,64] | |
self.writeLine(command) | |
#self.timeoutSet(48*self.dotPrintTime) | |
time.sleep(0.5) | |
def writePixelLine(self,pixels):#always takes a list of 384 pixels | |
width=len(pixels) | |
if width>384:print "error:width>384" | |
data=[27,42,1,48] | |
for i in range(0,width,8): | |
byt=0 | |
for j in range(8): | |
byt += pixels[i+j] << (7 - j) | |
data.append(byt) | |
self.timeoutWait() | |
self.writeLine(data) | |
d=self.dotFeedTime | |
self.timeoutSet(d) | |
def printPixelArray(self,pixels): | |
#self.reset() | |
for i in range(0,len(pixels),384): | |
self.writePixelLine(pixels[i:i+384]) | |
print "printed an image, sleeping now" | |
#self.feed() | |
print "woke up" | |
def writeLine(self, bytes): | |
line=''.join(chr(b) for b in bytes) | |
with open(self.printer, "w") as printer: | |
printer.write(line) | |
def timeoutSet(self, x): | |
self.resumeTime = time.time() + x | |
# Waits (if necessary) for the prior task to complete. | |
def timeoutWait(self): | |
#print "waiting" | |
while (time.time() - self.resumeTime) < 0: pass#original was pass | |
def main(): | |
import sys,select | |
# | |
printer=SimpleThermalPrinter() | |
img = [] | |
bol = False | |
for i in range(384*30): | |
tmp = i%16 | |
if tmp: | |
bol = not bol | |
img.append(bol) | |
try: | |
while True: | |
time.sleep(1) | |
while sys.stdin in select.select([sys.stdin], [], [], 0)[0]: | |
c = sys.stdin.readline() | |
c=c[0:1] | |
if(c=='s'): | |
printer.printPixelArray(img) | |
# for i in range(0,len(tmptmp),384): | |
# printer.writePixelLine(tmptmp[i:i+384]) | |
# printer.feed() | |
print "done - press s or d for lines" | |
except KeyboardInterrupt: | |
print("exiting") | |
pass | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment