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