annotate sw/mcsi-rxtx/mainloop.c @ 15:8b49ea8aeb99

fc-mcsi-rxtx: more reliable fflush(stdout) in main loop
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 28 Oct 2024 23:43:10 +0000
parents 315428573a25
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module holds our main loop code, factored out into a separate
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * function that is called from main() after initialization.
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/time.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/errno.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <unistd.h>
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 extern int target_fd;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
15 int is_active;
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
16
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 main_loop()
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 fd_set fds;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 struct timeval tv;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 u_char buf[320];
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 unsigned off;
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
23 int cc;
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
15
8b49ea8aeb99 fc-mcsi-rxtx: more reliable fflush(stdout) in main loop
Mychaela Falconia <falcon@freecalypso.org>
parents: 13
diff changeset
25 for (off = 0; ; fflush(stdout)) {
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 FD_ZERO(&fds);
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
27 FD_SET(0, &fds);
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 FD_SET(target_fd, &fds);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (is_active) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 tv.tv_sec = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 tv.tv_usec = 100000;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 cc = select(target_fd+1, &fds, 0, 0, &tv);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 } else
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 cc = select(target_fd+1, &fds, 0, 0, 0);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (cc < 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (errno == EINTR)
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 continue;
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
38 tty_cleanup();
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 perror("select");
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 exit(1);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (cc == 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 is_active = 0;
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
44 sprintf(buf, "Rx stream stopped, buffer dribble = %u",
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
45 off);
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
46 async_msg_output(buf);
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 off = 0;
13
315428573a25 fc-mcsi-rxtx: implement record function
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
48 record_auto_stop();
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 continue;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
51 if (FD_ISSET(0, &fds))
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
52 handle_tty_input();
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
53 if (FD_ISSET(target_fd, &fds)) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
54 cc = read(target_fd, buf + off, sizeof(buf) - off);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
55 if (cc < 0) {
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
56 tty_cleanup();
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
57 perror("serial port read");
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
58 exit(1);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
59 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
60 if (cc == 0) {
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
61 tty_cleanup();
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
62 fprintf(stderr, "read EOF from serial port\n");
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
63 exit(1);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
64 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
65 if (!is_active) {
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
66 async_msg_output("Rx stream started");
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
67 is_active = 1;
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
68 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
69 off += cc;
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
70 if (off >= sizeof(buf)) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
71 process_rx_block(buf);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
72 off = 0;
11
e93a11f44e6f fc-mcsi-rxtx: implement basic Tx
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
73 transmit_20ms_block();
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
74 }
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }