-debug
[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=20;
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   setbuf(stdout,NULL);
24   lptgpib_init(0x378);
25   int init=1;
26   for (;;) {
27
28     // GPIB -> obrazovka
29     if (init) {
30       lptgpib_command(UNL);
31       lptgpib_command(MLA+0);
32       lptgpib_command(MTA+zdroj_adresa);
33       init=0;
34     }
35     put_control(TRI | NDAC);    /* clear NRFD */
36     usleep(1000000/10);
37     if (get_control()&DAV) {    /* wait for DAV */
38       char flags,val=lptgpib_read_byte(&flags);
39       if (!(flags&ATN))
40         putchar(val);
41     }
42     put_control(TRI | NDAC | NRFD );  /* back to default state */
43     usleep(1000000/10); /* chvili pockat, asi zbytecne, jen aby to bylo videt na portu */
44
45     // klavesnice -> GPIB
46     struct pollfd pollfd;
47     memset(&pollfd,0,sizeof pollfd);
48     pollfd.fd=0;
49     pollfd.events=POLLIN;
50     int i=poll(&pollfd,1,0);
51     assert(i==0||i==1);
52     if (i==1) {
53       char str[LINE_MAX],*s=fgets(str,sizeof(str),stdin);
54       assert(s==str);
55       s=strchr(str,'\n');
56       assert(s);
57       assert(s[1]==0);
58       *s=0;
59       lptgpib_write(zdroj_adresa,str);
60       init=1;
61     }
62
63   }
64 }