libFAUDES
Sections
Index
|
iomonitor.cpp File ReferenceTest utility for IO devices. More... #include "libfaudes.h" #include "iop_vdevice.h" #include "iop_sdevice.h" #include <sys/time.h> #include <signal.h> Go to the source code of this file.
Detailed DescriptionTest utility for IO devices. This tutorial demonstrates elementary access to external signals via the class faudes::vDevice. It can be used as a test uitility for device configuration. /** @file iomonitor.cpp Test utility for IO devices This tutorial demonstrates elementary access to external signals via the class faudes::vDevice. It can be used as a test uitility for device configuration. @ingroup Tutorials @include iomonitor.cpp */ #include "libfaudes.h" #include "iop_vdevice.h" #include "iop_sdevice.h" #include <sys/time.h> #include <signal.h> // fix windows/mingw definition #ifdef FAUDES_WINEXTRA #define sleep(sec) Sleep((sec) * 1000) #define usleep(usec) Sleep((usec) / 1000) #define SIGQUIT SIGBREAK #define SIGHUP SIGBREAK #define strsignal(sig) "unknown" #endif using namespace faudes; /////////////////////////////////////////////////////// // clean exit on signals // iomonitor clean-up on exit void iomonitor_exit(void); // signal handler recursion flag volatile sig_atomic_t signal_in_progress = 0; // signal handler to stop devices void catch_signal(int sig) { // detect recursion, pass on if(signal_in_progress) raise(sig); signal_in_progress = 1; // report std::cerr << "iomonitor: signal: " << strsignal(sig) << std::endl; // call my exit function iomonitor_exit(); // re-install default handler signal(sig, SIG_DFL); // pass on signal raise(sig); } // iomonitor clean-up on exit void iomonitor_exit(void) { // stop all devices vDevice::StopAll(); } /////////////////////////////////////////////////////// //Basic handling // list all known events void ListEvents(const vDevice* dev) { std::cout<< "% ###############################################################" << std::endl; std::cout<< "% # InputEvents" << std::endl; dev->Inputs().Write(); std::cout<< "% ###############################################################" << std::endl; std::cout<< "% # OutputEvents " << std::endl; dev->Outputs().Write(); std::cout<< "% ###############################################################" << std::endl; } /////////////////////////////////////////////////////// // Query Time // read time void ReadTime(vDevice* dev) { // read and report std::cout << "% ###############################################################" << std::endl; std::cout << "% # ReadTime: current time in ftu: " << dev->CurrentTime() << std::endl; std::cout << "% # ReadTime: using scale: " << dev->TimeScale() << std::endl; } /////////////////////////////////////////////////////// //Signal-I/O // read signal value void ReadSignalValue(sDevice* dev) { // declare static buffer static bool* samplePrevious=0; static bool* sampleCurrent=0; static int sampleSize=-1; // allocate memory for buffer if(sampleSize != dev->MaxBitAddress()) { sampleSize = dev->MaxBitAddress(); if(samplePrevious!=0) delete samplePrevious; if(sampleCurrent!=0) delete sampleCurrent; samplePrevious= new bool[sampleSize]; sampleCurrent= new bool[sampleSize]; } // read and report std::cout << "% ###############################################################" << std::endl; std::cout << "% # ReadValue: current input reading: " << std::endl; for(int bit=0; bit<sampleSize; bit++) { samplePrevious[bit]=sampleCurrent[bit]; } for(int bit=0; bit<sampleSize; bit++) { sampleCurrent[bit]=dev->ReadSignal(bit);} for(int bit=0; bit<sampleSize; bit++) { std::cout<< "@"<< bit << ":" << sampleCurrent[bit] << " "; if((bit%8)==7) std::cout << std::endl; } std::cout <<"% # ReadValue: edges wrt previous reading: " << std::endl; int cnt =0; for(int bit=0; bit<sampleSize; bit++) { if(samplePrevious[bit]!=sampleCurrent[bit]) { std::cout<< "@"<< bit << ":" << sampleCurrent[bit] << " "; if((cnt%8)==7) std::cout << std::endl; cnt+=1; } } std::cout << std::endl << "% ###############################################################" << std::endl; } // write signal value void WriteSignalValue(sDevice* dev) { while(1) { int bit, val; std::cout<<"WriteValue: enter bit address (or -1 to exit): "; std::cin>>bit; if(bit<0) return; std::cout<<"WriteValue: enter value (or -1 to exit): "; std::cin>>val; if(val<0) return; std::cout<<"WriteValue: setting output " << bit << " to value " << val << std::endl; dev->WriteSignal(bit,val!=0); } } ////////////////////////////////////////////////////////////// //Event-handling // execute output event void WriteOutputEvent(vDevice* dev) { while(true) { std::cout<<"WriteOutput: enter event by name (or \"exit\"): "; std::string testEvent; std::cin>>testEvent; if (testEvent == "exit" )break; faudes::Idx fev= dev->Outputs().Index(testEvent); dev->WriteOutput(fev); } } // poll input events void PollInputEvent(vDevice* dev){ std::cout<<"ReadInputs: time (secs) to monitor input events: "; int time_all; std::cin>>time_all; time_all*=1000; // convert to msecs std::cout<<"ReadEvents: time (msecs) to sleep between two polls: "; int time_delta; std::cin>>time_delta; // reset all input data so far dev->Reset(); // report performace, part 1 struct timeval time_start, time_stop; gettimeofday(&time_start,NULL); // loop until time is up for(int time_togo=time_all; time_togo>0; time_togo-=time_delta) { Idx sev=dev->ReadInput(); if(sev!=0) std::cout<<"ReadInputs: event " << dev->Inputs().SymbolicName(sev) << std::endl; usleep(1000*time_delta); }; // report performance, part2 gettimeofday(&time_stop,NULL); double time_diff= ( time_stop.tv_sec - time_start.tv_sec + (time_stop.tv_usec - time_start.tv_usec) / 1000000.0 ) * 1000.0; double time_sleep=time_all; std::cout << "# performance: overall time: " << time_diff << "ms" << std::endl; std::cout << "# performance: sleep time: " << time_sleep << "ms" << std::endl; std::cout << "# performance: process time per loop: " << (time_diff - time_sleep) / (time_all/time_delta)<< "ms" << std::endl; } // WaitInputEvent(vDevice* dev) void WaitInputEvent(vDevice* dev){ std::cout<<"Enter max. duration (in faudes-time units) to wait for a input-event to occur"<<std::endl; std::cout<<"Note: 1 faudes-time unit is configured to " << dev->TimeScale() << " msecs" <<std::endl; tpTime::Type duration; std::cin>>duration; EventSet occuredEvents; //wait for input-event to occur dev->WaitInputs(duration); //identify occured events while(Idx sev=dev->ReadInput()) occuredEvents.Insert(sev); //report occured events if(!occuredEvents.Empty()) std::cout << occuredEvents.ToString(); else std::cout<<"No event recognized"; } ////////////////////////////////////////////////////////////// //User-Interface loop int main(int argc, char* argv[]) { // install my signal handler signal(SIGTERM, catch_signal); signal(SIGINT, catch_signal); signal(SIGQUIT, catch_signal); signal(SIGHUP, catch_signal); signal(SIGABRT, catch_signal); // first argument has to be filename if(argc!=2) { std::cerr << "iomonitor: " << FDVersionString() << std::endl; std::cerr << "usage: iomonitor <device-file>" << std::endl; return -1; } //initialize vDevice FD_DH("Initialize vDevice"); vDevice* dev; dev=vDevice::FromFile(std::string(argv[1])); sDevice* sdev=dynamic_cast<sDevice*>(dev); //start vDevice dev->Start(); //loop until device is up while(dev->Status()!=vDevice::Up){;} // loop until user terminates while(true) { // set up console userinterface std::cout << std::endl; std::cout << "# iomonitor commands are:" << std::endl; std::cout << "# read faudes events via wait (re)" << std::endl; std::cout << "# read faudes events via polling (rep)" << std::endl; if(sdev) std::cout << "# read signal value by bitaddress (rv)" << std::endl; std::cout << "# write faudes events (we)" << std::endl; if(sdev) std::cout << "# write signal value by bitaddress (wv)" << std::endl; std::cout << "# device time (time)" << std::endl; std::cout << "# reset device (reset)" << std::endl; std::cout << "# list all device events (list) " << std::endl; std::cout << "# exit (exit) " << std::endl; std::cout << ">"; // get user-choice std::string choice; std::cin >> choice; //execute user-choice if(choice=="exit") break; if(choice=="reset") {dev->Reset();}; if(choice=="time") ReadTime(dev); if(choice=="list") ListEvents(dev); if(sdev && choice=="rv") ReadSignalValue(sdev); //note: sdev->dev if(choice=="rp") PollInputEvent(dev); if(choice=="re") WaitInputEvent(dev); if(sdev && choice=="wv") WriteSignalValue(sdev); //note: sdev->dev if(choice=="we") WriteOutputEvent(dev); } std::cout << "# iomonitor: done " << std::endl; std::cout << "##########################################" << std::endl; FD_DH("Stopping vDevice") ; //stop background thread dev->Stop(); return 0; } Definition in file iomonitor.cpp. Function Documentation
Definition at line 46 of file iomonitor.cpp.
Definition at line 61 of file iomonitor.cpp.
Definition at line 71 of file iomonitor.cpp.
Definition at line 225 of file iomonitor.cpp.
Definition at line 171 of file iomonitor.cpp.
Definition at line 100 of file iomonitor.cpp.
Definition at line 86 of file iomonitor.cpp.
Definition at line 205 of file iomonitor.cpp.
Definition at line 158 of file iomonitor.cpp.
Definition at line 140 of file iomonitor.cpp. Variable Documentation
Definition at line 43 of file iomonitor.cpp. |
libFAUDES 2.20d --- 2011.04.26 --- c++ source docu by doxygen