Implementace rezimu kontroler.
[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 #include <limits.h>
9
10 static const int zdroj_adresa=1;
11
12 // lptgpib.c
13 #define get_control() (inb(lpt_base+2) ^ 0x04)
14 #define put_control(x) outb((x) ^ 0x04, lpt_base+2)
15 #define TRI 0x20 /* tristate data lines */
16 #define NDAC 0x04 /* LPT pin 16 */
17 #define DAV  0x01 /* LPT pin 1  */
18 #define NRFD 0x02 /* LPT pin 14 */
19
20 extern int lpt_base;
21
22 int main(int argc,char **argv) {
23   lptgpib_init(0x378);
24   int init=1;
25   for (;;) {
26
27     // GPIB -> obrazovka
28     if (init)
29       fprintf(stderr,"Inicializuji port... ");
30     lptgpib_command(UNL);
31     lptgpib_command(MLA+0);
32     lptgpib_command(MTA+zdroj_adresa);
33     if (init)
34       fprintf(stderr,"hotovo.\n");
35     init=0;
36     put_control(TRI | NDAC);    /* clear NRFD */
37     usleep(1000000/10);
38     if (get_control()&DAV) {    /* wait for DAV */
39       fprintf(stderr,"ctu znak...\n");
40       char flags,val=lptgpib_read_byte(&flags);
41       if (flags&ATN)
42         lptgpib_print_command(val);
43       else
44         fprintf(stderr,"precten znak: %02x (%c)\n",val,val);
45     }
46     put_control(TRI | NDAC | NRFD );  /* back to default state */
47
48     // klavesnice -> GPIB
49     struct pollfd pollfd;
50     memset(&pollfd,0,sizeof pollfd);
51     pollfd.fd=0;
52     pollfd.events=POLLIN;
53     int i=poll(&pollfd,1,0);
54     assert(i==0||i==1);
55     if (i==1) {
56       char str[LINE_MAX],*s=fgets(str,sizeof(str),stdin);
57       assert(s==str);
58       s=strchr(str,'\n');
59       assert(s);
60       assert(s[1]==0);
61       *s=0;
62       fprintf(stderr,"zapisuji radek: %s\n",str);
63       lptgpib_write(zdroj_adresa,str);
64       fprintf(stderr,"zapsan radek.\n");
65     }
66
67   }
68 }