annotate projects/analogDigitalDemo/main.cpp @ 19:c98863e63174 matrix_gpio

Renamed matrixGpio to digital and matrix to analog
author Giulio Moro <giuliomoro@yahoo.it>
date Thu, 30 Apr 2015 16:58:41 +0100
parents
children fbfeb5895efd
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 "../include/RTAudio.h"
giuliomoro@19 18 #include <unistd.h>
giuliomoro@19 19 #include <stdlib.h>
giuliomoro@19 20 #include <fcntl.h>
giuliomoro@19 21
giuliomoro@19 22 using namespace std;
giuliomoro@19 23
giuliomoro@19 24 // Handle Ctrl-C by requesting that the audio rendering stop
giuliomoro@19 25 void interrupt_handler(int var)
giuliomoro@19 26 {
giuliomoro@19 27 gShouldStop = true;
giuliomoro@19 28 }
giuliomoro@19 29
giuliomoro@19 30 // Print usage information
giuliomoro@19 31 void usage(const char * processName)
giuliomoro@19 32 {
giuliomoro@19 33 cerr << "Usage: " << processName << " [options]" << endl;
giuliomoro@19 34
giuliomoro@19 35 BeagleRT_usage();
giuliomoro@19 36
giuliomoro@19 37 cerr << " --help [-h]: Print this menu\n";
giuliomoro@19 38 }
giuliomoro@19 39
giuliomoro@19 40 int main(int argc, char *argv[])
giuliomoro@19 41 {
giuliomoro@19 42 RTAudioSettings settings; // Standard audio settings
giuliomoro@19 43 float frequency = 1000.0; // Frequency of crossover
giuliomoro@19 44
giuliomoro@19 45 struct option customOptions[] =
giuliomoro@19 46 {
giuliomoro@19 47 {"help", 0, NULL, 'h'},
giuliomoro@19 48 {"frequency", 1, NULL, 'f'},
giuliomoro@19 49 {NULL, 0, NULL, 0}
giuliomoro@19 50 };
giuliomoro@19 51
giuliomoro@19 52 // Set default settings
giuliomoro@19 53 BeagleRT_defaultSettings(&settings);
giuliomoro@19 54
giuliomoro@19 55 // Parse command-line arguments
giuliomoro@19 56 while (1) {
giuliomoro@19 57 int c;
giuliomoro@19 58 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
giuliomoro@19 59 break;
giuliomoro@19 60 switch (c) {
giuliomoro@19 61 case 'h':
giuliomoro@19 62 usage(basename(argv[0]));
giuliomoro@19 63 exit(0);
giuliomoro@19 64 case 'f':
giuliomoro@19 65 frequency = atof(optarg);
giuliomoro@19 66 if(frequency < 20.0)
giuliomoro@19 67 frequency = 20.0;
giuliomoro@19 68 if(frequency > 5000.0)
giuliomoro@19 69 frequency = 5000.0;
giuliomoro@19 70 break;
giuliomoro@19 71 case '?':
giuliomoro@19 72 default:
giuliomoro@19 73 usage(basename(argv[0]));
giuliomoro@19 74 exit(1);
giuliomoro@19 75 }
giuliomoro@19 76 }
giuliomoro@19 77
giuliomoro@19 78 // Initialise the PRU audio device
giuliomoro@19 79 if(BeagleRT_initAudio(&settings, &frequency) != 0) {
giuliomoro@19 80 cout << "Error: unable to initialise audio" << endl;
giuliomoro@19 81 return -1;
giuliomoro@19 82 }
giuliomoro@19 83
giuliomoro@19 84 // Start the audio device running
giuliomoro@19 85 if(BeagleRT_startAudio()) {
giuliomoro@19 86 cout << "Error: unable to start real-time audio" << endl;
giuliomoro@19 87 return -1;
giuliomoro@19 88 }
giuliomoro@19 89
giuliomoro@19 90 // Set up interrupt handler to catch Control-C
giuliomoro@19 91 signal(SIGINT, interrupt_handler);
giuliomoro@19 92 signal(SIGTERM, interrupt_handler);
giuliomoro@19 93
giuliomoro@19 94 // Run until told to stop
giuliomoro@19 95 while(!gShouldStop) {
giuliomoro@19 96 usleep(100000);
giuliomoro@19 97 }
giuliomoro@19 98
giuliomoro@19 99 // Stop the audio device
giuliomoro@19 100 BeagleRT_stopAudio();
giuliomoro@19 101
giuliomoro@19 102 // Clean up any resources allocated for audio
giuliomoro@19 103 BeagleRT_cleanupAudio();
giuliomoro@19 104
giuliomoro@19 105 // All done!
giuliomoro@19 106 return 0;
giuliomoro@19 107 }