aboutsummaryrefslogtreecommitdiffstats
path: root/src/ql570.c
blob: e642f91112d0a8149e54584aadb7ae07b220abe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* ql570.c: facilities for communicating with the QL-570 label printer
 *
 * Copyright (C) 2015 Clemens Fries <github-raster@xenoworld.de>
 *
 * This file is part of rastertoql570.
 *
 * rastertoql570 is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * rastertoql570 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rastertoql570.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ql570.h"

/**
 * Request status from printer.
 *
 * @param device file descriptor to write to
 */
void
ql_status_request(FILE *device)
{
	uint8_t request[3] = {QL_ESC, 0x69, 0x53};
	fwrite(request, 3, 1, device);
	fflush(device);
}

/**
 * Read a status response from the printer.
 *
 * @param status ql_status struct holding the response
 * @param device file descriptor to write to
 * @return true if successful, false if short read
 */
bool
ql_status_read(ql_status *status, FILE *device)
{
	size_t len = fread(status, sizeof(ql_status), 1, device);

	if (len != sizeof(ql_status)) {
		return false;
	}

	return true;
}

/**
 * Initialise printer.
 *
 * The protocol specification has an optional recommendation to flush lingering
 * partial commands with 200 bytes of 0x00.
 *
 * @param flush prepend 200 bytes of invalid (0x00) data
 * @param device file descriptor to write to
 */
void
ql_init(bool flush, FILE *device)
{
	if (flush) {
		uint8_t init_buffer[200];
		memset(&init_buffer, 0x0, 200);
		fwrite(init_buffer, sizeof(uint8_t), 200, device);
		fflush(device);
	}

	uint8_t request[2] = {QL_ESC, 0x40};
	fwrite(request, 2, 1, device);
	fflush(device);
}

/**
 * Write one line of raster data to the printer.
 *
 * For most printers in the QL series one raster line is at most 90 bytes long.
 * As these are monochrome printers this means a line has 720 pixels (at most).
 *
 * @param length length of the raster data
 * @param data pointer to the raster data
 * @param device file descriptor to write to
 */
void
ql_raster(uint8_t length, uint8_t *data, FILE *device)
{
	uint8_t request[3] = {0x67, 0x00, length};
	fwrite(request, 3, 1, device);
	fwrite(data, length, 1, device);
	fflush(device);
}

/**
 * Signal end of raster data.
 *
 * @param length length of raster data (0x00)
 * @param device file descriptor to write to
 */
void
ql_raster_end(uint8_t length, FILE *device)
{
	uint8_t request[3] = {0x67, 0xFF, length};

	uint8_t data[length];
	memset(&data, 0x00, length);

	fwrite(request, 3, 1, device);
	fwrite(data, length, 1, device);

	fflush(device);
}

/**
 * Start a new page.
 *
 * This will write out the contents of the print_info pointer to the printer.
 *
 * @param print_info print_info
 * @param device file descriptor to write to
 */
void
ql_page_start(ql_print_info *print_info, FILE *device)
{
	uint8_t request[3] = {QL_ESC, 0x69, 0x7A};
	fwrite(request, 3, 1, device);
	fwrite(print_info, sizeof(ql_print_info), 1, device);
	fflush(device);
}

/**
 * End the current page.
 *
 * @param last_page indicate that this is the last page
 * @param device file descriptor to write to
 */
void
ql_page_end(bool last_page, FILE *device)
{
	uint8_t request;

	if (last_page) {
		request = 0x1A;
	} else {
		request = 0x0C;
	}

	fwrite(&request, 1, 1, device);
	fflush(device);
}


/**
 * Set extended options.
 *
 * This is called 'set expanded mode' in the specification.
 *
 * ### Cutting
 *
 * Pain points with the specification:
 *
 * * The diagram is wrong. The description says, this is bit 3, the diagram
 *   says, this is bit 4. This cost me some time.
 *   TODO: Check 720N/NW. The documentation there lacks the diagram and the
 *   description sys it is bit 4. I bet this is wrong!
 * * Not cutting is the default. The description says that 'cut at end' is the
 *   default.
 *
 * TODO: This seems to cut after each page, regardless of whether last_page is
 * set. I don't get it.
 *
 * ### High resolution printing
 *
 * Note that this is the resolution along the label _length_. The resolution
 * along the _width_ of the media will stay the same. This means that more
 * lines will be squeezed in the same space.
 *
 * Selecting high resolution printing, will allow the shortest label produced
 * to be shrunk from 12.7mm to 6.35mm. The minimum label length is 150 lines,
 * doubling the resolution will halve the length.
 *
 * @param cut_at_end
 * @param high_resolution false: 300x300 dpi, true: 300x600 dpi
 * @param device file descriptor to write to
 */
void
ql_set_extended_options(bool cut_at_end, bool high_resolution, FILE *device)
{
	uint8_t options = 0x00;

	if (cut_at_end)
		options |= OPT_CUT_AT_END;

	if (high_resolution)
		options |= OPT_HIGH_RESOLUTION;

	uint8_t request[4] = {QL_ESC, 0x69, 0x4B, options};
	fwrite(request, 4, 1, device);
	fflush(device);
}

/**
 * Enable automatic label cutting.
 *
 * The specification calls this "set each mode". The bits of the last byte in
 * the command sequence are either described as unused or undefined, setting
 * bit 6 enables auto cut.
 *
 * @param device file descriptor to write to
 */
void
ql_autocut_enable(FILE *device)
{
	uint8_t request[4] = {QL_ESC, 0x69, 0x4D, 0b01000000};
	fwrite(request, 4, 1, device);
	fflush(device);
}

/**
 * Cut after each _n_ labels.
 *
 * For continuous length labels it makes sense to cut after every page. For 
 * other label types it might make sense to cut after several labels.
 *
 * TODO: default is 1, does that work? If yes, note here
 *
 * @param interval cut after _interval_ number of pages
 * @param device file descriptor to write to
 */
void
ql_autocut_interval(uint8_t interval, FILE *device)
{
	uint8_t request[4] = {QL_ESC, 0x69, 0x41, interval};
	fwrite(request, 4, 1, device);
	fflush(device);
}

/**
 * Sets the default margins.
 *
 * The specification recommends setting the margins to 35 for several printer
 * types, including the QL-570. For other types this is not indicated. Setting
 * this seems to be optional.
 *
 * If the media is 'die cut', then the margins will always be 0.
 *
 * See ql_set_margins for details.
 *
 * @param
 * @param device file descriptor to write to
 *
 */
void
ql_set_default_margins(enum ql_media_type media_type, FILE *device)
{
	if (media_type == MT_CONTINUOUS)
		ql_set_margins(35, device);
	else
		ql_set_margins(0, device);
}

/**
 * Set margins on continuous tape.
 *
 * This adds _margins_ amount of blank lines before and after the label.
 *
 * @param margins number of blank lines
 * @param device file descriptor to write to
 */
void
ql_set_margins(uint16_t margins, FILE *device)
{
	uint8_t request[5] = {QL_ESC, 0x69, 0x64, margins & 0x00FF, (margins & 0xFF00) >> 8};
	fwrite(request, 5, 1, device);
	fflush(device);
}