changeset 109:ad8a93cd7c39 scope-refactoring

Working for single-channel scope, or NetworkIO
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 18 Aug 2015 14:53:16 +0100
parents 836052c86e1e
children fb56681ab1d6
files .cproject include/Scope.h projects/scope/render.cpp
diffstat 3 files changed, 47 insertions(+), 96 deletions(-) [+]
line wrap: on
line diff
--- a/.cproject	Tue Aug 18 00:13:04 2015 +0100
+++ b/.cproject	Tue Aug 18 14:53:16 2015 +0100
@@ -31,8 +31,6 @@
 								<option id="gnu.cpp.compiler.option.include.paths.2031219124" name="Include paths (-I)" superClass="gnu.cpp.compiler.option.include.paths" valueType="includePath">
 									<listOptionValue builtIn="false" value="/usr/xenomai/include"/>
 									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/xenomai/include"/>
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/include"/>
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/include/xenomai/include"/>
 									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/ne10"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/BeagleRT/include}&quot;"/>
 								</option>
@@ -47,10 +45,8 @@
 								<option id="gnu.c.compiler.option.include.paths.358825414" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
 									<listOptionValue builtIn="false" value="/usr/xenomai/include"/>
 									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/xenomai/include"/>
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/include"/>
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/include/xenomai/include"/>
 									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/ne10"/>
-									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/BeagleRT/include}&quot;"/>
+									<listOptionValue builtIn="false" value="/BeagleRT/include"/>
 								</option>
 								<option id="gnu.c.compiler.option.misc.other.835792865" name="Other flags" superClass="gnu.c.compiler.option.misc.other" value="-c -fmessage-length=0 -Wpointer-arith -Wunused-result  -D_GNU_SOURCE -D_REENTRANT -D__XENO__ -std=gnu99" valueType="string"/>
 								<option id="gnu.c.compiler.option.warnings.allwarn.1145989346" name="All warnings (-Wall)" superClass="gnu.c.compiler.option.warnings.allwarn" value="true" valueType="boolean"/>
@@ -61,9 +57,10 @@
 								<option id="gnu.cpp.link.option.paths.462980690" name="Library search path (-L)" superClass="gnu.cpp.link.option.paths" valueType="libPaths">
 									<listOptionValue builtIn="false" value="/usr/xenomai/lib"/>
 									<listOptionValue builtIn="false" value="/usr/local/linaro/arm-linux-gnueabihf/include/xenomai/lib"/>
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/lib"/>
 									<listOptionValue builtIn="false" value="/usr/lib/arm-linux-gnueabihf"/>
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/lib/xenomai"/>
+									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/lib"/>
+									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/lib/xenomai"/>
+									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/xenomai/lib"/>
 								</option>
 								<option id="gnu.cpp.link.option.libs.139390951" name="Libraries (-l)" superClass="gnu.cpp.link.option.libs" valueType="libs">
 									<listOptionValue builtIn="false" value="rt"/>
@@ -82,10 +79,6 @@
 							</tool>
 							<tool command="arm-linux-gnueabihf-as" id="cdt.managedbuild.tool.gnu.assembler.exe.debug.37270610" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.exe.debug">
 								<option id="gnu.both.asm.option.include.paths.1403814918" name="Include paths (-I)" superClass="gnu.both.asm.option.include.paths" valueType="includePath">
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/include/xenomai/include"/>
-									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/xenomai/include"/>
-									<listOptionValue builtIn="false" value="/import/teaching/ECS732/arm-gcc/arm-linux-gnueabihf/include"/>
-									<listOptionValue builtIn="false" value="/usr/arm-linux-gnueabihf/include/ne10"/>
 									<listOptionValue builtIn="false" value="&quot;${workspace_loc:/BeagleRT/include}&quot;"/>
 								</option>
 								<inputType id="cdt.managedbuild.tool.gnu.assembler.input.1788972942" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
@@ -95,6 +88,7 @@
 					<sourceEntries>
 						<entry excluding="default_main.cpp|audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/>
 						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/>
