| 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 | #include <stdio.h> |
|---|
| 29 | #include <stdlib.h> |
|---|
| 30 | #include <unistd.h> |
|---|
| 31 | #include <fcntl.h> |
|---|
| 32 | #include <errno.h> |
|---|
| 33 | #include <stdarg.h> |
|---|
| 34 | #include <math.h> |
|---|
| 35 | |
|---|
| 36 | #include <sys/ioctl.h> |
|---|
| 37 | #include <linux/ppdev.h> |
|---|
| 38 | #include <linux/parport.h> |
|---|
| 39 | |
|---|
| 40 | #include <sys/time.h> // debugging |
|---|
| 41 | |
|---|
| 42 | #define PARPORT_DEVICE "/dev/parport0" |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | #define DPRINTF(bla...) { if(debug_flag) fprintf(stderr,bla); } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | unsigned char debug_flag; |
|---|
| 54 | size_t write_puncher(int fd, unsigned char buf); |
|---|
| 55 | void visualize_byte(unsigned char buf); |
|---|
| 56 | void calculate_time(int already_punched_bytes, int estimated_punch_length, int mtime_since_beginning); |
|---|
| 57 | |
|---|
| 58 | |
|---|
| 59 | int mtime(long long since) { |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | struct timeval time; |
|---|
| 64 | gettimeofday(&time, NULL); |
|---|
| 65 | |
|---|
| 66 | return (time.tv_sec * 1000000 + time.tv_usec) - since; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | int main(int argc, char **argv) { |
|---|
| 70 | int parport_fd; |
|---|
| 71 | int mode; |
|---|
| 72 | long long time_since_beginning; |
|---|
| 73 | int already_punched = 0; |
|---|
| 74 | int estimated_data_length = 0; |
|---|
| 75 | |
|---|
| 76 | printf("FACIT 4070 Tape punch-75 CPS userspace driver\n"); |
|---|
| 77 | printf("time ms |123.45678| hex=dec status: =) punch successful / :-( still busy\n"); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | debug_flag = 0; |
|---|
| 81 | opterr = 0; |
|---|
| 82 | int c; |
|---|
| 83 | while( (c = getopt(argc, argv, "dhl:s:")) != -1) |
|---|
| 84 | switch(c) { |
|---|
| 85 | case 'd': |
|---|
| 86 | debug_flag = 1; |
|---|
| 87 | break; |
|---|
| 88 | case 'h': |
|---|
| 89 | fprintf(stderr, "Usage: %s [-d] [-h] [-lNUM]\n" |
|---|
| 90 | " optional parameters:\n" |
|---|
| 91 | " -d debug mode, print out verbose informations about signals\n" |
|---|
| 92 | " if not set, only normal verbosity will be printed out on\n" |
|---|
| 93 | " stdout, painting an ASCII paper tape which visualizes the\n" |
|---|
| 94 | " bytes (see above for the caption)\n\n" |
|---|
| 95 | " -h display this help message\n\n" |
|---|
| 96 | " -s same as -l\n" |
|---|
| 97 | " -l sets the NUMBER OF BYTES which are expected on stdin.\n" |
|---|
| 98 | " with this information we can complete the status information\n" |
|---|
| 99 | " with useful informations about the estimated remaining time\n" |
|---|
| 100 | " to punch (useful for longer data)\n" |
|---|
| 101 | " example: cat 2k-data-file | %s -l2000\n", |
|---|
| 102 | argv[0], argv[0]); |
|---|
| 103 | return 0; |
|---|
| 104 | case 'l': |
|---|
| 105 | case 's': |
|---|
| 106 | c = atoi(optarg); |
|---|
| 107 | if( c > 0 ) { |
|---|
| 108 | estimated_data_length = c; |
|---|
| 109 | break; |
|---|
| 110 | } |
|---|
| 111 | printf("%s: data length must be a positive integer, like '123', not %s\n", argv[0], optarg); |
|---|
| 112 | case '?': break; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | parport_fd = open(PARPORT_DEVICE, O_RDWR); |
|---|
| 117 | if(parport_fd == -1) { |
|---|
| 118 | perror("opening device failed"); |
|---|
| 119 | return 1; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | if(ioctl(parport_fd, PPCLAIM)) { |
|---|
| 125 | perror ("claiming port (PPCLAIM)"); |
|---|
| 126 | close (parport_fd); |
|---|
| 127 | return 1; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | mode = IEEE1284_MODE_COMPAT; |
|---|
| 133 | if(ioctl(parport_fd, PPNEGOT, &mode)) { |
|---|
| 134 | perror ("Setting compatibilty mode (PPNEGOT)"); |
|---|
| 135 | close (parport_fd); |
|---|
| 136 | return 1; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | DPRINTF("Waiting for Puncher beeing ready for signals...\n"); |
|---|
| 140 | { |
|---|
| 141 | unsigned char status; |
|---|
| 142 | if(ioctl(parport_fd, PPRSTATUS, &status)) return -1; |
|---|
| 143 | if((status & PARPORT_STATUS_BUSY) == PARPORT_STATUS_BUSY) { |
|---|
| 144 | DPRINTF("Puncher is BUSY!\n"); |
|---|
| 145 | usleep(8000); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | DPRINTF("Start reading from stdin...\n"); |
|---|
| 153 | time_since_beginning = mtime(0); |
|---|
| 154 | for(;;) { |
|---|
| 155 | unsigned char buf; |
|---|
| 156 | size_t ret; |
|---|
| 157 | |
|---|
| 158 | ret = read(0, &buf, 1); |
|---|
| 159 | if(ret < 0) { |
|---|
| 160 | perror("read stdin"); |
|---|
| 161 | close(parport_fd); |
|---|
| 162 | return 1; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | if(ret == 0) { |
|---|
| 166 | DPRINTF("stdin EOF\n"); |
|---|
| 167 | break; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | printf("%5ims ", (int)(mtime(time_since_beginning)/1000)); |
|---|
| 172 | visualize_byte(buf); |
|---|
| 173 | printf(" 0x%02x=%03i ", buf, buf); |
|---|
| 174 | calculate_time(++already_punched, estimated_data_length, (int)(mtime(time_since_beginning)/1000)); |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | ret = write_puncher(parport_fd, buf); |
|---|
| 178 | if(ret < 0) { |
|---|
| 179 | perror("write puncher"); |
|---|
| 180 | close(parport_fd); |
|---|
| 181 | return 1; |
|---|
| 182 | } else if(ret == 0) { |
|---|
| 183 | printf("=)\n"); |
|---|
| 184 | } else if(ret == 1) { |
|---|
| 185 | printf(":-(\n"); |
|---|
| 186 | } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | DPRINTF("Transmission finished\n"); |
|---|
| 190 | ioctl(parport_fd, PPRELEASE); |
|---|
| 191 | close(parport_fd); |
|---|
| 192 | return 0; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | void visualize_byte(unsigned char buf) { |
|---|
| 196 | |
|---|
| 197 | unsigned char against = 0x01; |
|---|
| 198 | unsigned char check; |
|---|
| 199 | int pos; |
|---|
| 200 | |
|---|
| 201 | printf("|"); |
|---|
| 202 | for(pos=0; pos < 8; pos++) { |
|---|
| 203 | if(pos == 3) |
|---|
| 204 | printf("."); |
|---|
| 205 | check = buf; |
|---|
| 206 | check >>= pos; |
|---|
| 207 | |
|---|
| 208 | if((check & against) == 0) |
|---|
| 209 | printf(" "); |
|---|
| 210 | else |
|---|
| 211 | printf("*"); |
|---|
| 212 | } |
|---|
| 213 | printf("|"); |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | void calculate_time(int already_punched_bytes, int estimated_punch_length, int mtime_since_beginning) { |
|---|
| 217 | |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | int percentage, remaining_sec = 0; |
|---|
| 226 | |
|---|
| 227 | if(!estimated_punch_length) return; |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | percentage = (int)rint(((float)already_punched_bytes / (float)estimated_punch_length) * 100); |
|---|
| 232 | if(mtime_since_beginning != 0) |
|---|
| 233 | remaining_sec = (int)rint(((float)mtime_since_beginning / ((float)already_punched_bytes / (float)estimated_punch_length) - mtime_since_beginning)/1000); |
|---|
| 234 | |
|---|
| 235 | if(already_punched_bytes <= estimated_punch_length) { |
|---|
| 236 | printf("| %i%% (%i sec remaining) ", percentage, remaining_sec); |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | size_t write_puncher(int fd, unsigned char buf) { |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | static unsigned char strobe_on = (PARPORT_CONTROL_STROBE); |
|---|
| 249 | static unsigned char null_mask = 0x00; |
|---|
| 250 | unsigned char status; |
|---|
| 251 | long long time; |
|---|
| 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 | if(ioctl(fd, PPWDATA, &buf)) |
|---|
| 278 | return -2; |
|---|
| 279 | time = mtime(0); |
|---|
| 280 | |
|---|
| 281 | usleep(10); |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | if(ioctl(fd, PPWCONTROL, &null_mask)) |
|---|
| 285 | return -3; |
|---|
| 286 | DPRINTF("data set; strobe pulsed...\n"); |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | usleep(220); |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | if(ioctl(fd, PPRSTATUS, &status)) |
|---|
| 294 | return -4; |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | |
|---|
| 306 | |
|---|
| 307 | |
|---|
| 308 | DPRINTF("Control is %x. Ending data pins... ",status); |
|---|
| 309 | if(ioctl(fd, PPWDATA, &null_mask)) return -5; |
|---|
| 310 | DPRINTF("+ strobe... "); |
|---|
| 311 | if(ioctl(fd, PPWCONTROL, &strobe_on)) return -5; |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | { |
|---|
| 315 | int wait = 40*1000-mtime(time); |
|---|
| 316 | DPRINTF("\nWaiting for Puncher Ready (%i usec)...", wait); |
|---|
| 317 | if(wait>0) usleep(wait); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | if(ioctl(fd, PPRSTATUS, &status)) return -6; |
|---|
| 321 | if((status & PARPORT_STATUS_BUSY) == PARPORT_STATUS_BUSY) { |
|---|
| 322 | DPRINTF("Still busy (%x).\n", status); |
|---|
| 323 | return 1; |
|---|
| 324 | } else { |
|---|
| 325 | DPRINTF("Finished successfully (%x)\n", status); |
|---|
| 326 | return 0; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | } |
|---|