-
Notifications
You must be signed in to change notification settings - Fork 4
/
interactive.c
196 lines (175 loc) · 4.01 KB
/
interactive.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
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include <string.h>
void dump_buffer(unsigned length, char* data){
unsigned i;
for(i=0;i<length;i++){
printf("%02d|%02X ", i, (unsigned char)data[i]);
if(i>0&&((i+1)%16)==0){
printf("\n");
}
}
printf("\n");
}
int send_data(int fd, unsigned len, char* data){
int rv;
rv=write(fd, data, len);
return rv;
}
int fetch_response(int fd, unsigned timeout, unsigned maxbytes, char* data){
fd_set readfds;
struct timeval tv;
int rv;
FD_ZERO(&readfds);
FD_SET(fd, &readfds);
tv.tv_sec=(timeout/1000000);
tv.tv_usec=(timeout%1000000);
syncfs(fd);
rv=select(fd+1, &readfds, NULL, NULL, &tv);
if(rv<0){
perror("select");
return -1;
}
else if(rv==0){
//nothing
return 0;
}
else if(FD_ISSET(fd, &readfds)){
//data ready, read it
rv=read(fd, data, maxbytes);
return rv;
}
//weird. somethings broken.
fprintf(stderr, "select returned bs.\n");
return -1;
}
int main(int argc, char** argv){
int fd=-1;
int rv;
int oper;
char buffer[1024];
char bmode_buffer[8];
bool bmode=false;
unsigned bmode_byte=0;
unsigned bmode_bit=0;
memset(bmode_buffer, 0, 8);
printf("PT1230 Interactive mode\n");
//open device file
fd=open("/dev/usb/lp0", O_RDWR | O_SYNC);
if(fd<0){
printf("Failed to open device\n");
exit(1);
}
do{
oper=getchar();
if(!bmode&&(oper=='\n'||oper=='\r')){
continue;
}
switch(oper){
case 'i':
printf("Checking for incoming data.\n");
break;
case 'b':
bmode=!bmode;
if(!bmode){
printf("Disabled bitmap mode, print with p/f\n");
}
else{
printf("Enabled bitmap mode, enter lines of 64 0/1 ASCII bytes, terminate with b\n");
memset(bmode_buffer, 0, 8);
}
break;
case '1':
bmode_buffer[(7-bmode_byte)]|=((0x01)<<(bmode_bit));
case '0':
if(bmode){
bmode_bit++;
bmode_bit%=8;
if(bmode_bit==0){
bmode_byte++;
bmode_byte%=8;
}
}
break;
case '\n':
if(bmode){
printf("Sending bmode raster line\n");
dump_buffer(8, bmode_buffer);
rv=send_data(fd, 7, "G\x0C\0\0\0\0\0");
printf("Sent %d bytes\n", rv);
rv=send_data(fd, 8, bmode_buffer);
printf("Sent %d bytes\n", rv);
memset(bmode_buffer, 0, 8);
}
else{
printf("Something went wrong.\n");
}
break;
case 'c':
printf("Clear buffer\n");
rv=send_data(fd, 2, "\x1b@");
printf("Sent %d bytes\n", rv);
break;
case 's':
printf("Requesting status\n");
rv=send_data(fd, 3, "\x1biS");
printf("Sent %d bytes\n", rv);
break;
case 'r':
printf("Switching device to raster mode\n");
rv=send_data(fd, 4, "\x1biR\x01");
printf("Sent %d bytes\n", rv);
break;
case 'z':
printf("Sending blank raster line\n");
rv=send_data(fd, 1, "Z");
printf("Sent %d bytes\n", rv);
break;
case 'l':
printf("Sending raster line\n");
rv=send_data(fd, 15, "G\x0c\x00\0\0\0\0\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
printf("Sent %d bytes\n", rv);
break;
case 'x':
printf("Sending stripe raster line a\n");
rv=send_data(fd, 15, "G\x0c\x00\0\0\0\0\x8A\xAA\xAA\xAA\xAA\xAA\xAA\xA1");
printf("Sent %d bytes\n", rv);
break;
case 'y':
printf("Sending stripe raster line b\n");
rv=send_data(fd, 15, "G\x0c\x00\0\0\0\0\x85\x55\x55\x55\x55\x55\x55\x51");
printf("Sent %d bytes\n", rv);
break;
case 'f':
printf("Sending print and feed command\n");
rv=send_data(fd, 1, "\x1a");
printf("Sent %d bytes\n", rv);
break;
case 'p':
printf("Sending print command\n");
rv=send_data(fd, 1, "\x0c");
printf("Sent %d bytes\n", rv);
break;
}
rv=fetch_response(fd, 2000000, 1024, buffer);
if(rv<0){
printf("Response reading failed, aborting\n");
oper='q';
}
else{
if(rv>0){
printf("Read %d bytes\n", rv);
dump_buffer(rv, buffer);
}
}
}
while(oper!='q'&&oper!=EOF);
printf("Closing down\n");
close(fd);
}