Class: A2Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/a2_printer.rb

Defined Under Namespace

Classes: Bitmap

Constant Summary collapse

INVERSE_MASK =

Character commands

(1 << 1)
UPDOWN_MASK =
(1 << 2)
BOLD_MASK =
(1 << 3)
DOUBLE_HEIGHT_MASK =
(1 << 4)
DOUBLE_WIDTH_MASK =
(1 << 5)
STRIKE_MASK =
(1 << 6)
UPC_A =
0
UPC_E =
1
EAN13 =
2
EAN8 =
3
CODE39 =
4
I25 =
5
CODEBAR =
6
CODE93 =
7
CODE128 =
8
CODE11 =
9
MSI =
10

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ A2Printer

Returns a new instance of A2Printer.



4
5
6
7
# File 'lib/a2_printer.rb', line 4

def initialize(connection)
  @connection = connection
  @print_mode = 0
end

Instance Method Details

#begin(heat_time = 150) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/a2_printer.rb', line 13

def begin(heat_time=150)
  reset()

  heat_interval = 50 # 2 is default from page 23 of datasheet. Controls speed of printing and darkness
  print_density = 15 # Not sure what the default is. Testing shows the max helps darken text. From page 23.
  print_break_time = 15 # Not sure what the default is. Testing shows the max helps darken text. From page 23.

  write_bytes(27, 55)
  write_bytes(7) # Default 64 dots = 8*('7'+1)
  write_bytes(heat_time) # Default 80 or 800us
  write_bytes(heat_interval) # Default 2 or 20us

  # Modify the print density and timeout
  write_bytes(18, 35)
  print_setting = (print_density<<4) | print_break_time
  write_bytes(print_setting) # Combination of print_density and print_break_time
end

#bold_offObject



157
158
159
# File 'lib/a2_printer.rb', line 157

def bold_off
  unset_print_mode(BOLD_MASK)
end

#bold_onObject



153
154
155
# File 'lib/a2_printer.rb', line 153

def bold_on
  set_print_mode(BOLD_MASK)
end

#double_height_offObject



133
134
135
# File 'lib/a2_printer.rb', line 133

def double_height_off
  unset_print_mode(DOUBLE_HEIGHT_MASK)
end

#double_height_onObject



129
130
131
# File 'lib/a2_printer.rb', line 129

def double_height_on
  set_print_mode(DOUBLE_HEIGHT_MASK)
end

#double_width_offObject



141
142
143
# File 'lib/a2_printer.rb', line 141

def double_width_off
  unset_print_mode(DOUBLE_WIDTH_MASK)
end

#double_width_onObject



137
138
139
# File 'lib/a2_printer.rb', line 137

def double_width_on
  set_print_mode(DOUBLE_WIDTH_MASK)
end

#feed(lines = 1) ⇒ Object

Feeds by the specified number of lines



47
48
49
50
51
# File 'lib/a2_printer.rb', line 47

def feed(lines=1)
  # The datasheet claims sending bytes 27, 100, <x> will work
  # but it feeds much much more.
  lines.times { write(10) }
end

#feed_rows(rows) ⇒ Object

Feeds by the specified number of rows of pixels



54
55
56
# File 'lib/a2_printer.rb', line 54

def feed_rows(rows)
  write_bytes(27, 74, rows)
end

#flushObject



58
59
60
# File 'lib/a2_printer.rb', line 58

def flush
  write_bytes(12)
end

#inverse_offObject



117
118
119
# File 'lib/a2_printer.rb', line 117

def inverse_off
  unset_print_mode(INVERSE_MASK)
end

#inverse_onObject



113
114
115
# File 'lib/a2_printer.rb', line 113

def inverse_on
  set_print_mode(INVERSE_MASK)
end

#justify(position) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/a2_printer.rb', line 186

def justify(position)
  byte = case position
  when :left
    0
  when :center
    1
  when :right
    2
  end

  write_bytes(0x1B, 0x61, byte)
end

#normalObject

This will reset bold, inverse, strikeout, upside down and font size It does not reset underline, justification or line height



108
109
110
111
# File 'lib/a2_printer.rb', line 108

def normal
  @print_mode = 0
  write_print_mode
end

#offlineObject

