src/ptouch.c

Thu, 24 Sep 2009 22:29:12 +0100

author
Philip Pemberton <philpem@philpem.me.uk>
date
Thu, 24 Sep 2009 22:29:12 +0100
changeset 20
f8ca98e5f586
parent 19
b7fe751ea60d
child 21
629637abfe1f
permissions
-rw-r--r--

remove accidentally-included debug code (oops!)

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@20 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@19 62 // Separator ticks off
philpem@15 63 dev->mirror = false;
philpem@15 64 dev->autocut = false;
philpem@19 65 dev->separator = false;
philpem@15 66
philpem@15 67 return dev;
philpem@3 68 }
philpem@3 69
philpem@3 70 int pt_GetStatus(pt_Device *dev)
philpem@3 71 {
philpem@14 72 #ifdef DEBUG_SKIP_STATUS_READ
philpem@14 73 unsigned char buf[32];
philpem@14 74 memset(buf, 0x00, 32);
philpem@14 75 buf[0] = 0x80;
philpem@14 76 buf[1] = 0x20;
philpem@14 77 buf[2] = 0x42;
philpem@14 78 buf[3] = 0x30;
philpem@14 79 buf[4] = 0x4b;
philpem@14 80 buf[5] = 0x30;
philpem@14 81 buf[10] = 0x0c;
philpem@14 82 buf[11] = 0x01;
philpem@14 83 #else
philpem@3 84 // REQUEST STATUS
philpem@3 85 fprintf(dev->fp, "%c%c%c", ESC, 'i', 'S');
philpem@3 86
philpem@3 87 // Read status buffer from printer
philpem@3 88 unsigned char buf[32];
philpem@3 89 int timeout = 128;
philpem@3 90 do {
philpem@3 91 fread(buf, 1, 32, dev->fp);
philpem@3 92 } while ((buf[0] != 0x80) && (timeout-- > 0));
philpem@3 93
philpem@3 94 // Check for timeout
philpem@3 95 if (timeout == 0) {
philpem@3 96 // Timeout
philpem@9 97 return PT_ERR_TIMEOUT;
philpem@3 98 }
philpem@14 99 #endif
philpem@3 100
philpem@3 101 #ifdef DEBUG
philpem@3 102 printf("DEBUG: Printer status buffer = \n");
philpem@3 103 hex_dump(buf, 32);
philpem@3 104 #endif
philpem@3 105
philpem@3 106 // Decode the status buffer, store the results in the device object
philpem@3 107 dev->errorInfo[0] = buf[8];
philpem@3 108 dev->errorInfo[1] = buf[9];
philpem@3 109 dev->mediaWidth = buf[10];
philpem@3 110 dev->mediaType = buf[11];
philpem@3 111 dev->mediaLength = buf[17];
philpem@3 112 dev->statusType = buf[18];
philpem@3 113 dev->phaseType = buf[19];
philpem@3 114 dev->phaseHi = buf[20];
philpem@3 115 dev->phaseLo = buf[21];
philpem@3 116 dev->notification = buf[22];
philpem@3 117
philpem@9 118 // Set pixel width (label width in pixels)
philpem@9 119 if (dev->mediaWidth >= 24) {
philpem@9 120 // Label tape is 24mm or wider. Print head is 128 dots at 180dpi,
philpem@9 121 // which is 18.06mm. Thus, we can only print on the centre 18mm
philpem@9 122 // of a tape that is wider than 18mm.
philpem@9 123 dev->pixelWidth = 128;
philpem@9 124 } else {
philpem@9 125 // Print head is 180dpi. Pixel size is mediaWidth * dpi. If we
philpem@9 126 // multiply by ten, then divide by 254, we can avoid using
philpem@9 127 // floating point to convert from inches to mm. The -2 is a
philpem@9 128 // safety margin -- one pixel on either side of the label.
philpem@9 129 // This is far closer than Brother suggest, but hey-ho.
philpem@9 130 dev->pixelWidth = ((dev->mediaWidth * 180 * 10) / 254) - 2;
philpem@9 131 }
philpem@9 132
philpem@3 133 // Operation succeeded
philpem@9 134 return PT_ERR_SUCCESS;
philpem@3 135 }
philpem@3 136
philpem@14 137 int pt_SetOption(pt_Device *dev, PT_E_OPTION option, int value)
philpem@14 138 {
philpem@14 139 // trap dev == NULL
philpem@14 140 if (dev == NULL) {
philpem@14 141 return PT_ERR_BAD_PARAMETER;
philpem@14 142 }
philpem@14 143
philpem@14 144 // set option
philpem@14 145 switch(option) {
philpem@19 146 case PT_OPTION_MIRROR: // Mirror
philpem@14 147 dev->mirror = (value ? 1 : 0);
philpem@14 148 return PT_ERR_SUCCESS;
philpem@14 149
philpem@14 150 case PT_OPTION_AUTOCUT: // Auto-cutter enable/disable
philpem@14 151 dev->autocut = (value ? 1 : 0);
philpem@14 152 return PT_ERR_SUCCESS;
philpem@14 153
philpem@19 154 case PT_OPTION_SEPARATOR: // Separator ticks enable/disable
philpem@19 155 dev->separator = (value ? 1 : 0);
philpem@19 156 return PT_ERR_SUCCESS;
philpem@19 157
philpem@14 158 default:
philpem@14 159 return PT_ERR_BAD_PARAMETER;
philpem@14 160 }
philpem@14 161 }
philpem@9 162
philpem@14 163 int pt_GetOption(pt_Device *dev, PT_E_OPTION option, int *value)
philpem@14 164 {
philpem@14 165 // trap dev == NULL or value == NULL
philpem@14 166 if ((dev == NULL) || (value == NULL)) {
philpem@14 167 return PT_ERR_BAD_PARAMETER;
philpem@14 168 }
philpem@14 169
philpem@14 170 // get option value
philpem@14 171 switch(option) {
philpem@14 172 case PT_OPTION_MIRROR: // Mirror
philpem@14 173 *value = dev->mirror;
philpem@14 174 return PT_ERR_SUCCESS;
philpem@14 175
philpem@14 176 case PT_OPTION_AUTOCUT: // Auto-cutter enable/disable
philpem@14 177 *value = dev->autocut;
philpem@14 178 return PT_ERR_SUCCESS;
philpem@14 179
philpem@19 180 case PT_OPTION_SEPARATOR: // Separator ticks enable/disable
philpem@19 181 *value = dev->separator;
philpem@19 182 return PT_ERR_SUCCESS;
philpem@19 183
philpem@14 184 default:
philpem@14 185 return PT_ERR_BAD_PARAMETER;
philpem@14 186 }
philpem@14 187 }
philpem@14 188
philpem@9 189 int pt_Print(pt_Device *dev, gdImagePtr *labels, int count)
philpem@3 190 {
philpem@9 191 int err;
philpem@9 192 gdImagePtr *curLabel = labels;
philpem@9 193
philpem@9 194 // trap dev == NULL
philpem@9 195 if (dev == NULL) {
philpem@9 196 return PT_ERR_BAD_PARAMETER;
philpem@9 197 }
philpem@9 198
philpem@9 199 // trap labels == NULL
philpem@9 200 if (labels == NULL) {
philpem@9 201 return PT_ERR_BAD_PARAMETER;
philpem@9 202 }
philpem@9 203
philpem@9 204 // trap count == 0
philpem@9 205 if (count == 0) {
philpem@9 206 return PT_ERR_BAD_PARAMETER;
philpem@9 207 }
philpem@9 208
philpem@9 209 // Request current status from the printer
philpem@9 210 if ((err = pt_GetStatus(dev)) != PT_ERR_SUCCESS) {
philpem@9 211 return err;
philpem@9 212 }
philpem@9 213
philpem@9 214 // Make sure the printer has tape, and is ready
philpem@9 215 if ((dev->errorInfo[0] != 0x00) || (dev->errorInfo[1] != 0x00)) {
philpem@9 216 return PT_ERR_PRINTER_NOT_READY;
philpem@9 217 }
philpem@9 218
philpem@9 219 // Send the print initialisation commands
philpem@3 220 //
philpem@9 221 // ESC i M -- Set Mode
philpem@9 222 fprintf(dev->fp, "%ciM%c", ESC,
philpem@9 223 (dev->autocut ? 0x40 : 0x00) | (dev->mirror ? 0x80 : 0x00));
philpem@9 224
philpem@9 225 // ESC i K -- Set Expanded Mode
philpem@9 226 fprintf(dev->fp, "%ciK%c", ESC, 0x00);
philpem@9 227
philpem@9 228 // ESC i R {n1} -- Set Raster Graphics Transfer Mode
philpem@9 229 // {n1} = 0x01 ==> Raster Graphics mode
philpem@9 230 fprintf(dev->fp, "%ciR%c", ESC, 0x01);
philpem@9 231
philpem@9 232 // M {n1} -- Set Compression Mode
philpem@9 233 // {n1} = 0x00 ==> no compression
philpem@14 234 // Doesn't seem to work on the PT-2450DX...
philpem@9 235 // {n1} = 0x01 ==> reserved
philpem@9 236 // {n1} = 0x02 ==> TIFF/Packbits
philpem@14 237 // But this works fine on the PT-2450DX...
philpem@14 238 fprintf(dev->fp, "M%c", 0x02);
philpem@9 239
philpem@9 240 // Loop over the images that were passed in
philpem@9 241 for (int imnum=0; imnum < count; imnum++) {
philpem@9 242 // Make sure image is the right size for this label tape
philpem@9 243 if (gdImageSY(*curLabel) != dev->pixelWidth) {
philpem@9 244 return PT_ERR_LABEL_TOO_WIDE;
philpem@9 245 }
philpem@9 246
philpem@14 247 // Trap a label with a width of zero
philpem@14 248 // I'm not sure if this can happen, but it's a single if statement, so
philpem@14 249 // probably worth checking anyway...
philpem@14 250 if (gdImageSX(*curLabel) == 0) {
philpem@14 251 return PT_ERR_LABEL_ZERO_LENGTH;
philpem@14 252 }
philpem@14 253
philpem@14 254 // Get the index of the colour "white" (RGB:255,255,255) in the Gd image
philpem@14 255 int col_white = gdImageColorResolve(*curLabel, 255, 255, 255);
philpem@9 256
philpem@9 257 // Iterate left-to-right over the source image
philpem@9 258 for (int xpos = 0; xpos < gdImageSX(*curLabel); xpos++) {
philpem@9 259 char bitbuf[128/8]; // 128-dot printhead, 8 bits per byte
philpem@9 260 int rowclear = true; // TRUE if entire row is clear, else FALSE
philpem@9 261
philpem@9 262 // Fill bit buffer with zeroes
philpem@9 263 memset(bitbuf, 0x00, sizeof(bitbuf));
philpem@9 264
philpem@9 265 // Calculate left-side margin for this label size
philpem@9 266 // Again, 128-dot printhead.
philpem@14 267 int margin = (128 / 2) - (dev->pixelWidth / 2);
philpem@9 268
philpem@9 269 // Copy data from the image to the bit-buffer
philpem@19 270 // If the image is too tall for the label, only the topmost part
philpem@19 271 // will be printed -- the margin is enforced by (margin+ypos).
philpem@9 272 for (int ypos = 0; ypos < gdImageSY(*curLabel); ypos++) {
philpem@14 273 // Get pixel from gd, is it white?
philpem@14 274 if (gdImageGetPixel(*curLabel, xpos, ypos) != col_white) {
philpem@9 275 // No. Set the bit.
philpem@9 276 int bit = 1 << (7 - ((margin+ypos) % 8));
philpem@9 277 bitbuf[(margin+ypos) / 8] |= bit;
philpem@9 278
philpem@9 279 // Clear the "row is clear" flag
philpem@9 280 rowclear = false;
philpem@9 281 }
philpem@9 282 }
philpem@9 283
philpem@9 284 // We now have the image data in bitbuf, and a flag to say whether
philpem@9 285 // there are any black pixels in the buffer. We can pack full-rows
philpem@9 286 // by sending a "Z" character, i.e. "this row is entirely blank".
philpem@9 287 // If not, we have to send a (128/8)=16 byte chunk of image data.
philpem@9 288 if (rowclear) {
philpem@9 289 // Row is clear -- send a "clear row" command
philpem@9 290 fprintf(dev->fp, "Z");
philpem@9 291 } else {
philpem@9 292 // Row is not clear -- send the pixel data
philpem@9 293 //
philpem@9 294 // TODO: the printer supports Packbits compression. Implement!
philpem@14 295 // TODO: After Packbits is implemented, ((128/8)+1) must be
philpem@14 296 // changed to the length of the Packbits compressed data.
philpem@14 297 // (note: 128/8 is the printhead size in bytes, the +1 is for
philpem@14 298 // the control byte we add below...)
philpem@14 299 fprintf(dev->fp, "G%c%c", ((128/8)+1) & 0xff, ((128/8)+1) >> 8);
philpem@14 300
philpem@14 301 // This printer asks for Packbits compressed data. In this
philpem@14 302 // case, we send a "run of N" control byte and fake it...
philpem@14 303 fputc(sizeof(bitbuf) - 1, dev->fp);
philpem@9 304 for (int i=0; i<sizeof(bitbuf); i++) {
philpem@9 305 fputc(bitbuf[i], dev->fp);
philpem@9 306 }
philpem@9 307 }
philpem@9 308 }
philpem@9 309
philpem@19 310 // Print separator line
philpem@19 311 if (dev->separator) {
philpem@19 312 char bitbuf[128/8]; // 128-dot printhead, 8 bits per byte
philpem@19 313
philpem@19 314 // Calculate margin for this label size
philpem@19 315 // Again, 128-dot printhead.
philpem@19 316 int margin = (128 / 2) - (dev->pixelWidth / 2);
philpem@19 317
philpem@19 318 // One blank line
philpem@19 319 fprintf(dev->fp, "Z");
philpem@19 320
philpem@19 321 // Dotted line (assumes printhead size of 128 dots and that
philpem@19 322 // bitbuf has same size as printhead)
philpem@19 323 fprintf(dev->fp, "G%c%c", ((128/8)+1)&0xff, ((128/8)+1) >> 8);
philpem@19 324
philpem@19 325 // Clear the bit buffer
philpem@19 326 memset(&bitbuf, 0, sizeof(bitbuf));
philpem@19 327
philpem@19 328 // Draw the tick marks
philpem@19 329 int step = (dev->pixelWidth / 4);
philpem@19 330 for (int i=(margin+step); i<(margin+step+(step/2)); i++) {
philpem@19 331 // top tick mark
philpem@19 332 int bit = 1 << (7 - (i % 8));
philpem@19 333 bitbuf[i / 8] |= bit;
philpem@19 334 }
philpem@19 335 for (int i=(margin+(2*step)); i<(margin+(2*step)+(step/2)); i++) {
philpem@19 336 // bottom tick mark
philpem@19 337 int bit = 1 << (7 - (i % 8));
philpem@19 338 bitbuf[i / 8] |= bit;
philpem@19 339 }
philpem@19 340
philpem@19 341 // TODO: Add Packbits compression code?
philpem@19 342 // This printer asks for Packbits compressed data. In this
philpem@19 343 // case, we send a "run of N" control byte and fake it...
philpem@19 344 fputc(sizeof(bitbuf) - 1, dev->fp);
philpem@19 345 for (int i=0; i<sizeof(bitbuf); i++) {
philpem@19 346 // Draw tick-marks from the bit buffer
philpem@19 347 fputc(bitbuf[i], dev->fp);
philpem@19 348 }
philpem@19 349
philpem@19 350 // One final blank line
philpem@19 351 fprintf(dev->fp, "Z");
philpem@19 352 }
philpem@19 353
philpem@19 354
philpem@9 355 // Is this the last label?
philpem@9 356 if (imnum == (count-1)) {
philpem@9 357 // Yes, send an End Job command
philpem@9 358 fprintf(dev->fp, "%c", 0x1A);
philpem@9 359 } else {
philpem@9 360 // No, more labels to print. Send a formfeed.
philpem@9 361 fprintf(dev->fp, "%c", 0x0C);
philpem@9 362 }
philpem@9 363
philpem@9 364 // Move onto the next label
philpem@9 365 curLabel++;
philpem@9 366 }
philpem@9 367
philpem@9 368 // Operation successful.
philpem@9 369 return PT_ERR_SUCCESS;
philpem@3 370 }
philpem@3 371
philpem@3 372 void pt_Close(pt_Device *dev)
philpem@3 373 {
philpem@3 374 // Sanity check -- make sure dev is not null
philpem@3 375 if (dev == NULL) return;
philpem@3 376
philpem@3 377 // Close the printer stream
philpem@3 378 fclose(dev->fp);
philpem@3 379
philpem@3 380 // Release the memory allocated to the status buffer
philpem@3 381 free(dev);
philpem@3 382 }
philpem@3 383