Mercurial > hg > beaglert
comparison projects/basic_analog_output/main.cpp @ 0:8a575ba3ab52
Initial commit.
author | andrewm |
---|---|
date | Fri, 31 Oct 2014 19:10:17 +0100 |
parents | |
children | 09f03ac40fcc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a575ba3ab52 |
---|---|
1 /* | |
2 * main.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: parallels | |
6 */ | |
7 | |
8 #include <iostream> | |
9 #include <cstdlib> | |
10 #include <libgen.h> | |
11 #include <signal.h> | |
12 #include "../../include/RTAudio.h" | |
13 | |
14 using namespace std; | |
15 | |
16 // Handle Ctrl-C by requesting that the audio rendering stop | |
17 void interrupt_handler(int var) | |
18 { | |
19 gShouldStop = true; | |
20 } | |
21 | |
22 // Print usage information | |
23 void usage(const char * processName) | |
24 { | |
25 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f input] [-a input]" << endl; | |
26 cerr << " -h: Print this menu\n"; | |
27 cerr << " -v: Enable verbose messages\n"; | |
28 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; | |
29 cerr << " -f frequency: Set frequency of LED fade (default: 1.0)\n"; | |
30 } | |
31 | |
32 int main(int argc, char *argv[]) | |
33 { | |
34 int periodSize = 8; // Period size in sensor frames | |
35 int verbose = 0; // Verbose printing level | |
36 float frequency = 1.0; // Frequency of LED fades | |
37 | |
38 // Parse command-line arguments | |
39 while (1) { | |
40 int c; | |
41 if ((c = getopt(argc, argv, "hp:vf:")) < 0) | |
42 break; | |
43 switch (c) { | |
44 case 'h': | |
45 usage(basename(argv[0])); | |
46 exit(0); | |
47 case 'p': | |
48 periodSize = atoi(optarg); | |
49 if(periodSize < 1) | |
50 periodSize = 1; | |
51 break; | |
52 case 'v': | |
53 verbose = 1; | |
54 break; | |
55 case 'f': | |
56 frequency = atof(optarg); | |
57 if(frequency < 0) | |
58 frequency = 0; | |
59 if(frequency > 11025.0) | |
60 frequency = 11025.0; | |
61 break; | |
62 case '?': | |
63 default: | |
64 usage(basename(argv[0])); | |
65 exit(1); | |
66 } | |
67 } | |
68 | |
69 | |
70 // Set verbose logging information (optional by using value > 0; default is 0) | |
71 setVerboseLevel(verbose); | |
72 | |
73 if(verbose) { | |
74 cout << "Starting with period size " << periodSize << " and frequency " << frequency << endl; | |
75 } | |
76 | |
77 // Initialise the PRU audio device | |
78 if(initAudio(periodSize, 1, &frequency) != 0) { | |
79 cout << "Error: unable to initialise audio" << endl; | |
80 return -1; | |
81 } | |
82 | |
83 // Start the audio device running | |
84 if(startAudio()) { | |
85 cout << "Error: unable to start real-time audio" << endl; | |
86 return -1; | |
87 } | |
88 | |
89 // Set up interrupt handler to catch Control-C | |
90 signal(SIGINT, interrupt_handler); | |
91 | |
92 // Run until told to stop | |
93 while(!gShouldStop) { | |
94 usleep(100000); | |
95 } | |
96 | |
97 // Stop the audio device | |
98 stopAudio(); | |
99 | |
100 if(verbose) { | |
101 cout << "Cleaning up..." << endl; | |
102 } | |
103 | |
104 // Clean up any resources allocated for audio | |
105 cleanupAudio(); | |
106 | |
107 // All done! | |
108 return 0; | |
109 } |