]> git.sur5r.net Git - ptouch-print/blob - src/libptouch.c
added initial support for PT-2730PC
[ptouch-print] / src / libptouch.c
1 /*
2         libptouch - functions to help accessing a brother ptouch
3
4         Copyright (C) 2013-2017 Dominic Radermacher <blip@mockmoon-cybernetics.ch>
5
6         This program is free software; you can redistribute it and/or modify it
7         under the terms of the GNU General Public License version 3 as
8         published by the Free Software Foundation
9
10         This program is distributed in the hope that it will be useful, but
11         WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13         See the GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software Foundation,
17         Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #define _POSIX_C_SOURCE 199309L /* needed for nanosleep() when using -std=c11 */
21
22 #include <stdio.h>
23 #include <stdlib.h>     /* malloc() */
24 #include <string.h>     /* memcmp()  */
25 #include <sys/types.h>  /* open() */
26 #include <sys/stat.h>   /* open() */
27 #include <fcntl.h>      /* open() */
28 #include <time.h>       /* nanosleep(), struct timespec */
29 #include "config.h"
30 #include "gettext.h"    /* gettext(), ngettext() */
31 #include "ptouch.h"
32
33 #define _(s) gettext(s)
34
35 /* Print area width in 180 DPI pixels */
36 struct _pt_tape_info tape_info[]= {
37         { 6, 32},       /* 6 mm tape */
38         { 9, 52},       /* 9 mm tape */
39         {12, 76},       /* 12 mm tape */
40         {18, 120},      /* 18 mm tape */
41         {24, 128},      /* 24 mm tape */
42         {36, 192},      /* 36 mm tape */
43         { 0, 0}         /* terminating entry */
44 };
45
46 struct _pt_dev_info ptdevs[] = {
47         {0x04f9, 0x202d, "PT-2430PC", 128, FLAG_NONE},          /* 180dpi, maximum 128px */
48         {0x04f9, 0x2007, "PT-2420PC", 128, FLAG_FORCE_TIFF},    /* 180dpi, 128px, maximum tape width 24mm, must send TIFF compressed pixel data */
49         {0x04f9, 0x202c, "PT-1230PC", 76, FLAG_NONE},           /* 180dpi, supports tapes up to 12mm - I don't know how much pixels it can print! */
50         {0x04f9, 0x2061, "PT-P700", 120, FLAG_UNSUP_RASTER},    /* DOES NOT WORK */
51         {0x04f9, 0x2073, "PT-D450VP", 120, FLAG_UNSUP_RASTER},  /* DOES NOT WORK */
52         /* Notes about the PT-D450VP: Tape detecting works, but printing does
53            not. The tape is just blank. I assume, the printer does not understand
54            the sent rasterdata. I'm also unsure about how many dots width we have */
55         {0x04f9, 0x2041, "PT-2730PC", 128, FLAG_NONE},          /* 180dpi, maximum 128px, max tape width 24mm - reported to work with some quirks */
56         /* Notes about the PT-2730PC: was reported to need 48px whitespace
57            within png-images before content is actually printed - can not check this */
58         {0,0,"",0,0}
59 };
60
61 void ptouch_rawstatus(uint8_t raw[32]);
62
63 int ptouch_open(ptouch_dev *ptdev)
64 {
65         libusb_device **devs;
66         libusb_device *dev;
67         libusb_device_handle *handle = NULL;
68         struct libusb_device_descriptor desc;
69         ssize_t cnt;
70         int r,i=0;
71
72         if ((*ptdev=malloc(sizeof(struct _ptouch_dev))) == NULL) {
73                 fprintf(stderr, _("out of memory\n"));
74                 return -1;
75         }
76         if (((*ptdev)->devinfo=malloc(sizeof(struct _pt_dev_info))) == NULL) {
77                 fprintf(stderr, _("out of memory\n"));
78                 return -1;
79         }
80         if ((libusb_init(NULL)) < 0) {
81                 fprintf(stderr, _("libusb_init() failed\n"));
82                 return -1;
83         }
84 //      libusb_set_debug(NULL, 3);
85         if ((cnt=libusb_get_device_list(NULL, &devs)) < 0) {
86                 return -1;
87         }
88         while ((dev=devs[i++]) != NULL) {
89                 if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
90                         fprintf(stderr, _("failed to get device descriptor"));
91                         libusb_free_device_list(devs, 1);
92                         return -1;
93                 }
94                 for (int k=0; ptdevs[k].vid > 0; k++) {
95                         if ((desc.idVendor == ptdevs[k].vid) && (desc.idProduct == ptdevs[k].pid) && (ptdevs[k].flags >= 0)) {
96                                 fprintf(stderr, _("%s found on USB bus %d, device %d\n"),
97                                         ptdevs[k].name,
98                                         libusb_get_bus_number(dev),
99                                         libusb_get_device_address(dev));
100                                 if ((r=libusb_open(dev, &handle)) != 0) {
101                                         fprintf(stderr, _("libusb_open error :%s\n"), libusb_error_name(r));
102                                         return -1;
103                                 }
104                                 libusb_free_device_list(devs, 1);
105                                 if ((r=libusb_kernel_driver_active(handle, 0)) == 1) {
106                                         if ((r=libusb_detach_kernel_driver(handle, 0)) != 0) {
107                                                 fprintf(stderr, _("error while detaching kernel driver: %s\n"), libusb_error_name(r));
108                                         }
109                                 }
110                                 if ((r=libusb_claim_interface(handle, 0)) != 0) {
111                                         fprintf(stderr, _("interface claim error: %s\n"), libusb_error_name(r));
112                                         return -1;
113                                 }
114                                 (*ptdev)->h=handle;
115                                 (*ptdev)->devinfo->max_px=ptdevs[k].max_px;
116                                 (*ptdev)->devinfo->flags=ptdevs[k].flags;
117                                 return 0;
118                         }
119                 }
120         }
121         fprintf(stderr, _("No P-Touch printer found on USB (remember to put switch to position E)\n"));
122         libusb_free_device_list(devs, 1);
123         return -1;
124 }
125
126 int ptouch_close(ptouch_dev ptdev)
127 {
128         libusb_release_interface(ptdev->h, 0);
129         libusb_close(ptdev->h);
130         return 0;
131 }
132
133 int ptouch_send(ptouch_dev ptdev, uint8_t *data, int len)
134 {
135         int r,tx;
136
137         if (ptdev == NULL) {
138                 return -1;
139         }
140         if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, len, &tx, 0)) != 0) {
141                 fprintf(stderr, _("write error: %s\n"), libusb_error_name(r));
142                 return -1;
143         }
144         if (tx != len) {
145                 fprintf(stderr, _("write error: could send only %i of %i bytes\n"), tx, len);
146                 return -1;
147         }
148         return 0;
149 }
150
151 int ptouch_init(ptouch_dev ptdev)
152 {
153         char cmd[]="\x1b\x40";          /* 1B 40 = ESC @ = INIT */
154         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
155 }
156
157 int ptouch_rasterstart(ptouch_dev ptdev)
158 {
159         char cmd[] = "\x1b\x69\x52\x01";        /* 1B 69 52 01 = Select graphics transfer mode = Raster */
160         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
161 }
162
163 /* print an empty line */
164 int ptouch_lf(ptouch_dev ptdev)
165 {
166         char cmd[]="\x5a";
167         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
168 }
169
170 /* print and advance tape, but do not cut */
171 int ptouch_ff(ptouch_dev ptdev)
172 {
173         char cmd[]="\x0c";
174         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
175 }
176
177 /* print and cut tape */
178 int ptouch_eject(ptouch_dev ptdev)
179 {
180         char cmd[]="\x1a";
181         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
182 }
183
184 /* print a "cut here" mark (it's just a dashed line) */
185 #define CUTMARK_SPACING 5
186 int ptouch_cutmark(ptouch_dev ptdev)
187 {
188         uint8_t buf[32];
189         int i,len=16;
190
191         for (i=0; i<CUTMARK_SPACING; i++) {
192                 ptouch_lf(ptdev);
193         }
194         ptouch_rasterstart(ptdev);
195         buf[0]=0x47;
196         buf[1]=len;
197         buf[2]=0;
198         memset(buf+3, 0, len);
199         int offset=(64-ptouch_getmaxwidth(ptdev)/2);
200         for (i=0; i<ptouch_getmaxwidth(ptdev); i++) {
201                 if ((i%8) <= 3) {       /* pixels 0-3 get set, 4-7 are unset */
202                         buf[3+15-((offset+i)/8)] |= 1<<((offset+i)%8);
203                 }
204         }
205         ptouch_send(ptdev, buf, len+3);
206         for (i=0; i<CUTMARK_SPACING; i++) {
207                 ptouch_lf(ptdev);
208         }
209         return 0;
210 }
211
212 void ptouch_rawstatus(uint8_t raw[32])
213 {
214         fprintf(stderr, _("debug: dumping raw status bytes\n"));
215         for (int i=0; i<32; i++) {
216                 fprintf(stderr, "%02x ", raw[i]);
217                 if (((i+1) % 16) == 0) {
218                         fprintf(stderr, "\n");
219                 }
220         }
221         fprintf(stderr, "\n");
222         return;
223 }
224
225 int ptouch_getstatus(ptouch_dev ptdev)
226 {
227         char cmd[]="\x1b\x69\x53";
228         uint8_t buf[32];
229         int i, r, tx=0, tries=0;
230         struct timespec w;
231
232         ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
233         while (tx == 0) {
234                 w.tv_sec=0;
235                 w.tv_nsec=100000000;    /* 0.1 sec */
236                 r=nanosleep(&w, NULL);
237                 if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
238                         fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
239                         return -1;
240                 }
241                 tries++;
242                 if (tries > 10) {
243                         fprintf(stderr, _("timeout while waiting for status response\n"));
244                         return -1;
245                 }
246         }
247         if (tx == 32) {
248                 if (buf[0]==0x80 && buf[1]==0x20) {
249                         memcpy(ptdev->raw, buf, 32);
250                         if (buf[8] != 0) {
251                                 fprintf(stderr, _("Error 1 = %02x\n"), buf[8]);
252                         }
253                         if (buf[9] != 0) {
254                                 fprintf(stderr, _("Error 2 = %02x\n"), buf[9]);
255                         }
256                         ptdev->tape_width_mm=buf[10];
257                         ptdev->tape_width_px=0;
258                         for (i=0; tape_info[i].mm > 0; i++) {
259                                 if (tape_info[i].mm == buf[10]) {
260                                         ptdev->tape_width_px=tape_info[i].px;
261                                 }
262                         }
263                         if (ptdev->tape_width_px == 0) {
264                                 fprintf(stderr, _("unknown tape width of %imm, please report this.\n"), buf[10]);
265                         }
266                         ptdev->media_type=buf[11];
267                         ptdev->status=buf[18];
268                         return 0;
269                 }
270         }
271         if (tx == 16) {
272                 fprintf(stderr, _("got only 16 bytes... wondering what they are:\n"));
273                 ptouch_rawstatus(buf);
274         }
275         if (tx != 32) {
276                 fprintf(stderr, _("read error: got %i instead of 32 bytes\n"), tx);
277                 return -1;
278         }
279         fprintf(stderr, _("strange status:\n"));
280         ptouch_rawstatus(buf);
281         fprintf(stderr, _("trying to flush junk\n"));
282         if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
283                 fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
284                 return -1;
285         }
286         fprintf(stderr, _("got another %i bytes. now try again\n"), tx);
287         return -1;
288 }
289
290 int ptouch_getmaxwidth(ptouch_dev ptdev)
291 {
292         return ptdev->tape_width_px;
293 }
294
295 int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, int len)
296 {
297         uint8_t buf[70];
298         int rc;
299
300         if (len > ptdev->devinfo->max_px / 8) {
301                 return -1;
302         }
303
304         buf[0]=0x47;
305         if (ptdev->devinfo->flags & FLAG_FORCE_TIFF) {
306                 /* Fake compression by encoding a single uncompressed run */
307                 buf[1] = len + 1;
308                 buf[2] = 0;
309                 buf[3] = len - 1;
310                 memcpy(buf + 4, data, len);
311                 rc = ptouch_send(ptdev, buf, len + 4);
312         } else {
313                 buf[1] = len;
314                 buf[2] = 0;
315                 memcpy(buf + 3, data, len);
316                 rc = ptouch_send(ptdev, buf, len + 3);
317         }
318         return rc;
319 }