comparison projects/basic_analog_input/main.cpp @ 57:72726dd4f66c newapi

Renamed basic_sensor project to basic_analog_input for clarity
author andrewm
date Wed, 15 Jul 2015 12:12:43 +0100
parents
children
comparison
equal deleted inserted replaced
56:3c3a1357657d 57:72726dd4f66c
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 <getopt.h>
13 #include <BeagleRT.h>
14
15 using namespace std;
16
17 int gSensorInputFrequency = 0;
18 int gSensorInputAmplitude = 1;
19
20 // Handle Ctrl-C by requesting that the audio rendering stop
21 void interrupt_handler(int var)
22 {
23 gShouldStop = true;
24 }
25
26 // Print usage information
27 void usage(const char * processName)
28 {
29 cerr << "Usage: " << processName << " [options]" << endl;
30
31 BeagleRT_usage();
32
33 cerr << " --frequency [-f] input: Choose the analog input controlling frequency (0-7; default 0)\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";
36 }
37
38 int main(int argc, char *argv[])
39 {
40 BeagleRTInitSettings settings; // Standard audio settings
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);
52
53 // Parse command-line arguments
54 while (1) {
55 int c;
56 if ((c = BeagleRT_getopt_long(argc, argv, "hf:a:", customOptions, &settings)) < 0)
57 break;
58 switch (c) {
59 case 'h':
60 usage(basename(argv[0]));
61 exit(0);
62 case 'f':
63 gSensorInputFrequency = atoi(optarg);
64 if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) {
65 usage(basename(argv[0]));
66 exit(0);
67 }
68 break;
69 case 'a':
70 gSensorInputAmplitude = atoi(optarg);
71 if(gSensorInputAmplitude < 0 || gSensorInputAmplitude > 7) {
72 usage(basename(argv[0]));
73 exit(0);
74 }
75 break;
76 case '?':
77 default:
78 usage(basename(argv[0]));
79 exit(1);
80 }
81 }
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 }
88
89 if(settings.verbose) {
90 cout << "--> Frequency on input " << gSensorInputFrequency << endl;
91 cout << "--> Amplitude on input " << gSensorInputAmplitude << endl;
92 }
93
94 // Start the audio device running
95 if(BeagleRT_startAudio()) {
96 cout << "Error: unable to start real-time audio" << endl;
97 return -1;
98 }
99
100 // Set up interrupt handler to catch Control-C and SIGTERM
101 signal(SIGINT, interrupt_handler);
102 signal(SIGTERM, interrupt_handler);
103
104 // Run until told to stop
105 while(!gShouldStop) {
106 usleep(100000);
107 }
108
109 // Stop the audio device
110 BeagleRT_stopAudio();
111
112 // Clean up any resources allocated for audio
113 BeagleRT_cleanupAudio();
114
115 // All done!
116 return 0;
117 }