Sat, 01 Aug 2009 12:42:34 +0100
add new code
NOTES | file | annotate | diff | revisions | |
src/hexdump.h | file | annotate | diff | revisions | |
src/main.c | file | annotate | diff | revisions | |
src/pt_image.c | file | annotate | diff | revisions | |
src/pt_image.h | file | annotate | diff | revisions | |
src/ptouch.c | file | annotate | diff | revisions | |
src/ptouch.h | file | annotate | diff | revisions |
1.1 diff -r 81b0d2551776 -r 4aec27d9d4da NOTES 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/NOTES Sat Aug 01 12:42:34 2009 +0100 1.4 @@ -0,0 +1,1 @@ 1.5 +Hexdump code from http://sws.dett.de/mini/hexdump-c/
2.1 diff -r 81b0d2551776 -r 4aec27d9d4da src/hexdump.h 2.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.3 +++ b/src/hexdump.h Sat Aug 01 12:42:34 2009 +0100 2.4 @@ -0,0 +1,9 @@ 2.5 +#ifndef _HEXDUMP_H 2.6 +#define _HEXDUMP_H 2.7 + 2.8 +void hex_dump(void *data, int size); 2.9 +/* dumps size bytes of *data to stdout. Looks like: 2.10 + * [0000] 75 6E 6B 6E 6F 77 6E 20 30 FF 00 00 00 00 39 00 unknown 0.....9. 2.11 + */ 2.12 + 2.13 +#endif // _HEXDUMP_H
3.1 diff -r 81b0d2551776 -r 4aec27d9d4da src/main.c 3.2 --- a/src/main.c Sat Aug 01 12:41:12 2009 +0100 3.3 +++ b/src/main.c Sat Aug 01 12:42:34 2009 +0100 3.4 @@ -1,12 +1,49 @@ 3.5 +/************************** 3.6 + * P-Touch PT-2450DX printer driver 3.7 + * 3.8 + * P. Pemberton, 2009 3.9 + * 3.10 + * Specs: 3.11 + * Printer head is 128 dots, 180dpi, for a total print area of ~18mm vertical 3.12 + * by however long your TZ label tape is. 3.13 + * Printhead size is (128/180)=0.711[.] inches, or 18.0622[.] mm 3.14 + * Each dot is (18.062/128) = 0.1411[.] mm 3.15 + * Printable area is (numdots * 0.1411) mm 3.16 + * 3.17 + * Tape width Margins Printable area 3.18 + * mm dots mm dots mm dots 3.19 + * 6mm 42 1.0mm 7 4mm 28 3.20 + * 9mm 63 1.0mm 7 7mm 49 3.21 + * 12mm 85 2.0mm 14 8mm 57 3.22 + * 18mm 127 3.0mm 21 12mm 85 3.23 + * 24mm 170 3.0mm 128 18mm 128 *** 3.24 + * 3.25 + * 24mm is slightly odd. Because the printhead is only 128 dots (18mm), the 3.26 + * margins are enforced by this, and not the driver software. It is impossible 3.27 + * to print right to the edge of a 24mm label in a PT-2450DX. 3.28 + * 3.29 + **************************/ 3.30 + 3.31 #include <stdio.h> 3.32 #include <stdlib.h> 3.33 -#include "hexdump.h" 3.34 +#include "ptouch.h" 3.35 +#include "pt_image.h" 3.36 3.37 -#define ESC 0x1b 3.38 +/****************************************************************************/ 3.39 3.40 int main(int argc, char **argv) 3.41 { 3.42 - FILE *prn; 3.43 + pt_Device *dev; 3.44 + 3.45 + pt_Image *im; 3.46 + 3.47 + printf("create image\n"); 3.48 + im = ptimage_Create(123, 456); 3.49 + 3.50 + printf("delete image\n"); 3.51 + ptimage_Free(im); 3.52 + 3.53 + return 0; 3.54 3.55 // check command line args 3.56 if (argc < 2) { 3.57 @@ -14,34 +51,16 @@ 3.58 return -1; 3.59 } 3.60 3.61 - // open printer device 3.62 - if ((prn = fopen(argv[1], "r+b")) == NULL) { 3.63 - printf("ERROR: couldn't open printer device '%s'\n", argv[1]); 3.64 + // Open and initialise the printer 3.65 + dev = pt_Initialise(argv[1]); 3.66 + 3.67 + if (dev == NULL) { 3.68 + printf("Error opening printer device.\n"); 3.69 return -1; 3.70 } 3.71 3.72 - // INITIALISE 3.73 - fprintf(prn, "%c%c", ESC, '@'); 3.74 - 3.75 - // REQUEST STATUS 3.76 - fprintf(prn, "%c%c%c", ESC, 'i', 'S'); 3.77 + // Close the printer device 3.78 + pt_Close(dev); 3.79 3.80 - // Read status buffer from printer 3.81 - unsigned char buf[32]; 3.82 - int timeout = 128; 3.83 - do { 3.84 - fread(buf, 1, 32, prn); 3.85 - } while ((buf[0] != 0x80) && (timeout-- > 0)); 3.86 - 3.87 - if (timeout > 0) { 3.88 - printf("Printer status:\n"); 3.89 - hex_dump(buf, 32); 3.90 - } else { 3.91 - printf("TIMEOUT\n"); 3.92 - return -1; 3.93 - } 3.94 - 3.95 - // Close the printer stream 3.96 - fclose(prn); 3.97 return 0; 3.98 }
4.1 diff -r 81b0d2551776 -r 4aec27d9d4da src/pt_image.c 4.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 +++ b/src/pt_image.c Sat Aug 01 12:42:34 2009 +0100 4.4 @@ -0,0 +1,130 @@ 4.5 +#include <stdio.h> 4.6 +#include <stdlib.h> 4.7 +#include <string.h> 4.8 +#include "pt_image.h" 4.9 + 4.10 +/** 4.11 + * Create a new pt_Image object. 4.12 + * 4.13 + * Creates a new pt_Image of a specified width and height, and initialises 4.14 + * all the pixels in its buffer to zero. 4.15 + * 4.16 + * @param width The width of the image. 4.17 + * @param height The height of the image. 4.18 + * @return The new pt_Image, or NULL on failure. 4.19 + */ 4.20 +pt_Image *ptimage_Create(unsigned long width, unsigned long height) 4.21 +{ 4.22 + pt_Image *image; 4.23 + 4.24 +#ifdef DEBUG 4.25 + printf("%s[%d]: image size = %u\n", __FILE__, __LINE__, sizeof(pt_Image)); 4.26 + printf("%s[%d]: data size = %u\n", __FILE__, __LINE__, sizeof(image->data[0])); 4.27 + printf("%s[%d]: create %lu x %lu image\n", __FILE__, __LINE__, width, height); 4.28 +#endif 4.29 + 4.30 + // Allocate memory for the new image and its data buffer 4.31 + image = malloc(sizeof(pt_Image)); 4.32 + if (image == NULL) return NULL; 4.33 + 4.34 + image->data = malloc(width * height * sizeof(image->data[0])); 4.35 + if (image->data == NULL) return NULL; 4.36 + 4.37 + // Set the image's parameters 4.38 + image->width = width; 4.39 + image->height = height; 4.40 + 4.41 + // Clear the image's data buffer to "blank" 4.42 + memset(image->data, 0, width * height * sizeof(image->data[0])); 4.43 + 4.44 + return image; 4.45 +} 4.46 + 4.47 +/** 4.48 + * Free a pt_Image object. 4.49 + * 4.50 + * @param image The image object to free. 4.51 + */ 4.52 +void ptimage_Free(pt_Image *image) 4.53 +{ 4.54 + // Make sure image is non-null 4.55 + if (image == NULL) { 4.56 + return; 4.57 + } 4.58 + 4.59 + // Free the image data 4.60 + if (image->data != NULL) { 4.61 + free(image->data); 4.62 + } 4.63 + 4.64 + // Free the image 4.65 + free(image); 4.66 +} 4.67 + 4.68 +/** 4.69 + * Get the value of a pixel in a pt_Image object. 4.70 + * 4.71 + * @param image Image object. 4.72 + * @param x X position of the pixel, zero-based. 4.73 + * @param y Y position of the pixel, zero-based. 4.74 + * @return Value of the pixel, or negative on error. 4.75 + */ 4.76 +int ptimage_GetPixel(pt_Image *image, unsigned long x, unsigned long y) 4.77 +{ 4.78 + // Make sure the image is not null and that the data buffer has been 4.79 + // allocated 4.80 + if (image == NULL) { 4.81 + return -1; // TODO: make constant 4.82 + } 4.83 + 4.84 + if (image->data == NULL) { 4.85 + return -1; // TODO: make constant 4.86 + } 4.87 + 4.88 + // Range-check 4.89 + if ((x < 0) || (x > image->width)) { 4.90 + return -2; // TODO: make constant 4.91 + } 4.92 + if ((y < 0) || (y > image->height)) { 4.93 + return -3; // TODO: make constant 4.94 + } 4.95 + 4.96 + // Return the pixel value 4.97 + return image->data[(y*image->width)+x]; 4.98 +} 4.99 + 4.100 +/** 4.101 + * Set the value of a pixel in a pt_Image object. 4.102 + * 4.103 + * @param image Image object. 4.104 + * @param x X position of the pixel, zero-based. 4.105 + * @param y Y position of the pixel, zero-based. 4.106 + * @param val New value of the pixel. 4.107 + * @return Zero on success, or negative on error. 4.108 + */ 4.109 +int ptimage_SetPixel(pt_Image *image, unsigned long x, unsigned long y, unsigned char val) 4.110 +{ 4.111 + // Make sure the image is not null and that the data buffer has been 4.112 + // allocated 4.113 + if (image == NULL) { 4.114 + return -1; // TODO: make constant 4.115 + } 4.116 + 4.117 + if (image->data == NULL) { 4.118 + return -1; // TODO: make constant 4.119 + } 4.120 + 4.121 + // Range-check 4.122 + if ((x < 0) || (x > image->width)) { 4.123 + return -2; // TODO: make constant 4.124 + } 4.125 + if ((y < 0) || (y > image->height)) { 4.126 + return -3; // TODO: make constant 4.127 + } 4.128 + 4.129 + // Set the pixel value 4.130 + image->data[(y*image->width)+x] = val; 4.131 + 4.132 + return 0; 4.133 +} 4.134 +
5.1 diff -r 81b0d2551776 -r 4aec27d9d4da src/pt_image.h 5.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 +++ b/src/pt_image.h Sat Aug 01 12:42:34 2009 +0100 5.4 @@ -0,0 +1,20 @@ 5.5 +#ifndef PT_IMAGE_H 5.6 +#define PT_IMAGE_H 5.7 + 5.8 +/** 5.9 + * A storage class for simple images. 5.10 + * 5.11 + * Data is stored as bytes, as this makes the math easier (and modern 5.12 + * computer systems have plenty of RAM anyway). 5.13 + */ 5.14 +typedef struct { 5.15 + unsigned long width, height; 5.16 + unsigned char *data; 5.17 +} pt_Image; 5.18 + 5.19 +pt_Image *ptimage_Create(unsigned long width, unsigned long height); 5.20 +void ptimage_Free(pt_Image *image); 5.21 +int ptimage_GetPixel(pt_Image *image, unsigned long x, unsigned long y); 5.22 +int ptimage_SetPixel(pt_Image *image, unsigned long x, unsigned long y, unsigned char val); 5.23 + 5.24 +#endif // PT_IMAGE_H
6.1 diff -r 81b0d2551776 -r 4aec27d9d4da src/ptouch.c 6.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 +++ b/src/ptouch.c Sat Aug 01 12:42:34 2009 +0100 6.4 @@ -0,0 +1,129 @@ 6.5 +/**************************************************************************** 6.6 + * Project: P-touch printer driver library 6.7 + * Developer: Philip Pemberton 6.8 + * Purpose: Make Brother P-touch (PT-series) printers do something besides 6.9 + * gather dust. 6.10 + * 6.11 + * Currently supports: 6.12 + * PT-2450DX 6.13 + ****************************************************************************/ 6.14 + 6.15 +// TODO: disable 6.16 +#define DEBUG 6.17 + 6.18 +#include <stdio.h> 6.19 +#include <stdlib.h> 6.20 +#include <string.h> 6.21 +#include "hexdump.h" 6.22 +#include "pt_image.h" 6.23 +#include "ptouch.h" 6.24 + 6.25 +#define ESC 0x1b 6.26 + 6.27 +pt_Device *pt_Initialise(char *path) 6.28 +{ 6.29 + pt_Device *dev; 6.30 + FILE *prn; 6.31 + 6.32 + // Try and open the printer device 6.33 + prn = fopen(path, "r+b"); 6.34 + if (prn == NULL) { 6.35 + return NULL; 6.36 + } 6.37 + 6.38 + // Printer device open, send an init command and read the status 6.39 + fprintf(prn, "%c%c", ESC, '@'); 6.40 + 6.41 + // Allocate memory for the device block 6.42 + dev = malloc(sizeof(pt_Device)); 6.43 + if (dev == NULL) { 6.44 + fclose(prn); 6.45 + return NULL; 6.46 + } 6.47 + 6.48 + // Store the file pointer 6.49 + dev->fp = prn; 6.50 + 6.51 + // Memory allocation OK, now get the printer's status 6.52 + if (pt_GetStatus(dev) == 0) { 6.53 + return dev; 6.54 + } else { 6.55 + free(dev); 6.56 + return NULL; 6.57 + } 6.58 +} 6.59 + 6.60 +int pt_GetStatus(pt_Device *dev) 6.61 +{ 6.62 + // REQUEST STATUS 6.63 + fprintf(dev->fp, "%c%c%c", ESC, 'i', 'S'); 6.64 + 6.65 + // Read status buffer from printer 6.66 + unsigned char buf[32]; 6.67 + int timeout = 128; 6.68 + do { 6.69 + fread(buf, 1, 32, dev->fp); 6.70 + } while ((buf[0] != 0x80) && (timeout-- > 0)); 6.71 + 6.72 + // Check for timeout 6.73 + if (timeout == 0) { 6.74 + // Timeout 6.75 + return -1; 6.76 + } 6.77 + 6.78 +#ifdef DEBUG 6.79 + printf("DEBUG: Printer status buffer = \n"); 6.80 + hex_dump(buf, 32); 6.81 +#endif 6.82 + 6.83 + // Decode the status buffer, store the results in the device object 6.84 + dev->headMark = buf[0]; 6.85 + dev->size = buf[1]; 6.86 + dev->errorInfo[0] = buf[8]; 6.87 + dev->errorInfo[1] = buf[9]; 6.88 + dev->mediaWidth = buf[10]; 6.89 + dev->mediaType = buf[11]; 6.90 + dev->mediaLength = buf[17]; 6.91 + dev->statusType = buf[18]; 6.92 + dev->phaseType = buf[19]; 6.93 + dev->phaseHi = buf[20]; 6.94 + dev->phaseLo = buf[21]; 6.95 + dev->notification = buf[22]; 6.96 + 6.97 + // Operation succeeded 6.98 + return 0; 6.99 +} 6.100 + 6.101 +// TODO: print options struct parameter (e.g. fullcut, halfcut, print res, 6.102 +// 6.103 +int pt_Print(pt_Device *dev, pt_Image *image) 6.104 +{ 6.105 + // TODO: trap dev == NULL 6.106 + // TODO: trap image == NULL 6.107 + // TODO: trap image.height > printhead.height 6.108 + // TODO: trap image.height <= 0 6.109 + // TODO: trap image.width <= 0 6.110 + // 6.111 + // allocate print buffer 6.112 + // 6.113 + // pack pixels -- 8 pixels => 1 byte 6.114 + // 6.115 + // compress print data (packbits) 6.116 + // 6.117 + // send print buffer to printer 6.118 + // 6.119 + // free print buffer 6.120 +} 6.121 + 6.122 +void pt_Close(pt_Device *dev) 6.123 +{ 6.124 + // Sanity check -- make sure dev is not null 6.125 + if (dev == NULL) return; 6.126 + 6.127 + // Close the printer stream 6.128 + fclose(dev->fp); 6.129 + 6.130 + // Release the memory allocated to the status buffer 6.131 + free(dev); 6.132 +} 6.133 +
7.1 diff -r 81b0d2551776 -r 4aec27d9d4da src/ptouch.h 7.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 +++ b/src/ptouch.h Sat Aug 01 12:42:34 2009 +0100 7.4 @@ -0,0 +1,27 @@ 7.5 +/**************************************************************************** 7.6 + * Project: P-touch printer driver library 7.7 + * Developer: Philip Pemberton 7.8 + * Purpose: Make Brother P-touch (PT-series) printers do something besides 7.9 + * gather dust. 7.10 + * 7.11 + * Currently supports: 7.12 + * PT-2450DX 7.13 + ****************************************************************************/ 7.14 + 7.15 +#ifndef PTOUCH_H 7.16 +#define PTOUCH_H 7.17 + 7.18 +typedef struct { 7.19 + FILE *fp; 7.20 + int headMark, size, errorInfo[2]; 7.21 + int mediaWidth, mediaType, mediaLength; 7.22 + int statusType, phaseType, phaseHi, phaseLo; 7.23 + int notification; 7.24 +} pt_Device; 7.25 + 7.26 +// printer functions 7.27 +pt_Device *pt_Initialise(char *path); 7.28 +int pt_GetStatus(pt_Device *dev); 7.29 +void pt_Close(pt_Device *dev); 7.30 + 7.31 +#endif // PTOUCH_H