/*-*-C-*-*/ #include #include #include #include #include #include #include #include #include "knobUserEpics.h" #include "knobUserEpicsUtil.h" static char *knobConfigFile; static int MAX_LINE_LENGTH = 100; /* Arbitrary line length */ /*===================================================================== Condition Prec Start_char Type val=0,exp=0 3 10 or 11 fixed point exp=1 2 10 or 11 fixed point exp=2 1 10 or 11 fixed piont exp=3 1 9 or 10 fixed point exp=-1,-2 4 9 or 10 fixed point -10=1e100 N/A 9 or 10 N/A |val|<=1e-100 N/A 9 or 10 N/A =====================================================================*/ void knobUserSetLegendValue(knobdevice_ts *data, epicsFloat64 val, int dbg) { int exp = ( fabs(val) < 1e-101 ) ? 0 : floor( log10( fabs( val ) ) ); char sign = ( val < 0 ) ? '-' : ' '; float mult = fabs( val/(pow(10,exp)) ); char valstr[LEGEND_VALUE_SIZE+1] = {' '}; /* Add byte for string terminator */ int i, n; size_t l; if ( dbg >= HIGH ) printf("knobUserSetLegendValue val %f, exp %i, char %c, mult %f\n", val, exp, sign, mult); if( exp == 0 ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, " %c%.3f", sign, fabs( val ) ); else if( exp == 1 ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, " %c%.2f", sign, fabs( val ) ); else if( exp == 2 ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, " %c%.1f", sign, fabs( val ) ); else if( exp == 3 ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "%c%.1f", sign, fabs( val ) ); else if( (exp < 0) && (exp >= -3) ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "%c%.4f", sign, fabs( val ) ); else if( (exp < -3) && (exp > -10) ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "%c%.1fE%i", sign, mult, exp ); else if( (exp >= 4) && (exp < 10) ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "%c%.2fE%i", sign, mult, exp ); else if( (exp <= -10) && (exp > -100) ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "%c%.0fE%i", sign, mult, exp ); else if( (exp >= 10) && (exp < 100) ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "%c%.1fE%i", sign, mult, exp ); else if( exp >= 100 ) { if( val < 0 ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "<-1+99" ); else n = snprintf( valstr, LEGEND_VALUE_SIZE+1, ">1+99" ); } else if( exp <= 100 ) { if( val < 0 ) n = snprintf( valstr, LEGEND_VALUE_SIZE+1, ">-1-99" ); else n = snprintf( valstr, LEGEND_VALUE_SIZE+1, "<1-99" ); } else printf("error during setlegendvalue\n"); // make sure this can't happen /* Do not want to include null character in legend string, * so only copy strlen( valstr ) chars from valstr. * Then fill in rest of string with space characters */ l = strlen( valstr ); if ( dbg >= HIGH ) printf("knobUserSetLegendValue, value string is %s. n %i l %i\n\n", valstr, n, (int)l); for ( i = 0; i < l; i++ ) data->legend[LEGEND_VALUE_ROW][i] = valstr[i]; for ( i = l; i < LEGEND_VALUE_SIZE; i++ ) data->legend[LEGEND_VALUE_ROW][i] = ' '; } /* * Read device knob sensitivity from file * * File name is global variable defined at startup * * File format must be: * one device per row with syntax: * , * for example: * * QUAD:LI26:201:BCTRL,0.1 * * Scan file for this device name. If found, read sensitivity * and store in sens. If not, set to default sensitivity. */ void knobUserGetSensFromFile(knobdevice_ts *data, int *fsens, epicsFloat64 *sens, int dbg) { FILE *fd; /* File descriptor for knob config file */ int i = 0; char fline[MAX_LINE_LENGTH]; /* Input line currently read from file */ char fname[MAX_NAME_LENGTH]; /* Device name read from file */ char fsensstr[25]; /* Sensitivity read from file, as string */ if ( knobConfigFile ) { if ( dbg >= HIGH ) printf("knobUserEpicsUtil: get_sens file name %s\n", knobConfigFile); fd = fopen( knobConfigFile, "r" ); if ( fd ) { while ( fscanf(fd, "%s", fline) != EOF ) { i++; strcpy( fname, strtok( fline,"," ) ); if( strcmp(fname, data->rname) == 0 ) { strcpy( fsensstr, strtok(NULL,",") ); if ( dbg >= HIGH ) printf("knobUserEpicsUtil: %s read sensitivity string %s\n", fname, fsensstr); *sens = atof( fsensstr ); *fsens = 1; if ( dbg >= HIGH ) printf("knobUserEpicsUtil: %s sensitivity %f\n", fname, *sens); break; } } if ( i == 0 ) { if ( dbg >= LOW ) printf("knobUserEpicsUtil: file read error, errno %i\n", errno); } else { if ( dbg >= HIGH ) printf("knobUserEpicsUtil: read %i lines before EOF\n", i); } } else { if ( dbg >= LOW ) printf("knobUserEpicsUtil: file open failed, errno %i\n", errno); } if ( fclose( fd ) ) { if ( dbg >= LOW ) printf("knobUserEpicsUtil: file close error, errno %i\n", errno); } if ( *fsens ) return; } if ( data->drv ) *sens = (data->drvh - data->drvl)/10; else if ( data->hopr > data->lopr ) *sens = (data->hopr - data->lopr)/10; else *sens = 1.0; if ( dbg >= HIGH ) printf("knobUserEpicsUtil: %s determined sensitivity to be %f\n", data->rname, *sens); *fsens = 1; } /* * Routine to set file path and name to read desired knob sensitivity. Optional * * Arguments: * * arg0 - file "/" * */ static const iocshArg arg0 = { "Knob configuration file path/name", iocshArgString }; static const iocshArg *args[] = { &arg0 }; static const iocshFuncDef knobSetConfigFileFuncDef = { "knobSetConfigFilename", 1, args }; static void knobSetConfigFile(const iocshArgBuf *args) { if ( args[0].sval ) { knobConfigFile = calloc( 1+strlen( args[0].sval ), 1 ); if ( knobConfigFile ) strcpy( knobConfigFile, args[0].sval ); } else printf("knobSetConfigFilename: Warning: file name arg missing\n"); } static void drvKnobUserEpicsUtilRegistrar(void) { static int firstTime = 1; if ( firstTime ) { iocshRegister(&knobSetConfigFileFuncDef, knobSetConfigFile); firstTime = 0; } } epicsExportRegistrar(drvKnobUserEpicsUtilRegistrar);