Mercurial > hg > beaglert
changeset 273:0ee6eebb567a prerelease
Added scope_basic and scope_analogue example projects
author | Liam Donovan <l.b.donovan@qmul.ac.uk> |
---|---|
date | Tue, 17 May 2016 16:31:51 +0100 |
parents | 733006bdbca2 |
children | cf98c06c72fd |
files | projects/scope_analogue/render.cpp projects/scope_basic/render.cpp |
diffstat | 2 files changed, 113 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/scope_analogue/render.cpp Tue May 17 16:31:51 2016 +0100 @@ -0,0 +1,59 @@ +// this example reads the analogue inputs 0 and 1 +// and generates a sine wave with an amplitude and +// frequency determined by their values +// it then plots these on the oscilloscope + +#include <BeagleRT.h> +#include <cmath> +#include <Scope.h> + +Scope scope; + +float gInverseSampleRate; +float gPhase; + +bool setup(BeagleRTContext *context, void *userData) +{ + + // setup the scope with 3 channels at the audio sample rate + scope.setup(3, context->audioSampleRate); + + gInverseSampleRate = 1.0 / context->audioSampleRate; + gPhase = 0.0; + + return true; +} + +void render(BeagleRTContext *context, void *userData) +{ + + for(unsigned int n = 0; n < context->audioFrames; n++) { + + // read analogIn channels 0 and 1 + float in1 = analogReadFrame(context, n, 0); + float in2 = analogReadFrame(context, n, 1); + + // map in1 to amplitude and in2 to frequency + float amplitude = in1 * 0.8f; + float frequency = map(in2, 0, 1, 100, 1000); + + // generate a sine wave with the amplitude and frequency + float out = amplitude * sinf(gPhase); + gPhase += 2.0 * M_PI * frequency * gInverseSampleRate; + if(gPhase > 2.0 * M_PI) + gPhase -= 2.0 * M_PI; + + // log the sine wave and sensor values on the scope + scope.log(out, in1, in2); + + // pass the sine wave to the audio outputs + for(unsigned int channel = 0; channel < context->audioChannels; channel++) + context->audioOut[n * context->audioChannels + channel] = out; + + } +} + +void cleanup(BeagleRTContext *context, void *userData) +{ + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/scope_basic/render.cpp Tue May 17 16:31:51 2016 +0100 @@ -0,0 +1,54 @@ +#include <BeagleRT.h> +#include <Scope.h> +#include <cmath> + +// set the frequency of the oscillators +float gFrequency = 110.0; +float gPhase; +float gInverseSampleRate; + +// instantiate the scope +Scope scope; + +bool setup(BeagleRTContext *context, void *userData) +{ + // tell the scope how many channels and the sample rate + scope.setup(3, context->audioSampleRate); + + gPhase = 0; + gInverseSampleRate = 1.0f/context->audioSampleRate; + + return true; +} + +float lastOut = 0.0; +float lastOut2 = 0.0; +void render(BeagleRTContext *context, void *userData) +{ + // iterate over the audio frames and create three oscillators, seperated in phase by PI/2 + for (unsigned int n=0; n<context->audioFrames; n++){ + float out = 0.8f * sinf(gPhase); + float out2 = 0.8f * sinf(gPhase - M_PI/2); + float out3 = 0.8f * sinf(gPhase + M_PI/2); + gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; + if(gPhase > 2.0 * M_PI) + gPhase -= 2.0 * M_PI; + + // log the three oscillators to the scope + scope.log(out, out2, out3); + + // optional - tell the scope to trigger when oscillator 1 becomes less than oscillator 2 + // note this has no effect unless trigger mode is set to custom in the scope UI + if (lastOut >= lastOut2 && out < out2){ + scope.trigger(); + } + + lastOut = out; + lastOut2 = out2; + } +} + +void cleanup(BeagleRTContext *context, void *userData) +{ + +}