comparison 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
comparison
equal deleted inserted replaced
107:836052c86e1e 109:ad8a93cd7c39
1 //scope.cpp 1 //scope.cpp
2 #ifndef SCOPE_H_
3 #define SCOPE_H_
4
2 #include <BeagleRT.h> 5 #include <BeagleRT.h>
3 #include <rtdk.h> 6 #include <rtdk.h>
4 #include <cmath> 7 #include <cmath>
5 #include <UdpClient.h> 8 #include <UdpClient.h>
6 9
7 #define BUILD_FOR_UDPRECEIVE_PLUGIN 10 #define BUILD_FOR_UDPRECEIVE_PLUGIN
8 #define NETWORK_AUDIO_BUFFER_SIZE 302 11 #define NETWORK_AUDIO_BUFFER_SIZE 302
9 struct networkAudio{ 12
13 struct NetworkBuffer{
10 int timestamp; 14 int timestamp;
11 int currentBuffer; 15 int channelNumber;
16 int activeBuffer;
12 int index; 17 int index;
13 float buffers[2][NETWORK_AUDIO_BUFFER_SIZE]; 18 float buffers[2][NETWORK_AUDIO_BUFFER_SIZE];
14 int doneOnTime; 19 bool doneOnTime;
15 bool toBeSent; 20 bool readyToBeSent;
21 int headerLength=2;
16 UdpClient udpClient; 22 UdpClient udpClient;
17 }; 23 };
18 24
19 #define NUM_SCOPE_CHANNELS 6 25 #define NUM_SCOPE_CHANNELS 6
20 26
21 static void SendScopeData(); 27 class NetworkIO {
22
23 class Scope {
24 int sampleCount; 28 int sampleCount;
25 float sampleRate; 29 float sampleRate;
26 AuxiliaryTask scopeTask; 30 AuxiliaryTask scopeTask;
31 int port;
27 public: 32 public:
28 int numChannels; 33 NetworkBuffer channel;
29 networkAudio channel[NUM_SCOPE_CHANNELS]; 34 NetworkIO();
30 Scope(){ 35 ~NetworkIO();
31 numChannels = NUM_SCOPE_CHANNELS; 36 void setup(float aSampleRate);
32 sampleCount = 0; 37 void setup(float aSampleRate, int aChannelNumber);
33 #ifdef BUILD_FOR_UDPRECEIVE_PLUGIN 38 void sendData();
34 char server[]="192.168.7.1"; 39 void log(float value);
35 #else 40 void setPort(int aPort);
36 char server[]="127.0.0.1"; 41 int getPort();
37 #endif /* BUILD_FOR_UDPRECEIVE_PLUGIN */ 42 void setChannelNumber(int aChannelNumber);
38 printf("Sending messages to : %s\n", server); 43 int getChannelNumber();
39 for(int n=0; n<numChannels; n++){
40 channel[n].doneOnTime=1;
41 channel[n].index=2; //leave space for the heading message (channel, timestamp)
42 channel[n].timestamp=0;
43 channel[n].currentBuffer=0;
44 channel[n].toBeSent=false;
45 channel[n].udpClient.setServer(server);
46 #ifdef BUILD_FOR_UDPRECEIVE_PLUGIN
47 channel[n].udpClient.setPort(9999+n);
48 #else
49 channel[n].udpClient.setPort(9999);
50 #endif /* BUILD_FOR_UDPRECEIVE_PLUGIN */
51 }
52 }
53 void setup(float _sampleRate);
54 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){
55
56 for(int j=0; j<numChannels; j++){
57 if(channel[j].index==(NETWORK_AUDIO_BUFFER_SIZE)){ // when the buffer is ready ...
58 channel[j].buffers[channel[j].currentBuffer][0] = (float)j;
59 channel[j].buffers[channel[j].currentBuffer][1] = (float)channel[j].timestamp;
60 channel[j].toBeSent=true;
61 channel[j].index=2; //reset the counter
62 if(channel[j].doneOnTime==0)
63 rt_printf("Network buffer underrun :-{\n");
64 channel[j].timestamp=sampleCount;
65 channel[j].currentBuffer=!channel[j].currentBuffer; //switch buffer
66 channel[j].doneOnTime=0;
67 BeagleRT_scheduleAuxiliaryTask(scopeTask); //send the buffer
68 }
69 }
70
71 channel[0].buffers[channel[0].currentBuffer][channel[0].index++]=channel1;
72 channel[1].buffers[channel[1].currentBuffer][channel[1].index++]=channel2;
73 channel[2].buffers[channel[2].currentBuffer][channel[2].index++]=channel3;
74 channel[3].buffers[channel[3].currentBuffer][channel[3].index++]=channel4;
75 channel[4].buffers[channel[4].currentBuffer][channel[4].index++]=channel5;
76 channel[5].buffers[channel[5].currentBuffer][channel[5].index++]=channel6;
77
78 sampleCount++;
79 }
80 }; 44 };
81 45
82 Scope* gOscilloscopeInstance; 46 class Scope {
83 47 NetworkIO *channels;
84 void Scope::setup(float _sampleRate){ 48 int numChannels;
85 sampleRate = _sampleRate; 49 void deallocate();
86 gOscilloscopeInstance = this; 50 public:
87 scopeTask = BeagleRT_createAuxiliaryTask(*SendScopeData, 98, "transmit-receive-audio"); 51 Scope(int aNumChannels);
88 } 52 ~Scope();
89 53 void log(int channel, float value);
90 //Scope scope; 54 void setup(float sampleRate);
91 55 void sendData();
92 static void SendScopeData(){ 56 };
93 for(int n=0; n<gOscilloscopeInstance->numChannels; n++){ 57 #endif /* SCOPE_H */
94 if(gOscilloscopeInstance->channel[n].toBeSent){
95 gOscilloscopeInstance->channel[n].toBeSent=false;
96 gOscilloscopeInstance->channel[n].udpClient.send(
97 gOscilloscopeInstance->channel[n].buffers[!gOscilloscopeInstance->channel[n].currentBuffer],
98 NETWORK_AUDIO_BUFFER_SIZE*sizeof(float)
99 );
100 gOscilloscopeInstance->channel[n].doneOnTime=1;
101 }
102 }
103 }