diff --git a/Gemfile b/Gemfile index 1d32f49..1108559 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,5 @@ gem 'puma' gem 'rqrcode' gem 'logger' gem 'ostruct' -gem 'libusb' gem 'rackup', "~> 2.1" diff --git a/app.rb b/app.rb index 48fe183..eca6b62 100644 --- a/app.rb +++ b/app.rb @@ -3,7 +3,6 @@ require 'sinatra' require "sinatra/reloader" require 'tmpdir' require 'net/http' -require 'libusb' set :protection, :except => :frame_options set :environment, :production @@ -14,14 +13,10 @@ if !File.exist?('config.json') || !File.readable?('config.json') raise RuntimeException, 'Could not read config.json' end -# Addressed the same way as the Brother QL printers (usb://VID:PID/Serial via -# brother_ql_web) instead of a udev symlink -- self-contained, no reliance on -# OS device naming/ordering. -DATAMAX_VENDOR_ID = 0x0b0b -DATAMAX_PRODUCT_ID = 0x106e -DATAMAX_SERIAL = '24624' -DATAMAX_ENDPOINT = 0x02 # bulk OUT, interface 0 - +# Direct libusb bulk transfer (bypassing the kernel usblp driver, matching how +# brother_ql_web addresses its printers) times out on this Pi 3B's USB +# controller -- back to the kernel driver via a stable udev symlink instead. +PRINTER_DEVICE = '/dev/usb/lp_datamax' # see /etc/udev/rules.d/99-datamax.rules PRINTER_DPI = 203 # DatamaxONeil E4204B Mark III native head resolution PRINTER_DARKNESS = 8 # lower than factory default to stop thermal dot-gain from fusing barcode bars TEST_LABEL_PDF = File.join(File.dirname(__FILE__), 'test-label.pdf') @@ -90,20 +85,7 @@ helpers do end def print_to_datamax(data) - usb = LIBUSB::Context.new - device = usb.devices(idVendor: DATAMAX_VENDOR_ID, idProduct: DATAMAX_PRODUCT_ID) - .find { |d| d.serial_number == DATAMAX_SERIAL } - unless device - raise format('Datamax printer not found (USB %04x:%04x/%s)', - DATAMAX_VENDOR_ID, DATAMAX_PRODUCT_ID, DATAMAX_SERIAL) - end - - device.open do |handle| - handle.auto_detach_kernel_driver = true # usblp kernel driver otherwise holds the interface - handle.claim_interface(0) do - handle.bulk_transfer(endpoint: DATAMAX_ENDPOINT, dataOut: data, timeout: 30_000) # USB 1.1, large ZPL payloads need more than the 1s default - end - end + File.open(PRINTER_DEVICE, 'wb') { |f| f.write(data) } end # Transparently reverse-proxies the current request to one of the