serialport_mtp 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: df2c9010297a3d6f8745c36a8a1497070a1c3328
4
+ data.tar.gz: 15c6168b6efe2176135dd43a36795de46583df07
5
+ SHA512:
6
+ metadata.gz: b9f377253f514833b0435cfaf332a05c266e1211ac744d8d2b91f83c59a58f1aec8a73c2ec01f1c7321d94b80263dd82f5cbe82870b60a6fc42e6d90411b59a4
7
+ data.tar.gz: c532bbc26069d612efca340e65958b13ecd5908f3a982c8abfaccee9395a9847a4ef25252c20ca2a9b2d983cb3c072b23760a667383206939d43d14f1c79f81c
checksums.yaml.gz.sig ADDED
Binary file
data.tar.gz.sig ADDED
Binary file
@@ -0,0 +1,352 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: serialport_mtp.rb
4
+
5
+ # original code at https://github.com/lazyatom/a2_printer
6
+
7
+ # This gem can be used with the mini thermal printer, connected via
8
+ # the TX+RX pins on the Raspberry Pi
9
+
10
+ require 'serialport'
11
+
12
+
13
+ class SerialPortMTP
14
+
15
+ def initialize(port: "/dev/ttyAMA0", baud_rate: 19200, heat_time: 150)
16
+
17
+ #params for serial port
18
+ @port, @baud_rate, @data_bits, @stop_bits, @parity = port, baud_rate,
19
+ 8, 1, SerialPort::NONE
20
+
21
+ @heat_time = heat_time
22
+ @print_mode = 0
23
+
24
+ end
25
+
26
+ def start()
27
+
28
+ @serial = SerialPort.new(@port, @baud_rate, @data_bits, @stop_bits, @parity)
29
+ @serial.sync = true
30
+ reset()
31
+
32
+ heat_interval = 50 # 2 is default from page 23 of datasheet. Controls speed of printing and darkness
33
+ print_density = 15 # Not sure what the default is. Testing shows the max helps darken text. From page 23.
34
+ print_break_time = 15 # Not sure what the default is. Testing shows the max helps darken text. From page 23.
35
+
36
+ write_bytes(27, 55)
37
+ write_bytes(7) # Default 64 dots = 8*('7'+1)
38
+ write_bytes(@heat_time) # Default 80 or 800us
39
+ write_bytes(heat_interval) # Default 2 or 20us
40
+
41
+ # Modify the print density and timeout
42
+ write_bytes(18, 35)
43
+ print_setting = (print_density << 4) | print_break_time
44
+ write_bytes(print_setting) # Combination of print_density and print_break_time
45
+ end
46
+
47
+ def test_page
48
+ write_bytes(18, 84)
49
+ end
50
+
51
+ # reset printer
52
+ def reset
53
+ write_bytes(27, 64)
54
+ end
55
+
56
+ # reset formatting
57
+ def set_default
58
+ online
59
+ normal
60
+ underline_off
61
+ justify(:left)
62
+ set_line_height(32)
63
+ set_barcode_height(50)
64
+ end
65
+
66
+ # Feeds by the specified number of lines
67
+ def feed(lines=1)
68
+ # The datasheet claims sending bytes 27, 100, <x> will work
69
+ # but it feeds much much more.
70
+ lines.times { write(10) }
71
+ end
72
+
73
+ # Feeds by the specified number of rows of pixels
74
+ def feed_rows(rows)
75
+ write_bytes(27, 74, rows)
76
+ end
77
+
78
+ def flush
79
+ write_bytes(12)
80
+ end
81
+
82
+ def test_page
83
+ write_bytes(18, 84)
84
+ end
85
+
86
+ def print(string)
87
+ string.bytes { |b| write(b) }
88
+ end
89
+
90
+ def println(string)
91
+ print(string + "\n")
92
+ end
93
+
94
+ def write(c)
95
+ return if (c == 0x13)
96
+ write_bytes(c)
97
+ end
98
+
99
+ def write_bytes(*bytes)
100
+ bytes.each { |b| @serial.putc(b) }
101
+ end
102
+
103
+ # Character commands
104
+
105
+ INVERSE_MASK = (1 << 1)
106
+ UPDOWN_MASK = (1 << 2)
107
+ BOLD_MASK = (1 << 3)
108
+ DOUBLE_HEIGHT_MASK = (1 << 4)
109
+ DOUBLE_WIDTH_MASK = (1 << 5)
110
+ STRIKE_MASK = (1 << 6)
111
+
112
+ def set_print_mode(mask)
113
+ @print_mode |= mask;
114
+ write_print_mode
115
+ end
116
+
117
+ def unset_print_mode(mask)
118
+ @print_mode &= ~mask;
119
+ write_print_mode
120
+ end
121
+
122
+ def write_print_mode
123
+ write_bytes(27, 33, @print_mode)
124
+ end
125
+
126
+ # This will reset bold, inverse, strikeout, upside down and font size
127
+ # It does not reset underline, justification or line height
128
+ def normal
129
+ @print_mode = 0
130
+ write_print_mode
131
+ end
132
+
133
+ def inverse_on
134
+ set_print_mode(INVERSE_MASK)
135
+ end
136
+
137
+ def inverse_off
138
+ unset_print_mode(INVERSE_MASK)
139
+ end
140
+
141
+ def upside_down_on
142
+ set_print_mode(UPDOWN_MASK);
143
+ end
144
+
145
+ def upside_down_off
146
+ unset_print_mode(UPDOWN_MASK);
147
+ end
148
+
149
+ def double_height_on
150
+ set_print_mode(DOUBLE_HEIGHT_MASK)
151
+ end
152
+
153
+ def double_height_off
154
+ unset_print_mode(DOUBLE_HEIGHT_MASK)
155
+ end
156
+
157
+ def double_width_on
158
+ set_print_mode(DOUBLE_WIDTH_MASK)
159
+ end
160
+
161
+ def double_width_off
162
+ unset_print_mode(DOUBLE_WIDTH_MASK)
163
+ end
164
+
165
+ def strike_on
166
+ set_print_mode(STRIKE_MASK)
167
+ end
168
+
169
+ def strike_off
170
+ unset_print_mode(STRIKE_MASK)
171
+ end
172
+
173
+ def bold_on
174
+ set_print_mode(BOLD_MASK)
175
+ end
176
+
177
+ def bold_off
178
+ unset_print_mode(BOLD_MASK)
179
+ end
180
+
181
+ def set_size(size)
182
+ byte = case size
183
+ when :small
184
+ 0
185
+ when :medium
186
+ 10
187
+ when :large
188
+ 25
189
+ end
190
+
191
+ write_bytes(29, 33, byte, 10)
192
+ end
193
+
194
+ # Underlines of different weights can be produced:
195
+ # 0 - no underline
196
+ # 1 - normal underline
197
+ # 2 - thick underline
198
+ def underline_on(weight=1)
199
+ write_bytes(27, 45, weight)
200
+ end
201
+
202
+ def underline_off
203
+ underline_on(0)
204
+ end
205
+
206
+ def justify(position)
207
+ byte = case position
208
+ when :left
209
+ 0
210
+ when :center
211
+ 1
212
+ when :right
213
+ 2
214
+ end
215
+
216
+ write_bytes(0x1B, 0x61, byte)
217
+ end
218
+
219
+ # Bitmaps
220
+
221
+ class Bitmap
222
+ attr_reader :width, :height
223
+
224
+ def initialize(width_or_source, height=nil, source=nil)
225
+ if height.nil? && source.nil?
226
+ set_source(width_or_source)
227
+ extract_width_and_height_from_data
228
+ else
229
+ set_source(source)
230
+ @width = width_or_source
231
+ @height = height
232
+ end
233
+ end
234
+
235
+ def each_block
236
+ row_start = 0
237
+ width_in_bytes = width / 8
238
+ while row_start < height do
239
+ chunk_height = ((height - row_start) > 255) ? 255 : (height - row_start)
240
+ bytes = (0...(width_in_bytes * chunk_height)).map { @data.getbyte }
241
+ yield width_in_bytes, chunk_height, bytes
242
+ row_start += 255
243
+ end
244
+ end
245
+
246
+ private
247
+
248
+ def set_source(source)
249
+ if source.respond_to?(:getbyte)
250
+ @data = source
251
+ else
252
+ @data = StringIO.new(source.map(&:chr).join)
253
+ end
254
+ end
255
+
256
+ def extract_width_and_height_from_data
257
+ tmp = @data.getbyte
258
+ @width = (@data.getbyte << 8) + tmp
259
+ tmp = @data.getbyte
260
+ @height = (@data.getbyte << 8) + tmp
261
+ end
262
+ end
263
+
264
+ def print_bitmap(*args)
265
+ bitmap = Bitmap.new(*args)
266
+ return if (bitmap.width > 384) # maximum width of the printer
267
+ bitmap.each_block do |w, h, bytes|
268
+ write_bytes(18, 42)
269
+ write_bytes(h, w)
270
+ write_bytes(*bytes)
271
+ end
272
+ end
273
+
274
+ # def print_bitmap(stream)
275
+ # tmp = stream.getbyte
276
+ # width = (stream.getbyte << 8) + tmp
277
+ #
278
+ # tmp = stream.getbyte
279
+ # height = (stream.getbyte << 8) + tmp
280
+ #
281
+ # print_bitmap(width, height, stream)
282
+ # end
283
+
284
+ # Barcodes
285
+
286
+ def set_barcode_height(val)
287
+ # default is 50
288
+ write_bytes(29, 104, val)
289
+ end
290
+
291
+ UPC_A = 0
292
+ UPC_E = 1
293
+ EAN13 = 2
294
+ EAN8 = 3
295
+ CODE39 = 4
296
+ I25 = 5
297
+ CODEBAR = 6
298
+ CODE93 = 7
299
+ CODE128 = 8
300
+ CODE11 = 9
301
+ MSI = 10
302
+
303
+ def print_barcode(text, type)
304
+ write_bytes(29, 107, type) # set the type first
305
+ text.bytes { |b| write(b) }
306
+ write(0) # Terminator
307
+ end
308
+
309
+ # Take the printer offline. Print commands sent after this will be
310
+ # ignored until `online` is called
311
+ def offline
312
+ write_bytes(27, 61, 0)
313
+ end
314
+
315
+ # Take the printer back online. Subsequent print commands will be
316
+ # obeyed.
317
+ def online
318
+ write_bytes(27, 61, 1)
319
+ end
320
+
321
+ # Put the printer into a low-energy state immediately
322
+ def sleep
323
+ sleep_after(0)
324
+ end
325
+
326
+ # Put the printer into a low-energy state after the given number
327
+ # of seconds
328
+ def sleep_after(seconds)
329
+ write_bytes(27, 56, seconds)
330
+ end
331
+
332
+ # Wake the printer from a low-energy state. This command will wait
333
+ # for 50ms (as directed by the datasheet) before allowing further
334
+ # commands to be send.
335
+ def wake
336
+ write_bytes(255)
337
+ # delay(50) # ?
338
+ end
339
+
340
+ # ==== not working? ====
341
+ def tab
342
+ write(9)
343
+ end
344
+
345
+ def set_char_spacing(spacing)
346
+ write_bytes(27, 32, 0, 10)
347
+ end
348
+
349
+ def set_line_height(val=32)
350
+ write_bytes(27, 51, val) # default is 32
351
+ end
352
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serialport_mtp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - James Robertson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
14
+ YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
+ 8ixkARkWAmV1MB4XDTE1MDgyNzIwMTY0MloXDTE2MDgyNjIwMTY0MlowSDESMBAG
16
+ A1UEAwwJZ2VtbW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hKMaAQZWgjddCbOsHbof
32
+ 4bxShMp7fn3hMQ==
33
+ -----END CERTIFICATE-----
34
+ date: 2015-08-27 00:00:00.000000000 Z
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: serialport
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.3'
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 1.3.1
46
+ type: :runtime
47
+ prerelease: false
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - "~>"
51
+ - !ruby/object:Gem::Version
52
+ version: '1.3'
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 1.3.1
56
+ description:
57
+ email: james@r0bertson.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/serialport_mtp.rb
63
+ homepage: https://github.com/jrobertson/serialport_mtp
64
+ licenses:
65
+ - MIT
66
+ metadata: {}
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.8
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: This gem can be used with the mini thermal printer, connected via the TX+RX
87
+ pins on the Raspberry Pi
88
+ test_files: []
metadata.gz.sig ADDED
Binary file