1.1 diff -r 81b0d2551776 -r 4aec27d9d4da src/ptouch.c 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/src/ptouch.c Sat Aug 01 12:42:34 2009 +0100 1.4 @@ -0,0 +1,129 @@ 1.5 +/**************************************************************************** 1.6 + * Project: P-touch printer driver library 1.7 + * Developer: Philip Pemberton 1.8 + * Purpose: Make Brother P-touch (PT-series) printers do something besides 1.9 + * gather dust. 1.10 + * 1.11 + * Currently supports: 1.12 + * PT-2450DX 1.13 + ****************************************************************************/ 1.14 + 1.15 +// TODO: disable 1.16 +#define DEBUG 1.17 + 1.18 +#include <stdio.h> 1.19 +#include <stdlib.h> 1.20 +#include <string.h> 1.21 +#include "hexdump.h" 1.22 +#include "pt_image.h" 1.23 +#include "ptouch.h" 1.24 + 1.25 +#define ESC 0x1b 1.26 + 1.27 +pt_Device *pt_Initialise(char *path) 1.28 +{ 1.29 + pt_Device *dev; 1.30 + FILE *prn; 1.31 + 1.32 + // Try and open the printer device 1.33 + prn = fopen(path, "r+b"); 1.34 + if (prn == NULL) { 1.35 + return NULL; 1.36 + } 1.37 + 1.38 + // Printer device open, send an init command and read the status 1.39 + fprintf(prn, "%c%c", ESC, '@'); 1.40 + 1.41 + // Allocate memory for the device block 1.42 + dev = malloc(sizeof(pt_Device)); 1.43 + if (dev == NULL) { 1.44 + fclose(prn); 1.45 + return NULL; 1.46 + } 1.47 + 1.48 + // Store the file pointer 1.49 + dev->fp = prn; 1.50 + 1.51 + // Memory allocation OK, now get the printer's status 1.52 + if (pt_GetStatus(dev) == 0) { 1.53 + return dev; 1.54 + } else { 1.55 + free(dev); 1.56 + return NULL; 1.57 + } 1.58 +} 1.59 + 1.60 +int pt_GetStatus(pt_Device *dev) 1.61 +{ 1.62 + // REQUEST STATUS 1.63 + fprintf(dev->fp, "%c%c%c", ESC, 'i', 'S'); 1.64 + 1.65 + // Read status buffer from printer 1.66 + unsigned char buf[32]; 1.67 + int timeout = 128; 1.68 + do { 1.69 + fread(buf, 1, 32, dev->fp); 1.70 + } while ((buf[0] != 0x80) && (timeout-- > 0)); 1.71 + 1.72 + // Check for timeout 1.73 + if (timeout == 0) { 1.74 + // Timeout 1.75 + return -1; 1.76 + } 1.77 + 1.78 +#ifdef DEBUG 1.79 + printf("DEBUG: Printer status buffer = \n"); 1.80 + hex_dump(buf, 32); 1.81 +#endif 1.82 + 1.83 + // Decode the status buffer, store the results in the device object 1.84 + dev->headMark = buf[0]; 1.85 + dev->size = buf[1]; 1.86 + dev->errorInfo[0] = buf[8]; 1.87 + dev->errorInfo[1] = buf[9]; 1.88 + dev->mediaWidth = buf[10]; 1.89 + dev->mediaType = buf[11]; 1.90 + dev->mediaLength = buf[17]; 1.91 + dev->statusType = buf[18]; 1.92 + dev->phaseType = buf[19]; 1.93 + dev->phaseHi = buf[20]; 1.94 + dev->phaseLo = buf[21]; 1.95 + dev->notification = buf[22]; 1.96 + 1.97 + // Operation succeeded 1.98 + return 0; 1.99 +} 1.100 + 1.101 +// TODO: print options struct parameter (e.g. fullcut, halfcut, print res, 1.102 +// 1.103 +int pt_Print(pt_Device *dev, pt_Image *image) 1.104 +{ 1.105 + // TODO: trap dev == NULL 1.106 + // TODO: trap image == NULL 1.107 + // TODO: trap image.height > printhead.height 1.108 + // TODO: trap image.height <= 0 1.109 + // TODO: trap image.width <= 0 1.110 + // 1.111 + // allocate print buffer 1.112 + // 1.113 + // pack pixels -- 8 pixels => 1 byte 1.114 + // 1.115 + // compress print data (packbits) 1.116 + // 1.117 + // send print buffer to printer 1.118 + // 1.119 + // free print buffer 1.120 +} 1.121 + 1.122 +void pt_Close(pt_Device *dev) 1.123 +{ 1.124 + // Sanity check -- make sure dev is not null 1.125 + if (dev == NULL) return; 1.126 + 1.127 + // Close the printer stream 1.128 + fclose(dev->fp); 1.129 + 1.130 + // Release the memory allocated to the status buffer 1.131 + free(dev); 1.132 +} 1.133 +