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