annotate examples/scope_analogue/render.cpp @ 372:db2fe4e1b88e prerelease

Doxygen content added to each example render.cpp. References to AnalogReadFrame etc. removed from doxygen content.
author Robert Jack <robert.h.jack@gmail.com>
date Thu, 09 Jun 2016 18:16:05 +0100
parents 1feb9c23ac57
children
rev   line source
robert@372 1 /*
robert@372 2 ____ _____ _ _
robert@372 3 | __ )| ____| | / \
robert@372 4 | _ \| _| | | / _ \
robert@372 5 | |_) | |___| |___ / ___ \
robert@372 6 |____/|_____|_____/_/ \_\.io
robert@372 7
robert@372 8 */
robert@372 9
robert@372 10 /**
robert@372 11 \example 3_scope_analog
robert@372 12
robert@372 13 Connecting potentiometers
robert@372 14 -------------------------
robert@372 15
robert@372 16 This example reads from analogue inputs 0 and 1 via `analogReadFrame()` and
robert@372 17 generates a sine wave with amplitude and frequency determined by their values.
robert@372 18 It's best to connect a 10K potentiometer to each of these analog inputs. Far
robert@372 19 left and far right pins of the pot go to 3.3V and GND, the middle should be
robert@372 20 connected to the analog in pins.
robert@372 21
robert@372 22 The sine wave is then plotted on the oscilloscope. Click the Open Scope button to
robert@372 23 view the results. As you turn the potentiometers you will see the amplitude and
robert@372 24 frequency of the sine wave change.
robert@372 25
robert@372 26 This project also shows as example of `map()` which allows you to re-scale a number
robert@372 27 from one range to another. Note that `map()` does not constrain your variable
robert@372 28 within the upper and lower limits. If you want to do this use the `constrain()`
robert@372 29 function.
robert@372 30 */
l@273 31
giuliomoro@301 32 #include <Bela.h>
l@273 33 #include <cmath>
l@273 34 #include <Scope.h>
l@273 35
l@273 36 Scope scope;
l@273 37
l@273 38 float gInverseSampleRate;
l@273 39 float gPhase;
l@273 40
giuliomoro@301 41 bool setup(BelaContext *context, void *userData)
l@273 42 {
l@273 43
l@273 44 // setup the scope with 3 channels at the audio sample rate
l@273 45 scope.setup(3, context->audioSampleRate);
l@273 46
l@273 47 gInverseSampleRate = 1.0 / context->audioSampleRate;
l@273 48 gPhase = 0.0;
l@273 49
l@273 50 return true;
l@273 51 }
l@273 52
giuliomoro@301 53 void render(BelaContext *context, void *userData)
l@273 54 {
l@273 55
l@273 56 for(unsigned int n = 0; n < context->audioFrames; n++) {
l@273 57
l@273 58 // read analogIn channels 0 and 1
andrewm@308 59 float in1 = analogRead(context, n, 0);
andrewm@308 60 float in2 = analogRead(context, n, 1);
l@273 61
l@273 62 // map in1 to amplitude and in2 to frequency
l@273 63 float amplitude = in1 * 0.8f;
l@273 64 float frequency = map(in2, 0, 1, 100, 1000);
l@273 65
l@273 66 // generate a sine wave with the amplitude and frequency
l@273 67 float out = amplitude * sinf(gPhase);
l@273 68 gPhase += 2.0 * M_PI * frequency * gInverseSampleRate;
l@273 69 if(gPhase > 2.0 * M_PI)
l@273 70 gPhase -= 2.0 * M_PI;
l@273 71
l@273 72 // log the sine wave and sensor values on the scope
l@273 73 scope.log(out, in1, in2);
l@273 74
l@273 75 // pass the sine wave to the audio outputs
l@273 76 for(unsigned int channel = 0; channel < context->audioChannels; channel++)
l@273 77 context->audioOut[n * context->audioChannels + channel] = out;
l@273 78
l@273 79 }
l@273 80 }
l@273 81
giuliomoro@301 82 void cleanup(BelaContext *context, void *userData)
l@273 83 {
l@273 84
l@273 85 }