+						<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="projects/scope"/>
 						<entry flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name="source"/>
 					</sourceEntries>
 				</configuration>
--- a/include/Scope.h	Tue Aug 18 00:13:04 2015 +0100
+++ b/include/Scope.h	Tue Aug 18 14:53:16 2015 +0100
@@ -1,4 +1,7 @@
 //scope.cpp
+#ifndef SCOPE_H_
+#define SCOPE_H_
+
 #include <BeagleRT.h> 
 #include <rtdk.h>
 #include <cmath>
@@ -6,98 +9,49 @@
 
 #define BUILD_FOR_UDPRECEIVE_PLUGIN
 #define NETWORK_AUDIO_BUFFER_SIZE 302
-struct networkAudio{
+
+struct NetworkBuffer{
 	int timestamp;
-	int currentBuffer;
+	int channelNumber;
+	int activeBuffer;
 	int index;
 	float buffers[2][NETWORK_AUDIO_BUFFER_SIZE]; 
-	int doneOnTime;
-	bool toBeSent;
+	bool doneOnTime;
+	bool readyToBeSent;
+	int headerLength=2;
 	UdpClient udpClient;
 };
 
 #define NUM_SCOPE_CHANNELS 6
 
-static void SendScopeData();
-
-class Scope {
+class NetworkIO {
     int sampleCount; 
     float sampleRate;
     AuxiliaryTask scopeTask;
+    int port;
   public:
-    int numChannels;
-    networkAudio channel[NUM_SCOPE_CHANNELS];
-    Scope(){
-        numChannels = NUM_SCOPE_CHANNELS;
-        sampleCount = 0;
-#ifdef BUILD_FOR_UDPRECEIVE_PLUGIN
-        char server[]="192.168.7.1";
-#else
-        char server[]="127.0.0.1";
-#endif /* BUILD_FOR_UDPRECEIVE_PLUGIN */
-        printf("Sending messages to : %s\n", server);
-        for(int n=0; n<numChannels; n++){
-            channel[n].doneOnTime=1;
-            channel[n].index=2; //leave space for the heading message (channel, timestamp)
-            channel[n].timestamp=0;
-            channel[n].currentBuffer=0;
-            channel[n].toBeSent=false;
-            channel[n].udpClient.setServer(server);
-#ifdef BUILD_FOR_UDPRECEIVE_PLUGIN
-            channel[n].udpClient.setPort(9999+n);
-#else
-            channel[n].udpClient.setPort(9999);
-#endif /* BUILD_FOR_UDPRECEIVE_PLUGIN */
-    	}
-    }
-    void setup(float _sampleRate);
-    void log(float channel1=0.0, float channel2=0.0, float channel3=0.0, float channel4=0.0, float channel5=0.0, float channel6=0.0){
-        
-        for(int j=0; j<numChannels; j++){
-			if(channel[j].index==(NETWORK_AUDIO_BUFFER_SIZE)){ // when the buffer is ready ...
-              channel[j].buffers[channel[j].currentBuffer][0] = (float)j;
-              channel[j].buffers[channel[j].currentBuffer][1] = (float)channel[j].timestamp;
-				channel[j].toBeSent=true;
-				channel[j].index=2; //reset the counter
-				if(channel[j].doneOnTime==0)
-					rt_printf("Network buffer underrun :-{\n");
-				channel[j].timestamp=sampleCount;
-				channel[j].currentBuffer=!channel[j].currentBuffer; //switch buffer
-				channel[j].doneOnTime=0;
-				BeagleRT_scheduleAuxiliaryTask(scopeTask); //send the buffer
-			}
-		}
-      
-      	channel[0].buffers[channel[0].currentBuffer][channel[0].index++]=channel1;
-      	channel[1].buffers[channel[1].currentBuffer][channel[1].index++]=channel2;
-      	channel[2].buffers[channel[2].currentBuffer][channel[2].index++]=channel3;
-      	channel[3].buffers[channel[3].currentBuffer][channel[3].index++]=channel4;
-      	channel[4].buffers[channel[4].currentBuffer][channel[4].index++]=channel5;
-      	channel[5].buffers[channel[5].currentBuffer][channel[5].index++]=channel6;
-
-	sampleCount++;
-    }
+    NetworkBuffer channel;
+    NetworkIO();
+    ~NetworkIO();
+    void setup(float aSampleRate);
+    void setup(float aSampleRate, int aChannelNumber);
+    void sendData();
+    void log(float value);
+    void setPort(int aPort);
+    int getPort();
+    void setChannelNumber(int aChannelNumber);
+    int getChannelNumber();
 };
 
-Scope* gOscilloscopeInstance;
-
-void Scope::setup(float _sampleRate){
-    sampleRate = _sampleRate;
-    gOscilloscopeInstance = this;
-    scopeTask = BeagleRT_createAuxiliaryTask(*SendScopeData, 98, "transmit-receive-audio");
-}
-
-//Scope scope; 
-
-static void SendScopeData(){
-    for(int n=0; n<gOscilloscopeInstance->numChannels; n++){
-		if(gOscilloscopeInstance->channel[n].toBeSent){
-			gOscilloscopeInstance->channel[n].toBeSent=false;
-			gOscilloscopeInstance->channel[n].udpClient.send(
-				gOscilloscopeInstance->channel[n].buffers[!gOscilloscopeInstance->channel[n].currentBuffer],
-				NETWORK_AUDIO_BUFFER_SIZE*sizeof(float)
-			);
-			gOscilloscopeInstance->channel[n].doneOnTime=1;
-		}
-	}
-}
+class Scope {
+	NetworkIO *channels;
+	int numChannels;
+	void deallocate();
+public:
+	Scope(int aNumChannels);
+	~Scope();
+	void log(int channel, float value);
+    void setup(float sampleRate);
+    void sendData();
+};
+#endif /* SCOPE_H */
--- a/projects/scope/render.cpp	Tue Aug 18 00:13:04 2015 +0100
+++ b/projects/scope/render.cpp	Tue Aug 18 14:53:16 2015 +0100
@@ -6,7 +6,7 @@
 float gFrequency1, gFrequency2;
 float gInverseSampleRate;
 
-Scope scope;    //create a scope object
+Scope scope(1);    //create a scope object with numChannels
 
 // initialise_render() is called once before the audio rendering starts.
 // Use it to perform any initialisation and allocation which is dependent
@@ -37,9 +37,10 @@
 
 void render(BeagleRTContext *context, void *userData)
 {
+	static int count=0;
 	for(unsigned int n = 0; n < context->audioFrames; n++) {
 	    
-	    float chn1 = sinf(gPhase1);
+		float chn1 = sinf(gPhase1);
 	    float chn2 = sinf(gPhase2);
 	    
 	    float chn3 = context->audioIn[n*2 + 0];
@@ -47,16 +48,17 @@
 	    
 	    float chn5 = context->analogIn[(int)n/2*8 + 0];
 	    float chn6 = context->analogIn[(int)n/2*8 + 1];
+		if((n&1)==0)
+		    scope.log(0, chn1);
 	    
 //	    scope.log(chn1, chn2, chn3, chn4, chn5, chn6);
-	    scope.log(chn1);
 	    //call this once every audio frame
 	    //takes six or fewer floats as parameters
 	    //first parameter becomes channel 1 etc
 	    //to view, click the 'oscilloscope' button on the toolbar while BeagleRT is NOT running
 	    //then click the big red button on the toolbar on this page
 	    
-	    gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate;
+	    gPhase1 += 2.0 * M_PI * gFrequency1 * gInverseSampleRate * ((count&4095)/4096.0+1);
 	    gPhase2 += 2.0 * M_PI * gFrequency2 * gInverseSampleRate;
 		if(gPhase1 > 2.0 * M_PI)
 			gPhase1 -= 2.0 * M_PI;
@@ -64,6 +66,7 @@
 			gPhase2 -= 2.0 * M_PI;
 		
 	}
+	count++;
 }
 
 // cleanup_render() is called once at the end, after the audio has stopped.