annotate projects/analogDigitalDemo/main.cpp @ 93:8c7f537d0a21

edited Scope.h with ifdefs to be compatible with udpio plugin
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 21 Jul 2015 21:18:45 +0100
parents 3c3a1357657d
children
rev   line source
giuliomoro@19 1 /*
giuliomoro@19 2 * assignment1_crossover
giuliomoro@19 3 * RTDSP 2015
giuliomoro@19 4 *
giuliomoro@19 5 * First assignment for ECS732 RTDSP, to implement a 2-way audio crossover
giuliomoro@19 6 * using the BeagleBone Black.
giuliomoro@19 7 *
giuliomoro@19 8 * Andrew McPherson and Victor Zappi
giuliomoro@19 9 * Queen Mary, University of London
giuliomoro@19 10 */
giuliomoro@19 11
giuliomoro@19 12 #include <iostream>
giuliomoro@19 13 #include <cstdlib>
giuliomoro@19 14 #include <libgen.h>
giuliomoro@19 15 #include <signal.h>
giuliomoro@19 16 #include <getopt.h>
giuliomoro@19 17 #include <unistd.h>
giuliomoro@19 18 #include <stdlib.h>
giuliomoro@19 19 #include <fcntl.h>
giuliomoro@19 20
andrewm@56 21 #include <BeagleRT.h>
andrewm@56 22
giuliomoro@19 23 using namespace std;
giuliomoro@19 24
giuliomoro@19 25 // Handle Ctrl-C by requesting that the audio rendering stop
giuliomoro@19 26 void interrupt_handler(int var)
giuliomoro@19 27 {
giuliomoro@19 28 gShouldStop = true;
giuliomoro@19 29 }
giuliomoro@19 30
giuliomoro@19 31 // Print usage information
giuliomoro@19 32 void usage(const char * processName)
giuliomoro@19 33 {
giuliomoro@19 34 cerr << "Usage: " << processName << " [options]" << endl;
giuliomoro@19 35
giuliomoro@19 36 BeagleRT_usage();
giuliomoro@19 37
giuliomoro@19 38 cerr << " --help [-h]: Print this menu\n";
giuliomoro@19 39 }
giuliomoro@19 40
giuliomoro@19 41 int main(int argc, char *argv[])
giuliomoro@19 42 {
andrewm@52 43 BeagleRTInitSettings settings; // Standard audio settings
giuliomoro@19 44 float frequency = 1000.0; // Frequency of crossover
giuliomoro@19 45
giuliomoro@19 46 struct option customOptions[] =
giuliomoro@19 47 {
giuliomoro@19 48 {"help", 0, NULL, 'h'},
giuliomoro@19 49 {"frequency", 1, NULL, 'f'},
giuliomoro@19 50 {NULL, 0, NULL, 0}
giuliomoro@19 51 };
giuliomoro@19 52
giuliomoro@19 53 // Set default settings
giuliomoro@19 54 BeagleRT_defaultSettings(&settings);
giuliomoro@19 55
giuliomoro@19 56 // Parse command-line arguments
giuliomoro@19 57 while (1) {
giuliomoro@19 58 int c;
giuliomoro@19 59 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
giuliomoro@19 60 break;
giuliomoro@19 61 switch (c) {
giuliomoro@19 62 case 'h':
giuliomoro@19 63 usage(basename(argv[0]));
giuliomoro@19 64 exit(0);
giuliomoro@19 65 case 'f':
giuliomoro@19 66 frequency = atof(optarg);
giuliomoro@19 67 if(frequency < 20.0)
giuliomoro@19 68 frequency = 20.0;
giuliomoro@19 69 if(frequency > 5000.0)
giuliomoro@19 70 frequency = 5000.0;
giuliomoro@19 71 break;
giuliomoro@19 72 case '?':
giuliomoro@19 73 default:
giuliomoro@19 74 usage(basename(argv[0]));
giuliomoro@19 75 exit(1);
giuliomoro@19 76 }
giuliomoro@19 77 }
giuliomoro@19 78
giuliomoro@19 79 // Initialise the PRU audio device
giuliomoro@19 80 if(BeagleRT_initAudio(&settings, &frequency) != 0) {
giuliomoro@19 81 cout << "Error: unable to initialise audio" << endl;
giuliomoro@19 82 return -1;
giuliomoro@19 83 }
giuliomoro@19 84
giuliomoro@19 85 // Start the audio device running
giuliomoro@19 86 if(BeagleRT_startAudio()) {
giuliomoro@19 87 cout << "Error: unable to start real-time audio" << endl;
giuliomoro@19 88 return -1;
giuliomoro@19 89 }
giuliomoro@19 90
giuliomoro@19 91 // Set up interrupt handler to catch Control-C
giuliomoro@19 92 signal(SIGINT, interrupt_handler);
giuliomoro@19 93 signal(SIGTERM, interrupt_handler);
giuliomoro@19 94
giuliomoro@19 95 // Run until told to stop
giuliomoro@19 96 while(!gShouldStop) {
giuliomoro@19 97 usleep(100000);
giuliomoro@19 98 }
giuliomoro@19 99
giuliomoro@19 100 // Stop the audio device
giuliomoro@19 101 BeagleRT_stopAudio();
giuliomoro@19 102
giuliomoro@19 103 // Clean up any resources allocated for audio
giuliomoro@19 104 BeagleRT_cleanupAudio();
giuliomoro@19 105
giuliomoro@19 106 // All done!
giuliomoro@19 107 return 0;
giuliomoro@19 108 }