-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththermal.py
87 lines (68 loc) · 1.91 KB
/
thermal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Word Car automotive narration system
# Copyright (C) 2017, Ross Goodwin#
# Full license at: https://github.com/rossgoodwin/wordcar
import subprocess
import serial
import string
import os
import time
ser = serial.Serial('/dev/ttyUSB1')
ser.write(b'{LP}')
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def split_to_lines(text, ll=80):
if not text:
return ""
text_list = filter(
lambda x: x in string.printable,
list(text.strip())
)
char_count = 0
ws_indexes = list()
cur_line = list()
all_lines = list()
i = 0
start_ix = 0
while i < len(text_list):
c = text_list[i]
# print c
if c in [' ', '\t']:
ws_indexes.append(i)
cur_line.append(c)
char_count += 1
if c == '\n':
cur_line = text_list[start_ix:i]
start_ix = i
all_lines.append(''.join(cur_line))
cur_line = list()
char_count = 0
elif char_count >= ll:
tgt_ix = ws_indexes.pop()
cur_line = text_list[start_ix:tgt_ix]
i = tgt_ix
start_ix = i
all_lines.append(''.join(cur_line))
cur_line = list()
char_count = 0
i += 1
all_lines.append(''.join(cur_line))
return map(lambda x: x.strip(), all_lines)
def line_print_mode():
ser.write(b'{LP}')
def feed_paper():
ser.write(b'\n'*4)
def line_break():
ser.write(b'\n')
def basic_print(text):
ser.write(b'\n')
ser.write(bytes(text))
time.sleep(0.5)
def thermal_print(text):
font_switch = chr(27) + b'!4'
for l in split_to_lines(text, ll=38):
ser.write(font_switch)
ser.write(b' '+bytes(l))
ser.write(b'\n')
time.sleep(0.5)