?lang_form? ?lang_select? ?lang_submit? ?lang_endform?
{HEADER END}
{BLAME START}

library

?curdirlinks? -

Blame information for rev 32

Line No. Rev Author Line
1 32 kaklik /******************************************************************************
2 ESC/POS Printer Language Support
3  
4 Summary:
5 This file provides support for the ESC/POS printer language when using the
6 USB Embedded Host Printer Client Driver.
7  
8 Description:
9 This file provides support for the ESC/POS printer language when using the
10 USB Embedded Host Printer Client Driver.
11  
12 The exact implementation of ESC/POS varies across manufacturers and even
13 across different models from the same manufacturer. Some POS printers use
14 specialized Device ID strings (obtained with the GET DEVICE ID class-
15 specific request) to indicate the deviations from the strict ESC/POS
16 specification. Also, many printers indicate a custom USB Peripheral Device
17 rather than the Printer Class, and therefore do not support the
18 GET DEVICE ID device request. For example:
19 <code>
20 Printer Tested VID/PID Class Device ID Substring
21 -------------------------------------------------------------
22 Bixolon SRP-270 0x0419/0x3C01 Printer ESC
23 Epson TM-T88IV 0x04B8/0x0202 Custom Not supported
24 Seiko DPU-V445 0x0619/0x0111 Printer Not present
25 Seiko MPU-L465 0x0619/0x0109 Printer SIIMPU
26 </code>
27 Therefore, dynamic language determination is not recommended for POS
28 printers. Instead, create your application to target a either a single
29 printer model or a group of printer models with identical requirements,
30 and indicate specific VID, PID, and printer language via the USB
31 Configuration Tool, and test the application to ensure consistent behavior
32 across all supported printer models.
33  
34 The ESC/POS language support code provides several #defines that allow the
35 language support file to automatically configure itself for different
36 printer models. These #defines can be set using the USB Configuration Tool
37 (USBConfig.exe or MPLAB VDI). Note that they are determined at compile
38 time, not run time, so only one type of printer can be utilized by an
39 application. Printer models other that the ones explicitly tested may
40 require other modifications to the language support code.
41  
42 Notes:
43 Currently, only standard mode is supported.
44  
45 The black and white bit image polarity is 0=white, 1=black, which is
46 reversed from the Microchip Graphics Library polarity. This driver will
47 automatically convert the image data to the required format, as long as the
48 image data is located in ROM (USB_PRINTER_TRANSFER_FROM_ROM) or it is
49 copied from a RAM buffer (USB_PRINTER_TRANSFER_COPY_DATA). If the data is
50 to be sent directly from its original RAM location, the data must already
51 be in the format required by the printer language.
52  
53 *******************************************************************************/
54 //DOM-IGNORE-BEGIN
55 /******************************************************************************
56  
57 FileName: usb_host_printer_esc_pos.h
58 Dependencies: None
59 Processor: PIC24F/PIC32MX
60 Compiler: C30/C32
61 Company: Microchip Technology, Inc.
62  
63 Software License Agreement
64  
65 The software supplied herewith by Microchip Technology Incorporated
66 (the “Company”) for its PICmicro® Microcontroller is intended and
67 supplied to you, the Company’s customer, for use solely and
68 exclusively on Microchip PICmicro Microcontroller products. The
69 software is owned by the Company and/or its supplier, and is
70 protected under applicable copyright laws. All rights are reserved.
71 Any use in violation of the foregoing restrictions may subject the
72 user to criminal sanctions under applicable laws, as well as to
73 civil liability for the breach of the terms and conditions of this
74 license.
75  
76 THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
77 WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
78 TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
79 PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
80 IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
81 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
82  
83 Change History:
84 Rev Description
85 ---------- ----------------------------------------------------------
86 2.6 - 2.7 No change
87  
88 2.7a Provided macro wrapped versions of malloc() and free()
89 so that a user can override these functions easily.
90  
91 *******************************************************************************/
92 //DOM-IGNORE-END
93  
94  
95 #include <stdio.h>
96 #include <stdlib.h>
97 #include <string.h>
98 #include "GenericTypedefs.h"
99 #include "USB\usb.h"
100 #include "USB\usb_host_printer.h"
101 #include "USB\usb_host_printer_esc_pos.h"
102  
103  
104 //#define DEBUG_MODE
105 #if defined( DEBUG_MODE )
106 #include "uart2.h"
107 #endif
108  
109  
110 #ifdef USB_PRINTER_LANGUAGE_ESCPOS
111  
112  
113 // *****************************************************************************
114 // *****************************************************************************
115 // Section: Configuration
116 // *****************************************************************************
117 // *****************************************************************************
118  
119 #if !defined(USB_ENABLE_TRANSFER_EVENT)
120 #error The USB Host Printer Client Driver requires transfer events.
121 #endif
122  
123  
124 // *****************************************************************************
125 // *****************************************************************************
126 // Section: Constants
127 // *****************************************************************************
128 // *****************************************************************************
129  
130  
131 #define DC2 "\x12"
132 #define DC2_CHAR '\x12'
133 #define ESC "\x1B" //"\033"
134 #define ESC_CHAR '\x1B'
135 #define FF "\x0C"
136 #define FF_CHAR '\x0C'
137 #define LF "\x0A"
138 #define LF_CHAR '\x0A'
139 #define GS "\x1D"
140 #define GS_CHAR '\x1D'
141 #define NUL "\x00"
142 #define NUL_CHAR '\x00'
143  
144 #define COMMAND_COLOR_BLACK ESC "r0"
145 #define COMMAND_COLOR_RED ESC "r1"
146 #define COMMAND_FILL_SHADED DC2 "$4\x01"
147 #define COMMAND_FILL_SOLID DC2 "$4\x02"
148 #define COMMAND_FONT_REVERSE_ON GS "B1"
149 #define COMMAND_FONT_REVERSE_OFF GS "B0"
150 #define COMMAND_FORMFEED FF
151 #define COMMAND_JOB_START ESCAPE "@"
152 #define COMMAND_JUSTIFY_CENTER ESC "a1"
153 #define COMMAND_JUSTIFY_LEFT ESC "a0"
154 #define COMMAND_JUSTIFY_RIGHT ESC "a2"
155 //#define COMMAND_LINE_TYPE_DASHED DC2 "$2\x01"
156 //#define COMMAND_LINE_TYPE_DOTTED DC2 "$2\x02"
157 //#define COMMAND_LINE_TYPE_SOLID DC2 "$2\x00"
158 //#define COMMAND_LINE_WIDTH DC2 "$3\xFF"
159 #define COMMAND_MODE_PAGE ESC "L"
160 #define COMMAND_MODE_STANDARD ESC "S"
161 #define COMMAND_ORIENTATION_HORIZONTAL ESC "V0" ESC "{0"
162 #define COMMAND_ORIENTATION_VERTICAL ESC "V1"
163 #define COMMAND_ORIENTATION_UPSIDE_DOWN ESC "{1"
164 //#define COMMAND_PRINT_AND_FEED ESC "J\xFF"
165 //#define COMMAND_PRINT_LENGTH DC2 "H"
166 //#define COMMAND_PRINT_WIDTH DC2 "K"
167 //#define COMMAND_RECTANGLE DC2 "$1"
168 //#define COMMAND_SET_PITCH GS "P\xFF\xFF"
169 //#define COMMAND_SET_POSITION_X ESC "$"
170 //#define COMMAND_SET_POSITION_X_RELATIVE ESC "\"
171 //#define COMMAND_SET_POSITION_Y GS "$"
172 //#define COMMAND_SET_POSITION_Y_RELATIVE GS "\"
173  
174 #define COMMAND_SET_LEFT_MARGIN GS "L\xFF\xFF"
175  
176  
177 // *****************************************************************************
178 // *****************************************************************************
179 // Section: Data Structures
180 // *****************************************************************************
181 // *****************************************************************************
182  
183 //-----------------------------------------------------------------------------
184 /* Printer Status Structure
185  
186 This structure holds the information about an attached printer. One instance
187 of this structure is needed for each attached printer.
188 */
189  
190 typedef struct
191 {
192 USB_PRINTER_FUNCTION_SUPPORT support; // The functions supported by this printer.
193  
194 WORD currentHeight; // The current height of the page in points.
195 WORD currentWidth; // The current width of the page in points.
196  
197 WORD currentX; // Current X-axis position.
198 WORD currentY; // Current Y-axis position.
199  
200 BYTE deviceAddress; // Address of the attached printer
201 BYTE fontName; // Currently selected font, translated
202 BYTE fontSize; // Size of the current font, vertical and horizontal scaling
203  
204 BYTE density; // Vertical and horizontal dot density specification
205 // of the currently printing image.
206 BYTE imageDataWidth; // Number of bytes to send for each column of the
207 // currently printing image;
208 WORD imageWidth; // Dot width of the currently printing image.
209  
210 union
211 {
212 BYTE value;
213  
214 struct
215 {
216 BYTE isLandscape : 1; // Landscape(1) or portrait(0)
217 BYTE isBold : 1; // If the font is bold
218 BYTE isUnderlined : 1; // If the font is underlined
219 BYTE reversePrint : 1; // Text is printed in reversed colors.
220 BYTE inPageMode : 1; // If the printer is in Page or Standard mode.
221 };
222 } printerFlags;
223  
224 } PRINTER_STATUS_ESC_POS;
225  
226  
227  
228 // *****************************************************************************
229 // *****************************************************************************
230 // Section: Global Variables
231 // *****************************************************************************
232 // *****************************************************************************
233  
234 #ifdef USE_PRINTER_POS_EXTENDED_BARCODE_FORMAT
235 BYTE barCodeFormats[USB_PRINTER_POS_BARCODE_MAX] =
236 {
237 65, // USB_PRINTER_POS_BARCODE_UPC_A
238 66, // USB_PRINTER_POS_BARCODE_UPC_E
239 67, // USB_PRINTER_POS_BARCODE_EAN13
240 68, // USB_PRINTER_POS_BARCODE_EAN8
241 69, // USB_PRINTER_POS_BARCODE_CODE39
242 70, // USB_PRINTER_POS_BARCODE_ITF
243 71, // USB_PRINTER_POS_BARCODE_CODABAR
244 72, // USB_PRINTER_POS_BARCODE_CODE93
245 73, // USB_PRINTER_POS_BARCODE_CODE128
246 74 // USB_PRINTER_POS_BARCODE_EAN128
247 };
248 #endif
249  
250 USB_PRINTER_GRAPHICS_PARAMETERS localParams;
251 PRINTER_STATUS_ESC_POS printerListESCPOS[USB_MAX_PRINTER_DEVICES];
252  
253  
254 // *****************************************************************************
255 // *****************************************************************************
256 // Section: Macros
257 // *****************************************************************************
258 // *****************************************************************************
259  
260 #define PAGE_IS_LANDSCAPE(x) ((x & 0x01) == 0x01)
261 #define PAGE_IS_PORTRAIT(x) ((x & 0x01) == 0x00)
262 #define FONT_IS_BOLD(x) ((x & 0x02) == 0x02)
263 #define FONT_IS_ITALIC(x) ((x & 0x04) == 0x04)
264  
265 #define _SetCurrentPosition(x,y) \
266 { \
267 printerListESCPOS[printer].currentX = x; \
268 printerListESCPOS[printer].currentY = y; \
269 }
270  
271 #ifndef USB_MALLOC
272 #define USB_MALLOC(size) malloc(size)
273 #endif
274  
275 #ifndef USB_FREE
276 #define USB_FREE(ptr) free(ptr)
277 #endif
278  
279 #define USB_FREE_AND_CLEAR(ptr) {USB_FREE(ptr); ptr = NULL;}
280  
281 // *****************************************************************************
282 // *****************************************************************************
283 // Section: Local Prototypes
284 // *****************************************************************************
285 // *****************************************************************************
286  
287 #if defined( USB_PRINTER_POS_BARCODE_SUPPORT )
288 static BYTE _BarcodeCharacterValueCodabar( char c );
289 static BYTE _BarcodeCharacterValueCode39( char c );
290 static char _BarcodeValueCharacterCodabar( BYTE v );
291 static char _BarcodeValueCharacterCode39( BYTE v );
292 #endif
293 static BYTE _PrintFontCommand( BYTE printer, BYTE transferFlags );
294 static BYTE _PrintStaticCommand( BYTE printer, char *command, BYTE transferFlags );
295  
296  
297 // *****************************************************************************
298 // *****************************************************************************
299 // Section: Interface Functions
300 // *****************************************************************************
301 // *****************************************************************************
302  
303 /****************************************************************************
304 Function:
305 BYTE USBHostPrinterLanguageESCPOS( BYTE address,
306 USB_PRINTER_COMMAND command, USB_DATA_POINTER data, DWORD size, BYTE transferFlags )
307  
308 Summary:
309 This function executes printer commands for an ESC/POS printer.
310  
311 Description:
312 This function executes printer commands for an ESC/POS printer. When
313 the application issues a printer command, the printer client driver
314 determines what language to use to communicate with the printer, and
315 transfers the command to that language support routine. As much as
316 possible, commands are designed to produce the same output regardless
317 of what printer language is used.
318  
319 Not all printer commands support data from both RAM and ROM. Unless
320 otherwise noted, the data pointer is assumed to point to RAM, regardless of
321 the value of transferFlags. Refer to the specific command to see if ROM
322 data is supported.
323  
324 Preconditions:
325 None
326  
327 Parameters:
328 BYTE address - Device's address on the bus
329 USB_PRINTER_COMMAND command - Command to execute. See the enumeration
330 USB_PRINTER_COMMAND for the list of
331 valid commands and their requirements.
332 USB_DATA_POINTER data - Pointer to the required data. Note that
333 the caller must set transferFlags
334 appropriately to indicate if the pointer is
335 a RAM pointer or a ROM pointer.
336 DWORD size - Size of the data. For some commands, this
337 parameter is used to hold the data itself.
338 BYTE transferFlags - Flags that indicate details about the
339 transfer operation. Refer to these flags
340 * USB_PRINTER_TRANSFER_COPY_DATA
341 * USB_PRINTER_TRANSFER_STATIC_DATA
342 * USB_PRINTER_TRANSFER_NOTIFY
343 * USB_PRINTER_TRANSFER_FROM_ROM
344 * USB_PRINTER_TRANSFER_FROM_RAM
345  
346 Return Values:
347 USB_PRINTER_SUCCESS - The command was executed successfully.
348 USB_PRINTER_UNKNOWN_DEVICE - A printer with the indicated address is not
349 attached
350 USB_PRINTER_TOO_MANY_DEVICES - The printer status array does not have
351 space for another printer.
352 USB_PRINTER_OUT_OF_MEMORY - Not enough available heap space to
353 execute the command.
354 other - See possible return codes from the
355 function USBHostPrinterWrite().
356  
357 Remarks:
358 When developing new commands, keep in mind that the function
359 USBHostPrinterCommandReady() will be used before calling this function to
360 see if there is space available in the output transfer queue.
361 USBHostPrinterCommandReady() will routine TRUE if a single space is
362 available in the output queue. Therefore, each command can generate only
363 one output transfer.
364  
365 Multiple printer languages may be used in a single application. The USB
366 Embedded Host Printer Client Driver will call the routine required for the
367 attached device.
368 ***************************************************************************/
369  
370 BYTE USBHostPrinterLanguageESCPOS( BYTE address,
371 USB_PRINTER_COMMAND command, USB_DATA_POINTER data, DWORD size, BYTE transferFlags )
372 {
373 char *buffer;
374 int i;
375 BYTE printer = 0;
376  
377 if (command != USB_PRINTER_ATTACHED)
378 {
379 // Try to find the current printer. If we can't find the printer in the list,
380 // put it in the list at the first available location.
381 for (printer=0; (printer<USB_MAX_PRINTER_DEVICES) && (printerListESCPOS[printer].deviceAddress != address); printer++ );
382 if (printer == USB_MAX_PRINTER_DEVICES)
383 {
384 return USB_PRINTER_UNKNOWN_DEVICE;
385 }
386 }
387  
388 switch( command )
389 {
390 //---------------------------------------------------------------------
391 case USB_PRINTER_ATTACHED:
392 for (printer=0; (printer<USB_MAX_PRINTER_DEVICES) && (printerListESCPOS[printer].deviceAddress != 0); printer++ );
393 if (printer != USB_MAX_PRINTER_DEVICES)
394 {
395 printerListESCPOS[printer].deviceAddress = address;
396 printerListESCPOS[printer].support = *((USB_PRINTER_FUNCTION_SUPPORT *)(data.pointerRAM));
397 printerListESCPOS[printer].printerFlags.value = 0;
398 return USB_PRINTER_SUCCESS;
399 }
400 return USB_PRINTER_TOO_MANY_DEVICES;
401 break;
402  
403 //---------------------------------------------------------------------
404 case USB_PRINTER_DETACHED:
405 for (printer=0; (printer<USB_MAX_PRINTER_DEVICES) && (printerListESCPOS[printer].deviceAddress != address); printer++ );
406 if (printer != USB_MAX_PRINTER_DEVICES)
407 {
408 printerListESCPOS[printer].deviceAddress = 0;
409 }
410 return USB_PRINTER_SUCCESS;
411 break;
412  
413 //---------------------------------------------------------------------
414 case USB_PRINTER_JOB_START:
415 _SetCurrentPosition( 0, 0 );
416  
417 buffer = (char *)USB_MALLOC( 2 );
418 if (buffer == NULL)
419 {
420 return USB_PRINTER_OUT_OF_MEMORY;
421 }
422  
423 // Initialize printer
424 buffer[0] = ESC_CHAR;
425 buffer[1] = '@';
426  
427 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
428 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, 2, transferFlags );
429 break;
430  
431  
432 //---------------------------------------------------------------------
433 case USB_PRINTER_JOB_STOP:
434 // For compatibility with full sheet operation, if we are in page
435 // mode, perform the USB_PRINTER_EJECT_PAGE command.
436 if (printerListESCPOS[printer].printerFlags.inPageMode)
437 {
438 goto EjectPage;
439 }
440 return USB_PRINTER_SUCCESS;
441 break;
442  
443 //---------------------------------------------------------------------
444 case USB_PRINTER_FONT_NAME:
445 // If an illegal size is specified, return with an error.
446 if (size >= USB_PRINTER_FONT_POS_MAX_FONT)
447 {
448 return USB_PRINTER_BAD_PARAMETER;
449 }
450  
451 switch ((BYTE)size)
452 {
453 case USB_PRINTER_FONT_POS_18x36:
454 size = 0x00;
455 break;
456 case USB_PRINTER_FONT_POS_18x72:
457 size = 0x10;
458 break;
459 case USB_PRINTER_FONT_POS_36x36:
460 size = 0x20;
461 break;
462 case USB_PRINTER_FONT_POS_36x72:
463 size = 0x30;
464 break;
465 case USB_PRINTER_FONT_POS_12x24:
466 size = 0x01;
467 break;
468 case USB_PRINTER_FONT_POS_12x48:
469 size = 0x11;
470 break;
471 case USB_PRINTER_FONT_POS_24x24:
472 size = 0x21;
473 break;
474 case USB_PRINTER_FONT_POS_24x48:
475 size = 0x31;
476 break;
477 }
478  
479 printerListESCPOS[printer].fontName = (BYTE)size;
480 return _PrintFontCommand( printer, transferFlags );
481 break;
482  
483 //---------------------------------------------------------------------
484 case USB_PRINTER_FONT_UPRIGHT:
485 // Italic printing is not supported, but we will not return an
486 // error if the user specifies upright.
487 return USB_PRINTER_SUCCESS;
488 break;
489  
490 //---------------------------------------------------------------------
491 case USB_PRINTER_FONT_BOLD:
492 printerListESCPOS[printer].printerFlags.isBold = 1;
493 return _PrintFontCommand( printer, transferFlags );
494 break;
495  
496 //---------------------------------------------------------------------
497 case USB_PRINTER_FONT_MEDIUM:
498 printerListESCPOS[printer].printerFlags.isBold = 0;
499 return _PrintFontCommand( printer, transferFlags );
500 break;
501  
502 //---------------------------------------------------------------------
503 case USB_PRINTER_EJECT_PAGE:
504 EjectPage:
505 buffer = (char *)USB_MALLOC( 1 );
506 if (buffer == NULL)
507 {
508 return USB_PRINTER_OUT_OF_MEMORY;
509 }
510  
511 buffer[0] = FF_CHAR;
512  
513 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
514 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, 1, transferFlags );
515 break;
516  
517 //---------------------------------------------------------------------
518 case USB_PRINTER_TEXT_START:
519 // No text initialization is required.
520 return USB_PRINTER_SUCCESS;
521 break;
522  
523 //---------------------------------------------------------------------
524 case USB_PRINTER_TEXT:
525 case USB_PRINTER_TRANSPARENT:
526 // If the user's data is in ROM, we have to copy it to RAM first,
527 // so the USB Host routines can read it.
528 if (transferFlags & USB_PRINTER_TRANSFER_FROM_ROM)
529 {
530 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
531 }
532 if (transferFlags & USB_PRINTER_TRANSFER_COPY_DATA)
533 {
534 buffer = (char *)USB_MALLOC( size );
535 if (buffer == NULL)
536 {
537 return USB_PRINTER_OUT_OF_MEMORY;
538 }
539  
540 if (transferFlags & USB_PRINTER_TRANSFER_FROM_ROM)
541 {
542 DWORD di;
543 #if defined( __C30__ )
544 char __prog__ *ptr;
545 #elif defined( __PIC32MX__ )
546 const char *ptr;
547 #endif
548  
549 ptr = ((USB_DATA_POINTER)data).pointerROM;
550 for (di=0; di<size; di++)
551 {
552 buffer[di] = *ptr++;
553 }
554 }
555 else
556 {
557 DWORD di;
558 char *ptr;
559  
560 ptr = ((USB_DATA_POINTER)data).pointerRAM;
561 for (di=0; di<size; di++)
562 {
563 buffer[di] = *ptr++;
564 }
565 }
566 }
567 else
568 {
569 buffer = ((USB_DATA_POINTER)data).pointerRAM;
570 }
571  
572 return USBHostPrinterWrite( address, buffer, size, transferFlags );
573 break;
574  
575 //---------------------------------------------------------------------
576 case USB_PRINTER_TEXT_STOP:
577 buffer = (char *)USB_MALLOC( 3 );
578 if (buffer == NULL)
579 {
580 return USB_PRINTER_OUT_OF_MEMORY;
581 }
582  
583 buffer[0] = ESC_CHAR;
584 buffer[1] = 'd';
585 buffer[2] = (BYTE)size;
586  
587 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
588 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, 3, transferFlags );
589 break;
590  
591 //---------------------------------------------------------------------
592 case USB_PRINTER_IMAGE_START:
593 #define VERTICAL_DENSITY ((USB_PRINTER_IMAGE_INFO *)(data.pointerRAM))->densityVertical
594 #define HORIZONTAL_DENSITY ((USB_PRINTER_IMAGE_INFO *)(data.pointerRAM))->densityHorizontal
595  
596 // Check for legal density settings.
597 #if defined( USB_PRINTER_POS_24_DOT_IMAGE_SUPPORT ) && defined( USB_PRINTER_POS_36_DOT_IMAGE_SUPPORT )
598 if (!((VERTICAL_DENSITY == 8) || (VERTICAL_DENSITY == 24) || (VERTICAL_DENSITY == 36)))
599 #elif defined( USB_PRINTER_POS_24_DOT_IMAGE_SUPPORT ) && !defined( USB_PRINTER_POS_36_DOT_IMAGE_SUPPORT )
600 if (!((VERTICAL_DENSITY == 8) || (VERTICAL_DENSITY == 24)))
601 #else
602 if (!((VERTICAL_DENSITY == 8)))
603 #endif
604 {
605 return USB_PRINTER_BAD_PARAMETER;
606 }
607 if (!((HORIZONTAL_DENSITY == 1) || (HORIZONTAL_DENSITY == 2)))
608 {
609 return USB_PRINTER_BAD_PARAMETER;
610 }
611  
612 // Set up dot density specification.
613 printerListESCPOS[printer].density = 0; // 8-dot image
614 printerListESCPOS[printer].imageDataWidth = 1;
615 #if defined( USB_PRINTER_POS_24_DOT_IMAGE_SUPPORT ) && !defined( USB_PRINTER_POS_36_DOT_IMAGE_SUPPORT )
616 if (VERTICAL_DENSITY == 24)
617 {
618 printerListESCPOS[printer].density = 32;
619 printerListESCPOS[printer].imageDataWidth = 3;
620 }
621 #elif defined( USB_PRINTER_POS_24_DOT_IMAGE_SUPPORT ) && defined( USB_PRINTER_POS_36_DOT_IMAGE_SUPPORT )
622 if (VERTICAL_DENSITY == 24)
623 {
624 printerListESCPOS[printer].density = 16;
625 printerListESCPOS[printer].imageDataWidth = 3;
626 }
627 if (VERTICAL_DENSITY == 36)
628 {
629 printerListESCPOS[printer].density = 32;
630 printerListESCPOS[printer].imageDataWidth = 5;
631 }
632 #endif
633 if (HORIZONTAL_DENSITY == 2)
634 {
635 printerListESCPOS[printer].density ++;
636 }
637  
638 printerListESCPOS[printer].imageWidth = ((USB_PRINTER_IMAGE_INFO *)(data.pointerRAM))->width;
639  
640 buffer = (char *)USB_MALLOC( 3 );
641 if (buffer == NULL)
642 {
643 return USB_PRINTER_OUT_OF_MEMORY;
644 }
645  
646 i = 0;
647  
648 // Set line spacing to 8 dots, so there are no gaps between the image lines.
649 buffer[i++] = ESC_CHAR;
650 buffer[i++] = '3';
651 buffer[i++] = USB_PRINTER_POS_IMAGE_LINE_SPACING;
652  
653 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
654 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, i, transferFlags );
655 break;
656  
657 //---------------------------------------------------------------------
658 case USB_PRINTER_IMAGE_DATA_HEADER:
659 buffer = (char *)USB_MALLOC( 6 );
660 if (buffer == NULL)
661 {
662 return USB_PRINTER_OUT_OF_MEMORY;
663 }
664  
665 i = 0;
666  
667 buffer[i++] = LF_CHAR;
668  
669 buffer[i++] = ESC_CHAR;
670 buffer[i++] = '*';
671 buffer[i++] = printerListESCPOS[printer].density;
672  
673 buffer[i++] = printerListESCPOS[printer].imageWidth & 0xFF;
674 buffer[i++] = printerListESCPOS[printer].imageWidth >> 8;
675  
676 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
677 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, i, transferFlags );
678 break;
679  
680 //---------------------------------------------------------------------
681 case USB_PRINTER_IMAGE_DATA:
682 // The polarity of POS raster graphics (0=white, 1=black) is
683 // backwards from other printer languages and the graphics library.
684 // To maintain compatibility, the values will be flipped for all
685 // copied data.
686  
687 // For ESC/POS, the amount of data to transfer is the width of the
688 // image times the byte depth of the data, as specified by the
689 // vertical dot density.
690 size *= printerListESCPOS[printer].imageDataWidth;
691  
692 // If the user's data is in ROM, we have to copy it to RAM first,
693 // so the USB Host routines can read it.
694 if (transferFlags & USB_PRINTER_TRANSFER_FROM_ROM)
695 {
696 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
697 }
698 if (transferFlags & USB_PRINTER_TRANSFER_COPY_DATA)
699 {
700 buffer = (char *)USB_MALLOC( size );
701 if (buffer == NULL)
702 {
703 return USB_PRINTER_OUT_OF_MEMORY;
704 }
705  
706 if (transferFlags & USB_PRINTER_TRANSFER_FROM_ROM)
707 {
708 #if defined( __C30__ )
709 char __prog__ *ptr;
710 #elif defined( __PIC32MX__ )
711 const char *ptr;
712 #endif
713 DWORD i;
714  
715 ptr = ((USB_DATA_POINTER)data).pointerROM;
716 for (i=0; i<size; i++)
717 {
718 buffer[i] = ~(*ptr++);
719 }
720 }
721 else
722 {
723 char *ptr;
724 DWORD i;
725  
726 ptr = ((USB_DATA_POINTER)data).pointerRAM;
727 for (i=0; i<size; i++)
728 {
729 buffer[i] = ~(*ptr++);
730 }
731 }
732 }
733 else
734 {
735 buffer = ((USB_DATA_POINTER)data).pointerRAM;
736 }
737  
738 return USBHostPrinterWrite( address, buffer, size, transferFlags );
739 break;
740  
741 //---------------------------------------------------------------------
742 case USB_PRINTER_IMAGE_STOP:
743 // No termination required.
744 //return USB_PRINTER_SUCCESS;
745 buffer = (char *)USB_MALLOC( 3 );
746 if (buffer == NULL)
747 {
748 return USB_PRINTER_OUT_OF_MEMORY;
749 }
750  
751 i = 0;
752  
753 // Set line spacing back to the default
754 buffer[i++] = ESC_CHAR;
755 buffer[i++] = '2';
756  
757 // Feed after the last line.
758 buffer[i++] = LF_CHAR;
759  
760 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
761 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, i, transferFlags );
762 break;
763  
764 //---------------------------------------------------------------------
765 case USB_PRINTER_POS_STANDARD_MODE:
766 printerListESCPOS[printer].printerFlags.inPageMode = 0;
767 return _PrintStaticCommand( printer, COMMAND_MODE_STANDARD, transferFlags );
768 break;
769  
770 //---------------------------------------------------------------------
771 case USB_PRINTER_POS_FEED:
772 // Make sure the data pointer is NULL.
773 data.pointerRAM = NULL;
774 // Fall through the USB_PRINTER_POS_TEXT_LINE command processing.
775  
776 //---------------------------------------------------------------------
777 case USB_PRINTER_POS_TEXT_LINE:
778 if (transferFlags & USB_PRINTER_TRANSFER_FROM_ROM)
779 {
780 return USB_PRINTER_BAD_PARAMETER;
781 }
782  
783 {
784 WORD length;
785 char *ptr;
786  
787 length = 0;
788 ptr = ((USB_DATA_POINTER)data).pointerRAM;
789 if (ptr != NULL)
790 {
791 length = strlen( ptr );
792 }
793 buffer = (char *)USB_MALLOC( length + 3 );
794 if (buffer == NULL)
795 {
796 return USB_PRINTER_OUT_OF_MEMORY;
797 }
798  
799 for (i=0; i<length; i++)
800 {
801 buffer[i] = *ptr++;
802 }
803 buffer[i++] = ESC_CHAR;
804 buffer[i++] = 'd';
805 buffer[i++] = (BYTE)size;
806  
807 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
808 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, length + 3, transferFlags );
809 }
810 break;
811  
812 //---------------------------------------------------------------------
813 #ifdef USB_PRINTER_POS_CUTTER_SUPPORT
814 case USB_PRINTER_POS_CUT:
815 case USB_PRINTER_POS_CUT_PARTIAL:
816 // Using Function B
817 buffer = (char *)USB_MALLOC( 4 );
818 if (buffer == NULL)
819 {
820 return USB_PRINTER_OUT_OF_MEMORY;
821 }
822  
823 // Copy cut command.
824 buffer[0] = GS_CHAR;
825 buffer[1] = 'V';
826 buffer[2] = 65; // Full cut
827  
828 // Replace partial cut if necessary.
829 if (command == USB_PRINTER_POS_CUT_PARTIAL)
830 {
831 buffer[2] = 66;
832 }
833  
834 // Fill in feed amount. The default vertical motion unit varies
835 // between printers (often 1/360 inch). Not all printers can
836 // change this value.
837 buffer[3] = (char)size;
838  
839 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
840 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, 4, transferFlags );
841 break;
842 #endif
843  
844 //---------------------------------------------------------------------
845 case USB_PRINTER_POS_JUSTIFICATION_CENTER:
846 return _PrintStaticCommand( printer, COMMAND_JUSTIFY_CENTER, transferFlags );
847 break;
848  
849 //---------------------------------------------------------------------
850 case USB_PRINTER_POS_JUSTIFICATION_LEFT:
851 return _PrintStaticCommand( printer, COMMAND_JUSTIFY_LEFT, transferFlags );
852 break;
853  
854 //---------------------------------------------------------------------
855 case USB_PRINTER_POS_JUSTIFICATION_RIGHT:
856 return _PrintStaticCommand( printer, COMMAND_JUSTIFY_RIGHT, transferFlags );
857 break;
858  
859 //---------------------------------------------------------------------
860 #ifdef USB_PRINTER_POS_REVERSE_TEXT_SUPPORT
861 case USB_PRINTER_POS_FONT_REVERSE:
862 printerListESCPOS[printer].printerFlags.reversePrint = size & 0x01;
863 if (size == 0)
864 {
865 return _PrintStaticCommand( printer, COMMAND_FONT_REVERSE_OFF, transferFlags );
866 }
867 else
868 {
869 return _PrintStaticCommand( printer, COMMAND_FONT_REVERSE_ON, transferFlags );
870 }
871 break;
872 #endif
873  
874 //---------------------------------------------------------------------
875 case USB_PRINTER_POS_FONT_UNDERLINE:
876 printerListESCPOS[printer].printerFlags.isUnderlined = size & 0x01;
877 return _PrintFontCommand( printer, transferFlags );
878 break;
879  
880 //---------------------------------------------------------------------
881 #ifdef USB_PRINTER_POS_TWO_COLOR_SUPPORT
882 case USB_PRINTER_POS_COLOR_BLACK:
883 return _PrintStaticCommand( printer, COMMAND_COLOR_BLACK, transferFlags );
884 break;
885 #endif
886  
887 //---------------------------------------------------------------------
888 #ifdef USB_PRINTER_POS_TWO_COLOR_SUPPORT
889 case USB_PRINTER_POS_COLOR_RED:
890 return _PrintStaticCommand( printer, COMMAND_COLOR_RED, transferFlags );
891 break;
892 #endif
893  
894 //---------------------------------------------------------------------
895 #ifdef USB_PRINTER_POS_BARCODE_SUPPORT
896 case USB_PRINTER_POS_BARCODE:
897 {
898 #define BCD (BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.data
899  
900 WORD j;
901 BYTE dataLength; // The length of the bar code data, possibly adjusted for a checkdigit.
902  
903 dataLength = ((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.dataLength;
904  
905 // Do any preprocessing required for the particular bar code type.
906 switch ((BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.type)
907 {
908 case USB_PRINTER_POS_BARCODE_UPC_A:
909 // Validate the data length.
910 if (dataLength != 11)
911 {
912 return USB_PRINTER_BAD_PARAMETER;
913 }
914 break;
915  
916 case USB_PRINTER_POS_BARCODE_UPC_E:
917 // Validate the data length.
918 if (!((dataLength == 6) || (dataLength == 7) || (dataLength == 11)))
919 {
920 return USB_PRINTER_BAD_PARAMETER;
921 }
922 // If the length is not 6, then the first data byte must be '0'.
923 if ((dataLength != 6) && (BCD[0] != '0'))
924 {
925 return USB_PRINTER_BAD_PARAMETER;
926 }
927 break;
928  
929 case USB_PRINTER_POS_BARCODE_EAN13:
930 // Validate the data length.
931 if (dataLength != 12)
932 {
933 return USB_PRINTER_BAD_PARAMETER;
934 }
935 break;
936  
937 case USB_PRINTER_POS_BARCODE_EAN8:
938 // Validate the data length, and that the first byte is '0'.
939 if ((dataLength != 7) || (BCD[0] != '0'))
940 {
941 return USB_PRINTER_BAD_PARAMETER;
942 }
943 break;
944  
945 case USB_PRINTER_POS_BARCODE_CODE39:
946 case USB_PRINTER_POS_BARCODE_CODABAR:
947 if ((BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.flags.bits.bPrintCheckDigit)
948 {
949 // Add one for the check digit that we will add.
950 dataLength ++;
951 }
952 break;
953  
954 case USB_PRINTER_POS_BARCODE_ITF:
955 // Validate that the data length is an even number
956 if (dataLength & 0x01)
957 {
958 return USB_PRINTER_BAD_PARAMETER;
959 }
960 break;
961  
962 #ifdef USE_PRINTER_POS_EXTENDED_BARCODE_FORMAT
963 case USB_PRINTER_POS_BARCODE_CODE93:
964 case USB_PRINTER_POS_BARCODE_CODE128:
965 // No special checks are required.
966 break;
967  
968 case USB_PRINTER_POS_BARCODE_EAN128:
969 // NOT YET SUPPORTED
970 return USB_PRINTER_BAD_PARAMETER;
971 break;
972 #endif
973  
974 default:
975 // Invalid bar code format.
976 return USB_PRINTER_BAD_PARAMETER;
977 break;
978 }
979  
980 buffer = (char *)USB_MALLOC( 21 + dataLength );
981 if (buffer == NULL)
982 {
983 return USB_PRINTER_OUT_OF_MEMORY;
984 }
985  
986  
987 i = 0;
988  
989 // Set barcode height.
990 buffer[i++] = GS_CHAR;
991 buffer[i++] = 'h';
992 buffer[i++] = (BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.height;
993  
994 // Set readable characters position.
995 buffer[i++] = GS_CHAR;
996 buffer[i++] = 'H';
997 buffer[i++] = (BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.textPosition;
998  
999 // Set readable characters font.
1000 buffer[i++] = GS_CHAR;
1001 buffer[i++] = 'f';
1002 buffer[i++] = (BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.textFont;
1003  
1004 // Print the bar code.
1005 buffer[i++] = GS_CHAR;
1006 buffer[i++] = 'k';
1007  
1008 #ifdef USE_PRINTER_POS_EXTENDED_BARCODE_FORMAT
1009 // Use format 2
1010 buffer[i++] = barCodeFormats[(BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.type];
1011 buffer[i++] = dataLength;
1012 #else
1013 // Use format 1
1014 buffer[i++] = (BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.type;
1015 #endif
1016  
1017 // Put in the data as required for each particular bar code type.
1018 switch ((BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.type)
1019 {
1020 case USB_PRINTER_POS_BARCODE_UPC_A:
1021 case USB_PRINTER_POS_BARCODE_UPC_E:
1022 case USB_PRINTER_POS_BARCODE_EAN13:
1023 case USB_PRINTER_POS_BARCODE_EAN8:
1024 // Copy the data for printing.
1025 for (j=0; j<dataLength; j++)
1026 {
1027 buffer[i++] = BCD[j];
1028 }
1029  
1030 // UPC and EAN bar codes have a check digit, but we will let the
1031 // printer calculate the check digit for us!
1032 break;
1033  
1034 case USB_PRINTER_POS_BARCODE_CODE39:
1035 if ((BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.flags.bits.bPrintCheckDigit)
1036 {
1037 // These must all be ASCII, so copy them straight over.
1038 // Our length already accounts for the check digit that
1039 // we will add.
1040 for (j=0; j<dataLength-1; j++)
1041 {
1042 buffer[i++] = BCD[j];
1043 }
1044  
1045 // Calculate the check digit. The printer will not automatically
1046 // calculate and print the checkdigit for this format.
1047 {
1048 WORD sum = 0;
1049  
1050 for (j=0; j<dataLength-1; j++)
1051 {
1052 sum += _BarcodeCharacterValueCode39( BCD[j] );
1053 }
1054  
1055 sum %= 43;
1056  
1057 buffer[i++] = _BarcodeValueCharacterCode39( sum );
1058 }
1059 }
1060 else
1061 {
1062 // These must all be ASCII, so copy them straight over.
1063 for (j=0; j<dataLength; j++)
1064 {
1065 buffer[i++] = BCD[j];
1066 }
1067 }
1068 break;
1069  
1070 case USB_PRINTER_POS_BARCODE_CODABAR:
1071 if ((BYTE)((USB_PRINTER_GRAPHICS_PARAMETERS *)(data.pointerRAM))->sBarCode.flags.bits.bPrintCheckDigit)
1072 {
1073 // These must all be ASCII, so copy them straight over.
1074 // Don't copy the stop character yet - it goes after the
1075 // checkdigit. Our length already accounts for the
1076 // checkdigit.
1077 for (j=0; j<dataLength-2; j++)
1078 {
1079 buffer[i++] = BCD[j];
1080 }
1081  
1082 // Calculate the check digit. The printer will not automatically
1083 // calculate and print the checkdigit for this format.
1084 {
1085 WORD sum = 0;
1086  
1087 for (j=0; j<dataLength-1; j++)
1088 {
1089 sum += _BarcodeCharacterValueCodabar( BCD[j] );
1090 }
1091  
1092 sum &= 0x0F;
1093 if (sum != 0)
1094 {
1095 sum = 16 - sum;
1096 }
1097  
1098 buffer[i++] = _BarcodeValueCharacterCodabar( sum );
1099 }
1100  
1101 // Now copy the stop character.
1102 buffer[i++] = BCD[dataLength-2];
1103 }
1104 else
1105 {
1106 // These must all be ASCII, so copy them straight over.
1107 for (j=0; j<dataLength; j++)
1108 {
1109 buffer[i++] = BCD[j];
1110 }
1111 }
1112 break;
1113  
1114 case USB_PRINTER_POS_BARCODE_ITF:
1115 #ifdef USE_PRINTER_POS_EXTENDED_BARCODE_FORMAT
1116 case USB_PRINTER_POS_BARCODE_CODE93:
1117 #endif
1118 default:
1119 for (j=0; j<dataLength; j++)
1120 {
1121 buffer[i++] = BCD[j];
1122 }
1123 break;
1124 }
1125  
1126 #ifndef USE_PRINTER_POS_EXTENDED_BARCODE_FORMAT
1127 buffer[i++] = 0;
1128 #endif
1129 }
1130  
1131 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
1132 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, i, transferFlags );
1133 break;
1134 #endif
1135  
1136 //---------------------------------------------------------------------
1137 default:
1138 return USB_PRINTER_UNKNOWN_COMMAND;
1139 break;
1140 }
1141 return USB_PRINTER_UNKNOWN_COMMAND;
1142 }
1143  
1144  
1145 /****************************************************************************
1146 Function:
1147 BOOL USBHostPrinterLanguageESCPOSIsSupported( char *deviceID,
1148 USB_PRINTER_FUNCTION_SUPPORT *support )
1149  
1150 Description:
1151 This function determines if the printer with the given device ID string
1152 supports the ESC/POS printer language.
1153  
1154 Preconditions:
1155 None
1156  
1157 Parameters:
1158 char *deviceID - Pointer to the "COMMAND SET:" portion of the device ID
1159 string of the attached printer.
1160 USB_PRINTER_FUNCTION_SUPPORT *support - Pointer to returned information
1161 about what types of functions this printer supports.
1162  
1163 Return Values:
1164 TRUE - The printer supports ESC/POS.
1165 FALSE - The printer does not support ESC/POS.
1166  
1167 Remarks:
1168 The caller must first locate the "COMMAND SET:" section of the device ID
1169 string. To ensure that only the "COMMAND SET:" section of the device ID
1170 string is checked, the ";" at the end of the section should be temporarily
1171 replaced with a NULL. Otherwise, this function may find the printer
1172 language string in the comments or other section, and incorrectly indicate
1173 that the printer supports the language.
1174  
1175 Device ID strings are case sensitive.
1176  
1177 POS printers use specialized strings to indicate the deviations from the
1178 strict ESC/POS specification. Also, many printers indicate a custom USB
1179 Peripheral Device class rather than the Printer Class, and therefore do
1180 not support the standard device request used to obtain this string.
1181 Therefore, dynamic language determination is not recommended for POS
1182 printers. Instead, indicate specific VID, PID, and printer language via
1183 the USB Configuration Tool.
1184 ***************************************************************************/
1185  
1186 BOOL USBHostPrinterLanguageESCPOSIsSupported( char *deviceID,
1187 USB_PRINTER_FUNCTION_SUPPORT *support )
1188 {
1189 if (strstr( deviceID, LANGUAGE_ID_STRING_ESCPOS ))
1190 {
1191 support->val = LANGUAGE_SUPPORT_FLAGS_ESCPOS;
1192 return TRUE;
1193 }
1194 return FALSE;
1195 }
1196  
1197  
1198 /****************************************************************************
1199 Function:
1200 USB_DATA_POINTER USBHostPrinterPOSImageDataFormat( USB_DATA_POINTER image,
1201 BYTE imageLocation, WORD imageHeight, WORD imageWidth, WORD *currentRow,
1202 BYTE byteDepth, BYTE *imageData )
1203  
1204 Summary:
1205 This function formats data for a bitmapped image into the format required
1206 for sending to a POS printer.
1207  
1208 Description:
1209 This function formats data for a bitmapped image into the format required
1210 for sending to a POS printer. Bitmapped images are stored one row of pixels
1211 at a time. Suppose we have an image with vertical black bars, eight pixels
1212 wide and eight pixels deep. The image would appear as the following pixels,
1213 where 0 indicates a black dot and 1 indicates a white dot:
1214 <code>
1215  
1216  
1217  
1218  
1219  
1220  
1221  
1222  
1223 </code>
1224 The stored bitmap of the data would contain the data bytes, where each byte
1225 is one row of data:
1226 <code>
1227 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55
1228 </code>
1229 When printing to a full sheet printer, eight separate
1230 USB_PRINTER_IMAGE_DATA_HEADER / USB_PRINTER_IMAGE_DATA command combinations
1231 are required to print this image.
1232  
1233 POS printers, however, require image data formated either 8 dots or 24 dots
1234 deep, depending on the desired (and supported) vertical print density. For
1235 a POS printer performing an 8-dot vertical density print, the data needs to
1236 be in this format:
1237 <code>
1238 0x00 0xFF 0x00 0xFF 0x00 0xFF 0x00 0xFF
1239 </code>
1240 When printing to a POS printer, only one
1241 USB_PRINTER_IMAGE_DATA_HEADER / USB_PRINTER_IMAGE_DATA command combination
1242 is required to print this image.
1243  
1244 This function supports 8-dot and 24-dot vertical densities by specifying
1245 the byteDepth parameter as either 1 (8-dot) or 3 (24-dot).
1246  
1247 Precondition:
1248 None
1249  
1250 Parameters:
1251 USB_DATA_POINTER image Pointer to the image bitmap data.
1252 BYTE imageLocation Location of the image bitmap data. Valid values
1253 are USB_PRINTER_TRANSFER_FROM_ROM and
1254 USB_PRINTER_TRANSFER_FROM_RAM.
1255 WORD imageHeight Height of the image in pixels.
1256 WORD imageWidth Width of the image in pixels.
1257 WORD *currentRow The current pixel row. Upon return, this value is
1258 updated to the next pixel row to print.
1259 BYTE byteDepth The byte depth of the print. Valid values are 1
1260 (8-dot vertical density) and 3 (24-dot vertical
1261 density).
1262 BYTE *imageData Pointer to a RAM data buffer that will receive the
1263 manipulated data to send to the printer.
1264  
1265 Returns:
1266 The function returns a pointer to the next byte of image data.
1267  
1268 Example:
1269 The following example code will send a complete bitmapped image to a POS
1270 printer.
1271 <code>
1272 WORD currentRow;
1273 BYTE depthBytes;
1274 BYTE *imageDataPOS;
1275 USB_PRINTER_IMAGE_INFO imageInfo;
1276 BYTE returnCode;
1277 #if defined (__C30__)
1278 BYTE __prog__ *ptr;
1279 ptr = (BYTE __prog__ *)logoMCHP.address;
1280 #elif defined (__PIC32MX__)
1281 const BYTE *ptr;
1282 ptr = (const BYTE *)logoMCHP.address;
1283 #endif
1284  
1285 imageInfo.densityVertical = 24; // 24-dot density
1286 imageInfo.densityHorizontal = 2; // Double density
1287  
1288 // Extract the image height and width
1289 imageInfo.width = ((WORD)ptr[5] << 8) + ptr[4];
1290 imageInfo.height = ((WORD)ptr[3] << 8) + ptr[2];
1291  
1292 depthBytes = imageInfo.densityVertical / 8;
1293 imageDataPOS = (BYTE *)USB_MALLOC( imageInfo.width *
1294 depthBytes );
1295  
1296 if (imageDataPOS == NULL)
1297 {
1298 // Error - not enough heap space
1299 }
1300  
1301 USBHostPrinterCommandWithReadyWait( &returnCode,
1302 printerInfo.deviceAddress, USB_PRINTER_IMAGE_START,
1303 USB_DATA_POINTER_RAM(&imageInfo),
1304 sizeof(USB_PRINTER_IMAGE_INFO ),
1305  
1306  
1307 ptr += 10; // skip the header info
1308  
1309 currentRow = 0;
1310 while (currentRow < imageInfo.height)
1311 {
1312 USBHostPrinterCommandWithReadyWait( &returnCode,
1313 printerInfo.deviceAddress,
1314 USB_PRINTER_IMAGE_DATA_HEADER, USB_NULL,
1315 imageInfo.width, 0 );
1316  
1317 ptr = USBHostPrinterPOSImageDataFormat(
1318 USB_DATA_POINTER_ROM(ptr),
1319 USB_PRINTER_TRANSFER_FROM_ROM, imageInfo.height,
1320 imageInfo.width, &currentRow, depthBytes,
1321 imageDataPOS ).pointerROM;
1322  
1323 USBHostPrinterCommandWithReadyWait( &returnCode,
1324 printerInfo.deviceAddress, USB_PRINTER_IMAGE_DATA,
1325 USB_DATA_POINTER_RAM(imageDataPOS), imageInfo.width,
1326 USB_PRINTER_TRANSFER_COPY_DATA);
1327 }
1328  
1329 USB_FREE( imageDataPOS );
1330  
1331 USBHostPrinterCommandWithReadyWait( &returnCode,
1332 printerInfo.deviceAddress, USB_PRINTER_IMAGE_STOP,
1333 USB_NULL, 0, 0 );
1334 </code>
1335  
1336 Remarks:
1337 This routine currently does not support 36-dot density printing. Since
1338 the output for 36-dot vertical density is identical to 24-dot vertical
1339 density, 24-dot vertical density should be used instead.
1340  
1341 This routine does not yet support reading from external memory.
1342 ***************************************************************************/
1343  
1344 USB_DATA_POINTER USBHostPrinterPOSImageDataFormat( USB_DATA_POINTER image,
1345 BYTE imageLocation, WORD imageHeight, WORD imageWidth, WORD *currentRow,
1346 BYTE byteDepth, BYTE *imageData )
1347 {
1348 BYTE currentByte = 0x00;
1349 WORD j;
1350 WORD k;
1351 WORD m;
1352 BYTE maskHorizontal;
1353 BYTE maskVertical;
1354 BYTE *ptrRAM = NULL;
1355 #if defined( __C30__ )
1356 BYTE __prog__ *ptrROM = NULL;
1357 #elif defined( __PIC32MX__ )
1358 const BYTE *ptrROM = NULL;
1359 #endif
1360 WORD widthBytes;
1361  
1362  
1363 maskHorizontal = 0x80;
1364 switch( imageLocation )
1365 {
1366 case USB_PRINTER_TRANSFER_FROM_ROM:
1367 ptrROM = ((USB_DATA_POINTER)image).pointerROM;
1368 break;
1369  
1370 case USB_PRINTER_TRANSFER_FROM_RAM:
1371 ptrRAM = ((USB_DATA_POINTER)image).pointerRAM;
1372 break;
1373  
1374 //case USB_PRINTER_TRANSFER_FROM_EXTERNAL:
1375 default:
1376 return USB_NULL;
1377 }
1378  
1379 widthBytes = (imageWidth + 7) / 8;
1380  
1381 // 0=dot, 1=no dot
1382 for (j=0; j<imageWidth * byteDepth; j++)
1383 {
1384 imageData[j] = 0xFF;
1385 }
1386  
1387 for (j=0; j<imageWidth; j++)
1388 {
1389 for (m=0; m<byteDepth; m++)
1390 {
1391 maskVertical = 0x80;
1392 for (k=0; k<8; k++)
1393 {
1394 if ((*currentRow + (m*8) + k) < imageHeight)
1395 {
1396 switch( imageLocation )
1397 {
1398 case USB_PRINTER_TRANSFER_FROM_ROM:
1399 currentByte = ptrROM[(widthBytes*(m*8 + k)) + (j/8)];
1400 break;
1401  
1402 case USB_PRINTER_TRANSFER_FROM_RAM:
1403 currentByte = ptrRAM[(widthBytes*(m*8 + k)) + (j/8)];
1404 break;
1405  
1406 //case USB_PRINTER_TRANSFER_FROM_EXTERNAL:
1407 // break;
1408 }
1409 if (!(currentByte & maskHorizontal))
1410 {
1411 imageData[j*byteDepth + m] &= ~maskVertical;
1412 }
1413 }
1414 maskVertical >>= 1;
1415 }
1416 }
1417 maskHorizontal >>= 1;
1418 if (maskHorizontal == 0)
1419 {
1420 maskHorizontal = 0x80;
1421 }
1422 }
1423  
1424 *currentRow += 8*byteDepth;
1425 switch( imageLocation )
1426 {
1427 case USB_PRINTER_TRANSFER_FROM_ROM:
1428 return (USB_DATA_POINTER)(image.pointerROM + (imageWidth * byteDepth));
1429 break;
1430  
1431 case USB_PRINTER_TRANSFER_FROM_RAM:
1432 return (USB_DATA_POINTER)(image.pointerRAM + (imageWidth * byteDepth));
1433 break;
1434  
1435 //case USB_PRINTER_TRANSFER_FROM_EXTERNAL:
1436 default:
1437 return USB_NULL;
1438 }
1439 }
1440  
1441  
1442 // *****************************************************************************
1443 // *****************************************************************************
1444 // Section: Local Functions
1445 // *****************************************************************************
1446 // *****************************************************************************
1447  
1448 #ifdef USB_PRINTER_POS_BARCODE_SUPPORT
1449  
1450 #define NUM_OTHERS_CODABAR 6
1451 #define NUM_OTHERS_CODE39 7
1452  
1453 static char _OthersCodabar[NUM_OTHERS_CODABAR] = { '-', '$', ':', '/', '.', '+' };
1454 static char _OthersCode39[NUM_OTHERS_CODE39] = { '-', '.', ' ', '$', '/', '+', '%' };
1455  
1456 #endif
1457  
1458 /****************************************************************************
1459 Function:
1460 static BYTE _BarcodeCharacterValueCodabar( char c )
1461  
1462 Description:
1463 This function determines the value of a character when determining a
1464 Codabar checksum digit.
1465  
1466 Preconditions:
1467 None
1468  
1469 Parameters:
1470 char c - Character in the bar code.
1471  
1472 Returns:
1473 The value to use for the indicated character when determining the check
1474 digit.
1475  
1476 Remarks:
1477 Use the function _BarcodeValueCharacterCodabar() to perform the reverse
1478 procedure.
1479 ***************************************************************************/
1480  
1481 #ifdef USB_PRINTER_POS_BARCODE_SUPPORT
1482  
1483 static BYTE _BarcodeCharacterValueCodabar( char c )
1484 {
1485 int i;
1486  
1487 if (('0' <= c) && (c <= '9'))
1488 {
1489 return (c - '0');
1490 }
1491  
1492 if (('A' <= c) && (c <= 'D'))
1493 {
1494 return (c - 'A' + 16);
1495 }
1496  
1497 for (i=0; i<NUM_OTHERS_CODABAR; i++)
1498 {
1499 if (c == _OthersCodabar[i])
1500 {
1501 return (i + 10);
1502 }
1503 }
1504  
1505 return 0;
1506 }
1507  
1508 #endif
1509  
1510 /****************************************************************************
1511 Function:
1512 static BYTE _BarcodeCharacterValueCode39( char c )
1513  
1514 Description:
1515 This function determines the value of a character when determining a
1516 Code39 checksum digit.
1517  
1518 Preconditions:
1519 None
1520  
1521 Parameters:
1522 char c - Character in the bar code.
1523  
1524 Returns:
1525 The value to use for the indicated character when determining the check
1526 digit.
1527  
1528 Remarks:
1529 Use the function _BarcodeValueCharacterCode39() to perform the reverse
1530 procedure.
1531 ***************************************************************************/
1532  
1533 #if defined( USB_PRINTER_POS_BARCODE_SUPPORT )
1534  
1535 static BYTE _BarcodeCharacterValueCode39( char c )
1536 {
1537 int i;
1538  
1539 if (('0' <= c) && (c <= '9'))
1540 {
1541 return (c - '0');
1542 }
1543  
1544 if (('A' <= c) && (c <= 'Z'))
1545 {
1546 return (c - 'A' + 10);
1547 }
1548  
1549 for (i=0; i<NUM_OTHERS_CODE39; i++)
1550 {
1551 if (c == _OthersCode39[i])
1552 {
1553 return (i + 36);
1554 }
1555 }
1556  
1557 return 0;
1558 }
1559  
1560 #endif
1561  
1562 /****************************************************************************
1563 Function:
1564 static char _BarcodeValueCharacterCodabar( BYTE v )
1565  
1566 Description:
1567 This function determines the character used to represent a particular
1568 value for a Codabar checksum digit.
1569  
1570 Preconditions:
1571 None
1572  
1573 Parameters:
1574 BYTE v - Value of the required character.
1575  
1576 Returns:
1577 The character to use for the indicated value for the check digit.
1578  
1579 Remarks:
1580 Use the function _BarcodeCharacterValueCodabar() to perform the reverse
1581 procedure.
1582 ***************************************************************************/
1583  
1584 #ifdef USB_PRINTER_POS_BARCODE_SUPPORT
1585  
1586 static char _BarcodeValueCharacterCodabar( BYTE v )
1587 {
1588 if (v > 19)
1589 {
1590 return '0'; // Error
1591 }
1592  
1593 if (v <= 9)
1594 {
1595 return (v + '0');
1596 }
1597  
1598 if ((16 <= v) && (v <= 19))
1599 {
1600 return (v - 16 + 'A');
1601 }
1602  
1603 return _OthersCodabar[v - 10];
1604 }
1605  
1606 #endif
1607  
1608 /****************************************************************************
1609 Function:
1610 static char _BarcodeValueCharacterCode39( BYTE v )
1611  
1612 Description:
1613 This function determines the character used to represent a particular
1614 value for a Code39 checksum digit.
1615  
1616 Preconditions:
1617 None
1618  
1619 Parameters:
1620 BYTE v - Value of the required character.
1621  
1622 Returns:
1623 The character to use for the indicated value for the check digit.
1624  
1625 Remarks:
1626 Use the function _BarcodeCharacterValueCode39() to perform the reverse
1627 procedure.
1628 ***************************************************************************/
1629  
1630 #if defined( USB_PRINTER_POS_BARCODE_SUPPORT )
1631  
1632 static char _BarcodeValueCharacterCode39( BYTE v )
1633 {
1634 if (v > 42)
1635 {
1636 return '0'; // Error
1637 }
1638  
1639 if (v <= 9)
1640 {
1641 return (v + '0');
1642 }
1643  
1644 if ((10 <= v) && (v <= 35))
1645 {
1646 return (v - 10 + 'A');
1647 }
1648  
1649 return _OthersCode39[v - 36];
1650 }
1651  
1652 #endif
1653  
1654 /****************************************************************************
1655 Function:
1656 static BYTE _PrintFontCommand( BYTE printer, BYTE transferFlags )
1657  
1658 Description:
1659 This command sends a complete font command to the printer. It is called
1660 whenever there is a change to the font, the weight, or the underlining.
1661  
1662 Preconditions:
1663 None
1664  
1665 Parameters:
1666 BYTE printer - Index of the target printer.
1667 BYTE transferFlags - Transfer control string.
1668  
1669 Return Values:
1670 USB_PRINTER_SUCCESS - Command completed successfully.
1671 USB_PRINTER_OUT_OF_MEMORY - Not enough dynamic memory to perform the
1672 command.
1673 others - See the return values for the function
1674 USBHostPrinterWrite().
1675  
1676 Remarks:
1677 The font command parameter is comprised of the following bits:
1678 <code>
1679 Bit Value Meaning
1680 -------------------
1681  
1682 1 Font B
1683 1 Reserved
1684 2 Reserved
1685 3 0 Medium weight
1686 1 Bold
1687 4 0 Normal height
1688 1 Double height
1689 5 0 Normal width
1690 1 Double width
1691 6 Reserved
1692 7 0 No underline
1693 1 Underline
1694 </code>
1695 ***************************************************************************/
1696  
1697 static BYTE _PrintFontCommand( BYTE printer, BYTE transferFlags )
1698 {
1699 char *buffer;
1700  
1701 buffer = (char *)USB_MALLOC( 3 );
1702 if (buffer == NULL)
1703 {
1704 return USB_PRINTER_OUT_OF_MEMORY;
1705 }
1706  
1707 buffer[0] = ESC_CHAR;
1708 buffer[1] = '!';
1709 buffer[2] = printerListESCPOS[printer].fontName;
1710  
1711 if (printerListESCPOS[printer].printerFlags.isBold)
1712 {
1713 buffer[2] |= 0x08;
1714 }
1715 if (printerListESCPOS[printer].printerFlags.isUnderlined)
1716 {
1717 buffer[2] |= 0x80;
1718 }
1719  
1720 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
1721 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, 3, transferFlags );
1722 }
1723  
1724  
1725 /****************************************************************************
1726 Function:
1727 static BYTE _PrintStaticCommand( BYTE printer, char *command, BYTE transferFlags )
1728  
1729 Description:
1730 This function sends a hard-coded command to the printer. Many printer
1731 commands are constant text strings that require no parameters. This
1732 routine allocates dynamic memory for a copy of the command, copies the
1733 command into the string, and sends the command to the printer.
1734  
1735 Preconditions:
1736 None
1737  
1738 Parameters:
1739 BYTE printer - Index of the target printer.
1740 char *command - Printer command string.
1741 BYTE transferFlags - Transfer control string.
1742  
1743 Return Values:
1744 USB_PRINTER_SUCCESS - Command completed successfully.
1745 USB_PRINTER_OUT_OF_MEMORY - Not enough dynamic memory to perform the
1746 command.
1747 others - See the return values for the function
1748 USBHostPrinterWrite().
1749  
1750 Remarks:
1751 None
1752 ***************************************************************************/
1753  
1754 static BYTE _PrintStaticCommand( BYTE printer, char *command, BYTE transferFlags )
1755 {
1756 char *buffer;
1757  
1758 USBHOSTPRINTER_SETFLAG_COPY_DATA( transferFlags );
1759  
1760 buffer = (char *)USB_MALLOC( strlen(command) + 1 );
1761 if (buffer == NULL)
1762 {
1763 return USB_PRINTER_OUT_OF_MEMORY;
1764 }
1765 strcpy( buffer, command );
1766 return USBHostPrinterWrite( printerListESCPOS[printer].deviceAddress, buffer, strlen(buffer), transferFlags );
1767 }
1768  
1769  
1770 #endif
1771  
{BLAME END}
{FOOTER START}

Powered by WebSVN v2.8.3