Mercurial > hg > beaglert
comparison include/Scope.h @ 108:3068421c0737 ultra-staging
Merged default into ultra-staging
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Tue, 18 Aug 2015 00:35:15 +0100 |
parents | 836052c86e1e |
children | ad8a93cd7c39 |
comparison
equal
deleted
inserted
replaced
54:d3f869b98147 | 108:3068421c0737 |
---|---|
1 //scope.cpp | |
2 #include <BeagleRT.h> | |
3 #include <rtdk.h> | |
4 #include <cmath> | |
5 #include <UdpClient.h> | |
6 | |
7 #define BUILD_FOR_UDPRECEIVE_PLUGIN | |
8 #define NETWORK_AUDIO_BUFFER_SIZE 302 | |
9 struct networkAudio{ | |
10 int timestamp; | |
11 int currentBuffer; | |
12 int index; | |
13 float buffers[2][NETWORK_AUDIO_BUFFER_SIZE]; | |
14 int doneOnTime; | |
15 bool toBeSent; | |
16 UdpClient udpClient; | |
17 }; | |
18 | |
19 #define NUM_SCOPE_CHANNELS 6 | |
20 | |
21 static void SendScopeData(); | |
22 | |
23 class Scope { | |
24 int sampleCount; | |
25 float sampleRate; | |
26 AuxiliaryTask scopeTask; | |
27 public: | |
28 int numChannels; | |
29 networkAudio channel[NUM_SCOPE_CHANNELS]; | |
30 Scope(){ | |
31 numChannels = NUM_SCOPE_CHANNELS; | |
32 sampleCount = 0; | |
33 #ifdef BUILD_FOR_UDPRECEIVE_PLUGIN | |
34 char server[]="192.168.7.1"; | |
35 #else | |
36 char server[]="127.0.0.1"; | |
37 #endif /* BUILD_FOR_UDPRECEIVE_PLUGIN */ | |
38 printf("Sending messages to : %s\n", server); | |
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 }; | |
81 | |
82 Scope* gOscilloscopeInstance; | |
83 | |
84 void Scope::setup(float _sampleRate){ | |
85 sampleRate = _sampleRate; | |
86 gOscilloscopeInstance = this; | |
87 scopeTask = BeagleRT_createAuxiliaryTask(*SendScopeData, 98, "transmit-receive-audio"); | |
88 } | |
89 | |
90 //Scope scope; | |
91 | |
92 static void SendScopeData(){ | |
93 for(int n=0; n<gOscilloscopeInstance->numChannels; n++){ | |
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 } |