diff 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
line wrap: on
line diff
--- a/examples/03-Analog/analog-output/render.cpp	Fri Jun 24 02:29:05 2016 +0100
+++ b/examples/03-Analog/analog-output/render.cpp	Fri Jun 24 13:00:31 2016 +0100
@@ -23,24 +23,29 @@
 
 
 #include <Bela.h>
-#include <rtdk.h>
 #include <cmath>
 
 // Set range for analog outputs designed for driving LEDs
 const float kMinimumAmplitude = (1.5 / 5.0);
 const float kAmplitudeRange = 1.0 - kMinimumAmplitude;
 
-float gFrequency;
+float gFrequency = 3.0;
 float gPhase;
 float gInverseSampleRate;
 
 bool setup(BelaContext *context, void *userData)
 {
-	// Retrieve a parameter passed in from the initAudio() call
-	gFrequency = *(float *)userData;
 
-	if(context->analogFrames == 0) {
-		rt_printf("Error: this example needs the matrix enabled\n");
+	// Check if analog channels are enabled
+	if(context->analogFrames == 0 || context->analogFrames > context->audioFrames) {
+		rt_printf("Error: this example needs analog enabled, with 4 or 8 channels\n");
+		return false;
+	}
+
+	// Check that we have the same number of inputs and outputs.
+	if(context->audioInChannels != context->audioOutChannels ||
+			context->analogInChannels != context-> analogOutChannels){
+		printf("Error: for this project, you need the same number of input and output channels.\n");
 		return false;
 	}
 
@@ -55,7 +60,7 @@
 	for(unsigned int n = 0; n < context->analogFrames; n++) {
 		// Set LED to different phase for each matrix channel
 		float relativePhase = 0.0;
-		for(unsigned int channel = 0; channel < context->analogChannels; channel++) {
+		for(unsigned int channel = 0; channel < context->analogOutChannels; channel++) {
 			float out = kMinimumAmplitude + kAmplitudeRange * 0.5f * (1.0f + sinf(gPhase + relativePhase));
 
 			analogWrite(context, n, channel, out);
@@ -64,6 +69,7 @@
 			relativePhase += M_PI * 0.25;
 		}
 
+        // Update and wrap phase of sine tone
 		gPhase += 2.0 * M_PI * gFrequency * gInverseSampleRate;
 		if(gPhase > 2.0 * M_PI)
 			gPhase -= 2.0 * M_PI;
@@ -99,7 +105,7 @@
 arguments as follows `analogWrite(context, n, channel, out)`. Channel is 
 where the you give the address of the analog output pin (in this case we cycle 
 through each pin address in the for loop), out is the variable that holds the 
-desired output (in this case set by the sine wave).
+desired output (in this case set by the sine wave) and `n` is the frame number.
 
 Notice that the phase of the brightness cycle for each led is different. This 
 is achieved by updating a variable that stores a relative phase value. This