aboutsummaryrefslogtreecommitdiffstats
path: root/src/examples/minimal.c
blob: 2ebdc44e9b667b7eb166386b73490f6827890f4d (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
/* minimal.c: a minmal example for using the QL-570 utility functions
 *
 * 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"
#include <stdlib.h>
#include <errno.h>

/**
 * Raster line length for the QL-570 is 90 bytes. There are some other QL
 * series printers that need a larger buffer.
 */
#define BUFFER_SIZE 90

/**
 * Minimal example program for direct printer access.
 *
 * For a better understanding of what goes on here, please refer
 * to the documentation of each function. At times there are
 * interesting bits of information over there.
 *
 */
int main()
{
	char *devname = "/dev/usb/lp0";
	FILE *device = fopen(devname, "wb");

	if (device == NULL) {
		fprintf(stderr, "Error while opening %s: %s\n", devname, strerror(errno));
		return EXIT_FAILURE;
	}

	ql_status status = {0};
	uint8_t buffer[BUFFER_SIZE];

	/*
	 * Request a print job with 150 lines (the minimal amount of lines for
	 * the QL-570). Note that this number is mandatory. If you print less
	 * than 150 lines, or indicate some wrong amount of lines here, the
	 * printer will just resort to blinking its red LED while observing you
	 * with displeasure.
	 */
	ql_print_info print_info = { .raster_number[0] = 150 };

	/*
	 * Initialise printer. The first flag indicates that we won't be
	 * flushing the printer with 200 bytes of zeroes.
	 */
	ql_init(false, device);

	/*
	 * Ask the printer to give us a status report.
	 */
	ql_status_request(device);

	/*
	 * Retrieve the status report.
	 */
	ql_status_read(&status, device);

	/*
	 * Send the contents of `print_info` to the printer.
	 */
	ql_page_start(&print_info, device);

	/*
	 * Set some extended options: cut-at-end plus 300dpi printing.
	 */
	ql_set_extended_options(true, false, device);
	
	/*
	 * Print some black lines.
	 */
	for (int i = 0; i < 150; i++) {

		/*
		 * Every now and then: Many, many 1s.
		 */
	        memset(buffer, i % 5 == 0 ? 0xFF : 0x00, BUFFER_SIZE);

		/*
		 * We empty the first two bytes in order to force a small
		 * margin. Note that for 62mm the right and left margin is
		 * about 12 dots. I observed that I have a margin any way on
		 * one side, but it looks like it is printing outside the label
		 * area on the other side. It's not that important, but I do
		 * it anyway because I'm not sure about the mechanics and how
		 * well the print heads cope with doing their thing outside
		 * the printable area.
		 *
		 */
		buffer[0] = 0x00;
		buffer[1] = 0x00;

		/*
		 * Send this raster line to the printer.
		 */
	        ql_raster(BUFFER_SIZE, buffer, device);
	}
	
	/*
	 * Indicate the end of raster data to the printer.
	 */
	ql_raster_end(BUFFER_SIZE, device);

	/*
	 * Tell it that this was the last page.
	 */
	ql_page_end(true, device);

	fclose(device);
}