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