src/ptouch.c

Wed, 05 Aug 2009 15:15:47 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Wed, 05 Aug 2009 15:15:47 +0100
changeset 15
e5577dd259c6
parent 14
088286f9e1e4
child 19
b7fe751ea60d
permissions
-rw-r--r--

multiple fixes -- now prints successfully to a PT-2450DX.
FIX: job options were being set in GetStatus, now moved to their rightful home in Initialise()
REFACTOR: small mods to structure of Initialise, now uses PT_ERR_* consts, calls pt_Close on failure, exit logic slightly changed.
FIX: debug options should NOT have been set in Hg source (especially SKIP_STATUS_READBACK), now fixed.

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@15 12 //#define DEBUG
philpem@3 13
philpem@14 14 // This debug option forces Request Status to always "see" a good status
philpem@14 15 // block. Mostly useful for testing using write-to-file mode.
philpem@15 16 //#define DEBUG_SKIP_STATUS_READ
philpem@14 17
philpem@3 18 #include <stdio.h>
philpem@3 19 #include <stdlib.h>
philpem@9 20 #include <stdbool.h>
philpem@3 21 #include <string.h>
philpem@9 22 #include <gd.h>
philpem@3 23 #include "hexdump.h"
philpem@3 24 #include "ptouch.h"
philpem@3 25
philpem@3 26 #define ESC 0x1b
philpem@3 27
philpem@3 28 pt_Device *pt_Initialise(char *path)
philpem@3 29 {
philpem@3 30 pt_Device *dev;
philpem@3 31 FILE *prn;
philpem@3 32
philpem@3 33 // Try and open the printer device
philpem@3 34 prn = fopen(path, "r+b");
philpem@3 35 if (prn == NULL) {
philpem@3 36 return NULL;
philpem@3 37 }
philpem@3 38
philpem@3 39 // Printer device open, send an init command and read the status
philpem@3 40 fprintf(prn, "%c%c", ESC, '@');
philpem@3 41
philpem@3 42 // Allocate memory for the device block
philpem@3 43 dev = malloc(sizeof(pt_Device));
philpem@3 44 if (dev == NULL) {
philpem@3 45 fclose(prn);
philpem@3 46 return NULL;
philpem@3 47 }
philpem@3 48
philpem@3 49 // Store the file pointer
philpem@3 50 dev->fp = prn;
philpem@3 51
philpem@3 52 // Memory allocation OK, now get the printer's status
philpem@15 53 if (pt_GetStatus(dev) != PT_ERR_SUCCESS) {
philpem@15 54 // GetStatus failed, close the device and exit.
philpem@15 55 pt_Close(dev);
philpem@3 56 return NULL;
philpem@3 57 }
philpem@15 58
philpem@15 59 // Set printing parameters to defaults --
philpem@15 60 // Mirror off
philpem@15 61 // Autocut off
philpem@15 62 dev->mirror = false;
philpem@15 63 dev->autocut = false;
philpem@15 64
philpem@15 65 return dev;
philpem@3 66 }
philpem@3 67
philpem@3 68 int pt_GetStatus(pt_Device *dev)
philpem@3 69 {
philpem@14 70 #ifdef DEBUG_SKIP_STATUS_READ
philpem@14 71 unsigned char buf[32];
philpem@14 72 memset(buf, 0x00, 32);
philpem@14 73 buf[0] = 0x80;
philpem@14 74 buf[1] = 0x20;
philpem@14 75 buf[2] = 0x42;
philpem@14 76 buf[3] = 0x30;
philpem@14 77 buf[4] = 0x4b;
philpem@14 78 buf[5] = 0x30;
philpem@14 79 buf[10] = 0x0c;
philpem@14 80 buf[11] = 0x01;
philpem@14 81 #else
philpem@3 82 // REQUEST STATUS
philpem@3 83 fprintf(dev->fp, "%c%c%c", ESC, 'i', 'S');
philpem@3 84
philpem@3 85 // Read status buffer from printer
philpem@3 86 unsigned char buf[32];
philpem@3 87 int timeout = 128;
philpem@3 88 do {
philpem@3 89 fread(buf, 1, 32, dev->fp);
philpem@3 90 } while ((buf[0] != 0x80) && (timeout-- > 0));
philpem@3 91
philpem@3 92 // Check for timeout
philpem@3 93 if (timeout == 0) {
philpem@3 94 // Timeout
philpem@9 95 return PT_ERR_TIMEOUT;
philpem@3 96 }
philpem@14 97 #endif
philpem@3 98
philpem@3 99 #ifdef DEBUG
philpem@3 100 printf("DEBUG: Printer status buffer = \n");
philpem@3 101 hex_dump(buf, 32);
philpem@3 102 #endif
philpem@3 103
philpem@3 104 // Decode the status buffer, store the results in the device object
philpem@3 105 dev->errorInfo[0] = buf[8];
philpem@3 106 dev->errorInfo[1] = buf[9];
philpem@3 107 dev->mediaWidth = buf[10];
philpem@3 108 dev->mediaType = buf[11];
philpem@3 109 dev->mediaLength = buf[17];
philpem@3 110 dev->statusType = buf[18];
philpem@3 111 dev->phaseType = buf[19];
philpem@3 112 dev->phaseHi = buf[20];
philpem@3 113 dev->phaseLo = buf[21];
philpem@3 114 dev->notification = buf[22];
philpem@3 115
philpem@9 116 // Set pixel width (label width in pixels)
philpem@9 117 if (dev->mediaWidth >= 24) {
philpem@9 118 // Label tape is 24mm or wider. Print head is 128 dots at 180dpi,
philpem@9 119 // which is 18.06mm. Thus, we can only print on the centre 18mm
philpem@9 120 // of a tape that is wider than 18mm.
philpem@9 121 dev->pixelWidth = 128;
philpem@9 122 } else {
philpem@9 123 // Print head is 180dpi. Pixel size is mediaWidth * dpi. If we
philpem@9 124 // multiply by ten, then divide by 254, we can avoid using
philpem@9 125 // floating point to convert from inches to mm. The -2 is a
philpem@9 126 // safety margin -- one pixel on either side of the label.
philpem@9 127 // This is far closer than Brother suggest, but hey-ho.
philpem@9 128 dev->pixelWidth = ((dev->mediaWidth * 180 * 10) / 254) - 2;
philpem@9 129 }
philpem@9 130
philpem@3 131 // Operation succeeded
philpem@9 132 return PT_ERR_SUCCESS;
philpem@3 133 }
philpem@3 134
philpem@14 135 int pt_SetOption(pt_Device *dev, PT_E_OPTION option, int value)
philpem@14 136 {
philpem@14 137 // trap dev == NULL
philpem@14 138 if (dev == NULL) {
philpem@14 139 return PT_ERR_BAD_PARAMETER;
philpem@14 140 }
philpem@14 141
philpem@14 142 // set option
philpem@14 143 switch(option) {
philpem@14 144 case PT_OPTION_MIRROR: // Mirror
philpem@14 145 dev->mirror = (value ? 1 : 0);
philpem@14 146 return PT_ERR_SUCCESS;
philpem@14 147
philpem@14 148 case PT_OPTION_AUTOCUT: // Auto-cutter enable/disable
philpem@14 149 dev->autocut = (value ? 1 : 0);
philpem@14 150 return PT_ERR_SUCCESS;
philpem@14 151
philpem@14 152 default:
philpem@14 153 return PT_ERR_BAD_PARAMETER;
philpem@14 154 }
philpem@14 155 }
philpem@9 156
philpem@14 157 int pt_GetOption(pt_Device *dev, PT_E_OPTION option, int *value)
philpem@14 158 {
philpem@14 159 // trap dev == NULL or value == NULL
philpem@14 160 if ((dev == NULL) || (value == NULL)) {
philpem@14 161 return PT_ERR_BAD_PARAMETER;
philpem@14 162 }
philpem@14 163
philpem@14 164 // get option value
philpem@14 165 switch(option) {
philpem@14 166 case PT_OPTION_MIRROR: // Mirror
philpem@14 167 *value = dev->mirror;
philpem@14 168 return PT_ERR_SUCCESS;
philpem@14 169
philpem@14 170 case PT_OPTION_AUTOCUT: // Auto-cutter enable/disable
philpem@14 171 *value = dev->autocut;
philpem@14 172 return PT_ERR_SUCCESS;
philpem@14 173
philpem@14 174 default:
philpem@14 175 return PT_ERR_BAD_PARAMETER;
philpem@14 176 }
philpem@14 177 }
philpem@14 178
philpem@9 179 int pt_Print(pt_Device *dev, gdImagePtr *labels, int count)
philpem@3 180 {
philpem@9 181 int err;
philpem@9 182 gdImagePtr *curLabel = labels;
philpem@9 183
philpem@9 184 // trap dev == NULL
philpem@9 185 if (dev == NULL) {
philpem@9 186 return PT_ERR_BAD_PARAMETER;
philpem@9 187 }
philpem@9 188
philpem@9 189 // trap labels == NULL
philpem@9 190 if (labels == NULL) {
philpem@9 191 return PT_ERR_BAD_PARAMETER;
philpem@9 192 }
philpem@9 193
philpem@9 194 // trap count == 0
philpem@9 195 if (count == 0) {
philpem@9 196 return PT_ERR_BAD_PARAMETER;
philpem@9 197 }
philpem@9 198
philpem@9 199 // Request current status from the printer
philpem@9 200 if ((err = pt_GetStatus(dev)) != PT_ERR_SUCCESS) {
philpem@9 201 return err;
philpem@9 202 }
philpem@9 203
philpem@9 204 // Make sure the printer has tape, and is ready
philpem@9 205 if ((dev->errorInfo[0] != 0x00) || (dev->errorInfo[1] != 0x00)) {
philpem@9 206 return PT_ERR_PRINTER_NOT_READY;
philpem@9 207 }
philpem@9 208
philpem@9 209 // Send the print initialisation commands
philpem@3 210 //
philpem@9 211 // ESC i M -- Set Mode
philpem@9 212 fprintf(dev->fp, "%ciM%c", ESC,
philpem@9 213 (dev->autocut ? 0x40 : 0x00) | (dev->mirror ? 0x80 : 0x00));
philpem@9 214
philpem@9 215 // ESC i K -- Set Expanded Mode
philpem@9 216 fprintf(dev->fp, "%ciK%c", ESC, 0x00);
philpem@9 217
philpem@9 218 // ESC i R {n1} -- Set Raster Graphics Transfer Mode
philpem@9 219 // {n1} = 0x01 ==> Raster Graphics mode
philpem@9 220 fprintf(dev->fp, "%ciR%c", ESC, 0x01);
philpem@9 221
philpem@9 222 // M {n1} -- Set Compression Mode
philpem@9 223 // {n1} = 0x00 ==> no compression
philpem@14 224 // Doesn't seem to work on the PT-2450DX...
philpem@9 225 // {n1} = 0x01 ==> reserved
philpem@9 226 // {n1} = 0x02 ==> TIFF/Packbits
philpem@14 227 // But this works fine on the PT-2450DX...
philpem@14 228 fprintf(dev->fp, "M%c", 0x02);
philpem@9 229
philpem@9 230 // Loop over the images that were passed in
philpem@9 231 for (int imnum=0; imnum < count; imnum++) {
philpem@9 232 // Make sure image is the right size for this label tape
philpem@9 233 if (gdImageSY(*curLabel) != dev->pixelWidth) {
philpem@9 234 return PT_ERR_LABEL_TOO_WIDE;
philpem@9 235 }
philpem@9 236
philpem@14 237 // Trap a label with a width of zero
philpem@14 238 // I'm not sure if this can happen, but it's a single if statement, so
philpem@14 239 // probably worth checking anyway...
philpem@14 240 if (gdImageSX(*curLabel) == 0) {
philpem@14 241 return PT_ERR_LABEL_ZERO_LENGTH;
philpem@14 242 }
philpem@14 243
philpem@14 244 // Get the index of the colour "white" (RGB:255,255,255) in the Gd image
philpem@14 245 int col_white = gdImageColorResolve(*curLabel, 255, 255, 255);
philpem@9 246
philpem@9 247 // Iterate left-to-right over the source image
philpem@9 248 for (int xpos = 0; xpos < gdImageSX(*curLabel); xpos++) {
philpem@9 249 char bitbuf[128/8]; // 128-dot printhead, 8 bits per byte
philpem@9 250 int rowclear = true; // TRUE if entire row is clear, else FALSE
philpem@9 251
philpem@9 252 // Fill bit buffer with zeroes
philpem@9 253 memset(bitbuf, 0x00, sizeof(bitbuf));
philpem@9 254
philpem@9 255 // Calculate left-side margin for this label size
philpem@9 256 // Again, 128-dot printhead.
philpem@14 257 int margin = (128 / 2) - (dev->pixelWidth / 2);
philpem@9 258
philpem@9 259 // Copy data from the image to the bit-buffer
philpem@9 260 for (int ypos = 0; ypos < gdImageSY(*curLabel); ypos++) {
philpem@14 261 // Get pixel from gd, is it white?
philpem@14 262 if (gdImageGetPixel(*curLabel, xpos, ypos) != col_white) {
philpem@9 263 // No. Set the bit.
philpem@9 264 int bit = 1 << (7 - ((margin+ypos) % 8));
philpem@9 265 bitbuf[(margin+ypos) / 8] |= bit;
philpem@9 266
philpem@9 267 // Clear the "row is clear" flag
philpem@9 268 rowclear = false;
philpem@9 269 }
philpem@9 270 }
philpem@9 271
philpem@9 272 // We now have the image data in bitbuf, and a flag to say whether
philpem@9 273 // there are any black pixels in the buffer. We can pack full-rows
philpem@9 274 // by sending a "Z" character, i.e. "this row is entirely blank".
philpem@9 275 // If not, we have to send a (128/8)=16 byte chunk of image data.
philpem@9 276 if (rowclear) {
philpem@9 277 // Row is clear -- send a "clear row" command
philpem@9 278 fprintf(dev->fp, "Z");
philpem@9 279 } else {
philpem@9 280 // Row is not clear -- send the pixel data
philpem@9 281 //
philpem@9 282 // TODO: the printer supports Packbits compression. Implement!
philpem@14 283 // TODO: After Packbits is implemented, ((128/8)+1) must be
philpem@14 284 // changed to the length of the Packbits compressed data.
philpem@14 285 // (note: 128/8 is the printhead size in bytes, the +1 is for
philpem@14 286 // the control byte we add below...)
philpem@14 287 fprintf(dev->fp, "G%c%c", ((128/8)+1) & 0xff, ((128/8)+1) >> 8);
philpem@14 288
philpem@14 289 // This printer asks for Packbits compressed data. In this
philpem@14 290 // case, we send a "run of N" control byte and fake it...
philpem@14 291 fputc(sizeof(bitbuf) - 1, dev->fp);
philpem@9 292 for (int i=0; i<sizeof(bitbuf); i++) {
philpem@9 293 fputc(bitbuf[i], dev->fp);
philpem@9 294 }
philpem@9 295 }
philpem@9 296 }
philpem@9 297
philpem@9 298 // Is this the last label?
philpem@9 299 if (imnum == (count-1)) {
philpem@9 300 // Yes, send an End Job command
philpem@9 301 fprintf(dev->fp, "%c", 0x1A);
philpem@9 302 } else {
philpem@9 303 // No, more labels to print. Send a formfeed.
philpem@9 304 fprintf(dev->fp, "%c", 0x0C);
philpem@9 305 }
philpem@9 306
philpem@9 307 // Move onto the next label
philpem@9 308 curLabel++;
philpem@9 309 }
philpem@9 310
philpem@9 311 // Operation successful.
philpem@9 312 return PT_ERR_SUCCESS;
philpem@3 313 }
philpem@3 314
philpem@3 315 void pt_Close(pt_Device *dev)
philpem@3 316 {
philpem@3 317 // Sanity check -- make sure dev is not null
philpem@3 318 if (dev == NULL) return;
philpem@3 319
philpem@3 320 // Close the printer stream
philpem@3 321 fclose(dev->fp);
philpem@3 322
philpem@3 323 // Release the memory allocated to the status buffer
philpem@3 324 free(dev);
philpem@3 325 }
philpem@3 326