comparison examples/03-Analog/analog-output/render.cpp @ 542:3016638b4da2 prerelease

Analog examples updated
author Robert Jack <robert.h.jack@gmail.com>
date Fri, 24 Jun 2016 13:00:31 +0100
parents 1cec96845a23
children
comparison
equal deleted inserted replaced
541:c301cc07ae11 542:3016638b4da2
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt 21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
22 */ 22 */
23 23
24 24
25 #include <Bela.h> 25 #include <Bela.h>
26 #include <rtdk.h>
27 #include <cmath> 26 #include <cmath>
28 27
29 // Set range for analog outputs designed for driving LEDs 28 // Set range for analog outputs designed for driving LEDs
30 const float kMinimumAmplitude = (1.5 / 5.0); 29 const float kMinimumAmplitude = (1.5 / 5.0);
31 const float kAmplitudeRange = 1.0 - kMinimumAmplitude; 30 const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
32 31
33 float gFrequency; 32 float gFrequency = 3.0;
34 float gPhase; 33 float gPhase;
35 float gInverseSampleRate; 34 float gInverseSampleRate;
36 35
37 bool setup(BelaContext *context, void *userData) 36 bool setup(BelaContext *context, void *userData)
38 { 37 {
39 // Retrieve a parameter passed in from the initAudio() call
40 gFrequency = *(float *)userData;
41 38
42 if(context->analogFrames == 0) { 39 // Check if analog channels are enabled
43 rt_printf("Error: this example needs the matrix enabled\n"); 40 if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
41 rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
42 return false;
43 }
44
45 // Check that we have the same number of inputs and outputs.
46 if(context->audioInChannels != context->audioOutChannels ||
47 context->analogInChannels != context-> analogOutChannels){
48 printf("Error: for this project, you need the same number of input and output channels.\n");
44 return false; 49 return false;
45 } 50 }
46 51
47 gInverseSampleRate = 1.0 / context->analogSampleRate; 52 gInverseSampleRate = 1.0 / context->analogSampleRate;
48 gPhase = 0.0; 53 gPhase = 0.0;
53 void render(BelaContext *context, void *userData) 58 void render(BelaContext *context, void *userData)
54 { 59 {
55 for(unsigned int n = 0; n < context->analogFrames; n++) { 60 for(unsigned int n = 0; n < context->analogFrames; n++) {
56 // Set LED to different phase for each matrix channel 61 // Set LED to different phase for each matrix channel
57 float relativePhase = 0.0; 62 float relativePhase = 0.0;
58 for(unsigned int channel = 0; channel < context->analogChannels; channel++) { 63 for(unsigned int channel = 0; channel < context->analogOutChannels; channel++) {
59 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase)); 64 float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
60 65
61 analogWrite(context, n, channel, out); 66 analogWrite(context, n, channel, out);
62 67
63 // Advance by pi/4 (1/8 of a full rotation) for each channel 68 // Advance by pi/4 (1/8 of a full rotation) for each channel
64 relativePhase += M_PI * 0.25; 69 relativePhase += M_PI * 0.25;
65 } 70 }
66 71
72 // Update and wrap phase of sine tone
67 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate; 73 gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
68 if(gPhase > 2.0 * M_PI) 74 if(gPhase > 2.0 * M_PI)
69 gPhase -= 2.0 * M_PI; 75 gPhase -= 2.0 * M_PI;
70 } 76 }
71 } 77 }
97 The output on each pin is set with `analogWrite()` within the for loop that 103 The output on each pin is set with `analogWrite()` within the for loop that
98 cycles through the analog output channels. This needs to be provided with 104 cycles through the analog output channels. This needs to be provided with
99 arguments as follows `analogWrite(context, n, channel, out)`. Channel is 105 arguments as follows `analogWrite(context, n, channel, out)`. Channel is
100 where the you give the address of the analog output pin (in this case we cycle 106 where the you give the address of the analog output pin (in this case we cycle
101 through each pin address in the for loop), out is the variable that holds the 107 through each pin address in the for loop), out is the variable that holds the
102 desired output (in this case set by the sine wave). 108 desired output (in this case set by the sine wave) and `n` is the frame number.
103 109
104 Notice that the phase of the brightness cycle for each led is different. This 110 Notice that the phase of the brightness cycle for each led is different. This
105 is achieved by updating a variable that stores a relative phase value. This 111 is achieved by updating a variable that stores a relative phase value. This
106 variable is advanced by pi/4 (1/8 of a full rotation) for each channel giving 112 variable is advanced by pi/4 (1/8 of a full rotation) for each channel giving
107 each of the eight LEDs a different phase. 113 each of the eight LEDs a different phase.