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

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