annotate projects/basic/main.cpp @ 45:579c86316008 newapi

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