annotate projects/basic_writeFile/main.cpp @ 153:3b7270949a97

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