5de10a8f0fc80e6ac092b2583fe9d3d241493753
[lptgpib.git] / terminal.c
1 #include "lptgpib.h"
2 #include <stdio.h>
3 #include <string.h>
4 #include <poll.h>
5 #include <unistd.h>
6 #include <assert.h>
7 #include <sys/io.h>
8
9 // lptgpib.c
10 #define get_control() (inb(lpt_base+2) ^ 0x04)
11 #define put_control(x) outb((x) ^ 0x04, lpt_base+2)
12 #define TRI 0x20 /* tristate data lines */
13 #define NDAC 0x04 /* LPT pin 16 */
14 #define DAV  0x01 /* LPT pin 1  */
15
16 extern int lpt_base;
17
18 int main(int argc,char **argv) {
19   lptgpib_init(0x378);
20   for (;;) {
21     put_control(TRI | NDAC);    /* clear NRFD */
22     if (get_control()&DAV) {    /* wait for DAV */
23       fprintf(stderr,"ctu znak...\n");
24       char flags,val=lptgpib_read_byte(&flags);
25       if (flags&ATN)
26         lptgpib_print_command(val);
27       else
28         fprintf(stderr,"precten znak: %02x (%c)\n",val,val);
29     }
30     struct pollfd pollfd;
31     memset(&pollfd,0,sizeof pollfd);
32     pollfd.fd=0;
33     pollfd.events=POLLIN;
34     int i=poll(&pollfd,1,0);
35     assert(i==0||i==1);
36     if (i==1) {
37       char val;
38       i=read(0,&val,1);
39       assert(i==1);
40       fprintf(stderr,"zapisuji znak... %02x (%c)\n",val,val);
41       lptgpib_write_byte(val,0);
42       fprintf(stderr,"zapsan znak.\n");
43     }
44   }
45 }