]> git.sur5r.net Git - ptouch-print/blob - src/libptouch.c
Fix PT-2024PC support and some minor cleanup
[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, 53},       /* 9 mm tape */
39         {12, 75},       /* 12 mm tape */
40         {18, 117},      /* 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         {0,0,"",0,0}
56 };
57
58 void ptouch_rawstatus(uint8_t raw[32]);
59
60 int ptouch_open(ptouch_dev *ptdev)
61 {
62         libusb_device **devs;
63         libusb_device *dev;
64         libusb_device_handle *handle = NULL;
65         struct libusb_device_descriptor desc;
66         ssize_t cnt;
67         int r,i=0;
68
69         if ((*ptdev=malloc(sizeof(struct _ptouch_dev))) == NULL) {
70                 fprintf(stderr, _("out of memory\n"));
71                 return -1;
72         }
73         if (((*ptdev)->devinfo=malloc(sizeof(struct _pt_dev_info))) == NULL) {
74                 fprintf(stderr, _("out of memory\n"));
75                 return -1;
76         }
77         if ((libusb_init(NULL)) < 0) {
78                 fprintf(stderr, _("libusb_init() failed\n"));
79                 return -1;
80         }
81 //      libusb_set_debug(NULL, 3);
82         if ((cnt=libusb_get_device_list(NULL, &devs)) < 0) {
83                 return -1;
84         }
85         while ((dev=devs[i++]) != NULL) {
86                 if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
87                         fprintf(stderr, _("failed to get device descriptor"));
88                         libusb_free_device_list(devs, 1);
89                         return -1;
90                 }
91                 for (int k=0; ptdevs[k].vid > 0; k++) {
92                         if ((desc.idVendor == ptdevs[k].vid) && (desc.idProduct == ptdevs[k].pid) && (ptdevs[k].flags >= 0)) {
93                                 fprintf(stderr, _("%s found on USB bus %d, device %d\n"),
94                                         ptdevs[k].name,
95                                         libusb_get_bus_number(dev),
96                                         libusb_get_device_address(dev));
97                                 if ((r=libusb_open(dev, &handle)) != 0) {
98                                         fprintf(stderr, _("libusb_open error :%s\n"), libusb_error_name(r));
99                                         return -1;
100                                 }
101                                 libusb_free_device_list(devs, 1);
102                                 if ((r=libusb_kernel_driver_active(handle, 0)) == 1) {
103                                         if ((r=libusb_detach_kernel_driver(handle, 0)) != 0) {
104                                                 fprintf(stderr, _("error while detaching kernel driver: %s\n"), libusb_error_name(r));
105                                         }
106                                 }
107                                 if ((r=libusb_claim_interface(handle, 0)) != 0) {
108                                         fprintf(stderr, _("interface claim error: %s\n"), libusb_error_name(r));
109                                         return -1;
110                                 }
111                                 (*ptdev)->h=handle;
112                                 (*ptdev)->devinfo->max_px=ptdevs[k].max_px;
113                                 (*ptdev)->devinfo->flags=ptdevs[k].flags;
114                                 return 0;
115                         }
116                 }
117         }
118         fprintf(stderr, _("No P-Touch printer found on USB (remember to put switch to position E)\n"));
119         libusb_free_device_list(devs, 1);
120         return -1;
121 }
122
123 int ptouch_close(ptouch_dev ptdev)
124 {
125         libusb_release_interface(ptdev->h, 0);
126         libusb_close(ptdev->h);
127         return 0;
128 }
129
130 int ptouch_send(ptouch_dev ptdev, uint8_t *data, int len)
131 {
132         int r,tx;
133
134         if (ptdev == NULL) {
135                 return -1;
136         }
137         if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, len, &tx, 0)) != 0) {
138                 fprintf(stderr, _("write error: %s\n"), libusb_error_name(r));
139                 return -1;
140         }
141         if (tx != len) {
142                 fprintf(stderr, _("write error: could send only %i of %i bytes\n"), tx, len);
143                 return -1;
144         }
145         return 0;
146 }
147
148 int ptouch_init(ptouch_dev ptdev)
149 {
150         char cmd[]="\x1b\x40";          /* 1B 40 = ESC @ = INIT */
151         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
152 }
153
154 int ptouch_rasterstart(ptouch_dev ptdev)
155 {
156         char cmd[] = "\x1b\x69\x52\x01";        /* 1B 69 52 01 = Select graphics transfer mode = Raster */
157         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
158 }
159
160 /* print an empty line */
161 int ptouch_lf(ptouch_dev ptdev)
162 {
163         char cmd[]="\x5a";
164         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
165 }
166
167 /* print and advance tape, but do not cut */
168 int ptouch_ff(ptouch_dev ptdev)
169 {
170         char cmd[]="\x0c";
171         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
172 }
173
174 /* print and cut tape */
175 int ptouch_eject(ptouch_dev ptdev)
176 {
177         char cmd[]="\x1a";
178         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
179 }
180
181 /* print a "cut here" mark (it's just a dashed line) */
182 #define CUTMARK_SPACING 5
183 int ptouch_cutmark(ptouch_dev ptdev)
184 {
185         uint8_t buf[32];
186         int i,len=16;
187
188         for (i=0; i<CUTMARK_SPACING; i++) {
189                 ptouch_lf(ptdev);
190         }
191         ptouch_rasterstart(ptdev);
192         buf[0]=0x47;
193         buf[1]=len;
194         buf[2]=0;
195         memset(buf+3, 0, len);
196         int offset=(64-ptouch_getmaxwidth(ptdev)/2);
197         for (i=0; i<ptouch_getmaxwidth(ptdev); i++) {
198                 if ((i%8) <= 3) {       /* pixels 0-3 get set, 4-7 are unset */
199                         buf[3+15-((offset+i)/8)] |= 1<<((offset+i)%8);
200                 }
201         }
202         ptouch_send(ptdev, buf, len+3);
203         for (i=0; i<CUTMARK_SPACING; i++) {
204                 ptouch_lf(ptdev);
205         }
206         return 0;
207 }
208
209 void ptouch_rawstatus(uint8_t raw[32])
210 {
211         fprintf(stderr, _("debug: dumping raw status bytes\n"));
212         for (int i=0; i<32; i++) {
213                 fprintf(stderr, "%02x ", raw[i]);
214                 if (((i+1) % 16) == 0) {
215                         fprintf(stderr, "\n");
216                 }
217         }
218         fprintf(stderr, "\n");
219         return;
220 }
221
222 int ptouch_getstatus(ptouch_dev ptdev)
223 {
224         char cmd[]="\x1b\x69\x53";
225         uint8_t buf[32];
226         int i, r, tx=0, tries=0;
227         struct timespec w;
228
229         ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
230         while (tx == 0) {
231                 w.tv_sec=0;
232                 w.tv_nsec=100000000;    /* 0.1 sec */
233                 r=nanosleep(&w, NULL);
234                 if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
235                         fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
236                         return -1;
237                 }
238                 tries++;
239                 if (tries > 10) {
240                         fprintf(stderr, _("timeout while waiting for status response\n"));
241                         return -1;
242                 }
243         }
244         if (tx == 32) {
245                 if (buf[0]==0x80 && buf[1]==0x20) {
246                         memcpy(ptdev->raw, buf, 32);
247                         if (buf[8] != 0) {
248                                 fprintf(stderr, _("Error 1 = %02x\n"), buf[8]);
249                         }
250                         if (buf[9] != 0) {
251                                 fprintf(stderr, _("Error 2 = %02x\n"), buf[9]);
252                         }
253                         ptdev->tape_width_mm=buf[10];
254                         ptdev->tape_width_px=0;
255                         for (i=0; tape_info[i].mm > 0; i++) {
256                                 if (tape_info[i].mm == buf[10]) {
257                                         ptdev->tape_width_px=tape_info[i].px;
258                                 }
259                         }
260                         if (ptdev->tape_width_px == 0) {
261                                 fprintf(stderr, _("unknown tape width of %imm, please report this.\n"), buf[10]);
262                         }
263                         ptdev->media_type=buf[11];
264                         ptdev->status=buf[18];
265                         return 0;
266                 }
267         }
268         if (tx == 16) {
269                 fprintf(stderr, _("got only 16 bytes... wondering what they are:\n"));
270                 ptouch_rawstatus(buf);
271         }
272         if (tx != 32) {
273                 fprintf(stderr, _("read error: got %i instead of 32 bytes\n"), tx);
274                 return -1;
275         }
276         fprintf(stderr, _("strange status:\n"));
277         ptouch_rawstatus(buf);
278         fprintf(stderr, _("trying to flush junk\n"));
279         if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
280                 fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
281                 return -1;
282         }
283         fprintf(stderr, _("got another %i bytes. now try again\n"), tx);
284         return -1;
285 }
286
287 int ptouch_getmaxwidth(ptouch_dev ptdev)
288 {
289         return ptdev->tape_width_px;
290 }
291
292 int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, int len)
293 {
294         uint8_t buf[70];
295         int rc;
296
297         if (len > ptdev->devinfo->max_px / 8) {
298                 return -1;
299         }
300
301         buf[0]=0x47;
302         if (ptdev->devinfo->flags & FLAG_FORCE_TIFF) {
303                 /* Fake compression by encoding a single uncompressed run */
304                 buf[1] = len + 1;
305                 buf[2] = 0;
306                 buf[3] = len - 1;
307                 memcpy(buf + 4, data, len);
308                 rc = ptouch_send(ptdev, buf, len + 4);
309         } else {
310                 buf[1] = len;
311                 buf[2] = 0;
312                 memcpy(buf + 3, data, len);
313                 rc = ptouch_send(ptdev, buf, len + 3);
314         }
315         return rc;
316 }