00001
00010 #include <EDDSourceLib/inc/EDDPlotDataSource.h>
00011 #include <EDDSourceLib/inc/EDDMsgLoop.h>
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <string.h>
00015
00016 #if defined(WIN32)
00017 #define snprintf sprintf_s
00018 #else
00019 using namespace std;
00020 #endif
00021
00026 static char* maFields[] =
00027 {
00028 "DESC",
00029 "X",
00030 "Y",
00031 "Z"
00032 };
00033
00045 tcEDDPlotDataSource::tcEDDPlotDataSource(char* apDesc, bool abIsContinuous, int anMaxContPoints)
00046 : tcEDDDataSource()
00047 , mnPlotNo(0)
00048 , mpOutputBuffer(0)
00049 , mnOutputBufferLen(0)
00050 , mbIsContinuous(abIsContinuous)
00051 , mnMaxContPoints(anMaxContPoints)
00052 {
00053
00054 strncpy(maDescription, apDesc, gnMaxStringLen);
00055 snprintf(maXLabel, gnMaxStringLen, "X");
00056 snprintf(maYLabel, gnMaxStringLen, "Y");
00057 snprintf(maZLabel, gnMaxStringLen, "Z");
00058
00059 for (int i = 0; i < 3; i++)
00060 {
00061 maScale[i] = 1.0;
00062 maOffset[i] = 0.0;
00063 }
00064
00065
00066 RegisterWithMsgLoop();
00067 }
00068
00072 tcEDDPlotDataSource::~tcEDDPlotDataSource(void)
00073 {
00074 UnRegisterWithMsgLoop();
00075 if (NULL != mpOutputBuffer)
00076 {
00077 delete [] mpOutputBuffer;
00078 }
00079 }
00080
00088 int
00089 tcEDDPlotDataSource::HandleMsg(tsEDDMsgHdr* apMsg)
00090 {
00091 int rv = EDDMSG_OK;
00092 switch(apMsg->mnMsgId)
00093 {
00094 case EDDMSGID_SOURCEDESCREQMSG:
00095 {
00096 char* maDescData[4];
00097 maDescData[0] = maDescription;
00098 maDescData[1] = maXLabel;
00099 maDescData[2] = maYLabel;
00100 maDescData[3] = maZLabel;
00101 SendSourceDescMsg(maFields, maDescData, 4);
00102 }
00103 break;
00104 case EDDMSGID_PLOTDESCREQMSG:
00105 break;
00106 default:
00107 rv = EDDMSG_UNHANDLED;
00108 break;
00109 }
00110 return rv;
00111 }
00112
00113
00114 int
00115 tcEDDPlotDataSource::SendPlotData(float* X, float* Y, int N)
00116 {
00117 int rv = 0;
00118
00119 rv = Send2DData(X,Y,N,sizeof(float), EDDPLOTMSG_FLOAT_2D);
00120
00121 return rv;
00122 }
00123
00124 int
00125 tcEDDPlotDataSource::SendPlotData(int32_t* X, int32_t* Y, int N)
00126 {
00127 int rv = 0;
00128
00129 rv = Send2DData(X,Y,N,sizeof(int32_t), EDDPLOTMSG_INT32_2D);
00130
00131 return rv;
00132 }
00133
00134 int
00135 tcEDDPlotDataSource::SendPlotData(int16_t* X, int16_t* Y, int N)
00136 {
00137 int rv = 0;
00138
00139 rv = Send2DData(X,Y,N,sizeof(int16_t), EDDPLOTMSG_INT16_2D);
00140
00141 return rv;
00142 }
00143
00144 int
00145 tcEDDPlotDataSource::SendPlotData(int8_t* X, int8_t* Y, int N)
00146 {
00147 int rv = 0;
00148
00149 rv = Send2DData(X,Y,N,sizeof(int8_t), EDDPLOTMSG_INT8_2D);
00150
00151 return rv;
00152 }
00153
00154 int
00155 tcEDDPlotDataSource::SendPlotData(uint32_t* X, uint32_t* Y, int N)
00156 {
00157 int rv = 0;
00158
00159 rv = Send2DData(X,Y,N,sizeof(uint32_t), EDDPLOTMSG_UINT32_2D);
00160
00161 return rv;
00162 }
00163
00164 int
00165 tcEDDPlotDataSource::SendPlotData(uint16_t* X, uint16_t* Y, int N)
00166 {
00167 int rv = 0;
00168
00169 rv = Send2DData(X,Y,N,sizeof(uint16_t), EDDPLOTMSG_UINT16_2D);
00170
00171 return rv;
00172 }
00173
00174 int
00175 tcEDDPlotDataSource::SendPlotData(uint8_t* X, uint8_t* Y, int N)
00176 {
00177 int rv = 0;
00178
00179 rv = Send2DData(X,Y,N,sizeof(uint8_t), EDDPLOTMSG_UINT8_2D);
00180
00181 return rv;
00182 }
00183
00184 int
00185 tcEDDPlotDataSource::SendPlotData(float* Y, int N)
00186 {
00187 int rv = 0;
00188
00189 rv = Send1DData(Y,N,sizeof(float), EDDPLOTMSG_FLOAT_1D);
00190
00191 return rv;
00192
00193 }
00194
00195 int
00196 tcEDDPlotDataSource::SendPlotData(uint32_t* Y, int N)
00197 {
00198 int rv = 0;
00199
00200 rv = Send1DData(Y,N,sizeof(uint32_t), EDDPLOTMSG_UINT32_1D);
00201
00202 return rv;
00203
00204 }
00205
00206 int
00207 tcEDDPlotDataSource::SendPlotData(uint16_t* Y, int N)
00208 {
00209 int rv = 0;
00210
00211 rv = Send1DData(Y,N,sizeof(uint16_t), EDDPLOTMSG_UINT16_1D);
00212
00213 return rv;
00214
00215 }
00216
00217 int
00218 tcEDDPlotDataSource::SendPlotData(uint8_t* Y, int N)
00219 {
00220 int rv = 0;
00221
00222 rv = Send1DData(Y,N,sizeof(uint8_t), EDDPLOTMSG_UINT8_1D);
00223
00224 return rv;
00225
00226 }
00227
00228 int
00229 tcEDDPlotDataSource::SendPlotData(int32_t* Y, int N)
00230 {
00231 int rv = 0;
00232
00233 rv = Send1DData(Y,N,sizeof(int32_t), EDDPLOTMSG_INT32_1D);
00234
00235 return rv;
00236
00237 }
00238
00239 int
00240 tcEDDPlotDataSource::SendPlotData(int16_t* Y, int N)
00241 {
00242 int rv = 0;
00243
00244 rv = Send1DData(Y,N,sizeof(int16_t), EDDPLOTMSG_INT16_1D);
00245
00246 return rv;
00247
00248 }
00249
00250 int
00251 tcEDDPlotDataSource::SendPlotData(int8_t* Y, int N)
00252 {
00253 int rv = 0;
00254
00255 rv = Send1DData(Y,N,sizeof(int8_t), EDDPLOTMSG_INT8_1D);
00256
00257 return rv;
00258
00259 }
00260
00261 int
00262 tcEDDPlotDataSource::Send2DData(void* Xdata, void* Ydata, int N, int WordSizeBytes, uint8_t anDataType)
00263 {
00264 int rv = 0;
00265 int points = 0;
00266 int points_to_send;
00267 int points_per_msg;
00268
00269
00270 if (!IsSubscribed())
00271 {
00272 return rv;
00273 }
00274
00275 BuildOutputBuffer();
00276
00277
00278 SendPlotDescriptor(N, anDataType);
00279
00280
00281 points_per_msg = (mnOutputBufferLen-sizeof(tsEDDPlotMsg))/WordSizeBytes/2;
00282
00283
00284 points = 0;
00285
00286 while(points < N)
00287 {
00288
00289 tsEDDPlotMsg* lpMsg = (tsEDDPlotMsg*)mpOutputBuffer;
00290 lpMsg->mnStartOffset = points;
00291 lpMsg->mnPlotNumber = mnPlotNo;
00292
00293
00294 points_to_send = N-points;
00295 if (points_to_send > points_per_msg) points_to_send = points_per_msg;
00296 lpMsg->mnNumPoints = points_to_send;
00297
00298
00299 char* lpData = (char*)(lpMsg+1);
00300 memcpy(lpData, (char*)Xdata+WordSizeBytes*points, WordSizeBytes*points_to_send);
00301 lpData += WordSizeBytes*points_to_send;
00302 memcpy(lpData, (char*)Ydata+WordSizeBytes*points, WordSizeBytes*points_to_send);
00303
00304
00305 BuildHeader(&lpMsg->msHeader, EDDMSGID_PLOTMSG, sizeof(tsEDDPlotMsg)+lpMsg->mnNumPoints*2*WordSizeBytes);
00306 tcEDDMsgLoop::GetInstance()->SendMsg(&lpMsg->msHeader);
00307
00308 points += points_to_send;
00309 }
00310 mnPlotNo++;
00311 return rv;
00312
00313
00314 }
00315
00316
00317 int
00318 tcEDDPlotDataSource::Send1DData(void* Ydata, int N, int WordSizeBytes, uint8_t anDataType)
00319 {
00320 int rv = 0;
00321 int points = 0;
00322 int points_to_send;
00323 int points_per_msg;
00324
00325
00326 if (!IsSubscribed())
00327 {
00328 return rv;
00329 }
00330
00331 BuildOutputBuffer();
00332
00333
00334 SendPlotDescriptor(N, anDataType);
00335
00336
00337 points_per_msg = (mnOutputBufferLen-sizeof(tsEDDPlotMsg))/WordSizeBytes;
00338
00339
00340 points = 0;
00341
00342 while(points < N)
00343 {
00344
00345 tsEDDPlotMsg* lpMsg = (tsEDDPlotMsg*)mpOutputBuffer;
00346 lpMsg->mnStartOffset = points;
00347 lpMsg->mnPlotNumber = mnPlotNo;
00348
00349
00350 points_to_send = N-points;
00351 if (points_to_send > points_per_msg) points_to_send = points_per_msg;
00352 lpMsg->mnNumPoints = points_to_send;
00353
00354
00355 char* lpData = (char*)(lpMsg+1);
00356 memcpy(lpData, (char*)Ydata+WordSizeBytes*points, WordSizeBytes*points_to_send);
00357
00358
00359 BuildHeader(&lpMsg->msHeader, EDDMSGID_PLOTMSG, sizeof(tsEDDPlotMsg)+lpMsg->mnNumPoints*WordSizeBytes);
00360 tcEDDMsgLoop::GetInstance()->SendMsg(&lpMsg->msHeader);
00361
00362 points += points_to_send;
00363 }
00364 mnPlotNo++;
00365 return rv;
00366
00367 }
00368
00377 int
00378 tcEDDPlotDataSource::SetLabel(teAxis aeAxis, char* apLabel)
00379 {
00380 char* lpPtr;
00381 switch (aeAxis)
00382 {
00383 case eeX:
00384 lpPtr = &maXLabel[0];
00385 break;
00386 case eeY:
00387 lpPtr = &maYLabel[0];
00388 break;
00389 case eeZ:
00390 default:
00391 lpPtr = &maZLabel[0];
00392 break;
00393 }
00394 strncpy(lpPtr, apLabel, gnMaxStringLen);
00395 return 0;
00396 }
00397
00405 int
00406 tcEDDPlotDataSource::SetDescription(char* apDesc)
00407 {
00408 strncpy(maDescription, apDesc, gnMaxStringLen);
00409 return 0;
00410 }
00411
00412 void
00413 tcEDDPlotDataSource::SendPlotDescriptor(int32_t anNumPoints, uint8_t anType)
00414 {
00415 tsEDDPlotDescMsg* lpDescMsg = (tsEDDPlotDescMsg*)mpOutputBuffer;
00416
00417 lpDescMsg->mnNumPoints = (mbIsContinuous) ? -mnMaxContPoints : anNumPoints;
00418 lpDescMsg->mnPlotNumber = mnPlotNo;
00419 lpDescMsg->mnPlotType = anType;
00420 lpDescMsg->mnOffset[0] = maOffset[0];
00421 lpDescMsg->mnOffset[1] = maOffset[1];
00422 lpDescMsg->mnOffset[2] = maOffset[2];
00423 lpDescMsg->mnScale[0] = maScale[0];
00424 lpDescMsg->mnScale[1] = maScale[1];
00425 lpDescMsg->mnScale[2] = maScale[2];
00426 BuildHeader(&lpDescMsg->msHeader, EDDMSGID_PLOTDESCMSG, sizeof(tsEDDPlotDescMsg));
00427 tcEDDMsgLoop::GetInstance()->SendMsg(&lpDescMsg->msHeader);
00428
00429 }
00430
00431 void
00432 tcEDDPlotDataSource::BuildOutputBuffer(void)
00433 {
00434 int maxlen = tcEDDMsgLoop::GetInstance()->GetMaxMessageLen(mnSourceId);
00435 if (maxlen < 0) maxlen = 1400;
00436 if (mnOutputBufferLen < maxlen)
00437 {
00438 mnOutputBufferLen = maxlen;
00439 if (NULL != mpOutputBuffer)
00440 {
00441 delete [] mpOutputBuffer;
00442 }
00443 mpOutputBuffer = new char[mnOutputBufferLen];
00444 }
00445 }
00446