annotate projects/loggingSensors/main.cpp @ 224:97faaf985164 mergingClockSync

Added callback for Midi channel messages
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 08 Mar 2016 15:49:42 +0000
parents f36313cbb55d
children
rev   line source
giuliomoro@157 1 /*
giuliomoro@157 2 * main.cpp
giuliomoro@157 3 *
giuliomoro@157 4 * Created on: Oct 24, 2014
giuliomoro@157 5 * Author: parallels
giuliomoro@157 6 */
giuliomoro@157 7 #include <unistd.h>
giuliomoro@157 8 #include <iostream>
giuliomoro@157 9 #include <cstdlib>
giuliomoro@157 10 #include <libgen.h>
giuliomoro@157 11 #include <signal.h>
giuliomoro@157 12 #include <getopt.h>
giuliomoro@157 13 #include <BeagleRT.h>
giuliomoro@157 14
giuliomoro@157 15 using namespace std;
giuliomoro@157 16
giuliomoro@157 17 // Handle Ctrl-C by requesting that the audio rendering stop
giuliomoro@157 18 void interrupt_handler(int var)
giuliomoro@157 19 {
giuliomoro@157 20 gShouldStop = true;
giuliomoro@157 21 }
giuliomoro@157 22
giuliomoro@157 23 // Print usage information
giuliomoro@157 24 void usage(const char * processName)
giuliomoro@157 25 {
giuliomoro@157 26 cerr << "Usage: " << processName << " [options]" << endl;
giuliomoro@157 27
giuliomoro@157 28 BeagleRT_usage();
giuliomoro@157 29
giuliomoro@157 30 cerr << " --frequency [-f] frequency: Set the frequency of the oscillator\n";
giuliomoro@157 31 cerr << " --help [-h]: Print this menu\n";
giuliomoro@157 32 }
giuliomoro@157 33
giuliomoro@157 34 int main(int argc, char *argv[])
giuliomoro@157 35 {
giuliomoro@157 36 BeagleRTInitSettings settings; // Standard audio settings
giuliomoro@157 37 float frequency = 440.0; // Frequency of oscillator
giuliomoro@157 38
giuliomoro@157 39 struct option customOptions[] =
giuliomoro@157 40 {
giuliomoro@157 41 {"help", 0, NULL, 'h'},
giuliomoro@157 42 {"frequency", 1, NULL, 'f'},
giuliomoro@157 43 {NULL, 0, NULL, 0}
giuliomoro@157 44 };
giuliomoro@157 45
giuliomoro@157 46 // Set default settings
giuliomoro@157 47 BeagleRT_defaultSettings(&settings);
giuliomoro@157 48
giuliomoro@157 49 // Parse command-line arguments
giuliomoro@157 50 while (1) {
giuliomoro@157 51 int c;
giuliomoro@157 52 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
giuliomoro@157 53 break;
giuliomoro@157 54 switch (c) {
giuliomoro@157 55 case 'h':
giuliomoro@157 56 usage(basename(argv[0]));
giuliomoro@157 57 exit(0);
giuliomoro@157 58 case 'f':
giuliomoro@157 59 frequency = atof(optarg);
giuliomoro@157 60 break;
giuliomoro@157 61 case '?':
giuliomoro@157 62 default:
giuliomoro@157 63 usage(basename(argv[0]));
giuliomoro@157 64 exit(1);
giuliomoro@157 65 }
giuliomoro@157 66 }
giuliomoro@157 67
giuliomoro@157 68 // Initialise the PRU audio device
giuliomoro@157 69 if(BeagleRT_initAudio(&settings, &frequency) != 0) {
giuliomoro@157 70 cout << "Error: unable to initialise audio" << endl;
giuliomoro@157 71 return -1;
giuliomoro@157 72 }
giuliomoro@157 73
giuliomoro@157 74 // Start the audio device running
giuliomoro@157 75 if(BeagleRT_startAudio()) {
giuliomoro@157 76 cout << "Error: unable to start real-time audio" << endl;
giuliomoro@157 77 return -1;
giuliomoro@157 78 }
giuliomoro@157 79
giuliomoro@157 80 // Set up interrupt handler to catch Control-C and SIGTERM
giuliomoro@157 81 signal(SIGINT, interrupt_handler);
giuliomoro@157 82 signal(SIGTERM, interrupt_handler);
giuliomoro@157 83
giuliomoro@157 84 // Run until told to stop
giuliomoro@157 85 while(!gShouldStop) {
giuliomoro@157 86 usleep(100000);
giuliomoro@157 87 }
giuliomoro@157 88
giuliomoro@157 89 // Stop the audio device
giuliomoro@157 90 BeagleRT_stopAudio();
giuliomoro@157 91
giuliomoro@157 92 // Clean up any resources allocated for audio
giuliomoro@157 93 BeagleRT_cleanupAudio();
giuliomoro@157 94
giuliomoro@157 95 // All done!
giuliomoro@157 96 return 0;
giuliomoro@157 97 }