andrewm@13: /*
andrewm@13:  * main.cpp
andrewm@13:  *
andrewm@13:  *  Created on: Oct 24, 2014
andrewm@13:  *      Author: parallels
andrewm@13:  */
andrewm@13: 
andrewm@13: #include <iostream>
andrewm@13: #include <cstdlib>
andrewm@13: #include <libgen.h>
andrewm@13: #include <signal.h>
andrewm@13: #include <getopt.h>
andrewm@56: #include <BeagleRT.h>
andrewm@13: 
andrewm@13: using namespace std;
andrewm@13: 
andrewm@13: // Handle Ctrl-C by requesting that the audio rendering stop
andrewm@13: void interrupt_handler(int var)
andrewm@13: {
andrewm@13: 	gShouldStop = true;
andrewm@13: }
andrewm@13: 
andrewm@13: // Print usage information
andrewm@13: void usage(const char * processName)
andrewm@13: {
andrewm@13: 	cerr << "Usage: " << processName << " [options]" << endl;
andrewm@13: 
andrewm@13: 	BeagleRT_usage();
andrewm@13: 
andrewm@13: 	cerr << "   --help [-h]:             Print this menu\n";
andrewm@13: }
andrewm@13: 
andrewm@13: int main(int argc, char *argv[])
andrewm@13: {
andrewm@52: 	BeagleRTInitSettings settings;	// Standard audio settings
andrewm@13: 
andrewm@13: 	struct option customOptions[] =
andrewm@13: 	{
andrewm@13: 		{"help", 0, NULL, 'h'},
andrewm@13: 		{NULL, 0, NULL, 0}
andrewm@13: 	};
andrewm@13: 
andrewm@13: 	// Set default settings
andrewm@13: 	BeagleRT_defaultSettings(&settings);
andrewm@13: 
andrewm@13: 	// Parse command-line arguments
andrewm@13: 	while (1) {
andrewm@13: 		int c;
andrewm@13: 		if ((c = BeagleRT_getopt_long(argc, argv, "h", customOptions, &settings)) < 0)
andrewm@13: 				break;
andrewm@13: 		switch (c) {
andrewm@13: 		case 'h':
andrewm@13: 				usage(basename(argv[0]));
andrewm@13: 				exit(0);
andrewm@13: 		case '?':
andrewm@13: 		default:
andrewm@13: 				usage(basename(argv[0]));
andrewm@13: 				exit(1);
andrewm@13: 		}
andrewm@13: 	}
andrewm@13: 
andrewm@13: 	// Initialise the PRU audio device
andrewm@13: 	if(BeagleRT_initAudio(&settings, 0) != 0) {
andrewm@13: 		cout << "Error: unable to initialise audio" << endl;
andrewm@13: 		return -1;
andrewm@13: 	}
andrewm@13: 
andrewm@13: 	// Start the audio device running
andrewm@13: 	if(BeagleRT_startAudio()) {
andrewm@13: 		cout << "Error: unable to start real-time audio" << endl;
andrewm@13: 		return -1;
andrewm@13: 	}
andrewm@13: 
andrewm@15: 	// Set up interrupt handler to catch Control-C and SIGTERM
andrewm@13: 	signal(SIGINT, interrupt_handler);
andrewm@15: 	signal(SIGTERM, interrupt_handler);
andrewm@13: 
andrewm@13: 	// Run until told to stop
andrewm@13: 	while(!gShouldStop) {
andrewm@13: 		usleep(100000);
andrewm@13: 	}
andrewm@13: 
andrewm@13: 	// Stop the audio device
andrewm@13: 	BeagleRT_stopAudio();
andrewm@13: 
andrewm@13: 	// Clean up any resources allocated for audio
andrewm@13: 	BeagleRT_cleanupAudio();
andrewm@13: 
andrewm@13: 	// All done!
andrewm@13: 	return 0;
andrewm@13: }