annotate projects/basic_network/main.cpp @ 68:59edd5780fef

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