src/ptouch.c

Wed, 05 Aug 2009 15:04:55 +0100

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

Fixed printing so uncompressed mode is no longer used; now sends an uncompressed Packbits stream instead.
Added SetOption and GetOption, so can now turn autocut and mirror on and off. Defaults to both off after init.

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