Class: A2Printer
- Inherits:
-
Object
- Object
- A2Printer
- 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
- #begin(heat_time = 150) ⇒ Object
- #bold_off ⇒ Object
- #bold_on ⇒ Object
- #double_height_off ⇒ Object
- #double_height_on ⇒ Object
- #double_width_off ⇒ Object
- #double_width_on ⇒ Object
-
#feed(lines = 1) ⇒ Object
Feeds by the specified number of lines.
-
#feed_rows(rows) ⇒ Object
Feeds by the specified number of rows of pixels.
- #flush ⇒ Object
-
#initialize(connection) ⇒ A2Printer
constructor
A new instance of A2Printer.
- #inverse_off ⇒ Object
- #inverse_on ⇒ Object
- #justify(position) ⇒ Object
-
#normal ⇒ Object
This will reset bold, inverse, strikeout, upside down and font size It does not reset underline, justification or line height.
-
#offline ⇒ Object
Take the printer offline.
-
#online ⇒ Object
Take the printer back online.
- #print(string) ⇒ Object
- #print_barcode(text, type) ⇒ Object
- #print_bitmap(*args) ⇒ Object
- #println(string) ⇒ Object
-
#reset ⇒ Object
reset printer.
-
#set_barcode_height(val) ⇒ Object
Barcodes.
- #set_char_spacing(spacing) ⇒ Object
-
#set_default ⇒ Object
reset formatting.
- #set_line_height(val = 32) ⇒ Object
- #set_print_mode(mask) ⇒ Object
- #set_size(size) ⇒ Object
-
#sleep ⇒ Object
Put the printer into a low-energy state immediately.
-
#sleep_after(seconds) ⇒ Object
Put the printer into a low-energy state after the given number of seconds.
- #strike_off ⇒ Object
- #strike_on ⇒ Object
-
#tab ⇒ Object
not working? ====.
- #test_page ⇒ Object
- #underline_off ⇒ Object
-
#underline_on(weight = 1) ⇒ Object
Underlines of different weights can be produced: 0 - no underline 1 - normal underline 2 - thick underline.
- #unset_print_mode(mask) ⇒ Object
- #upside_down_off ⇒ Object
- #upside_down_on ⇒ Object
-
#wake ⇒ Object
Wake the printer from a low-energy state.
- #write(c) ⇒ Object
- #write_bytes(*bytes) ⇒ Object
- #write_print_mode ⇒ Object
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_off ⇒ Object
157 158 159 |
# File 'lib/a2_printer.rb', line 157 def bold_off unset_print_mode(BOLD_MASK) end |
#bold_on ⇒ Object
153 154 155 |
# File 'lib/a2_printer.rb', line 153 def bold_on set_print_mode(BOLD_MASK) end |
#double_height_off ⇒ Object
133 134 135 |
# File 'lib/a2_printer.rb', line 133 def double_height_off unset_print_mode(DOUBLE_HEIGHT_MASK) end |
#double_height_on ⇒ Object
129 130 131 |
# File 'lib/a2_printer.rb', line 129 def double_height_on set_print_mode(DOUBLE_HEIGHT_MASK) end |
#double_width_off ⇒ Object
141 142 143 |
# File 'lib/a2_printer.rb', line 141 def double_width_off unset_print_mode(DOUBLE_WIDTH_MASK) end |
#double_width_on ⇒ Object
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 |
#flush ⇒ Object
58 59 60 |
# File 'lib/a2_printer.rb', line 58 def flush write_bytes(12) end |
#inverse_off ⇒ Object
117 118 119 |
# File 'lib/a2_printer.rb', line 117 def inverse_off unset_print_mode(INVERSE_MASK) end |
#inverse_on ⇒ Object
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 |
#normal ⇒ Object
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 |
#offline ⇒ Object
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 |
#online ⇒ Object
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 |
#print(string) ⇒ Object
66 67 68 |
# File 'lib/a2_printer.rb', line 66 def print(string) string.bytes { |b| write(b) } end |
#print_barcode(text, type) ⇒ Object
283 284 285 286 287 |
# File 'lib/a2_printer.rb', line 283 def (text, type) write_bytes(29, 107, type) # set the type first text.bytes { |b| write(b) } write(0) # Terminator end |
#print_bitmap(*args) ⇒ Object
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 |
#reset ⇒ Object
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 (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_default ⇒ Object
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) (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 |
#sleep ⇒ Object
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_off ⇒ Object
149 150 151 |
# File 'lib/a2_printer.rb', line 149 def strike_off unset_print_mode(STRIKE_MASK) end |
#strike_on ⇒ Object
145 146 147 |
# File 'lib/a2_printer.rb', line 145 def strike_on set_print_mode(STRIKE_MASK) end |
#tab ⇒ Object
not working? ====
321 322 323 |
# File 'lib/a2_printer.rb', line 321 def tab write(9) end |
#test_page ⇒ Object
9 10 11 |
# File 'lib/a2_printer.rb', line 9 def test_page write_bytes(18, 84) end |
#underline_off ⇒ Object
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_off ⇒ Object
125 126 127 |
# File 'lib/a2_printer.rb', line 125 def upside_down_off unset_print_mode(UPDOWN_MASK); end |
#upside_down_on ⇒ Object
121 122 123 |
# File 'lib/a2_printer.rb', line 121 def upside_down_on set_print_mode(UPDOWN_MASK); end |
#wake ⇒ Object
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_mode ⇒ Object
102 103 104 |
# File 'lib/a2_printer.rb', line 102 def write_print_mode write_bytes(27, 33, @print_mode) end |