00001 /*!@file AppDevices/test-EphysBoard.C receive data from EphysBoard and plot it continuously*/ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Farhan Baluch <fbaluch@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/AppDevices/test-EphysBoard.C $ 00035 // $Id: $ 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 /***** static void* getData(void *arg) gets data fast over USB**/ 00070 // ###################################################################### 00071 static void* getData(void *) 00072 { 00073 //int my_fd = (intptr_t)fd; 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 //setup a new file read manually 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 //wait for sync 00099 while (start[0] != '0') 00100 { 00101 fReturn = read(fd,start,1); 00102 // printf("\nwaiting sync %s bytes returned %d",start,fReturn); 00103 } 00104 ///printf("synced"); 00105 //now read bytes 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 // printf("read %d bytes start is %s",fReturn,start); 00113 pthread_mutex_unlock(&qmutex_data); 00114 printf("framepush: %s\n",frame); 00115 } 00116 else 00117 { 00118 // printf("bad read"); 00119 } 00120 } 00121 00122 pthread_mutex_lock(&mutexDone); 00123 done = true; 00124 pthread_mutex_unlock(&mutexDone); 00125 return NULL; 00126 } 00127 00128 // ###################################################################### 00129 //this process frame assumes frame starts with 0 and in block of 4 bytes 00130 //msb1 msb2 lsb1 lsb2 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 //LINFO("byte %d",i); 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 //LINFO("byte %d: %s",i,resultStr); 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 //LINFO("time %f, time/ms %f, val:%d,vol:%f",tim.getSecs(), 00168 // tim.getMilliSecs(),data,dataF); 00169 outFile << dataF << std::endl; 00170 signalWin.push_back(dataF); 00171 } 00172 00173 } 00174 00175 // ###################################################################### 00176 //this process frame assumes frame starts with 0 and in block of 2 bytes 00177 void processFrame2(unsigned char *frame) 00178 { 00179 00180 00181 } 00182 00183 00184 // ###################################################################### 00185 static int submain(int argc, char **argv) 00186 { 00187 // Instantiate a ModelManager: 00188 ModelManager manager("Test EphysBoard"); 00189 00190 nub::ref<OutputFrameSeries> ofs(new OutputFrameSeries(manager)); 00191 manager.addSubComponent(ofs); 00192 00193 Timer tim; 00194 00195 // Parse command-line: 00196 if (manager.parseCommandLine(argc, argv, "Ephys board--", 0, 0) == false) 00197 return(1); 00198 00199 // let's get all our ModelComponent instances started: 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) /* could not create thread */ 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 //printf("here"); 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 //ofs->writeRGB(itsPlot,"Output",FrameInfo("output",SRC_POS)); 00250 pthread_mutex_lock(&mutexDone); 00251 imDone = done; 00252 pthread_mutex_unlock(&mutexDone); 00253 } 00254 pthread_exit(NULL); 00255 00256 // stop all our ModelComponents 00257 manager.stop(); 00258 00259 // all done! 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 /* So things look consistent in everyone's emacs... */ 00279 /* Local Variables: */ 00280 /* indent-tabs-mode: nil */ 00281 /* End: */ 00282