-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMicroprinter.rb
More file actions
248 lines (209 loc) · 4.9 KB
/
Microprinter.rb
File metadata and controls
248 lines (209 loc) · 4.9 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
require 'rubygems'
require 'serialport'
class Microprinter
COMMAND = 0x1B
FULLCUT = 0x69
PARTIALCUT = 0x6D
PRINT_MODE = 0x21
DOUBLEPRINT = 0x47
UNDERLINE = 0x2D
FEED_RATE = 0x33
COMMAND_IMAGE = 0x2A
COMMAND_BARCODE = 0x1D
COMMAND_BARCODE_PRINT = 0x6B
COMMAND_BARCODE_WIDTH = 0x77
COMMAND_BARCODE_HEIGHT = 0x68
COMMAND_BARCODE_TEXTPOSITION = 0x48
COMMAND_BARCODE_FONT = 0x66
BARCODE_WIDTH_NARROW = 0x02
BARCODE_WIDTH_MEDIUM = 0x03
BARCODE_WIDTH_WIDE = 0x04
BARCODE_TEXT_NONE = 0x00
BARCODE_TEXT_ABOVE = 0x01
BARCODE_TEXT_BELOW = 0x02
BARCODE_TEXT_BOTH = 0x03
BARCODE_MODE_UPCA = 0x00
BARCODE_MODE_UPCE = 0x01
BARCODE_MODE_JAN13AEN = 0x02
BARCODE_MODE_JAN8EAN = 0x03
BARCODE_MODE_CODE39 = 0x04
BARCODE_MODE_ITF = 0x05
BARCODE_MODE_CODEABAR = 0x06
BARCODE_MODE_CODE128 = 0x07
def initialize(port_str = "/dev/cu.usbserial-A1001NFW")
@port_str = port_str
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
@sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
@sp.sync = true
sleep(2) # give arduino a chance to restart
end
def close
@sp.flush
@sp.close
end
# Standard font: 42 characters per line if using 80mm paper
def set_character_width_normal
set_print_mode 0
set_linefeed_rate 55
end
# Narrow font, more characters per line
def set_character_width_narrow
set_print_mode 1
set_linefeed_rate 40
end
def set_print_mode_a
set_character_width_normal
end
def set_print_mode_b
set_character_width_narrow
end
def set_print_mode(i)
@sp.putc COMMAND
@sp.putc PRINT_MODE
@sp.putc i
@sp.flush
end
# Bold weight text
def set_double_print_on
# set_double_print(0x01)
set_font_weight_bold
end
# Maintain backwards compatibility...
def set_font_weight_bold
# set_double_print_on
set_double_print(0x01)
end
# Normal weight text
def set_double_print_off
# set_double_print(0x00)
set_font_weight_normal
end
# Maintain backwards compatibility...
def set_font_weight_normal
set_double_print(0x00)
end
def set_double_print(i)
@sp.putc COMMAND
@sp.putc DOUBLEPRINT
@sp.putc i
@sp.flush
sleep (0.01)
end
def set_underline_on
set_underline(1)
end
def set_underline_off
set_underline(0)
end
def set_underline(i) # n = 0, 1 or 2 dot underline
@sp.putc COMMAND
@sp.putc UNDERLINE
@sp.putc i
@sp.flush
sleep (0.01)
end
def print_line(text)
@sp.print("#{text}\n")
sleep (0.3)
end
# Feed this method either a string or an array of strings; each will be printed on its own line.
# TODO: should be able to feed this method a long string with line endings, and have it print properly, the same way you can feed `erb` or `haml` a set of lines in sinatra.
#def print_text(text)
# text.each do |line|
# print_line(line)
# end
#end
def print(text)
@sp.print(text)
@sp.flush
sleep (0.01)
end
def feed_and_cut # utility method.
set_linefeed_rate 55
feed
cut
end
def print_and_cut(text) # utility method. print line (or array of lines) then feed & cut
print_line(text)
feed_and_cut
end
def feed()
@sp.print("\n")
@sp.print("\n")
@sp.print("\n")
@sp.print("\n")
@sp.flush
sleep (0.01)
end
def cut()
@sp.putc COMMAND
@sp.putc FULLCUT
@sp.flush
sleep (0.01)
end
def partial_cut()
@sp.putc COMMAND
@sp.putc PARTIALCUT
@sp.flush
end
def print_barcode(barcode, barcode_mode = BARCODE_MODE_CODE39)
@sp.putc COMMAND_BARCODE
@sp.putc COMMAND_BARCODE_PRINT
@sp.putc barcode_mode
@sp.print barcode
@sp.putc 0x00
@sp.flush
sleep (0.01)
end
def set_barcode_height(height) # in dots. default = 162
height = 0 if (height.to_i < 0)
@sp.putc COMMAND_BARCODE
@sp.putc COMMAND_BARCODE_HEIGHT
@sp.putc height.to_i
@sp.flush
sleep (0.01)
end
def set_barcode_width(width)
@sp.putc COMMAND_BARCODE
@sp.putc COMMAND_BARCODE_WIDTH
@sp.putc width
@sp.flush
sleep (0.01)
end
def set_barcode_text_position(position)
position = 0 if (position.to_i < 0)
position = 3 if (position.to_i > 3)
@sp.putc COMMAND_BARCODE
@sp.putc COMMAND_BARCODE_TEXTPOSITION
@sp.putc position
@sp.flush
sleep (0.01)
end
def set_linefeed_rate(rate) #def = 22?
@sp.putc COMMAND
@sp.putc FEED_RATE
@sp.putc rate
@sp.flush
sleep (0.01)
end
def print_image_bytes(mode, data) # mode = 0, 1, 20, 21
density = 1
density = 3 if (mode > 1)
datalength = data.length / density
@sp.putc COMMAND
@sp.putc COMMAND_IMAGE
@sp.putc mode
@sp.putc datalength%256
@sp.putc datalength/256
data.each do |x|
@sp.putc x
@sp.flush
sleep (0.001)
end
@sp.flush
sleep (0.01)
end
end