comparison projects/basic_sensor/main.cpp @ 5:09f03ac40fcc

API improvements and cleanups. Now all common audio command-line options can be parsed automatically.
author andrewm
date Sat, 08 Nov 2014 16:16:55 +0100
parents 8a575ba3ab52
children 901d205d1a3c
comparison
equal deleted inserted replaced
4:f34c63568523 5:09f03ac40fcc
7 7
8 #include <iostream> 8 #include <iostream>
9 #include <cstdlib> 9 #include <cstdlib>
10 #include <libgen.h> 10 #include <libgen.h>
11 #include <signal.h> 11 #include <signal.h>
12 #include <getopt.h>
12 #include "../../include/RTAudio.h" 13 #include "../../include/RTAudio.h"
13 14
14 using namespace std; 15 using namespace std;
15 16
16 int gSensorInputFrequency = 0; 17 int gSensorInputFrequency = 0;
23 } 24 }
24 25
25 // Print usage information 26 // Print usage information
26 void usage(const char * processName) 27 void usage(const char * processName)
27 { 28 {
28 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f input] [-a input]" << endl; 29 cerr << "Usage: " << processName << " [options]" << endl;
29 cerr << " -h: Print this menu\n"; 30
30 cerr << " -v: Enable verbose messages\n"; 31 BeagleRT_usage();
31 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; 32
32 cerr << " -f input: Choose the analog input controlling frequency (0-7; default 0)\n"; 33 cerr << " --frequency [-f] input: Choose the analog input controlling frequency (0-7; default 0)\n";
33 cerr << " -a input: Choose the analog input controlling amplitude (0-7; default 1)\n"; 34 cerr << " --amplitude [-a] input: Choose the analog input controlling amplitude (0-7; default 1)\n";
35 cerr << " --help [-h]: Print this menu\n";
34 } 36 }
35 37
36 int main(int argc, char *argv[]) 38 int main(int argc, char *argv[])
37 { 39 {
38 int periodSize = 8; // Period size in sensor frames 40 RTAudioSettings settings; // Standard audio settings
39 int verbose = 0; // Verbose printing level 41
42 struct option customOptions[] =
43 {
44 {"help", 0, NULL, 'h'},
45 {"frequency", 1, NULL, 'f'},
46 {"amplitude", 1, NULL, 'a'},
47 {NULL, 0, NULL, 0}
48 };
49
50 // Set default settings
51 BeagleRT_defaultSettings(&settings);
40 52
41 // Parse command-line arguments 53 // Parse command-line arguments
42 while (1) { 54 while (1) {
43 int c; 55 int c;
44 if ((c = getopt(argc, argv, "hp:vf:a:")) < 0) 56 if ((c = BeagleRT_getopt_long(argc, argv, "hf:a:", customOptions, &settings)) < 0)
45 break; 57 break;
46 switch (c) { 58 switch (c) {
47 case 'h': 59 case 'h':
48 usage(basename(argv[0])); 60 usage(basename(argv[0]));
49 exit(0); 61 exit(0);
50 case 'p':
51 periodSize = atoi(optarg);
52 if(periodSize < 1)
53 periodSize = 1;
54 break;
55 case 'v':
56 verbose = 1;
57 break;
58 case 'f': 62 case 'f':
59 gSensorInputFrequency = atoi(optarg); 63 gSensorInputFrequency = atoi(optarg);
60 if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) { 64 if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) {
61 usage(basename(argv[0])); 65 usage(basename(argv[0]));
62 exit(0); 66 exit(0);
74 usage(basename(argv[0])); 78 usage(basename(argv[0]));
75 exit(1); 79 exit(1);
76 } 80 }
77 } 81 }
78 82
83 // Initialise the PRU audio device
84 if(BeagleRT_initAudio(&settings, 0) != 0) {
85 cout << "Error: unable to initialise audio" << endl;
86 return -1;
87 }
79 88
80 // Set verbose logging information (optional by using value > 0; default is 0) 89 if(settings.verbose) {
81 setVerboseLevel(verbose);
82
83 if(verbose) {
84 cout << "Starting with period size " << periodSize << endl;
85 cout << "--> Frequency on input " << gSensorInputFrequency << endl; 90 cout << "--> Frequency on input " << gSensorInputFrequency << endl;
86 cout << "--> Amplitude on input " << gSensorInputAmplitude << endl; 91 cout << "--> Amplitude on input " << gSensorInputAmplitude << endl;
87 } 92 }
88 93
89 // Initialise the PRU audio device
90 if(initAudio(periodSize, 1, 0) != 0) {
91 cout << "Error: unable to initialise audio" << endl;
92 return -1;
93 }
94
95 // Start the audio device running 94 // Start the audio device running
96 if(startAudio()) { 95 if(BeagleRT_startAudio()) {
97 cout << "Error: unable to start real-time audio" << endl; 96 cout << "Error: unable to start real-time audio" << endl;
98 return -1; 97 return -1;
99 } 98 }
100 99
101 // Set up interrupt handler to catch Control-C 100 // Set up interrupt handler to catch Control-C
105 while(!gShouldStop) { 104 while(!gShouldStop) {
106 usleep(100000); 105 usleep(100000);
107 } 106 }
108 107
109 // Stop the audio device 108 // Stop the audio device
110 stopAudio(); 109 BeagleRT_stopAudio();
111
112 if(verbose) {
113 cout << "Cleaning up..." << endl;
114 }
115 110
116 // Clean up any resources allocated for audio 111 // Clean up any resources allocated for audio
117 cleanupAudio(); 112 BeagleRT_cleanupAudio();
118 113
119 // All done! 114 // All done!
120 return 0; 115 return 0;
121 } 116 }