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