comparison examples/03-Analog/analog-output/render.cpp @ 464:8fcfbfb32aa0 prerelease

Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author Robert Jack <robert.h.jack@gmail.com>
date Mon, 20 Jun 2016 16:20:38 +0100
parents
children b935f890e512
comparison
equal deleted inserted replaced
463:c47709e8b5c9 464:8fcfbfb32aa0
1 /*
2 ____ _____ _ _
3 | __ )| ____| | / \
4 | _ \| _| | | / _ \
5 | |_) | |___| |___ / ___ \
6 |____/|_____|_____/_/ \_\
7
8 The platform for ultra-low latency audio and sensor processing
9
10 http://bela.io
11
12 A project of the Augmented Instruments Laboratory within the
13 Centre for Digital Music at Queen Mary University of London.
14 http://www.eecs.qmul.ac.uk/~andrewm
15
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
19
20 The Bela software is distributed under the GNU Lesser General Public License
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
22 */
23
24
25 #include <Bela.h>
26 #include <rtdk.h>
27 #include <cmath>
28
29 // Set range for analog outputs designed for driving LEDs
30 const float kMinimumAmplitude = (1.5 / 5.0);
31 const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
32
33 float gFrequency;
34 float gPhase;
35 float gInverseSampleRate;
36
37 bool setup(BelaContext *context, void *userData)
38 {
39 // Retrieve a parameter passed in from the initAudio() call
40 gFrequency = *(float *)userData;
41
42 if(context->analogFrames == 0) {
43 rt_printf("Error: this example needs the matrix enabled\n");
44 return false;
45 }
46
47 gInverseSampleRate = 1.0 / context->analogSampleRate;
48 gPhase = 0.0;
49
50 return true;
51 }
52
53 void render(BelaContext *context, void *userData)
54 {
55 for(unsigned int n = 0; n < context->analogFrames; n++) {
56 // Set LED to different phase for each matrix channel
57 float relativePhase = 0.0;
58 for(unsigned int channel = 0; channel < context->analogChannels; channel++) {
59 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
60
61 analogWrite(context, n, channel, out);
62
63 // Advance by pi/4 (1/8 of a full rotation) for each channel
64 relativePhase += M_PI * 0.25;
65 }
66
67 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
68 if(gPhase > 2.0 * M_PI)
69 gPhase -= 2.0 * M_PI;
70 }
71 }
72
73 void cleanup(BelaContext *context, void *userData)
74 {
75
76 }
77
78 /* ------------ Project Explantation ------------ */
79
80 /**
81 \example 03-analog-output
82
83 Fading LEDs
84 -----------
85
86 This sketch uses a sine wave to drive the brightness of a series of LEDs
87 connected to the eight analog out pins. Again you can see the nested `for` loop
88 structure but this time for the analog output channels rather than the audio.
89
90 - connect an LED in series with a 470ohm resistor between each of the analogOut pins and ground.
91
92 Within the first for loop in render we cycle through each frame in the analog
93 output matrix. At each frame we then cycle through the analog output channels
94 with another for loop and set the output voltage according to the phase of a
95 sine tone that acts as an LFO. The analog output pins can provide a voltage of
96 ~4.092V.
97
98 The output on each pin is set with `analogWrite()` within the for loop that
99 cycles through the analog output channels. This needs to be provided with
100 arguments as follows `analogWrite(context, n, channel, out)`. Channel is
101 where the you give the address of the analog output pin (in this case we cycle
102 through each pin address in the for loop), out is the variable that holds the
103 desired output (in this case set by the sine wave).
104
105 Notice that the phase of the brightness cycle for each led is different. This
106 is achieved by updating a variable that stores a relative phase value. This
107 variable is advanced by pi/4 (1/8 of a full rotation) for each channel giving
108 each of the eight LEDs a different phase.
109 */