annotate sw/mcsi-rxtx/mainloop.c @ 13:315428573a25

fc-mcsi-rxtx: implement record function
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 28 Oct 2024 07:24:53 +0000
parents e93a11f44e6f
children 8b49ea8aeb99
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
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 is_active = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 off = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 for (;;) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 FD_ZERO(&fds);
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
29 FD_SET(0, &fds);
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 FD_SET(target_fd, &fds);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (is_active) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 tv.tv_sec = 0;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 tv.tv_usec = 100000;
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, &tv);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 } else
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 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
37 if (cc < 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 if (errno == EINTR)
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 continue;
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
40 tty_cleanup();
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 perror("select");
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 exit(1);
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 if (cc == 0) {
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 is_active = 0;
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
46 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
47 off);
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
48 async_msg_output(buf);
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 off = 0;
13
315428573a25 fc-mcsi-rxtx: implement record function
Mychaela Falconia <falcon@freecalypso.org>
parents: 11
diff changeset
50 record_auto_stop();
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 continue;
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
53 if (FD_ISSET(0, &fds))
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
54 handle_tty_input();
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
55 if (FD_ISSET(target_fd, &fds)) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
56 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
57 if (cc < 0) {
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
58 tty_cleanup();
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
59 perror("serial port read");
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
60 exit(1);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
61 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
62 if (cc == 0) {
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
63 tty_cleanup();
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
64 fprintf(stderr, "read EOF from serial port\n");
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
65 exit(1);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
66 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
67 if (!is_active) {
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
68 async_msg_output("Rx stream started");
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
69 is_active = 1;
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
70 }
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
71 off += cc;
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
72 if (off >= sizeof(buf)) {
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
73 process_rx_block(buf);
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
74 off = 0;
11
e93a11f44e6f fc-mcsi-rxtx: implement basic Tx
Mychaela Falconia <falcon@freecalypso.org>
parents: 9
diff changeset
75 transmit_20ms_block();
7
8a386263dd51 fc-mcsi-rxtx skeleton put together
Mychaela Falconia <falcon@freecalypso.org>
parents: 3
diff changeset
76 }
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 }
9
62579cfff4fc fc-mcsi-rxtx: more proper integration with ttyhacks
Mychaela Falconia <falcon@freecalypso.org>
parents: 7
diff changeset
78 fflush(stdout);
3
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
de85c3680d7e sw: fc-mcsi-rx program put together
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }