libFAUDES
Sections
Index
|
iobridge.cppGo to the documentation of this file.00001 /** @file iobridge.cpp Test utility for IO devices 00002 00003 00004 This tutorial demonstrates how to use elementary access to 00005 external signals for a bridging device, ie inputs of one device 00006 are mapped to outputs of the other device and vice versa. 00007 This addresses the situation where digital io events are to be 00008 transmitted to or receviced from a network device without running 00009 a simulation. 00010 00011 @ingroup Tutorials 00012 00013 @include iobridge.cpp 00014 00015 */ 00016 00017 #include "libfaudes.h" 00018 #include "iop_vdevice.h" 00019 #include <signal.h> 00020 00021 00022 // fix windows/mingw definition 00023 #ifdef FAUDES_WINEXTRA 00024 #define sleep(sec) Sleep((sec) * 1000) 00025 #define usleep(usec) Sleep((usec) / 1000) 00026 #define SIGQUIT SIGBREAK 00027 #define SIGHUP SIGBREAK 00028 #define strsignal(sig) "unknown" 00029 #endif 00030 00031 using namespace faudes; 00032 00033 // iobridge clean-up on exit 00034 void iobridge_exit(void); 00035 00036 // signal handler recursion flag 00037 volatile sig_atomic_t signal_in_progress = 0; 00038 00039 // signal handler to stop devices 00040 void catch_signal(int sig) { 00041 // detect recursion, pass on 00042 if(signal_in_progress) raise(sig); 00043 signal_in_progress = 1; 00044 // report 00045 std::cerr << "iobridge: signal: " << strsignal(sig) << std::endl; 00046 // call my exit function 00047 iobridge_exit(); 00048 // re-install default handler 00049 signal(sig, SIG_DFL); 00050 // pass on signal 00051 raise(sig); 00052 } 00053 00054 // iobridge clean-up on exit 00055 void iobridge_exit(void) { 00056 // stop all devices 00057 vDevice::StopAll(); 00058 } 00059 00060 00061 // iobridge 00062 int main(int argc, char* argv[]) { 00063 00064 // install my signal handler 00065 signal(SIGTERM, catch_signal); 00066 signal(SIGINT, catch_signal); 00067 signal(SIGQUIT, catch_signal); 00068 signal(SIGHUP, catch_signal); 00069 signal(SIGABRT, catch_signal); 00070 00071 // install my exit fnct 00072 atexit(iobridge_exit); 00073 00074 00075 // first two arguments must be the device files 00076 if(argc!=3) { 00077 std::cerr << "iobridge: " << FDVersionString() << std::endl; 00078 std::cerr << "usage: iobridge <one device-file> <other device-file>" << std::endl; 00079 return(-1); 00080 } 00081 00082 //initialize vDevice A 00083 FD_DH("Initialize vDevice A"); 00084 vDevice* adev; 00085 adev=vDevice::FromFile(std::string(argv[1])); 00086 00087 //initialize vDevice A 00088 FD_DH("Initialize vDevice B"); 00089 vDevice* bdev; 00090 bdev=vDevice::FromFile(std::string(argv[2])); 00091 00092 // have mutex/condition for common wait 00093 pthread_mutex_t wmutex; 00094 pthread_cond_t wcond; 00095 pthread_mutex_init(&wmutex,NULL); 00096 pthread_cond_init(&wcond,NULL); 00097 adev->UseCondition(&wmutex,&wcond); 00098 bdev->UseCondition(&wmutex,&wcond); 00099 00100 // set up devices 00101 adev->Compile(); 00102 bdev->Compile(); 00103 00104 // start devices 00105 adev->Start(); 00106 bdev->Start(); 00107 00108 // loop forever 00109 while(1) { 00110 00111 std::cout << "% ################ iobridge: waiting for input events" << std::endl; 00112 00113 // lock waiting 00114 pthread_mutex_lock(&wmutex); 00115 00116 // wait for inputs 00117 pthread_cond_wait(&wcond,&wmutex); 00118 00119 // test inputs 00120 while(Idx ev =adev->ReadInput()) { 00121 std::cout << "% ################ iobridge: sensed " << adev->Name() << "-input " << adev->EStr(ev) << std::endl; 00122 bdev->WriteOutput(ev); 00123 } 00124 while(Idx ev =bdev->ReadInput()) { 00125 std::cout << "% ################ iobridge: sensed " << bdev->Name() << "-input " << bdev->EStr(ev) << std::endl; 00126 adev->WriteOutput(ev); 00127 } 00128 00129 // unlock waiting 00130 pthread_mutex_unlock(&wmutex); 00131 00132 } 00133 00134 // never happens 00135 iobridge_exit(); 00136 00137 // never happens 00138 return 0; 00139 } 00140 00141 00142 |
libFAUDES 2.20s --- 2011.10.12 --- c++ source docu by doxygen