annotate projects/tank_wars/main.cpp @ 15:901d205d1a3c

Updated to latest PRU library; external PRU file no longer needed. Also catch SIGTERM as well as SIGINT to clean up gracefully.
author andrewm
date Sat, 07 Feb 2015 16:41:56 +0000
parents 49f22e1246b2
children fbfeb5895efd
rev   line source
andrewm@10 1 /*
andrewm@10 2 * main.cpp
andrewm@10 3 *
andrewm@10 4 * Created on: Oct 24, 2014
andrewm@10 5 * Author: parallels
andrewm@10 6 */
andrewm@10 7
andrewm@10 8 #include <iostream>
andrewm@10 9 #include <cstdlib>
andrewm@10 10 #include <libgen.h>
andrewm@10 11 #include <signal.h>
andrewm@10 12 #include <getopt.h>
andrewm@10 13 #include "../../include/RTAudio.h"
andrewm@10 14
andrewm@10 15 extern int gScreenFramesPerSecond;
andrewm@10 16
andrewm@10 17 using namespace std;
andrewm@10 18
andrewm@10 19 // Handle Ctrl-C by requesting that the audio rendering stop
andrewm@10 20 void interrupt_handler(int var)
andrewm@10 21 {
andrewm@10 22 gShouldStop = true;
andrewm@10 23 }
andrewm@10 24
andrewm@10 25 // Print usage information
andrewm@10 26 void usage(const char * processName)
andrewm@10 27 {
andrewm@10 28 cerr << "Usage: " << processName << " [options]" << endl;
andrewm@10 29
andrewm@10 30 BeagleRT_usage();
andrewm@10 31
andrewm@10 32 cerr << " --fps [-f] value: Set target frames per second\n";
andrewm@10 33 cerr << " --help [-h]: Print this menu\n";
andrewm@10 34 }
andrewm@10 35
andrewm@10 36 int main(int argc, char *argv[])
andrewm@10 37 {
andrewm@10 38 RTAudioSettings settings; // Standard audio settings
andrewm@10 39
andrewm@10 40 struct option customOptions[] =
andrewm@10 41 {
andrewm@10 42 {"help", 0, NULL, 'h'},
andrewm@10 43 {"fps", 1, NULL, 'f'},
andrewm@10 44 {NULL, 0, NULL, 0}
andrewm@10 45 };
andrewm@10 46
andrewm@10 47 // Set default settings
andrewm@10 48 BeagleRT_defaultSettings(&settings);
andrewm@10 49
andrewm@10 50 // Parse command-line arguments
andrewm@10 51 while (1) {
andrewm@10 52 int c;
andrewm@10 53 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
andrewm@10 54 break;
andrewm@10 55 switch (c) {
andrewm@10 56 case 'f':
andrewm@10 57 gScreenFramesPerSecond = atoi(optarg);
andrewm@10 58 if(gScreenFramesPerSecond < 1)
andrewm@10 59 gScreenFramesPerSecond = 1;
andrewm@10 60 if(gScreenFramesPerSecond > 100)
andrewm@10 61 gScreenFramesPerSecond = 100;
andrewm@10 62 break;
andrewm@10 63 case 'h':
andrewm@10 64 usage(basename(argv[0]));
andrewm@10 65 exit(0);
andrewm@10 66 case '?':
andrewm@10 67 default:
andrewm@10 68 usage(basename(argv[0]));
andrewm@10 69 exit(1);
andrewm@10 70 }
andrewm@10 71 }
andrewm@10 72
andrewm@10 73 // Initialise the PRU audio device
andrewm@10 74 if(BeagleRT_initAudio(&settings, 0) != 0) {
andrewm@10 75 cout << "Error: unable to initialise audio" << endl;
andrewm@10 76 return -1;
andrewm@10 77 }
andrewm@10 78
andrewm@10 79 // Start the audio device running
andrewm@10 80 if(BeagleRT_startAudio()) {
andrewm@10 81 cout << "Error: unable to start real-time audio" << endl;
andrewm@10 82 return -1;
andrewm@10 83 }
andrewm@10 84
andrewm@15 85 // Set up interrupt handler to catch Control-C and SIGTERM
andrewm@10 86 signal(SIGINT, interrupt_handler);
andrewm@15 87 signal(SIGTERM, interrupt_handler);
andrewm@10 88
andrewm@10 89 // Run until told to stop
andrewm@10 90 while(!gShouldStop) {
andrewm@10 91 usleep(100000);
andrewm@10 92 }
andrewm@10 93
andrewm@10 94 // Stop the audio device
andrewm@10 95 BeagleRT_stopAudio();
andrewm@10 96
andrewm@10 97 // Clean up any resources allocated for audio
andrewm@10 98 BeagleRT_cleanupAudio();
andrewm@10 99
andrewm@10 100 // All done!
andrewm@10 101 return 0;
andrewm@10 102 }