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