view sw/mcsi-rxtx/mainloop.c @ 16:f422d19c0118 default tip

fc-mcsi-rxtx: fix bug in PCM sample Rx
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 29 Oct 2024 01:41:33 +0000
parents 8b49ea8aeb99
children
line wrap: on
line source

/*
 * 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;

int is_active;

main_loop()
{
	fd_set fds;
	struct timeval tv;
	u_char buf[320];
	unsigned off;
	int cc;

	for (off = 0; ; fflush(stdout)) {
		FD_ZERO(&fds);
		FD_SET(0, &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;
			tty_cleanup();
			perror("select");
			exit(1);
		}
		if (cc == 0) {
			is_active = 0;
			sprintf(buf, "Rx stream stopped, buffer dribble = %u",
				off);
			async_msg_output(buf);
			off = 0;
			record_auto_stop();
			continue;
		}
		if (FD_ISSET(0, &fds))
			handle_tty_input();
		if (FD_ISSET(target_fd, &fds)) {
			cc = read(target_fd, buf + off, sizeof(buf) - off);
			if (cc < 0) {
				tty_cleanup();
				perror("serial port read");
				exit(1);
			}
			if (cc == 0) {
				tty_cleanup();
				fprintf(stderr, "read EOF from serial port\n");
				exit(1);
			}
			if (!is_active) {
				async_msg_output("Rx stream started");
				is_active = 1;
			}
			off += cc;
			if (off >= sizeof(buf)) {
				process_rx_block(buf);
				off = 0;
				transmit_20ms_block();
			}
		}
	}
}