-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpx84-connector.c
101 lines (79 loc) · 2.01 KB
/
hpx84-connector.c
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
/*
A simple tool to feed an HPX-84 plotter data
Copyright (C) 2014 Gnoxter
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <linux/parport.h>
#include <linux/ppdev.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define BUFSIZE 512
void usage() {
printf("./hpx84-connector <file.hpgl>\n");
printf("\t -d device : set different port\n");
printf("\t reads from stdin if no file given\n");
}
int main(int argc, char *argv[]) {
int portFD, fd, i, readFromSTDIN = 2;
FILE *file;
int mode = IEEE1284_MODE_COMPAT;
char *ppdev = "/dev/parport0";
char buf[BUFSIZE];
size_t nread;
for(i= 0; i < argc; i++) {
if(argv[i][0] != '-')
continue;
//set non default ppdev port
if (argv[i][1] == 'd' && argc > i+1) {
ppdev = argv[i+1];
readFromSTDIN += 2;
continue;
}
usage();
exit(EXIT_FAILURE);
}
/*
Setup Parallel Port
*/
portFD = open(ppdev, O_RDWR);
if(ioctl(portFD, PPCLAIM)) {
perror("ERROR PPCLAIM");
exit(1);
}
if(ioctl(portFD, PPNEGOT, &mode)) {
perror("ERROR PPSETMODE");
exit(1);
}
/*
Setup File
*/
if (argc < readFromSTDIN) {
file = stdin;
} else {
file = fopen(argv[argc-1], "r");
if(file == NULL) {
perror("ERROR opening FILE");
exit(1);
}
}
//write to plotter
while((nread = fread(buf, 1, BUFSIZE, file)) > 0) {
write(portFD, buf, nread);
}
return 0;
};