Mercurial > hg > beaglert
diff include/Scope.h @ 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 |
line wrap: on
line diff
--- 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 */