annotate examples/basic_writeFile/main.cpp @ 391:db1714fe2814 prerelease

Renamed BeagleRT to Bela in the startup script and Makefile
author andrewm
date Tue, 14 Jun 2016 21:13:40 +0100
parents e4392164b458
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@301 13 #include <Bela.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@301 28 Bela_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@301 36 BelaInitSettings 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@301 47 Bela_defaultSettings(&settings);
giuliomoro@153 48
giuliomoro@153 49 // Parse command-line arguments
giuliomoro@153 50 while (1) {
giuliomoro@153 51 int c;
giuliomoro@301 52 if ((c = Bela_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@301 69 if(Bela_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@301 75 if(Bela_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@301 90 Bela_stopAudio();
giuliomoro@153 91
giuliomoro@153 92 // Clean up any resources allocated for audio
giuliomoro@301 93 Bela_cleanupAudio();
giuliomoro@153 94
giuliomoro@153 95 // All done!
giuliomoro@153 96 return 0;
giuliomoro@153 97 }