Take the printer offline. Print commands sent after this will be ignored until ‘online` is called



291
292
293
# File 'lib/a2_printer.rb', line 291

def offline
  write_bytes(27, 61, 0)
end

#onlineObject

Take the printer back online. Subsequent print commands will be obeyed.



297
298
299
# File 'lib/a2_printer.rb', line 297

def online
  write_bytes(27, 61, 1)
end


66
67
68
# File 'lib/a2_printer.rb', line 66

def print(string)
  string.bytes { |b| write(b) }
end


283
284
285
286
287
# File 'lib/a2_printer.rb', line 283

def print_barcode(text, type)
  write_bytes(29, 107, type) # set the type first
  text.bytes { |b| write(b) }
  write(0) # Terminator
end


244
245
246
247
248
249
250
251
252
# File 'lib/a2_printer.rb', line 244

def print_bitmap(*args)
  bitmap = Bitmap.new(*args)
  return if (bitmap.width > 384) # maximum width of the printer
  bitmap.each_block do |w, h, bytes|
    write_bytes(18, 42)
    write_bytes(h, w)
    write_bytes(*bytes)
  end
end

#println(string) ⇒ Object



70
71
72
# File 'lib/a2_printer.rb', line 70

def println(string)
  print(string + "\n")
end

#resetObject

reset printer



32
33
34
# File 'lib/a2_printer.rb', line 32

def reset
  write_bytes(27, 64)
end

#set_barcode_height(val) ⇒ Object

Barcodes



266
267
268
269
# File 'lib/a2_printer.rb', line 266

def set_barcode_height(val)
  # default is 50
  write_bytes(29, 104, val)
end

#set_char_spacing(spacing) ⇒ Object



325
326
327
# File 'lib/a2_printer.rb', line 325

def set_char_spacing(spacing)
  write_bytes(27, 32, 0, 10)
end

#set_defaultObject

reset formatting



37
38
39
40
41
42
43
44
# File 'lib/a2_printer.rb', line 37

def set_default
  online
  normal
  underline_off
  justify(:left)
  set_line_height(32)
  set_barcode_height(50)
end

#set_line_height(val = 32) ⇒ Object



329
330
331
# File 'lib/a2_printer.rb', line 329

def set_line_height(val=32)
  write_bytes(27, 51, val) # default is 32
end

#set_print_mode(mask) ⇒ Object



92
93
94
95
# File 'lib/a2_printer.rb', line 92

def set_print_mode(mask)
  @print_mode |= mask;
  write_print_mode
end

#set_size(size) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/a2_printer.rb', line 161

def set_size(size)
  byte = case size
  when :small
    0
  when :medium
    10
  when :large
    25
  end

  write_bytes(29, 33, byte, 10)
end

#sleepObject

Put the printer into a low-energy state immediately



302
303
304
# File 'lib/a2_printer.rb', line 302

def sleep
  sleep_after(0)
end

#sleep_after(seconds) ⇒ Object

Put the printer into a low-energy state after the given number of seconds



308
309
310
# File 'lib/a2_printer.rb', line 308

def sleep_after(seconds)
  write_bytes(27, 56, seconds)
end

#strike_offObject



149
150
151
# File 'lib/a2_printer.rb', line 149

def strike_off
  unset_print_mode(STRIKE_MASK)
end

#strike_onObject



145
146
147
# File 'lib/a2_printer.rb', line 145

def strike_on
  set_print_mode(STRIKE_MASK)
end

#tabObject

not working? ====



321
322
323
# File 'lib/a2_printer.rb', line 321

def tab
  write(9)
end

#test_pageObject



9
10
11
# File 'lib/a2_printer.rb', line 9

def test_page
  write_bytes(18, 84)
end

#underline_offObject



182
183
184
# File 'lib/a2_printer.rb', line 182

def underline_off
  underline_on(0)
end

#underline_on(weight = 1) ⇒ Object

Underlines of different weights can be produced: 0 - no underline 1 - normal underline 2 - thick underline



178
179
180
# File 'lib/a2_printer.rb', line 178

def underline_on(weight=1)
  write_bytes(27, 45, weight)
end

#unset_print_mode(mask) ⇒ Object



97
98
99
100
# File 'lib/a2_printer.rb', line 97

def unset_print_mode(mask)
  @print_mode &= ~mask;
  write_print_mode
end

#upside_down_offObject



125
126
127
# File 'lib/a2_printer.rb', line 125

def upside_down_off
  unset_print_mode(UPDOWN_MASK);
end

#upside_down_onObject



121
122
123
# File 'lib/a2_printer.rb', line 121

def upside_down_on
  set_print_mode(UPDOWN_MASK);
end

#wakeObject

Wake the printer from a low-energy state. This command will wait for 50ms (as directed by the datasheet) before allowing further commands to be send.



315
316
317
318
# File 'lib/a2_printer.rb', line 315

def wake
  write_bytes(255)
  # delay(50) # ?
end

#write(c) ⇒ Object



74
75
76
77
# File 'lib/a2_printer.rb', line 74

def write(c)
  return if (c == 0x13)
  write_bytes(c)
end

#write_bytes(*bytes) ⇒ Object



79
80
81
# File 'lib/a2_printer.rb', line 79

def write_bytes(*bytes)
  bytes.each { |b| @connection.putc(b) }
end

#write_print_modeObject



102
103
104
# File 'lib/a2_printer.rb', line 102

def write_print_mode
  write_bytes(27, 33, @print_mode)
end