Mercurial > hg > beaglert
comparison examples/basic_analog_output/main.cpp @ 300:dbeed520b014 prerelease
Renamed projects to examples
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 13:58:20 +0100 |
parents | projects/basic_analog_output/main.cpp@3c3a1357657d |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
297:a3d83ebdf49b | 300:dbeed520b014 |
---|---|
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 // Handle Ctrl-C by requesting that the audio rendering stop | |
18 void interrupt_handler(int var) | |
19 { | |
20 gShouldStop = true; | |
21 } | |
22 | |
23 // Print usage information | |
24 void usage(const char * processName) | |
25 { | |
26 cerr << "Usage: " << processName << " [options]" << endl; | |
27 | |
28 BeagleRT_usage(); | |
29 | |
30 cerr << " --frequency [-f] frequency: Set the frequency of the LED fade (default: 1.0)\n"; | |
31 cerr << " --help [-h]: Print this menu\n"; | |
32 } | |
33 | |
34 int main(int argc, char *argv[]) | |
35 { | |
36 BeagleRTInitSettings settings; // Standard audio settings | |
37 float frequency = 1.0; // Frequency of LED fades | |
38 | |
39 struct option customOptions[] = | |
40 { | |
41 {"help", 0, NULL, 'h'}, | |
42 {"frequency", 1, NULL, 'f'}, | |
43 {NULL, 0, NULL, 0} | |
44 }; | |
45 | |
46 // Set default settings | |
47 BeagleRT_defaultSettings(&settings); | |
48 | |
49 // In this example, audio isn't used so might as well leave speaker muted | |
50 settings.beginMuted = 1; | |
51 | |
52 // Parse command-line arguments | |
53 while (1) { | |
54 int c; | |
55 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0) | |
56 break; | |
57 switch (c) { | |
58 case 'h': | |
59 usage(basename(argv[0])); | |
60 exit(0); | |
61 case 'f': | |
62 frequency = atof(optarg); | |
63 if(frequency < 0) | |
64 frequency = 0; | |
65 if(frequency > 11025.0) | |
66 frequency = 11025.0; | |
67 break; | |
68 case '?': | |
69 default: | |
70 usage(basename(argv[0])); | |
71 exit(1); | |
72 } | |
73 } | |
74 | |
75 // Initialise the PRU audio device | |
76 if(BeagleRT_initAudio(&settings, &frequency) != 0) { | |
77 cout << "Error: unable to initialise audio" << endl; | |
78 return -1; | |
79 } | |
80 | |
81 // Start the audio device running | |
82 if(BeagleRT_startAudio()) { | |
83 cout << "Error: unable to start real-time audio" << endl; | |
84 return -1; | |
85 } | |
86 | |
87 // Set up interrupt handler to catch Control-C and SIGTERM | |
88 signal(SIGINT, interrupt_handler); | |
89 signal(SIGTERM, interrupt_handler); | |
90 | |
91 // Run until told to stop | |
92 while(!gShouldStop) { | |
93 usleep(100000); | |
94 } | |
95 | |
96 // Stop the audio device | |
97 BeagleRT_stopAudio(); | |
98 | |
99 // Clean up any resources allocated for audio | |
100 BeagleRT_cleanupAudio(); | |
101 | |
102 // All done! | |
103 return 0; | |
104 } |