andrewm@57: /* andrewm@57: * main.cpp andrewm@57: * andrewm@57: * Created on: Oct 24, 2014 andrewm@57: * Author: parallels andrewm@57: */ andrewm@57: andrewm@57: #include andrewm@57: #include andrewm@57: #include andrewm@57: #include andrewm@57: #include giuliomoro@301: #include andrewm@57: andrewm@57: using namespace std; andrewm@57: andrewm@57: int gSensorInputFrequency = 0; andrewm@57: int gSensorInputAmplitude = 1; andrewm@57: andrewm@57: // Handle Ctrl-C by requesting that the audio rendering stop andrewm@57: void interrupt_handler(int var) andrewm@57: { andrewm@57: gShouldStop = true; andrewm@57: } andrewm@57: andrewm@57: // Print usage information andrewm@57: void usage(const char * processName) andrewm@57: { andrewm@57: cerr << "Usage: " << processName << " [options]" << endl; andrewm@57: giuliomoro@301: Bela_usage(); andrewm@57: andrewm@57: cerr << " --frequency [-f] input: Choose the analog input controlling frequency (0-7; default 0)\n"; andrewm@57: cerr << " --amplitude [-a] input: Choose the analog input controlling amplitude (0-7; default 1)\n"; andrewm@57: cerr << " --help [-h]: Print this menu\n"; andrewm@57: } andrewm@57: andrewm@57: int main(int argc, char *argv[]) andrewm@57: { giuliomoro@301: BelaInitSettings settings; // Standard audio settings andrewm@57: andrewm@57: struct option customOptions[] = andrewm@57: { andrewm@57: {"help", 0, NULL, 'h'}, andrewm@57: {"frequency", 1, NULL, 'f'}, andrewm@57: {"amplitude", 1, NULL, 'a'}, andrewm@57: {NULL, 0, NULL, 0} andrewm@57: }; andrewm@57: andrewm@57: // Set default settings giuliomoro@301: Bela_defaultSettings(&settings); andrewm@57: andrewm@57: // Parse command-line arguments andrewm@57: while (1) { andrewm@57: int c; giuliomoro@301: if ((c = Bela_getopt_long(argc, argv, "hf:a:", customOptions, &settings)) < 0) andrewm@57: break; andrewm@57: switch (c) { andrewm@57: case 'h': andrewm@57: usage(basename(argv[0])); andrewm@57: exit(0); andrewm@57: case 'f': andrewm@57: gSensorInputFrequency = atoi(optarg); andrewm@57: if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) { andrewm@57: usage(basename(argv[0])); andrewm@57: exit(0); andrewm@57: } andrewm@57: break; andrewm@57: case 'a': andrewm@57: gSensorInputAmplitude = atoi(optarg); andrewm@57: if(gSensorInputAmplitude < 0 || gSensorInputAmplitude > 7) { andrewm@57: usage(basename(argv[0])); andrewm@57: exit(0); andrewm@57: } andrewm@57: break; andrewm@57: case '?': andrewm@57: default: andrewm@57: usage(basename(argv[0])); andrewm@57: exit(1); andrewm@57: } andrewm@57: } andrewm@57: andrewm@57: // Initialise the PRU audio device giuliomoro@301: if(Bela_initAudio(&settings, 0) != 0) { andrewm@57: cout << "Error: unable to initialise audio" << endl; andrewm@57: return -1; andrewm@57: } andrewm@57: andrewm@57: if(settings.verbose) { andrewm@57: cout << "--> Frequency on input " << gSensorInputFrequency << endl; andrewm@57: cout << "--> Amplitude on input " << gSensorInputAmplitude << endl; andrewm@57: } andrewm@57: andrewm@57: // Start the audio device running giuliomoro@301: if(Bela_startAudio()) { andrewm@57: cout << "Error: unable to start real-time audio" << endl; andrewm@57: return -1; andrewm@57: } andrewm@57: andrewm@57: // Set up interrupt handler to catch Control-C and SIGTERM andrewm@57: signal(SIGINT, interrupt_handler); andrewm@57: signal(SIGTERM, interrupt_handler); andrewm@57: andrewm@57: // Run until told to stop andrewm@57: while(!gShouldStop) { andrewm@57: usleep(100000); andrewm@57: } andrewm@57: andrewm@57: // Stop the audio device giuliomoro@301: Bela_stopAudio(); andrewm@57: andrewm@57: // Clean up any resources allocated for audio giuliomoro@301: Bela_cleanupAudio(); andrewm@57: andrewm@57: // All done! andrewm@57: return 0; andrewm@57: }