comparison projects/basic_sensor/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 int gSensorInputFrequency = 0;
17 int gSensorInputAmplitude = 1;
18
19 // Handle Ctrl-C by requesting that the audio rendering stop
20 void interrupt_handler(int var)
21 {
22 gShouldStop = true;
23 }
24
25 // Print usage information
26 void usage(const char * processName)
27 {
28 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f input] [-a input]" << endl;
29 cerr << " -h: Print this menu\n";
30 cerr << " -v: Enable verbose messages\n";
31 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n";
32 cerr << " -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 }
35
36 int main(int argc, char *argv[])
37 {
38 int periodSize = 8; // Period size in sensor frames
39 int verbose = 0; // Verbose printing level
40
41 // Parse command-line arguments
42 while (1) {
43 int c;
44 if ((c = getopt(argc, argv, "hp:vf:a:")) < 0)
45 break;
46 switch (c) {
47 case 'h':
48 usage(basename(argv[0]));
49 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':
59 gSensorInputFrequency = atoi(optarg);
60 if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) {
61 usage(basename(argv[0]));
62 exit(0);
63 }
64 break;
65 case 'a':
66 gSensorInputAmplitude = atoi(optarg);
67 if(gSensorInputAmplitude < 0 || gSensorInputAmplitude > 7) {
68 usage(basename(argv[0]));
69 exit(0);
70 }
71 break;
72 case '?':
73 default:
74 usage(basename(argv[0]));
75 exit(1);
76 }
77 }
78
79
80 // Set verbose logging information (optional by using value > 0; default is 0)
81 setVerboseLevel(verbose);
82
83 if(verbose) {
84 cout << "Starting with period size " << periodSize << endl;
85 cout << "--> Frequency on input " << gSensorInputFrequency << endl;
86 cout << "--> Amplitude on input " << gSensorInputAmplitude << endl;
87 }
88
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
96 if(startAudio()) {
97 cout << "Error: unable to start real-time audio" << endl;
98 return -1;
99 }
100
101 // Set up interrupt handler to catch Control-C
102 signal(SIGINT, interrupt_handler);
103
104 // Run until told to stop
105 while(!gShouldStop) {
106 usleep(100000);
107 }
108
109 // Stop the audio device
110 stopAudio();
111
112 if(verbose) {
113 cout << "Cleaning up..." << endl;
114 }
115
116 // Clean up any resources allocated for audio
117 cleanupAudio();
118
119 // All done!
120 return 0;
121 }