annotate projects/measure_noisefloor/main.cpp @ 151:e9c9404e3d1f ClockSync

Pff partially working. No PID. When setting the audio clock on the bbb to 44098 the master and slave clock keep diverging instead of converging ...
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 22 Sep 2015 04:10:07 +0100
parents d41631e0fe0e
children
rev   line source
andrewm@89 1 /*
andrewm@89 2 * main.cpp
andrewm@89 3 *
andrewm@89 4 * Created on: Oct 24, 2014
andrewm@89 5 * Author: parallels
andrewm@89 6 */
andrewm@89 7
andrewm@89 8 #include <iostream>
andrewm@89 9 #include <cstdlib>
andrewm@89 10 #include <libgen.h>
andrewm@89 11 #include <signal.h>
andrewm@89 12 #include <getopt.h>
andrewm@89 13 #include <BeagleRT.h>
andrewm@89 14
andrewm@89 15 extern int gBufferSize;
andrewm@89 16
andrewm@89 17 using namespace std;
andrewm@89 18
andrewm@89 19 // Handle Ctrl-C by requesting that the audio rendering stop
andrewm@89 20 void interrupt_handler(int var)
andrewm@89 21 {
andrewm@89 22 gShouldStop = true;
andrewm@89 23 }
andrewm@89 24
andrewm@89 25 // Print usage information
andrewm@89 26 void usage(const char * processName)
andrewm@89 27 {
andrewm@89 28 cerr << "Usage: " << processName << " [options]" << endl;
andrewm@89 29
andrewm@89 30 BeagleRT_usage();
andrewm@89 31
andrewm@89 32 cerr << " --buffer-size [-b] size Set the analysis buffer size\n";
andrewm@89 33 cerr << " --help [-h]: Print this menu\n";
andrewm@89 34 }
andrewm@89 35
andrewm@89 36 int main(int argc, char *argv[])
andrewm@89 37 {
andrewm@89 38 BeagleRTInitSettings settings; // Standard audio settings
andrewm@89 39
andrewm@89 40 struct option customOptions[] =
andrewm@89 41 {
andrewm@89 42 {"help", 0, NULL, 'h'},
andrewm@89 43 {"buffer-size", 1, NULL, 'b'},
andrewm@89 44 {NULL, 0, NULL, 0}
andrewm@89 45 };
andrewm@89 46
andrewm@89 47 // Set default settings
andrewm@89 48 BeagleRT_defaultSettings(&settings);
andrewm@89 49
andrewm@89 50 // By default use a longer period size because latency is not an issue
andrewm@89 51 settings.periodSize = 32;
andrewm@89 52
andrewm@89 53 // Parse command-line arguments
andrewm@89 54 while (1) {
andrewm@89 55 int c;
andrewm@89 56 if ((c = BeagleRT_getopt_long(argc, argv, "hb:", customOptions, &settings)) < 0)
andrewm@89 57 break;
andrewm@89 58 switch (c) {
andrewm@89 59 case 'b':
andrewm@89 60 gBufferSize = atoi(optarg);
andrewm@89 61 break;
andrewm@89 62 case 'h':
andrewm@89 63 usage(basename(argv[0]));
andrewm@89 64 exit(0);
andrewm@89 65 case '?':
andrewm@89 66 default:
andrewm@89 67 usage(basename(argv[0]));
andrewm@89 68 exit(1);
andrewm@89 69 }
andrewm@89 70 }
andrewm@89 71
andrewm@89 72 if(gBufferSize < settings.periodSize)
andrewm@89 73 gBufferSize = settings.periodSize;
andrewm@89 74
andrewm@89 75 // Initialise the PRU audio device
andrewm@89 76 if(BeagleRT_initAudio(&settings, 0) != 0) {
andrewm@89 77 cout << "Error: unable to initialise audio" << endl;
andrewm@89 78 return -1;
andrewm@89 79 }
andrewm@89 80
andrewm@89 81 // Start the audio device running
andrewm@89 82 if(BeagleRT_startAudio()) {
andrewm@89 83 cout << "Error: unable to start real-time audio" << endl;
andrewm@89 84 return -1;
andrewm@89 85 }
andrewm@89 86
andrewm@89 87 // Set up interrupt handler to catch Control-C and SIGTERM
andrewm@89 88 signal(SIGINT, interrupt_handler);
andrewm@89 89 signal(SIGTERM, interrupt_handler);
andrewm@89 90
andrewm@89 91 // Run until told to stop
andrewm@89 92 while(!gShouldStop) {
andrewm@89 93 usleep(100000);
andrewm@89 94 }
andrewm@89 95
andrewm@89 96 // Stop the audio device
andrewm@89 97 BeagleRT_stopAudio();
andrewm@89 98
andrewm@89 99 // Clean up any resources allocated for audio
andrewm@89 100 BeagleRT_cleanupAudio();
andrewm@89 101
andrewm@89 102 // All done!
andrewm@89 103 return 0;
andrewm@89 104 }