test-EphysBoard.C
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "Component/ModelManager.H"
00039 #include "Devices/Serial.H"
00040 #include "Util/Types.H"
00041 #include "Util/log.H"
00042 #include "GUI/XWindow.H"
00043 #include "Image/Image.H"
00044 #include "Image/DrawOps.H"
00045 #include "Transport/FrameInfo.H"
00046 #include "Media/FrameSeries.H"
00047 #include "Transport/FrameOstream.H"
00048 #include "Util/Timer.H"
00049 #include <unistd.h>
00050 #include <stdio.h>
00051 #include <signal.h>
00052 #include <fcntl.h>
00053 #include <errno.h>
00054 #include <sys/ioctl.h>
00055 #include <math.h>
00056 #include <fstream>
00057 #include <queue>
00058 #include <pthread.h>
00059 #include <sys/soundcard.h>
00060
00061 std::queue<unsigned char*> dataCache;
00062 std::vector<float> signalWin;
00063 long int cnt = 0;
00064 std::ofstream outFile("Ephysdata.dat");
00065 pthread_mutex_t qmutex_data;
00066 pthread_mutex_t mutexDone;
00067 bool done=0;
00068
00069
00070
00071 static void* getData(void *)
00072 {
00073
00074
00075 int numBytes = 5;
00076 unsigned char frame[numBytes-2];
00077 char start[2];
00078
00079 std::string USBDEV = "/dev/ttyACM0";
00080
00081 int fd;
00082 int status;
00083
00084
00085 if(!(fd = open(USBDEV.c_str(), O_RDONLY)))
00086 perror("error opening device");
00087
00088 if(ioctl(fd, TIOCMGET, &status) == -1)
00089 LFATAL("TIOCMGET faileed: %s\n",strerror(errno));
00090
00091 int fReturn;
00092 Timer tim;
00093 tim.reset();
00094 while(tim.getSecs() < 1)
00095 {
00096 start[0] = '1';
00097
00098
00099 while (start[0] != '0')
00100 {
00101 fReturn = read(fd,start,1);
00102
00103 }
00104
00105
00106 fReturn = read(fd,frame,numBytes-1);
00107 if (fReturn == (numBytes-1))
00108 {
00109 pthread_mutex_lock(&qmutex_data);
00110 frame[numBytes-1] = '\0';
00111 dataCache.push(frame);
00112
00113 pthread_mutex_unlock(&qmutex_data);
00114 printf("framepush: %s\n",frame);
00115 }
00116 else
00117 {
00118
00119 }
00120 }
00121
00122 pthread_mutex_lock(&mutexDone);
00123 done = true;
00124 pthread_mutex_unlock(&mutexDone);
00125 return NULL;
00126 }
00127
00128
00129
00130
00131 void processFrame(unsigned char *frame)
00132 {
00133 int chunk = 4;
00134 unsigned char block[4];
00135
00136 std::string sFrame = std::string(reinterpret_cast<const char*> (frame));
00137
00138 printf("\n%s",sFrame.c_str());
00139 for (int i=0; i < (int)sFrame.length()-1; i+=4)
00140 {
00141
00142 for (int kk=0; kk<chunk; kk++)
00143 {
00144 unsigned char iFrame = sFrame[i+kk];
00145 if (kk <=1)
00146 iFrame = iFrame - 97;
00147 else
00148 iFrame = iFrame - 65;
00149
00150 block[kk] = iFrame;
00151
00152 }
00153
00154 unsigned int data = (block[0] << 12 | block[1] << 8) |
00155 (block[2] << 4 | block[3]);
00156
00157
00158 if (cnt > 1000)
00159 {
00160 signalWin.erase(signalWin.begin());
00161 cnt = 0;
00162 }
00163 else
00164 cnt++;
00165
00166 float dataF = data*(4.096F/65536);
00167
00168
00169 outFile << dataF << std::endl;
00170 signalWin.push_back(dataF);
00171 }
00172
00173 }
00174
00175
00176
00177 void processFrame2(unsigned char *frame)
00178 {
00179
00180
00181 }
00182
00183
00184
00185 static int submain(int argc, char **argv)
00186 {
00187
00188 ModelManager manager("Test EphysBoard");
00189
00190 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager));
00191 manager.addSubComponent(ofs);
00192
00193 Timer tim;
00194
00195
00196 if (manager.parseCommandLine(argc, argv, "Ephys board--", 0, 0) == false)
00197 return(1);
00198
00199
00200 manager.start();
00201 usleep(1000);
00202
00203 tim.reset();
00204
00205 pthread_t acqThread;
00206 int rc;
00207 rc = pthread_create(&acqThread,NULL,&getData,NULL);
00208 if(rc)
00209 {
00210 printf("\n ERROR: return code from pthread_create is %d \n", rc);
00211 exit(1);
00212 }
00213
00214 printf("\n Created new thread (%d) ... \n", (int)acqThread);
00215
00216 Image<PixRGB<byte> > itsPlot(700,500,ZEROS);
00217
00218 bool imDone;
00219 pthread_mutex_lock(&mutexDone);
00220 imDone = done;
00221 pthread_mutex_unlock(&mutexDone);
00222
00223 while(!imDone)
00224 {
00225
00226 unsigned char *frame=NULL;
00227
00228 pthread_mutex_lock(&qmutex_data);
00229 if(dataCache.size())
00230 {
00231
00232 frame = dataCache.front();
00233 dataCache.pop();
00234 }
00235 pthread_mutex_unlock(&qmutex_data);
00236
00237 if (frame != NULL)
00238 {
00239
00240 processFrame(frame);
00241 itsPlot = linePlot(signalWin, 600,500,0.0F,0.0F, "Signal","voltage V",
00242 "time/ms",PixRGB<byte>(0,0,0),
00243 PixRGB<byte>(255,255,255),5,0);
00244 }
00245 else
00246 usleep(1000);
00247
00248
00249
00250 pthread_mutex_lock(&mutexDone);
00251 imDone = done;
00252 pthread_mutex_unlock(&mutexDone);
00253 }
00254 pthread_exit(NULL);
00255
00256
00257 manager.stop();
00258
00259
00260 return 0;
00261
00262 }
00263
00264 extern "C" int main(const int argc, char** argv)
00265 {
00266 try
00267 {
00268 return submain(argc, argv);
00269 }
00270 catch (...)
00271 {
00272 REPORT_CURRENT_EXCEPTION;
00273 }
00274
00275 return 1;
00276 }
00277
00278
00279
00280
00281
00282