#include "dsp.h" #include #include #include using namespace std; #include #include #include #include #include #include #include short used_recv_chans = 1; short used_xmit_chans = 2; short xmit_append = 1; char* xmit_prefix = "out"; char* recv_prefix = "in"; char* extension = ".dat"; static short xmit[6*BlockLen]; static short recv[4*BlockLen]; static ofstream fout[6]; static ifstream fin[2]; static struct termios term_saved; static void set_term(void) { struct termios term_new; static short has_set = 0; if (has_set) return; has_set = 1; tcgetattr(0,&term_saved); term_new = term_saved; /* Disable canonical mode, and set buffer size to 0 byte */ // term_new.c_lflag &= (~ICANON); // term_new.c_lflag &= (~ECHO); term_new.c_lflag &= ~(ECHO|ECHONL|ICANON); // term_new.c_cc[VTIME] = 0; // term_new.c_cc[VMIN] = 0; tcsetattr(0,TCSANOW,&term_new); return; } static void reset_term(void) { tcsetattr(0,TCSANOW,&term_saved); return; } static char getch(void) { set_term(); char ch = getchar(); return ch; } static void delay(int ms) { usleep(1000*ms); } static bool file_exists(const char* filename) { struct stat buf; int ret = stat(filename, &buf); if (ret < 0) return false; else return true; } static void open_xmit_channel(short chan) { if (fout[chan].is_open()) return; char tmp[1024]; snprintf(tmp, sizeof(tmp), "%s%d%s", xmit_prefix, chan, extension); // Ensure the file has been opened. fout[chan].open(tmp, xmit_append ? ios::app : ios::trunc); if (fout[chan].fail()) { cerr << "Couldn't write to " << tmp << endl; return; } // Clear errors from false opens. fout[chan].clear(); } static void open_recv_channel(short chan) { if (fin[chan].is_open()) return; char tmp[1024]; snprintf(tmp, sizeof(tmp), "%s%d%s", recv_prefix, chan, extension); cerr << "Waiting for data from " << tmp << "... "; // Ensure the file has been opened. while (!fin[chan].is_open()) { fin[chan].open(tmp); delay(10); } // Clear errors from false opens. fin[chan].clear(); fin[chan].seekg(0, ios::end); } static void write_channel(short chan) { for (short i = 0; i < BlockLen; i++) { fout[chan] << xmit[6*i + chan] << endl; } } static void read_channel(short chan) { string line; short num; short i; // Read in each number from the file, ignoring comments (#). for (i = 0; i < BlockLen; ) { getline(fin[chan], line); // Wait until we have all the data. if (fin[chan].fail()) { fin[chan].clear(); delay(10); continue; } if (line[0] == '#') continue; istringstream sin(line); while (sin >> num) { recv[4*i + 2*chan] = num; i++; if (i == BlockLen) break; } } //cerr << i << " samples received." << endl; } void WaitAudio(short** precv, short** pxmit) { // Slow down transmission a little. if (used_xmit_chans > 0) delay(10); for (short chan = 0; chan < used_xmit_chans; chan++) { open_xmit_channel(chan); write_channel(chan); } for (short chan = 0; chan < used_recv_chans; chan++) { open_recv_channel(chan); read_channel(chan); } *pxmit = xmit; *precv = recv; } void SerialTX(int ch) { cout << char(ch) << flush; } int SerialRX() { char ch = getch(); return (ch == EOF ? -1 : ch); }