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