src/ptouch.c

Sat, 01 Aug 2009 12:44:09 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Sat, 01 Aug 2009 12:44:09 +0100
changeset 4
5edfbd3e7a46
parent 3
4aec27d9d4da
child 9
ebce4a7615e9
permissions
-rw-r--r--

update makefile

     1 /****************************************************************************
     2  * Project:   P-touch printer driver library
     3  * Developer: Philip Pemberton
     4  * Purpose:   Make Brother P-touch (PT-series) printers do something besides
     5  *            gather dust.
     6  *
     7  *            Currently supports:
     8  *              PT-2450DX
     9  ****************************************************************************/
    11 // TODO: disable
    12 #define DEBUG
    14 #include <stdio.h>
    15 #include <stdlib.h>
    16 #include <string.h>
    17 #include "hexdump.h"
    18 #include "pt_image.h"
    19 #include "ptouch.h"
    21 #define ESC 0x1b
    23 pt_Device *pt_Initialise(char *path)
    24 {
    25 	pt_Device	*dev;
    26 	FILE		*prn;
    28 	// Try and open the printer device
    29 	prn = fopen(path, "r+b");
    30 	if (prn == NULL) {
    31 		return NULL;
    32 	}
    34 	// Printer device open, send an init command and read the status
    35 	fprintf(prn, "%c%c", ESC, '@');
    37 	// Allocate memory for the device block
    38 	dev = malloc(sizeof(pt_Device));
    39 	if (dev == NULL) {
    40 		fclose(prn);
    41 		return NULL;
    42 	}
    44 	// Store the file pointer
    45 	dev->fp = prn;
    47 	// Memory allocation OK, now get the printer's status
    48 	if (pt_GetStatus(dev) == 0) {
    49 		return dev;
    50 	} else {
    51 		free(dev);
    52 		return NULL;
    53 	}
    54 }
    56 int pt_GetStatus(pt_Device *dev)
    57 {
    58 	// REQUEST STATUS
    59 	fprintf(dev->fp, "%c%c%c", ESC, 'i', 'S');
    61 	// Read status buffer from printer
    62 	unsigned char buf[32];
    63 	int timeout = 128;
    64 	do {
    65 		fread(buf, 1, 32, dev->fp);
    66 	} while ((buf[0] != 0x80) && (timeout-- > 0));
    68 	// Check for timeout
    69 	if (timeout == 0) {
    70 		// Timeout
    71 		return -1;
    72 	}
    74 #ifdef DEBUG
    75 	printf("DEBUG: Printer status buffer = \n");
    76 	hex_dump(buf, 32);
    77 #endif
    79 	// Decode the status buffer, store the results in the device object
    80 	dev->headMark     = buf[0];
    81 	dev->size         = buf[1];
    82 	dev->errorInfo[0] = buf[8];
    83 	dev->errorInfo[1] = buf[9];
    84 	dev->mediaWidth   = buf[10];
    85 	dev->mediaType    = buf[11];
    86 	dev->mediaLength  = buf[17];
    87 	dev->statusType   = buf[18];
    88 	dev->phaseType    = buf[19];
    89 	dev->phaseHi      = buf[20];
    90 	dev->phaseLo      = buf[21];
    91 	dev->notification = buf[22];
    93 	// Operation succeeded
    94 	return 0;
    95 }
    97 // TODO: print options struct parameter (e.g. fullcut, halfcut, print res,
    98 //
    99 int pt_Print(pt_Device *dev, pt_Image *image)
   100 {
   101 	// TODO: trap dev == NULL
   102 	// TODO: trap image == NULL
   103 	// TODO: trap image.height > printhead.height
   104 	// TODO: trap image.height <= 0
   105 	// TODO: trap image.width <= 0
   106 	//
   107 	// allocate print buffer
   108 	//
   109 	// pack pixels -- 8 pixels => 1 byte
   110 	//
   111 	// compress print data (packbits)
   112 	//
   113 	// send print buffer to printer
   114 	//
   115 	// free print buffer
   116 }
   118 void pt_Close(pt_Device *dev)
   119 {
   120 	// Sanity check -- make sure dev is not null
   121 	if (dev == NULL) return;
   123 	// Close the printer stream
   124 	fclose(dev->fp);
   126 	// Release the memory allocated to the status buffer
   127 	free(dev);
   128 }