Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /***************************************************************************** |
2 | |||
3 | Microchip USB Host Printer Client Driver, Graphics Library Interfaces Layer |
||
4 | |||
5 | Summary: |
||
6 | This file contains the routines needed to utilize the Microchip Graphics |
||
7 | Library functions to create graphic images on a USB printer. |
||
8 | |||
9 | Description: |
||
10 | This file contains the routines needed to utilize the Microchip Graphics |
||
11 | Library functions to create graphic images on a USB printer. |
||
12 | |||
13 | The label USE_GRAPHICS_LIBRARY_PRINTER_INTERFACE must be defining in the |
||
14 | USB configuration header file usb_config.h to utilize these functions. |
||
15 | |||
16 | Remarks: |
||
17 | None |
||
18 | |||
19 | ******************************************************************************/ |
||
20 | //DOM-IGNORE-BEGIN |
||
21 | /****************************************************************************** |
||
22 | |||
23 | FileName: usb_host_printer_primitives.c |
||
24 | Dependencies: None |
||
25 | Processor: PIC24F/PIC32MX |
||
26 | Compiler: C30/C32 |
||
27 | Company: Microchip Technology, Inc. |
||
28 | |||
29 | Software License Agreement |
||
30 | |||
31 | The software supplied herewith by Microchip Technology Incorporated |
||
32 | (the Company) for its PICmicro® Microcontroller is intended and |
||
33 | supplied to you, the Companys customer, for use solely and |
||
34 | exclusively on Microchip PICmicro Microcontroller products. The |
||
35 | software is owned by the Company and/or its supplier, and is |
||
36 | protected under applicable copyright laws. All rights are reserved. |
||
37 | Any use in violation of the foregoing restrictions may subject the |
||
38 | user to criminal sanctions under applicable laws, as well as to |
||
39 | civil liability for the breach of the terms and conditions of this |
||
40 | license. |
||
41 | |||
42 | THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
43 | WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
44 | TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
45 | PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
46 | IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
47 | CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
48 | |||
49 | Change History: |
||
50 | Rev Description |
||
51 | ---------- ---------------------------------------------------------- |
||
52 | 2.6 - 2.6a No change |
||
53 | |||
54 | 2.7a Provided macro wrapped versions of malloc() and free() |
||
55 | so that a user can override these functions easily. |
||
56 | |||
57 | *******************************************************************************/ |
||
58 | //DOM-IGNORE-END |
||
59 | |||
60 | |||
61 | #include <stdlib.h> |
||
62 | #include <string.h> |
||
63 | #include "Graphics\Graphics.h" |
||
64 | #include "USB\usb.h" |
||
65 | #include "usb\usb_host_printer_primitives.h" |
||
66 | |||
67 | //#define DEBUG_MODE |
||
68 | #ifdef DEBUG_MODE |
||
69 | #include "uart2.h" |
||
70 | #endif |
||
71 | |||
72 | #ifndef USB_MALLOC |
||
73 | #define USB_MALLOC(size) malloc(size) |
||
74 | #endif |
||
75 | |||
76 | #ifndef USB_FREE |
||
77 | #define USB_FREE(ptr) free(ptr) |
||
78 | #endif |
||
79 | |||
80 | #define USB_FREE_AND_CLEAR(ptr) {USB_FREE(ptr); ptr = NULL;} |
||
81 | |||
82 | #ifdef USE_GRAPHICS_LIBRARY_PRINTER_INTERFACE |
||
83 | |||
84 | |||
85 | // ***************************************************************************** |
||
86 | // ***************************************************************************** |
||
87 | // Section: Subroutines |
||
88 | // ***************************************************************************** |
||
89 | // ***************************************************************************** |
||
90 | |||
91 | /**************************************************************************** |
||
92 | Function: |
||
93 | SHORT PrintScreen( BYTE address, USB_PRINT_SCREEN_INFO *printScreenInfo ) |
||
94 | |||
95 | Summary: |
||
96 | This routine will extract the image that is currently on the specified |
||
97 | portion of the graphics display, and print it at the specified location. |
||
98 | |||
99 | Description: |
||
100 | This routine is intended for use in an application that is using the |
||
101 | Graphics Library to control a graphics display. This routine will |
||
102 | extract the image that is currently on the specified portion of |
||
103 | the graphics display, and print it at the specified location. |
||
104 | Since the display may be in color and the printer can print only black |
||
105 | and white, the pixel color to interpret as black must be specified in the |
||
106 | USB_PRINT_SCREEN_INFO structure. |
||
107 | |||
108 | The function can be compiled as either a blocking function or a |
||
109 | non-blocking function. When compiled as a blocking function, the routine |
||
110 | will wait to enqueue all printer instructions. If an error occurs, then |
||
111 | this function will return the error. If all printer instructions are |
||
112 | enqueued successfully, the function will return -1. When compiled as a |
||
113 | non-blocking function, this function will return 0 if the operation is |
||
114 | proceeding correctly but has not yet completed. The application must |
||
115 | continue to call this function, with the same parameters, until a |
||
116 | non-zero value is returned. A value of -1 indicates that all printer |
||
117 | instructions have been enqueued successfully. Any other value is an error |
||
118 | code, and the state machine will be set back to the beginning state. |
||
119 | |||
120 | Precondition: |
||
121 | None |
||
122 | |||
123 | Parameters: |
||
124 | BYTE address - USB address of the printer. |
||
125 | USB_PRINT_SCREEN_INFO *printScreenInfo - Information about the screen |
||
126 | area to print, how to interpret the screen image, |
||
127 | and how and where to print the image. Note that |
||
128 | the width and height members of the structure do |
||
129 | not need to be filled in by the application. |
||
130 | |||
131 | Return Values: |
||
132 | |||
133 | complete, but is proceeding normally. |
||
134 | (-1) - Image output was completed successfully. |
||
135 | other - Printing was aborted due to an error. See the return values |
||
136 | for USBHostPrinterCommand(). Note that the return code |
||
137 | USB_PRINTER_SUCCESS will not be returned. Instead, (-1) will |
||
138 | be returned upon successful completion. |
||
139 | |||
140 | Remarks: |
||
141 | None |
||
142 | ***************************************************************************/ |
||
143 | |||
144 | #define PRINT_SCREEN_STATE_BEGIN 0 |
||
145 | #define PRINT_SCREEN_STATE_SEND_START 1 |
||
146 | #define PRINT_SCREEN_STATE_SEND_HEADER 2 |
||
147 | #define PRINT_SCREEN_STATE_SEND_DATA 3 |
||
148 | #define PRINT_SCREEN_STATE_SEND_STOP 4 |
||
149 | |||
150 | |||
151 | SHORT PrintScreen( BYTE address, USB_PRINT_SCREEN_INFO *printScreenInfo ) |
||
152 | { |
||
153 | #ifdef USE_NONBLOCKING_CONFIG |
||
154 | static WORD imageLine; |
||
155 | WORD imagePixel; |
||
156 | static BYTE lineDepth; |
||
157 | BYTE mask; |
||
158 | static BYTE *oneRow = NULL; |
||
159 | WORD size; |
||
160 | static BYTE state = PRINT_SCREEN_STATE_BEGIN; |
||
161 | BYTE returnCode; |
||
162 | |||
163 | if (!USBHostPrinterCommandReady( address )) |
||
164 | { |
||
165 | return 0; |
||
166 | } |
||
167 | |||
168 | switch (state) |
||
169 | { |
||
170 | case PRINT_SCREEN_STATE_BEGIN: |
||
171 | printScreenInfo->printerInfo.width = printScreenInfo->xR - printScreenInfo->xL + 1; |
||
172 | printScreenInfo->printerInfo.height = printScreenInfo->yB - printScreenInfo->yT + 1; |
||
173 | |||
174 | lineDepth = 1; |
||
175 | if (printScreenInfo->printerType.supportFlags.supportsPOS) |
||
176 | { |
||
177 | if (printScreenInfo->printerInfo.densityVertical == 24) |
||
178 | { |
||
179 | lineDepth = 3; |
||
180 | } |
||
181 | else if (printScreenInfo->printerInfo.densityVertical == 36) |
||
182 | { |
||
183 | lineDepth = 5; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | imageLine = 0; |
||
188 | if (oneRow != NULL) |
||
189 | { |
||
190 | USB_FREE( oneRow ); |
||
191 | } |
||
192 | |||
193 | if (printScreenInfo->printerType.supportFlags.supportsPOS) |
||
194 | { |
||
195 | size = printScreenInfo->printerInfo.width * lineDepth; |
||
196 | } |
||
197 | else |
||
198 | { |
||
199 | size = (printScreenInfo->printerInfo.width + 7) / 8 ; |
||
200 | } |
||
201 | |||
202 | oneRow = (BYTE *)USB_MALLOC( size ); |
||
203 | if (oneRow == NULL) |
||
204 | { |
||
205 | return USB_PRINTER_OUT_OF_MEMORY; |
||
206 | } |
||
207 | |||
208 | state++; |
||
209 | // Fall through. |
||
210 | |||
211 | case PRINT_SCREEN_STATE_SEND_START: |
||
212 | returnCode = USBHostPrinterCommand( address, USB_PRINTER_IMAGE_START, |
||
213 | USB_DATA_POINTER_RAM(&(printScreenInfo->printerInfo)), sizeof(printScreenInfo->printerInfo), 0 ); |
||
214 | if (returnCode) |
||
215 | { |
||
216 | USB_FREE( oneRow ); |
||
217 | state = PRINT_SCREEN_STATE_BEGIN; |
||
218 | return returnCode; |
||
219 | } |
||
220 | break; |
||
221 | |||
222 | case PRINT_SCREEN_STATE_SEND_HEADER: |
||
223 | // Read the pixels for the current row of data. |
||
224 | if (printScreenInfo->printerType.supportFlags.supportsPOS) |
||
225 | { |
||
226 | WORD i; |
||
227 | WORD j; |
||
228 | |||
229 | memset( oneRow, 0xFF, printScreenInfo->printerInfo.width * lineDepth ); |
||
230 | |||
231 | mask = 0x80; |
||
232 | for (imagePixel=0; imagePixel<printScreenInfo->printerInfo.width; imagePixel++) |
||
233 | { |
||
234 | for (i=0; i<lineDepth; i++) |
||
235 | { |
||
236 | for (j=0; j<8; j++) |
||
237 | { |
||
238 | if (((imageLine + (i*8) + j) < printScreenInfo->printerInfo.height) && |
||
239 | (GetPixel( printScreenInfo->xL + imagePixel, printScreenInfo->yT + imageLine + (i*8) + j) == printScreenInfo->colorBlack)) |
||
240 | { |
||
241 | oneRow[imagePixel*lineDepth + i] &= ~mask; |
||
242 | } |
||
243 | mask >>= 1; |
||
244 | if (mask == 0) |
||
245 | { |
||
246 | mask = 0x80; |
||
247 | } |
||
248 | } |
||
249 | } |
||
250 | } |
||
251 | } |
||
252 | else |
||
253 | { |
||
254 | memset( oneRow, 0, (printScreenInfo->printerInfo.width + 7) / 8 ); |
||
255 | |||
256 | mask = 0x80; |
||
257 | for (imagePixel=0; imagePixel<printScreenInfo->printerInfo.width; imagePixel++) |
||
258 | { |
||
259 | if (GetPixel( printScreenInfo->xL + imagePixel, printScreenInfo->yT + imageLine ) != printScreenInfo->colorBlack) |
||
260 | { |
||
261 | oneRow[imagePixel/8] |= mask; |
||
262 | } |
||
263 | mask >>= 1; |
||
264 | if (mask == 0) |
||
265 | { |
||
266 | mask = 0x80; |
||
267 | } |
||
268 | } |
||
269 | |||
270 | #ifdef DEBUG_MODE |
||
271 | UART2PrintString( "\r\n" ); |
||
272 | for (imagePixel=0; imagePixel<printScreenInfo->printerInfo.width; imagePixel+=8) |
||
273 | { |
||
274 | UART2PutHex( oneRow[imagePixel/8] ); |
||
275 | } |
||
276 | UART2PrintString( "\r\n" ); |
||
277 | #endif |
||
278 | } |
||
279 | |||
280 | returnCode = USBHostPrinterCommand( address, USB_PRINTER_IMAGE_DATA_HEADER, USB_NULL, printScreenInfo->printerInfo.width, 0 ); |
||
281 | if (returnCode) |
||
282 | { |
||
283 | USB_FREE( oneRow ); |
||
284 | state = PRINT_SCREEN_STATE_BEGIN; |
||
285 | return returnCode; |
||
286 | } |
||
287 | break; |
||
288 | |||
289 | case PRINT_SCREEN_STATE_SEND_DATA: |
||
290 | returnCode = USBHostPrinterCommand( address, USB_PRINTER_IMAGE_DATA, USB_DATA_POINTER_RAM(oneRow), printScreenInfo->printerInfo.width, USB_PRINTER_TRANSFER_COPY_DATA); |
||
291 | if (returnCode) |
||
292 | { |
||
293 | USB_FREE( oneRow ); |
||
294 | state = PRINT_SCREEN_STATE_BEGIN; |
||
295 | return returnCode; |
||
296 | } |
||
297 | |||
298 | if (printScreenInfo->printerType.supportFlags.supportsPOS) |
||
299 | { |
||
300 | if (printScreenInfo->printerInfo.densityVertical == 24) |
||
301 | { |
||
302 | imageLine += 24; |
||
303 | } |
||
304 | else if (printScreenInfo->printerInfo.densityVertical == 36) |
||
305 | { |
||
306 | imageLine += 36; |
||
307 | } |
||
308 | else |
||
309 | { |
||
310 | imageLine += 8; |
||
311 | } |
||
312 | } |
||
313 | else |
||
314 | { |
||
315 | imageLine++; |
||
316 | } |
||
317 | if (imageLine < printScreenInfo->printerInfo.height) |
||
318 | { |
||
319 | state = PRINT_SCREEN_STATE_SEND_HEADER; |
||
320 | return 0; |
||
321 | } |
||
322 | break; |
||
323 | |||
324 | case PRINT_SCREEN_STATE_SEND_STOP: |
||
325 | returnCode = USBHostPrinterCommand( address, USB_PRINTER_IMAGE_STOP, USB_NULL, 0, 0 ); |
||
326 | if (!returnCode) |
||
327 | { |
||
328 | returnCode = -1; |
||
329 | } |
||
330 | |||
331 | USB_FREE( oneRow ); |
||
332 | state = PRINT_SCREEN_STATE_BEGIN; |
||
333 | return returnCode; |
||
334 | break; |
||
335 | } |
||
336 | |||
337 | state ++; |
||
338 | return 0; |
||
339 | |||
340 | #else |
||
341 | |||
342 | WORD imageLine; |
||
343 | WORD imagePixel; |
||
344 | BYTE lineDepth; |
||
345 | BYTE mask; |
||
346 | BYTE *oneRow; |
||
347 | BYTE returnCode; |
||
348 | WORD size; |
||
349 | |||
350 | printScreenInfo->printerInfo.width = printScreenInfo->xR - printScreenInfo->xL + 1; |
||
351 | printScreenInfo->printerInfo.height = printScreenInfo->yB - printScreenInfo->yT + 1; |
||
352 | |||
353 | lineDepth = 1; |
||
354 | if (printScreenInfo->printerType.supportFlags.supportsPOS) |
||
355 | { |
||
356 | if (printScreenInfo->printerInfo.densityVertical == 24) |
||
357 | { |
||
358 | lineDepth = 3; |
||
359 | } |
||
360 | else if (printScreenInfo->printerInfo.densityVertical == 36) |
||
361 | { |
||
362 | lineDepth = 5; |
||
363 | } |
||
364 | } |
||
365 | |||
366 | if (printScreenInfo->printerType.supportFlags.supportsPOS) |
||
367 | { |
||
368 | size = printScreenInfo->printerInfo.width * lineDepth; |
||
369 | } |
||
370 | else |
||
371 | { |
||
372 | size = (printScreenInfo->printerInfo.width + 7) / 8 ; |
||
373 | } |
||
374 | |||
375 | oneRow = (BYTE *)USB_MALLOC( size ); |
||
376 | if (oneRow == NULL) |
||
377 | { |
||
378 | return USB_PRINTER_OUT_OF_MEMORY; |
||
379 | } |
||
380 | |||
381 | USBHostPrinterCommandWithReadyWait( &returnCode, address, USB_PRINTER_IMAGE_START, |
||
382 | USB_DATA_POINTER_RAM(&(printScreenInfo->printerInfo)), sizeof(printScreenInfo->printerInfo), 0 ); |
||
383 | if (!returnCode) |
||
384 | { |
||
385 | imageLine = 0; |
||
386 | while (!returnCode && (imageLine < printScreenInfo->printerInfo.height)) |
||
387 | { |
||
388 | if (printScreenInfo->printerType.supportFlags.supportsPOS) |
||
389 | { |
||
390 | WORD i; |
||
391 | WORD j; |
||
392 | |||
393 | // Read the pixels for the current wide row of data. |
||
394 | memset( oneRow, 0xFF, printScreenInfo->printerInfo.width * lineDepth ); |
||
395 | mask = 0x80; |
||
396 | for (imagePixel=0; imagePixel<printScreenInfo->printerInfo.width; imagePixel++) |
||
397 | { |
||
398 | for (i=0; i<lineDepth; i++) |
||
399 | { |
||
400 | for (j=0; j<8; j++) |
||
401 | { |
||
402 | if (((imageLine + (i*8) + j) < printScreenInfo->printerInfo.height) && |
||
403 | (GetPixel( printScreenInfo->xL + imagePixel, printScreenInfo->yT + imageLine + (i*8) + j) == printScreenInfo->colorBlack)) |
||
404 | { |
||
405 | oneRow[imagePixel*lineDepth + i] &= ~mask; |
||
406 | } |
||
407 | mask >>= 1; |
||
408 | if (mask == 0) |
||
409 | { |
||
410 | mask = 0x80; |
||
411 | } |
||
412 | } |
||
413 | } |
||
414 | } |
||
415 | imageLine += printScreenInfo->printerInfo.densityVertical; |
||
416 | } |
||
417 | else |
||
418 | { |
||
419 | // Read the pixels for the current row of data. |
||
420 | memset( oneRow, 0, (printScreenInfo->printerInfo.width + 7) / 8 ); |
||
421 | mask = 0x80; |
||
422 | for (imagePixel=0; imagePixel<printScreenInfo->printerInfo.width; imagePixel++) |
||
423 | { |
||
424 | if (GetPixel( printScreenInfo->xL + imagePixel, printScreenInfo->yT + imageLine ) != printScreenInfo->colorBlack) |
||
425 | { |
||
426 | oneRow[imagePixel/8] |= mask; |
||
427 | } |
||
428 | mask >>= 1; |
||
429 | if (mask == 0) |
||
430 | { |
||
431 | mask = 0x80; |
||
432 | } |
||
433 | } |
||
434 | |||
435 | #ifdef DEBUG_MODE |
||
436 | UART2PrintString( "\r\n" ); |
||
437 | for (imagePixel=0; imagePixel<printScreenInfo->printerInfo.width; imagePixel+=8) |
||
438 | { |
||
439 | UART2PutHex( oneRow[imagePixel/8] ); |
||
440 | } |
||
441 | UART2PrintString( "\r\n" ); |
||
442 | #endif |
||
443 | |||
444 | imageLine ++; |
||
445 | } |
||
446 | |||
447 | USBHostPrinterCommandWithReadyWait( &returnCode, address, USB_PRINTER_IMAGE_DATA_HEADER, USB_NULL, printScreenInfo->printerInfo.width, 0 ); |
||
448 | if (!returnCode) |
||
449 | { |
||
450 | USBHostPrinterCommandWithReadyWait( &returnCode, address, USB_PRINTER_IMAGE_DATA, USB_DATA_POINTER_RAM(oneRow), printScreenInfo->printerInfo.width, USB_PRINTER_TRANSFER_COPY_DATA); |
||
451 | } |
||
452 | } |
||
453 | |||
454 | if (!returnCode) |
||
455 | { |
||
456 | USBHostPrinterCommandWithReadyWait( &returnCode, address, USB_PRINTER_IMAGE_STOP, USB_NULL, 0, 0 ); |
||
457 | } |
||
458 | } |
||
459 | |||
460 | USB_FREE( oneRow ); |
||
461 | if (!returnCode) |
||
462 | { |
||
463 | returnCode = -1; // The image completed successfully. |
||
464 | } |
||
465 | return returnCode; |
||
466 | |||
467 | #endif |
||
468 | } |
||
469 | |||
470 | |||
471 | |||
472 | #endif |
||
473 |
Powered by WebSVN v2.8.3