diff projects/analogDigitalDemo/render.cpp @ 23:182ae9367104 matrix_gpio

- persistency: the last frame of each digital and analogOut buffers is used to initialize all the frames in the next buffer. This means that once a value is set, the pin will hold the value until you change it - AnalogXyz macros have been renamed to analogXyz - the short option -P has been removed. The long option --pru-file has to be used instead
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 05 May 2015 17:28:00 +0100
parents fbfeb5895efd
children 83baffda5786
line wrap: on
line diff
--- a/projects/analogDigitalDemo/render.cpp	Sun May 03 01:10:17 2015 +0100
+++ b/projects/analogDigitalDemo/render.cpp	Tue May 05 17:28:00 2015 +0100
@@ -1,4 +1,4 @@
-/*
+    /*
  *
  * First assignment for ECS732 RTDSP, to implement a 2-way audio crossover
  * using the BeagleBone Black.
@@ -29,6 +29,7 @@
 					   void *userData)
 {
 	gNumAnalogChannels=numAnalogChannels;
+    gNumDigitalChannels=numDigitalChannels;
 	return true;
 }
 
@@ -41,32 +42,54 @@
 void render(int numAnalogFrames, int numDigitalFrames, int numAudioFrames, float *audioIn, float *audioOut,
 			float *analogIn, float *analogOut, uint32_t *digital)
 /*
- * Hey, expect buffer underruns to happen here, as we are doing lots of printfs
+we assume that gNumAnalogChannels=8, numAnalogFrames==8 and  numDigitalFrames==numAudioFrames
  * */
 {
 	gNumDigitalFrames=numDigitalFrames;
-	if(gCountFrames==0){ //this will be executed only on the first call to render(), but the bits will go through this cycle for every subsequent buffer
-						// that is, P8_29 will pulse at the beginning of each buffer
+
+	if((gCountFrames&31)==0){ //every 32 frames...
+        //ANALOG channels
+		analogWrite(0, 0, analogRead(0,0)); // read the input0 at frame0  and write it to output0 frame0. Using analogWrite will fill the rest of the buffer with the same value
+                                            // The value at the last frame will persist through the successive buffers until is set again.
+                                            // This effectively is a pass-through with downsampling by 32 times
+        analogWrite(3, 0, 1.0);  // write 1.0 to channel3 from frame0 to the end of the buffer
+        analogWrite(3, 4, 0.1);  // write 0.1  to channel3 from frame4 to the end of the buffer
+        analogWriteFrame(3,6,0.2); //write 0.2 to channel3 only on frame 6
+        //this buffer for channel 3 will look like this:  1 1 1 1 0.1 0.1 0.2 0.1 
+        //the next buffers for channel 3 will be filled up with 0.1 ....
+        //DIGITAL channels
+        digitalWrite(P8_07,0,GPIO_HIGH); //sets all the frames  to HIGH for channel 0
+        digitalWriteFrame(P8_07,4,GPIO_LOW); //only frame 4 will be LOW  for channel 0
+        // in this buffer the frames of channel 0 will look like this: 1 1 1 1 0 1 1 1 ...... 1 
+        // in the next buffer each frame of channel 0 will be initialized to 1 (the last value of this buffer)
+        digitalWrite(P8_08,0,GPIO_HIGH);
+        digitalWrite(P8_08,2,GPIO_LOW);
+        digitalWrite(P8_08,4,GPIO_HIGH);
+        digitalWrite(P8_08,5,GPIO_LOW);
+        // in this buffer the frames of channel 1 will look like this: 1 1 0 0 1 0 0 0 .... 0
+        // in the next buffer each frame of channel 1 will be initialized to 0 (the last value of this buffer)
 	}
-	for(int i=1; i<gNumDigitalFrames; i++)
-		digitalWriteAll(i, GPIO_LOW); //write all channels on the given frame. Initialize them to zero
-	digitalWrite(0, 4, GPIO_HIGH); // set pin 0 HIGH from the current frame to the end of the buffer
-	for(int n=0; n<numAnalogFrames; n++) {
-		for(int c=0; c<gNumAnalogChannels; c++)
-			AnalogWriteFrame(c,n,0); //set channel c on frame n to 0, equivalent to analogOut[n*numAnalogChannels+c]=0;
+	for(int n=0; n<numAudioFrames; n++){
+		for(int c=0; c<gNumAudioChannels; c++){
+			audioOut[n*gNumAudioChannels + c]=audioIn[n*gNumAudioChannels + c];
+		}
+        //use digital channels 2-5 to create a 4 bit binary counter
+        digital[n]=digital[n] & (~0b111100); // set to zero (GPIO_OUTPUT) the bits in the lower word
+        digital[n]=digital[n] & (~0b111100<<16); //initialize to zero the bits in the higher word (output value)
+        digital[n]=digital[n] | (n<<(16+2));  // set the bits in the higher word to the desired output value
 	}
-	AnalogWrite(0,3,0.2); //set channel 0 to 0.2 from frame 3 onwards ...
-	AnalogWrite(1,3,0.7); //set channel 1 to 0.7 from frame 3 onwards ...
-	AnalogWrite(2,6,0.5); //set channel 2 to 0.5 from frame 6 onwards ...
-	for(int n=0; n<numAudioFrames; n++){
-		printf("Digital frame %d: 0x%08x;\n",n,digital[n]);
+
+	for(int n=0; n<numAnalogFrames; n++){
+    	analogWriteFrame(1,n,(gCountFrames&8191)/8192.0); // writes a single frame. channel 1 is a ramp that follows gCountFrames
+    	analogWriteFrame(2,n,analogRead(2,n)); // writes a single frame. channel2 is just a passthrough
+//		rt_printf("Analog out frame %d :",n);
+//		for(int c=0; c<gNumAnalogChannels; c++)
+//			rt_printf("%.1f ",analogOut[n*gNumAnalogChannels + c]);
+//		rt_printf("\n");
+		gCountFrames++;
 	}
-	for(int n=0; n<numAnalogFrames; n++){
-		printf("Analog out frame %d :",n);
-		for(int c=0; c<gNumAnalogChannels; c++)
-			printf("%.1f ",analogOut[n*gNumAnalogChannels + c]);
-		printf("\n");
-	}
+	return;
+
 }
 // cleanup_render() is called once at the end, after the audio has stopped.
 // Release any resources that were allocated in initialise_render().