File: ping-apollo.c

package info (click to toggle)
emacspeak-ss 1.12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 320 kB
  • ctags: 13
  • sloc: tcl: 533; sh: 243; makefile: 135; ansic: 75
file content (112 lines) | stat: -rw-r--r-- 2,604 bytes parent folder | download | duplicates (8)
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
/*
  ping-apollo - exchange data with Apollo speech synthesizer

*/

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <unistd.h>
#include <linux/serial.h>

#define DEFAULT_DEVICE "/dev/ttyS0"
#define MAXCHARS 128

static int readable (int fd, int usec);

/* write usage message and exit with status n.  Write message to
   stderr if n is nonzero, otherwise to stdout. */
void usage(int n)
{
  fprintf(n?stderr:stdout, 
	  "ping-apollo - exchange data with Apollo synthesizer\n"
	  "usage: ping-apollo  data-to-write  [device]\n"
	  "default device is /dev/ttyS0\n");
  exit(n);
}

/* return nonzero if file descriptor fd could be read without
   blocking. */
static int readable (int fd, int usec)
{  
  int retval;
  fd_set rfds, wfds, efds;
  struct timeval tv;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  FD_ZERO(&wfds);
  FD_ZERO(&efds);
  tv.tv_sec = usec/1000000;	/* wait the specified time for the files to
				   become readable */
  tv.tv_usec = usec%1000000;
  retval = select(fd+1, &rfds, &wfds, &efds, &tv);
  return retval;
}

int main(int argc, char **argv)
{
  unsigned long status=0;
  int fd;
  int numchars=0;
  char *devname=DEFAULT_DEVICE;
  char buf[MAXCHARS+1];

  if (argc < 2) usage(1);
  if (argc > 2) devname=argv[2];
  fd = open(devname,O_RDWR);
  if (fd == -1)
    {
      fprintf(stderr, "cannot open %s\n", devname);
      exit(1);
    }

  ioctl(fd, TIOCMGET, &status);
  status |= TIOCM_RTS;
  ioctl(fd, TIOCMSET, &status); /* assert RTS i.e. raise RTS line
				   (required before the Apollo will
				   read) */

  usleep(10000);		/* 100 usec is enough */
  write(fd, argv[1], strlen(argv[1]));
  {
    unsigned lsr;
    		
    /* wait until UART transmit buffer is empty */
    while(1)
      {
	ioctl(fd, TIOCSERGETLSR, &lsr); /* get line status register */
	if (lsr) break;
	usleep(10000);
      } 
  }

  usleep(10000);		/* 100 usec is enough */
  status &= ~TIOCM_RTS;
  ioctl(fd, TIOCMSET, &status); /* un-assert RTS i.e. raise RTS line
				   (required before the Apollo will
				   write) */
  /*
  fprintf(stderr, "status=%lx RTS=%d DTR=%d DSR=%d\n",
	 status,
	 (status&TIOCM_RTS)!=0,
	 (status&TIOCM_DTR)!=0,
	 (status&TIOCM_DSR)!=0);
  */
  /* 100000: stops after first punctuation
     70000: stops after first punctuation
     30000: stops after first punctuation
     10000: stops after first punctuation
  */
  while(readable(fd, 10000) && numchars < MAXCHARS)
    {
      if (read(fd, buf+numchars++, 1) < 0)
	{
	  perror("read-apollo");
	  exit(1);
	}
    }
  buf[numchars]=0;
  printf("%s", buf);
  return 0;
}