libFAUDES
Sections
Index
|
iomonitor.cppGo to the documentation of this file.00001 /** @file iomonitor.cpp Test utility for IO devices 00002 00003 00004 This tutorial demonstrates elementary access to external signals 00005 via the class faudes::vDevice. It can be used as a test uitility 00006 for device configuration. 00007 00008 00009 @ingroup Tutorials 00010 00011 @include iomonitor.cpp 00012 00013 */ 00014 00015 00016 #include "libfaudes.h" 00017 #include "iop_vdevice.h" 00018 #include "iop_sdevice.h" 00019 00020 #include <sys/time.h> 00021 #include <signal.h> 00022 00023 00024 00025 // fix windows/mingw definition 00026 #ifdef FAUDES_WINEXTRA 00027 #define sleep(sec) Sleep((sec) * 1000) 00028 #define usleep(usec) Sleep((usec) / 1000) 00029 #define SIGQUIT SIGBREAK 00030 #define SIGHUP SIGBREAK 00031 #define strsignal(sig) "unknown" 00032 #endif 00033 00034 using namespace faudes; 00035 00036 /////////////////////////////////////////////////////// 00037 // clean exit on signals 00038 00039 // iomonitor clean-up on exit 00040 void iomonitor_exit(void); 00041 00042 // signal handler recursion flag 00043 volatile sig_atomic_t signal_in_progress = 0; 00044 00045 // signal handler to stop devices 00046 void catch_signal(int sig) { 00047 // detect recursion, pass on 00048 if(signal_in_progress) raise(sig); 00049 signal_in_progress = 1; 00050 // report 00051 std::cerr << "iomonitor: signal: " << strsignal(sig) << std::endl; 00052 // call my exit function 00053 iomonitor_exit(); 00054 // re-install default handler 00055 signal(sig, SIG_DFL); 00056 // pass on signal 00057 raise(sig); 00058 } 00059 00060 // iomonitor clean-up on exit 00061 void iomonitor_exit(void) { 00062 // stop all devices 00063 vDevice::StopAll(); 00064 } 00065 00066 00067 /////////////////////////////////////////////////////// 00068 //Basic handling 00069 00070 // list all known events 00071 void ListEvents(const vDevice* dev) { 00072 std::cout<< "% ###############################################################" << std::endl; 00073 std::cout<< "% # InputEvents" << std::endl; 00074 dev->Inputs().Write(); 00075 std::cout<< "% ###############################################################" << std::endl; 00076 std::cout<< "% # OutputEvents " << std::endl; 00077 dev->Outputs().Write(); 00078 std::cout<< "% ###############################################################" << std::endl; 00079 } 00080 00081 00082 /////////////////////////////////////////////////////// 00083 // Query Time 00084 00085 // read time 00086 void ReadTime(vDevice* dev) { 00087 00088 // read and report 00089 std::cout << "% ###############################################################" << std::endl; 00090 std::cout << "% # ReadTime: current time in ftu: " << dev->CurrentTime() << std::endl; 00091 std::cout << "% # ReadTime: using scale: " << dev->TimeScale() << std::endl; 00092 00093 } 00094 00095 00096 /////////////////////////////////////////////////////// 00097 //Signal-I/O 00098 00099 // read signal value 00100 void ReadSignalValue(sDevice* dev) { 00101 // declare static buffer 00102 static bool* samplePrevious=0; 00103 static bool* sampleCurrent=0; 00104 static int sampleSize=-1; 00105 // allocate memory for buffer 00106 if(sampleSize != dev->MaxBitAddress()) { 00107 sampleSize = dev->MaxBitAddress(); 00108 if(samplePrevious!=0) delete samplePrevious; 00109 if(sampleCurrent!=0) delete sampleCurrent; 00110 samplePrevious= new bool[sampleSize]; 00111 sampleCurrent= new bool[sampleSize]; 00112 } 00113 // read and report 00114 std::cout << "% ###############################################################" << std::endl; 00115 std::cout << "% # ReadValue: current input reading: " << std::endl; 00116 for(int bit=0; bit<sampleSize; bit++) { 00117 samplePrevious[bit]=sampleCurrent[bit]; 00118 } 00119 for(int bit=0; bit<sampleSize; bit++) { 00120 sampleCurrent[bit]=dev->ReadSignal(bit);} 00121 for(int bit=0; bit<sampleSize; bit++) { 00122 std::cout<< "@"<< bit << ":" << sampleCurrent[bit] << " "; 00123 if((bit%8)==7) std::cout << std::endl; 00124 } 00125 std::cout <<"% # ReadValue: edges wrt previous reading: " << std::endl; 00126 int cnt =0; 00127 for(int bit=0; bit<sampleSize; bit++) { 00128 if(samplePrevious[bit]!=sampleCurrent[bit]) { 00129 std::cout<< "@"<< bit << ":" << sampleCurrent[bit] << " "; 00130 if((cnt%8)==7) std::cout << std::endl; 00131 cnt+=1; 00132 } 00133 } 00134 std::cout << std::endl 00135 << "% ###############################################################" << std::endl; 00136 } 00137 00138 00139 // write signal value 00140 void WriteSignalValue(sDevice* dev) { 00141 while(1) { 00142 int bit, val; 00143 std::cout<<"WriteValue: enter bit address (or -1 to exit): "; 00144 std::cin>>bit; 00145 if(bit<0) return; 00146 std::cout<<"WriteValue: enter value (or -1 to exit): "; 00147 std::cin>>val; 00148 if(val<0) return; 00149 std::cout<<"WriteValue: setting output " << bit << " to value " << val << std::endl; 00150 dev->WriteSignal(bit,val!=0); 00151 } 00152 } 00153 00154 ////////////////////////////////////////////////////////////// 00155 //Event-handling 00156 00157 // execute output event 00158 void WriteOutputEvent(vDevice* dev) { 00159 while(true) { 00160 std::cout<<"WriteOutput: enter event by name (or \"exit\"): "; 00161 std::string testEvent; 00162 std::cin>>testEvent; 00163 if (testEvent == "exit" )break; 00164 faudes::Idx fev= dev->Outputs().Index(testEvent); 00165 dev->WriteOutput(fev); 00166 } 00167 } 00168 00169 00170 // poll input events 00171 void PollInputEvent(vDevice* dev){ 00172 std::cout<<"ReadInputs: time (secs) to monitor input events: "; 00173 int time_all; 00174 std::cin>>time_all; 00175 time_all*=1000; // convert to msecs 00176 std::cout<<"ReadEvents: time (msecs) to sleep between two polls: "; 00177 int time_delta; 00178 std::cin>>time_delta; 00179 // reset all input data so far 00180 dev->Reset(); 00181 // report performace, part 1 00182 struct timeval time_start, time_stop; 00183 gettimeofday(&time_start,NULL); 00184 00185 // loop until time is up 00186 for(int time_togo=time_all; time_togo>0; time_togo-=time_delta) { 00187 Idx sev=dev->ReadInput(); 00188 if(sev!=0) 00189 std::cout<<"ReadInputs: event " << dev->Inputs().SymbolicName(sev) << std::endl; 00190 usleep(1000*time_delta); 00191 }; 00192 00193 // report performance, part2 00194 gettimeofday(&time_stop,NULL); 00195 double time_diff= ( time_stop.tv_sec - time_start.tv_sec 00196 + (time_stop.tv_usec - time_start.tv_usec) / 1000000.0 ) * 1000.0; 00197 double time_sleep=time_all; 00198 std::cout << "# performance: overall time: " << time_diff << "ms" << std::endl; 00199 std::cout << "# performance: sleep time: " << time_sleep << "ms" << std::endl; 00200 std::cout << "# performance: process time per loop: " << 00201 (time_diff - time_sleep) / (time_all/time_delta)<< "ms" << std::endl; 00202 } 00203 00204 // WaitInputEvent(vDevice* dev) 00205 void WaitInputEvent(vDevice* dev){ 00206 std::cout<<"Enter max. duration (in faudes-time units) to wait for a input-event to occur"<<std::endl; 00207 std::cout<<"Note: 1 faudes-time unit is configured to " << dev->TimeScale() << " msecs" <<std::endl; 00208 tpTime::Type duration; 00209 std::cin>>duration; 00210 EventSet occuredEvents; 00211 //wait for input-event to occur 00212 dev->WaitInputs(duration); 00213 //identify occured events 00214 while(Idx sev=dev->ReadInput()) occuredEvents.Insert(sev); 00215 //report occured events 00216 if(!occuredEvents.Empty()) std::cout << occuredEvents.ToString(); 00217 else std::cout<<"No event recognized"; 00218 00219 } 00220 00221 00222 ////////////////////////////////////////////////////////////// 00223 //User-Interface loop 00224 00225 int main(int argc, char* argv[]) { 00226 00227 // install my signal handler 00228 signal(SIGTERM, catch_signal); 00229 signal(SIGINT, catch_signal); 00230 signal(SIGQUIT, catch_signal); 00231 signal(SIGHUP, catch_signal); 00232 signal(SIGABRT, catch_signal); 00233 00234 // first argument has to be filename 00235 if(argc!=2) { 00236 std::cerr << "iomonitor: " << FDVersionString() << std::endl; 00237 std::cerr << "usage: iomonitor <device-file>" << std::endl; 00238 return -1; 00239 } 00240 00241 //initialize vDevice 00242 FD_DH("Initialize vDevice"); 00243 vDevice* dev; 00244 dev=vDevice::FromFile(std::string(argv[1])); 00245 sDevice* sdev=dynamic_cast<sDevice*>(dev); 00246 00247 //start vDevice 00248 dev->Start(); 00249 00250 //loop until device is up 00251 while(dev->Status()!=vDevice::Up){;} 00252 00253 // loop until user terminates 00254 while(true) { 00255 00256 // set up console userinterface 00257 std::cout << std::endl; 00258 std::cout << "# iomonitor commands are:" << std::endl; 00259 std::cout << "# read faudes events via wait (re)" << std::endl; 00260 std::cout << "# read faudes events via polling (rep)" << std::endl; 00261 if(sdev) std::cout << "# read signal value by bitaddress (rv)" << std::endl; 00262 std::cout << "# write faudes events (we)" << std::endl; 00263 if(sdev) std::cout << "# write signal value by bitaddress (wv)" << std::endl; 00264 std::cout << "# device time (time)" << std::endl; 00265 std::cout << "# reset device (reset)" << std::endl; 00266 std::cout << "# list all device events (list) " << std::endl; 00267 std::cout << "# exit (exit) " << std::endl; 00268 std::cout << ">"; 00269 // get user-choice 00270 std::string choice; 00271 std::cin >> choice; 00272 //execute user-choice 00273 if(choice=="exit") break; 00274 if(choice=="reset") {dev->Reset();}; 00275 if(choice=="time") ReadTime(dev); 00276 if(choice=="list") ListEvents(dev); 00277 if(sdev && choice=="rv") ReadSignalValue(sdev); //note: sdev->dev 00278 if(choice=="rp") PollInputEvent(dev); 00279 if(choice=="re") WaitInputEvent(dev); 00280 if(sdev && choice=="wv") WriteSignalValue(sdev); //note: sdev->dev 00281 if(choice=="we") WriteOutputEvent(dev); 00282 } 00283 00284 std::cout << "# iomonitor: done " << std::endl; 00285 std::cout << "##########################################" << std::endl; 00286 00287 00288 FD_DH("Stopping vDevice") ; 00289 //stop background thread 00290 00291 dev->Stop(); 00292 00293 return 0; 00294 } 00295 00296 00297 |
libFAUDES 2.20d --- 2011.04.26 --- c++ source docu by doxygen