annotate projects/basic_sensor/main.cpp @ 0:8a575ba3ab52

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