A Cardiovascular Simulator for Research 1.0.0

File: <base>/src/wave_remote.c (3,463 bytes)
/* The function wave_remote.c, which is periodically called by simulate.m,
   invokes WAVE to display most recently generated data by writing the
   new display starting time (as well as other information) to a .wave 
   file in the /tmp directory and signaling WAVE to execute the commands
   in the file.  This program is a modified version of George Moody's 
   wave-remote.c program. 

   Function arguments:
         recordin - MATLAB array of the name of the prefix of the MIT format
	            files
	 annotatorin - MATLAB array of the name of the annotation
                     = -1, then no annotation will be displayed
         ptimein - MATLAB array indicating new starting time of the waveforms
                   to be displayed
         signalsin - MATLAB array indicating which waveforms are to be
	             displayed

   Function outputs: none */
   
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <matlab.h>

void mlfWave_remote(const mxArray *recordin, const mxArray *annotatorin, const mxArray *ptimein, const mxArray *signalsin)

{

    /* Declaring variables. */
    DIR *dir;
    struct dirent *dir_entry;
    char fname[30], pattern[16], *record, *annotator, *signals;
    FILE *ofile;
    int i, imax = 0, pl, pid;
    double ptime;

    /* Converting MATLAB types to C types. */
    record=mxArrayToString(recordin);
    annotator=mxArrayToString(annotatorin);
    ptime=mxGetScalar(ptimein);
    signals=mxArrayToString(signalsin);

    /* Determining the process id of the existing WAVE window. */
    dir = opendir("/tmp");
    sprintf(pattern, ".wave.%d.", (int)getuid());
    pl = strlen(pattern);
    while (dir_entry = readdir(dir)) {
        if (strncmp(dir_entry->d_name, pattern, pl) == 0) {
            i = atoi(dir_entry->d_name+pl);
            if (i > imax) imax = i;
        }
    }
    closedir(dir);
    pid = imax;

    /* If there is no existing WAVE window, starting a new WAVE process
       provided that the outputfile exists, freeing allocated dynamic 
       memory, and returning to simulate.m.   If the output file does,
       not exist, freeing allocated dynamic memory and exiting program 
       with error. */
    if (pid == 0) {
        if (record) {
            char command[128];

            sprintf(command, "wave -s %s -r %s &",signals, record);
            system(command);

	    
	    mxFree(record);
	    mxFree(annotator);

	    return;
        }
        else {
            fprintf(stderr, "%s: can't find WAVE process\n", record);
 	    mxFree(record);
	    mxFree(annotator);
            exit(2);
        }
    }

    /* If the pid does exist, writing outputfile prefix name, annotation if
       desired, start time, and signals to be displayed to the .wave file
       in the /tmp directory with the following format. */

    sprintf(fname, "/tmp/.wave.%d.%d", (int)getuid(), pid);

    ofile = fopen(fname, "w");
    if (record) fprintf(ofile, "-r %s\n", record);
    if (strcmp(annotator,"-1") != 0) fprintf(ofile, "-a %s\n", annotator);
    fprintf(ofile, "-f %8.2f\n", ptime);
    fprintf(ofile, "-s %s\n", signals);
    fclose(ofile);

     /* Signaling to WAVE that the message in the file is ready to execute. */
    kill(pid, SIGUSR1);

    /* Freeing allocated dynamic memory and returning to simulate.m */    
    mxFree(record);
    mxFree(annotator);
    mxFree(signals);
    return;

}