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