annotate projects/basic_analog_output/render.cpp @ 285:5433c83ce04e Doxy prerelease

Doxygen content added to more project render.cpp files and amended in others.
author Robert Jack <robert.h.jack@gmail.com>
date Tue, 17 May 2016 18:46:55 +0100
parents ac8eb07afcf5
children
rev   line source
andrewm@0 1 /*
robert@269 2 ____ _____ _ _
robert@269 3 | __ )| ____| | / \
robert@269 4 | _ \| _| | | / _ \
robert@269 5 | |_) | |___| |___ / ___ \
robert@269 6 |____/|_____|_____/_/ \_\.io
robert@269 7
robert@269 8 */
robert@269 9
robert@269 10 /*
andrewm@0 11 *
robert@269 12 * Andrew McPherson and Victor Zappi
robert@269 13 * Queen Mary, University of London
andrewm@0 14 */
andrewm@0 15
robert@269 16 /**
robert@269 17 \example 3_analog_output
robert@269 18
robert@269 19 Fading LEDs
robert@269 20 -----------
robert@269 21
robert@269 22 This sketch uses a sine wave to drive the brightness of a series of LEDs
robert@269 23 connected to the eight analog out pins. Again you can see the nested `for` loop
robert@269 24 structure but this time for the analog output channels rather than the audio.
robert@269 25
robert@285 26 - connect an LED in series with a 470ohm resistor between each of the analogOut pins and ground.
robert@285 27
robert@285 28 Within the first for loop in render we cycle through each frame in the analog
robert@269 29 output matrix. At each frame we then cycle through the analog output channels
robert@285 30 with another for loop and set the output voltage according to the phase of a
robert@269 31 sine tone that acts as an LFO. The analog output pins can provide a voltage of
robert@269 32 ~4.092V.
robert@269 33
robert@285 34 The output on each pin is set with `analogWriteFrame()` within the for loop that
robert@269 35 cycles through the analog output channels. This needs to be provided with
robert@269 36 arguments as follows `analogWriteFrame(context, n, channel, out)`. Channel is
robert@269 37 where the you give the address of the analog output pin (in this case we cycle
robert@269 38 through each pin address in the for loop), out is the variable that holds the
robert@269 39 desired output (in this case set by the sine wave).
robert@269 40
robert@269 41 Notice that the phase of the brightness cycle for each led is different. This
robert@269 42 is achieved by updating a variable that stores a relative phase value. This
robert@269 43 variable is advanced by pi/4 (1/8 of a full rotation) for each channel giving
robert@269 44 each of the eight LEDs a different phase.
robert@269 45
robert@269 46 */
robert@269 47
andrewm@0 48
andrewm@56 49 #include <BeagleRT.h>
andrewm@56 50 #include <Utilities.h>
andrewm@0 51 #include <rtdk.h>
andrewm@0 52 #include <cmath>
andrewm@0 53
andrewm@0 54 // Set range for analog outputs designed for driving LEDs
andrewm@52 55 const float kMinimumAmplitude = (1.5 / 5.0);
andrewm@52 56 const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
andrewm@0 57
andrewm@0 58 float gFrequency;
andrewm@0 59 float gPhase;
andrewm@0 60 float gInverseSampleRate;
andrewm@0 61
andrewm@56 62 // setup() is called once before the audio rendering starts.
andrewm@0 63 // Use it to perform any initialisation and allocation which is dependent
andrewm@0 64 // on the period size or sample rate.
andrewm@0 65 //
andrewm@0 66 // userData holds an opaque pointer to a data structure that was passed
andrewm@0 67 // in from the call to initAudio().
andrewm@0 68 //
andrewm@0 69 // Return true on success; returning false halts the program.
andrewm@0 70
andrewm@56 71 bool setup(BeagleRTContext *context, void *userData)
andrewm@0 72 {
andrewm@0 73 // Retrieve a parameter passed in from the initAudio() call
andrewm@0 74 gFrequency = *(float *)userData;
andrewm@0 75
andrewm@52 76 if(context->analogFrames == 0) {
andrewm@12 77 rt_printf("Error: this example needs the matrix enabled\n");
andrewm@0 78 return false;
andrewm@0 79 }
andrewm@0 80
andrewm@52 81 gInverseSampleRate = 1.0 / context->analogSampleRate;
andrewm@0 82 gPhase = 0.0;
andrewm@0 83
andrewm@0 84 return true;
andrewm@0 85 }
andrewm@0 86
andrewm@0 87 // render() is called regularly at the highest priority by the audio engine.
andrewm@0 88 // Input and output are given from the audio hardware and the other
andrewm@0 89 // ADCs and DACs (if available). If only audio is available, numMatrixFrames
andrewm@0 90 // will be 0.
andrewm@0 91
andrewm@52 92 void render(BeagleRTContext *context, void *userData)
andrewm@0 93 {
andrewm@56 94 for(unsigned int n = 0; n < context->analogFrames; n++) {
andrewm@0 95 // Set LED to different phase for each matrix channel
andrewm@0 96 float relativePhase = 0.0;
andrewm@56 97 for(unsigned int channel = 0; channel < context->analogChannels; channel++) {
andrewm@0 98 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
andrewm@0 99
andrewm@52 100 analogWriteFrame(context, n, channel, out);
andrewm@0 101
andrewm@0 102 // Advance by pi/4 (1/8 of a full rotation) for each channel
andrewm@0 103 relativePhase += M_PI * 0.25;
andrewm@0 104 }
andrewm@0 105
andrewm@0 106 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
andrewm@0 107 if(gPhase > 2.0 * M_PI)
andrewm@0 108 gPhase -= 2.0 * M_PI;
andrewm@0 109 }
andrewm@0 110 }
andrewm@0 111
andrewm@56 112 // cleanup() is called once at the end, after the audio has stopped.
andrewm@56 113 // Release any resources that were allocated in setup().
andrewm@0 114
andrewm@56 115 void cleanup(BeagleRTContext *context, void *userData)
andrewm@0 116 {
andrewm@0 117
andrewm@0 118 }