FreeCalypso > hg > fc-pcm-if
diff sw/mcsi-rx/mainloop.c @ 3:de85c3680d7e
sw: fc-mcsi-rx program put together
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 11 Oct 2024 23:54:39 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sw/mcsi-rx/mainloop.c Fri Oct 11 23:54:39 2024 +0000 @@ -0,0 +1,67 @@ +/* + * This module holds our main loop code, factored out into a separate + * function that is called from main() after initialization. + */ + +#include <sys/types.h> +#include <sys/time.h> +#include <sys/errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + +extern int target_fd; +extern FILE *out_binfile; + +main_loop() +{ + fd_set fds; + struct timeval tv; + u_char buf[320]; + unsigned off; + int cc, is_active; + + is_active = 0; + off = 0; + for (;;) { + FD_ZERO(&fds); + FD_SET(target_fd, &fds); + if (is_active) { + tv.tv_sec = 0; + tv.tv_usec = 100000; + cc = select(target_fd+1, &fds, 0, 0, &tv); + } else + cc = select(target_fd+1, &fds, 0, 0, 0); + if (cc < 0) { + if (errno == EINTR) + continue; + perror("select"); + exit(1); + } + if (cc == 0) { + is_active = 0; + fflush(out_binfile); + printf("Rx stream stopped, buffer dribble = %u\n", off); + off = 0; + continue; + } + cc = read(target_fd, buf + off, sizeof(buf) - off); + if (cc < 0) { + perror("serial port read"); + exit(1); + } + if (cc == 0) { + fprintf(stderr, "read EOF from serial port\n"); + exit(1); + } + if (!is_active) { + printf("Rx stream started\n"); + is_active = 1; + } + off += cc; + if (off >= sizeof(buf)) { + process_rx_block(buf); + off = 0; + } + } +}