]> git.sur5r.net Git - ptouch-print/blob - src/libptouch.c
f0f69f0ffefe210d04a4a837671de8f83f6e3536
[ptouch-print] / src / libptouch.c
1 /*
2         libptouch - functions to help accessing a brother ptouch
3
4         Copyright (C) 2013 Dominic Radermacher <dominic.radermacher@gmail.com>
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 struct _pt_tape_info tape_info[6]= {
36         {6, 32},        /* 6mm tape is 32px wide? works for me ;-) */
37         {9, 52},        /* 9mm tape is 52px wide? works for me ;-) */
38         {12,76},        /* and 76px work for me on a 12mm tape - maybe its only 64px */
39         {18,120},
40         {24,128},
41         {0,0}           /* terminating entry */
42 };
43
44 struct _pt_dev_info ptdevs[] = {
45         {0x04f9, 0x202d, "PT-2430PC", 128, FLAG_NONE},  /* 180dpi, maximum 128px */
46         {0x04f9, 0x202c, "PT-1230PC", 76, FLAG_NONE},   /* 180dpi, supports tapes up to 12mm - I don't know how much pixels it can print! */
47         {0x04f9, 0x2061, "PT-P700", 120, FLAG_UNSUP_RASTER},    /* DOES NOT WORK */
48         {0x04f9, 0x2073, "PT-D450VP", 120, FLAG_UNSUP_RASTER},  /* DOES NOT WORK */
49         /* Notes about the PT-D450VP: Tape detecting works, but printing does
50            not. The tape is just blank. I assume, the printer does not understand
51            the sent rasterdata. I'm also unsure about how many dots width we have */
52         {0,0,"",0,0}
53 };
54
55 void ptouch_rawstatus(uint8_t raw[32]);
56
57 int ptouch_open(ptouch_dev *ptdev)
58 {
59         libusb_device **devs;
60         libusb_device *dev;
61         libusb_device_handle *handle = NULL;
62         struct libusb_device_descriptor desc;
63         ssize_t cnt;
64         int r,i=0;
65
66         if ((*ptdev=malloc(sizeof(struct _ptouch_dev))) == NULL) {
67                 fprintf(stderr, _("out of memory\n"));
68                 return -1;
69         }
70         if (((*ptdev)->devinfo=malloc(sizeof(struct _pt_dev_info))) == NULL) {
71                 fprintf(stderr, _("out of memory\n"));
72                 return -1;
73         }
74         if ((libusb_init(NULL)) < 0) {
75                 fprintf(stderr, _("libusb_init() failed\n"));
76                 return -1;
77         }
78 //      libusb_set_debug(NULL, 3);
79         if ((cnt=libusb_get_device_list(NULL, &devs)) < 0) {
80                 return -1;
81         }
82         while ((dev=devs[i++]) != NULL) {
83                 if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
84                         fprintf(stderr, _("failed to get device descriptor"));
85                         libusb_free_device_list(devs, 1);
86                         return -1;
87                 }
88                 for (int k=0; ptdevs[k].vid > 0; k++) {
89                         if ((desc.idVendor == ptdevs[k].vid) && (desc.idProduct == ptdevs[k].pid) && (ptdevs[k].flags >= 0)) {
90                                 fprintf(stderr, _("%s found on USB bus %d, device %d\n"),
91                                         ptdevs[k].name,
92                                         libusb_get_bus_number(dev),
93                                         libusb_get_device_address(dev));
94                                 if ((r=libusb_open(dev, &handle)) != 0) {
95                                         fprintf(stderr, _("libusb_open error :%s\n"), libusb_error_name(r));
96                                         return -1;
97                                 }
98                                 libusb_free_device_list(devs, 1);
99                                 if ((r=libusb_kernel_driver_active(handle, 0)) == 1) {
100                                         if ((r=libusb_detach_kernel_driver(handle, 0)) != 0) {
101                                                 fprintf(stderr, _("error while detaching kernel driver: %s\n"), libusb_error_name(r));
102                                         }
103                                 }
104                                 if ((r=libusb_claim_interface(handle, 0)) != 0) {
105                                         fprintf(stderr, _("interface claim error: %s\n"), libusb_error_name(r));
106                                         return -1;
107                                 }
108                                 (*ptdev)->h=handle;
109                                 (*ptdev)->devinfo->max_px=ptdevs[k].max_px;
110                                 (*ptdev)->devinfo->flags=ptdevs[k].flags;
111                                 return 0;
112                         }
113                 }
114         }
115         fprintf(stderr, _("No P-Touch printer found on USB (remember to put switch to position E)\n"));
116         libusb_free_device_list(devs, 1);
117         return -1;
118 }
119
120 int ptouch_close(ptouch_dev ptdev)
121 {
122         libusb_release_interface(ptdev->h, 0);
123         libusb_close(ptdev->h);
124         return 0;
125 }
126
127 int ptouch_send(ptouch_dev ptdev, uint8_t *data, int len)
128 {
129         int r,tx;
130
131         if (ptdev == NULL) {
132                 return -1;
133         }
134         if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, len, &tx, 0)) != 0) {
135                 fprintf(stderr, _("write error: %s\n"), libusb_error_name(r));
136                 return -1;
137         }
138         if (tx != len) {
139                 fprintf(stderr, _("write error: could send only %i of %i bytes\n"), tx, len);
140                 return -1;
141         }
142         return 0;
143 }
144
145 int ptouch_init(ptouch_dev ptdev)
146 {
147         char cmd[]="\x1b\x40";          /* 1B 40 = ESC @ = INIT */
148         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
149 }
150
151 int ptouch_rasterstart(ptouch_dev ptdev)
152 {
153         char cmd[]="\x1b\x69\x52\x01";  /* 1B 69 52 01 = RASTER DATA */
154         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
155 }
156
157 /* print an empty line */
158 int ptouch_lf(ptouch_dev ptdev)
159 {
160         char cmd[]="\x5a";
161         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
162 }
163
164 /* print and advance tape, but do not cut */
165 int ptouch_ff(ptouch_dev ptdev)
166 {
167         char cmd[]="\x0c";
168         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
169 }
170
171 /* print and cut tape */
172 int ptouch_eject(ptouch_dev ptdev)
173 {
174         char cmd[]="\x1a";
175         return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
176 }
177
178 /* print a "cut here" mark (it's just a dashed line) */
179 #define CUTMARK_SPACING 5
180 int ptouch_cutmark(ptouch_dev ptdev)
181 {
182         uint8_t buf[32];
183         int i,len=16;
184
185         for (i=0; i<CUTMARK_SPACING; i++) {
186                 ptouch_lf(ptdev);
187         }
188         ptouch_rasterstart(ptdev);
189         buf[0]=0x47;
190         buf[1]=len;
191         buf[2]=0;
192         memset(buf+3, 0, len);
193         int offset=(64-ptouch_getmaxwidth(ptdev)/2);
194         for (i=0; i<ptouch_getmaxwidth(ptdev); i++) {
195                 if ((i%8) <= 3) {       /* pixels 0-3 get set, 4-7 are unset */
196                         buf[3+15-((offset+i)/8)] |= 1<<((offset+i)%8);
197                 }
198         }
199         ptouch_send(ptdev, buf, len+3);
200         for (i=0; i<CUTMARK_SPACING; i++) {
201                 ptouch_lf(ptdev);
202         }
203         return 0;
204 }
205
206 void ptouch_rawstatus(uint8_t raw[32])
207 {
208         fprintf(stderr, _("debug: dumping raw status bytes\n"));
209         for (int i=0; i<32; i++) {
210                 fprintf(stderr, "%02x ", raw[i]);
211                 if (((i+1) % 16) == 0) {
212                         fprintf(stderr, "\n");
213                 }
214         }
215         fprintf(stderr, "\n");
216         return;
217 }
218
219 int ptouch_getstatus(ptouch_dev ptdev)
220 {
221         char cmd[]="\x1b\x69\x53";
222         uint8_t buf[32];
223         int i, r, tx=0, tries=0;
224         struct timespec w;
225
226         ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
227         while (tx == 0) {
228                 w.tv_sec=0;
229                 w.tv_nsec=100000000;    /* 0.1 sec */
230                 r=nanosleep(&w, NULL);
231                 if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
232                         fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
233                         return -1;
234                 }
235                 tries++;
236                 if (tries > 10) {
237                         fprintf(stderr, _("timeout while waiting for status response\n"));
238                         return -1;
239                 }
240         }
241         if (tx == 32) {
242                 if (buf[0]==0x80 && buf[1]==0x20) {
243                         memcpy(ptdev->raw, buf, 32);
244                         if (buf[8] != 0) {
245                                 fprintf(stderr, _("Error 1 = %02x\n"), buf[8]);
246                         }
247                         if (buf[9] != 0) {
248                                 fprintf(stderr, _("Error 2 = %02x\n"), buf[9]);
249                         }
250                         ptdev->tape_width_mm=buf[10];
251                         ptdev->tape_width_px=0;
252                         for (i=0; tape_info[i].mm > 0; i++) {
253                                 if (tape_info[i].mm == buf[10]) {
254                                         ptdev->tape_width_px=tape_info[i].px;
255                                 }
256                         }
257                         if (ptdev->tape_width_px == 0) {
258                                 fprintf(stderr, _("unknown tape width of %imm, please report this.\n"), buf[10]);
259                         }
260                         ptdev->media_type=buf[11];
261                         ptdev->status=buf[18];
262                         return 0;
263                 }
264         }
265         if (tx == 16) {
266                 fprintf(stderr, _("got only 16 bytes... wondering what they are:\n"));
267                 ptouch_rawstatus(buf);
268         }
269         if (tx != 32) {
270                 fprintf(stderr, _("read error: got %i instead of 32 bytes\n"), tx);
271                 return -1;
272         }
273         fprintf(stderr, _("strange status:\n"));
274         ptouch_rawstatus(buf);
275         fprintf(stderr, _("trying to flush junk\n"));
276         if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
277                 fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
278                 return -1;
279         }
280         fprintf(stderr, _("got another %i bytes. now try again\n"), tx);
281         return -1;
282 }
283
284 int ptouch_getmaxwidth(ptouch_dev ptdev)
285 {
286         /* TODO: should also check what the device supports. but I assume,
287            you can't use a large tape in a printe that doesn't support it anyways */
288         return ptdev->tape_width_px;
289 }
290
291 int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, int len)
292 {
293         uint8_t buf[32];
294
295         if (len > 16) {         /* PT-2430PC can not print more than 128 px */
296                 return -1;      /* as we support more devices, we need to check */
297         }                       /* how much pixels each device support */
298         buf[0]=0x47;
299         buf[1]=len;
300         buf[2]=0;
301         memcpy(buf+3, data, len);
302         return ptouch_send(ptdev, buf, len+3);
303 }