Make the structure of the ESC/POS commands less opaque

(The commands are intended to be readable by people who grew up with ANSI
 escape codes so represent them that way in the code)
This commit is contained in:
Stephan Sokolow 2014-05-22 00:40:05 -04:00
parent 42309fedfe
commit ac640785cc
1 changed files with 90 additions and 52 deletions

View File

@ -1,54 +1,92 @@
""" ESC/POS Commands (Constants) """ """ ESC/POS Commands (Constants) """
# Feed control sequences #{ Control characters
CTL_LF = '\x0a' # Print and line feed # as labelled in http://www.novopos.ch/client/EPSON/TM-T20/TM-T20_eng_qr.pdf
CTL_FF = '\x0c' # Form feed NUL = '\x00'
CTL_CR = '\x0d' # Carriage return EOT = '\x04'
CTL_HT = '\x09' # Horizontal tab ENQ = '\x05'
CTL_VT = '\x0b' # Vertical tab DLE = '\x10'
# Printer hardware DC4 = '\x14'
HW_INIT = '\x1b\x40' # Clear data in buffer and reset modes CAN = '\x18'
HW_SELECT = '\x1b\x3d\x01' # Printer select ESC = '\x1b'
HW_RESET = '\x1b\x3f\x0a\x00' # Reset printer hardware FS = '\x1c'
# Cash Drawer GS = '\x1d'
CD_KICK_2 = '\x1b\x70\x00' # Sends a pulse to pin 2 []
CD_KICK_5 = '\x1b\x70\x01' # Sends a pulse to pin 5 [] #{ Feed control sequences
# Paper CTL_LF = '\n' # Print and line feed
PAPER_FULL_CUT = '\x1d\x56\x00' # Full cut paper CTL_FF = '\f' # Form feed
PAPER_PART_CUT = '\x1d\x56\x01' # Partial cut paper CTL_CR = '\r' # Carriage return
# Text format CTL_HT = '\t' # Horizontal tab
TXT_NORMAL = '\x1b\x21\x00' # Normal text CTL_VT = '\v' # Vertical tab
TXT_2HEIGHT = '\x1b\x21\x10' # Double height text
TXT_2WIDTH = '\x1b\x21\x20' # Double width text #{ Printer hardware
TXT_4SQUARE = '\x1b\x21\x30' # Quad area text HW_INIT = ESC + '@' # Clear data in buffer and reset modes
TXT_UNDERL_OFF = '\x1b\x2d\x00' # Underline font OFF HW_SELECT = ESC + '=\x01' # Printer select
TXT_UNDERL_ON = '\x1b\x2d\x01' # Underline font 1-dot ON
TXT_UNDERL2_ON = '\x1b\x2d\x02' # Underline font 2-dot ON HW_RESET = ESC + '\x3f\x0a\x00' # Reset printer hardware
TXT_BOLD_OFF = '\x1b\x45\x00' # Bold font OFF # (TODO: Where is this specified?)
TXT_BOLD_ON = '\x1b\x45\x01' # Bold font ON
TXT_FONT_A = '\x1b\x4d\x00' # Font type A #{ Cash Drawer (ESC p <pin> <on time: 2*ms> <off time: 2*ms>)
TXT_FONT_B = '\x1b\x4d\x01' # Font type B _CASH_DRAWER = lambda m, t1='', t2='': ESC + 'p' + m + t1 + t2
TXT_ALIGN_LT = '\x1b\x61\x00' # Left justification CD_KICK_2 = _CASH_DRAWER('\x00') # Sends a pulse to pin 2 []
TXT_ALIGN_CT = '\x1b\x61\x01' # Centering CD_KICK_5 = _CASH_DRAWER('\x01') # Sends a pulse to pin 5 []
TXT_ALIGN_RT = '\x1b\x61\x02' # Right justification
# Barcode format #{ Paper Cutter
BARCODE_TXT_OFF = '\x1d\x48\x00' # HRI barcode chars OFF _CUT_PAPER = lambda m: GS + 'V' + m
BARCODE_TXT_ABV = '\x1d\x48\x01' # HRI barcode chars above PAPER_FULL_CUT = _CUT_PAPER('\x00') # Full cut paper
BARCODE_TXT_BLW = '\x1d\x48\x02' # HRI barcode chars below PAPER_PART_CUT = _CUT_PAPER('\x01') # Partial cut paper
BARCODE_TXT_BTH = '\x1d\x48\x03' # HRI barcode chars both above and below
BARCODE_FONT_A = '\x1d\x66\x00' # Font type A for HRI barcode chars #{ Text format
BARCODE_FONT_B = '\x1d\x66\x01' # Font type B for HRI barcode chars # TODO: Acquire the "ESC/POS Application Programming Guide for Paper Roll
BARCODE_HEIGHT = '\x1d\x68\x64' # Barcode Height [1-255] # Printers" and tidy up this stuff too.
BARCODE_WIDTH = '\x1d\x77\x03' # Barcode Width [2-6] TXT_NORMAL = ESC + '!\x00' # Normal text
BARCODE_UPC_A = '\x1d\x6b\x00' # Barcode type UPC-A TXT_2HEIGHT = ESC + '!\x10' # Double height text
BARCODE_UPC_E = '\x1d\x6b\x01' # Barcode type UPC-E TXT_2WIDTH = ESC + '!\x20' # Double width text
BARCODE_EAN13 = '\x1d\x6b\x02' # Barcode type EAN13 TXT_4SQUARE = ESC + '!\x30' # Quad area text
BARCODE_EAN8 = '\x1d\x6b\x03' # Barcode type EAN8 TXT_UNDERL_OFF = ESC + '\x2d\x00' # Underline font OFF
BARCODE_CODE39 = '\x1d\x6b\x04' # Barcode type CODE39 TXT_UNDERL_ON = ESC + '\x2d\x01' # Underline font 1-dot ON
BARCODE_ITF = '\x1d\x6b\x05' # Barcode type ITF TXT_UNDERL2_ON = ESC + '\x2d\x02' # Underline font 2-dot ON
BARCODE_NW7 = '\x1d\x6b\x06' # Barcode type NW7 TXT_BOLD_OFF = ESC + '\x45\x00' # Bold font OFF
# Image format TXT_BOLD_ON = ESC + '\x45\x01' # Bold font ON
S_RASTER_N = '\x1d\x76\x30\x00' # Set raster image normal size TXT_FONT_A = ESC + '\x4d\x00' # Font type A
S_RASTER_2W = '\x1d\x76\x30\x01' # Set raster image double width TXT_FONT_B = ESC + '\x4d\x01' # Font type B
S_RASTER_2H = '\x1d\x76\x30\x02' # Set raster image double height TXT_ALIGN_LT = ESC + '\x61\x00' # Left justification
S_RASTER_Q = '\x1d\x76\x30\x03' # Set raster image quadruple TXT_ALIGN_CT = ESC + '\x61\x01' # Centering
TXT_ALIGN_RT = ESC + '\x61\x02' # Right justification
#{ Barcode format
_SET_BARCODE_TXT_POS = lambda n: GS + 'H' + n
BARCODE_TXT_OFF = _SET_BARCODE_TXT_POS('\x00') # HRI barcode chars OFF
BARCODE_TXT_ABV = _SET_BARCODE_TXT_POS('\x01') # HRI barcode chars above
BARCODE_TXT_BLW = _SET_BARCODE_TXT_POS('\x02') # HRI barcode chars below
BARCODE_TXT_BTH = _SET_BARCODE_TXT_POS('\x03') # HRI both above and below
_SET_HRI_FONT = lambda n: GS + 'f' + n
BARCODE_FONT_A = _SET_HRI_FONT('\x00') # Font type A for HRI barcode chars
BARCODE_FONT_B = _SET_HRI_FONT('\x01') # Font type B for HRI barcode chars
BARCODE_HEIGHT = GS + 'h' + '\x64' # Barcode Height [1-255]
BARCODE_WIDTH = GS + 'w' + '\x03' # Barcode Width [2-6]
#NOTE: This isn't actually an ESC/POS command. It's the common prefix to the
# two "print bar code" commands:
# - "GS k <type as integer> <data> NUL"
# - "GS k <type as letter> <data length> <data>"
# The latter command supports more barcode types
_SET_BARCODE_TYPE = lambda m: GS + 'k' + m
BARCODE_UPC_A = _SET_BARCODE_TYPE('\x00') # Barcode type UPC-A
BARCODE_UPC_E = _SET_BARCODE_TYPE('\x01') # Barcode type UPC-E
BARCODE_EAN13 = _SET_BARCODE_TYPE('\x02') # Barcode type EAN13
BARCODE_EAN8 = _SET_BARCODE_TYPE('\x03') # Barcode type EAN8
BARCODE_CODE39 = _SET_BARCODE_TYPE('\x04') # Barcode type CODE39
BARCODE_ITF = _SET_BARCODE_TYPE('\x05') # Barcode type ITF
BARCODE_NW7 = _SET_BARCODE_TYPE('\x06') # Barcode type NW7
#{ Image format
# NOTE: _PRINT_RASTER_IMG is the obsolete ESC/POS "print raster bit image"
# command. The constants include a fragment of the data's header.
_PRINT_RASTER_IMG = lambda data: GS + 'v0' + data
S_RASTER_N = _PRINT_RASTER_IMG('\x00') # Set raster image normal size
S_RASTER_2W = _PRINT_RASTER_IMG('\x01') # Set raster image double width
S_RASTER_2H = _PRINT_RASTER_IMG('\x02') # Set raster image double height
S_RASTER_Q = _PRINT_RASTER_IMG('\x03') # Set raster image quadruple