Mercurial > hg > beaglert
changeset 94:2fe6690fcab7
Added scope project, copied and edited from nodejsbeaglert
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Wed, 22 Jul 2015 11:38:41 +0100 |
parents | 8c7f537d0a21 |
children | c529505203ee ef9392d077a4 |
files | projects/scope/main.cpp projects/scope/render.cpp projects/scope/undefined |
diffstat | 3 files changed, 256 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/scope/main.cpp Wed Jul 22 11:38:41 2015 +0100 @@ -0,0 +1,97 @@ +/* + * main.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ +#include <unistd.h> +#include <iostream> +#include <cstdlib> +#include <libgen.h> +#include <signal.h> +#include <getopt.h> +#include <BeagleRT.h> + +using namespace std; + +// Handle Ctrl-C by requesting that the audio rendering stop +void interrupt_handler(int var) +{ + gShouldStop = true; +} + +// Print usage information +void usage(const char * processName) +{ + cerr << "Usage: " << processName << " [options]" << endl; + + BeagleRT_usage(); + + cerr << " --frequency [-f] frequency: Set the frequency of the oscillator\n"; + cerr << " --help [-h]: Print this menu\n"; +} + +int main(int argc, char *argv[]) +{ + BeagleRTInitSettings settings; // Standard audio settings + float frequency = 440.0; // Frequency of oscillator + + struct option customOptions[] = + { + {"help", 0, NULL, 'h'}, + {"frequency", 1, NULL, 'f'}, + {NULL, 0, NULL, 0} + }; + + // Set default settings + BeagleRT_defaultSettings(&settings); + + // Parse command-line arguments + while (1) { + int c; + if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0) + break; + switch (c) { + case 'h': + usage(basename(argv[0])); + exit(0); + case 'f': + frequency = atof(optarg); + break; + case '?': + default: + usage(basename(argv[0])); + exit(1); + } + } + + // Initialise the PRU audio device + if(BeagleRT_initAudio(&settings, &frequency) != 0) { + cout << "Error: unable to initialise audio" << endl; + return -1; + } + + // Start the audio device running + if(BeagleRT_startAudio()) { + cout << "Error: unable to start real-time audio" << endl; + return -1; + } + + // Set up interrupt handler to catch Control-C and SIGTERM + signal(SIGINT, interrupt_handler); + signal(SIGTERM, interrupt_handler); + + // Run until told to stop + while(!gShouldStop) { + usleep(100000); + } + + // Stop the audio device + BeagleRT_stopAudio(); + + // Clean up any resources allocated for audio + BeagleRT_cleanupAudio(); + + // All done! + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/scope/render.cpp Wed Jul 22 11:38:41 2015 +0100 @@ -0,0 +1,75 @@ +#include <BeagleRT.h> +#include <Scope.h> +#include <cmath> + +float gPhase1, gPhase2; +float gFrequency1, gFrequency2; +float gInverseSampleRate; + +Scope scope; //create a scope object + +// initialise_render() is called once before the audio rendering starts. +// Use it to perform any initialisation and allocation which is dependent +// on the period size or sample rate. +// +// userData holds an opaque pointer to a data structure that was passed +// in from the call to initAudio(). +// +// Return true on success; returning false halts the program. +bool setup(BeagleRTContext *context, void *userData) +{ + scope.setup(context->audioSampleRate); //call this once in setup to initialise the scope + + gInverseSampleRate = 1.0/context->audioSampleRate; + + gPhase1 = 0.0; + gPhase2 = 0.0; + + gFrequency1 = 200.0; + gFrequency2 = 201.0; + return true; +} + +// render() is called regularly at the highest priority by the audio engine. +// Input and output are given from the audio hardware and the other +// ADCs and DACs (if available). If only audio is available, numMatrixFrames +// will be 0. + +void render(BeagleRTContext *context, void *userData) +{ + for(unsigned int n = 0; n < context->audioFrames; n++) { + + float chn1 = sinf(gPhase1); + float chn2 = sinf(gPhase2); + + float chn3 = context->audioIn[n*2 + 0]; + float chn4 = context->audioIn[n*2 + 1]; + + float chn5 = context->analogIn[(int)n/2*8 + 0]; + float chn6 = context->analogIn[(int)n/2*8 + 1]; + +// scope.log(chn1, chn2, chn3, chn4, chn5, chn6); + scope.log(chn1); + //call this once every audio frame + //takes six or fewer floats as parameters + //first parameter becomes channel 1 etc + //to view, click the 'oscilloscope' button on the toolbar while BeagleRT is NOT running + //then click the big red button on the toolbar on this page + + gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate; + gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate; + if(gPhase1 > 2.0 * M_PI) + gPhase1 -= 2.0 * M_PI; + if(gPhase2 > 2.0 * M_PI) + gPhase2 -= 2.0 * M_PI; + + } +} + +// cleanup_render() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in initialise_render(). + +void cleanup(BeagleRTContext *context, void *userData) +{ + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/scope/undefined Wed Jul 22 11:38:41 2015 +0100 @@ -0,0 +1,84 @@ +/* + * render.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ + +#include <BeagleRT.h> +#include <cmath> +#include <client.h> + +float gFrequency; +float gPhase; +float gInverseSampleRate; +int gCount=0; +networkData networkObject; +AuxiliaryTask transmitReceiveDataTask; + +void transmitReceiveData(){ + printf("transmitReceiveData auxiliary task has started\n"); + while(!gShouldStop){ + sendMessage(networkObject); + receiveMessage(networkObject); + usleep(1000); + } + closeSockets(); +} + +// setup() is called once before the audio rendering starts. +// Use it to perform any initialisation and allocation which is dependent +// on the period size or sample rate. +// +// userData holds an opaque pointer to a data structure that was passed +// in from the call to initAudio(). +// +// Return true on success; returning false halts the program. +bool setup(BeagleRTContext *context, void *userData) +{ + // Retrieve a parameter passed in from the initAudio() call + gFrequency = *(float *)userData; + + gInverseSampleRate = 1.0 / context->audioSampleRate; + gPhase = 0.0; + + networkObject.counter=&gCount; + networkObject.variables[0]=&gFrequency; + networkObject.variables[1]=&gPhase; + networkObject.numVariables=2; + + setupSockets(settings->receivePort, settings->transmitPort, settings->serverName); + transmitReceiveDataTask= BeagleRT_createAuxiliaryTask(*transmitReceiveData, 80, "transmit-receive-data"); + //scheduleAuxiliaryTask(transmitReceiveDataTask); //here it does not work + return true; +} + +// render() is called regularly at the highest priority by the audio engine. +// Input and output are given from the audio hardware and the other +// ADCs and DACs (if available). If only audio is available, numMatrixFrames +// will be 0. + +void render(BeagleRTContext *context, void *userData) +{ + for(unsigned int n = 0; n < context->audioFrames; n++) { + float out = 0.7f * sinf(gPhase); + gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; + if(gPhase > 2.0 * M_PI) + gPhase -= 2.0 * M_PI; + + for(unsigned int channel = 0; channel < context->audioChannels; channel++) + context->audioOut[n * context->audioChannels + channel] = out; + + if(gCount == 0){ + BeagleRT_scheduleAuxiliaryTask(transmitReceiveDataTask); + } + gCount++; + } +} + +// cleanup() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in setup(). + +void cleanup(BeagleRTContext *context, void *userData) +{ +}