From 63b007952713448d0035ea949917c5d2e51ecc18 Mon Sep 17 00:00:00 2001 From: Frank Mori Hess Date: Mon, 24 Feb 2003 19:20:12 +0000 Subject: [PATCH] added command line option for specifiying name of file you want to write calibration to --- comedi_calibrate/calib.h | 12 ++-- comedi_calibrate/cb.c | 4 +- comedi_calibrate/comedi_calibrate.c | 9 ++- comedi_calibrate/save_cal.c | 89 ++++++++++++++++++++++++----- 4 files changed, 91 insertions(+), 23 deletions(-) diff --git a/comedi_calibrate/calib.h b/comedi_calibrate/calib.h index 5efa2d2..39c1afa 100644 --- a/comedi_calibrate/calib.h +++ b/comedi_calibrate/calib.h @@ -60,6 +60,7 @@ struct calibration_setup_struct { caldac_t caldacs[ N_CALDACS ]; unsigned int n_caldacs; int (*do_cal) ( calibration_setup_t *setup ); + char *cal_save_file_path; }; extern char *devicename; @@ -191,20 +192,18 @@ int new_sv_measure(comedi_t *dev, new_sv_t *sv); int new_sv_init(new_sv_t *sv,comedi_t *dev,int subdev,unsigned int chanspec); /* saving calibrations to file */ -#define SC_MAX_CHANNELS_LENGTH 128 -#define SC_MAX_RANGES_LENGTH 128 #define SC_MAX_AREFS_LENGTH 4 typedef struct { unsigned int subdevice; - caldac_t caldacs[ N_CALDACS ]; + caldac_t *caldacs; unsigned int caldacs_length; /* channels that caldac settings are restricted to */ - int channels[ SC_MAX_CHANNELS_LENGTH ]; + int *channels; /* number of elements in channels array, 0 means allow all channels */ unsigned int channels_length; /* ranges that caldac settings are restricted to */ - int ranges[ SC_MAX_RANGES_LENGTH ]; + int *ranges; /* number of elements in ranges array, 0 means allow all ranges */ unsigned int ranges_length; /* arefs that caldac settings are used restricted to */ @@ -217,12 +216,13 @@ static const int SC_ALL_CHANNELS = -1; static const int SC_ALL_RANGES = -1; static const int SC_ALL_AREFS = -1; -int write_calibration_file( comedi_t *dev, saved_calibration_t settings[], +int write_calibration_file( calibration_setup_t *setup, saved_calibration_t settings[], unsigned int num_settings ); void sc_push_caldac( saved_calibration_t *saved_cal, caldac_t caldac ); void sc_push_channel( saved_calibration_t *saved_cal, int channel ); void sc_push_range( saved_calibration_t *saved_cal, int range ); void sc_push_aref( saved_calibration_t *saved_cal, int aref ); +void clear_saved_calibration( saved_calibration_t *saved_cal ); #endif diff --git a/comedi_calibrate/cb.c b/comedi_calibrate/cb.c index 9bc8729..03eb17b 100644 --- a/comedi_calibrate/cb.c +++ b/comedi_calibrate/cb.c @@ -870,7 +870,9 @@ int cal_cb_pci_60xx( calibration_setup_t *setup ) sc_push_aref( &saved_cals[ i ], SC_ALL_AREFS ); } - retval = write_calibration_file( setup->dev, saved_cals, num_ranges ); + retval = write_calibration_file( setup, saved_cals, num_ranges ); + for( i = 0; i < num_ranges; i++ ) + clear_saved_calibration( &saved_cals[ i ] ); free( saved_cals ); return retval; } diff --git a/comedi_calibrate/comedi_calibrate.c b/comedi_calibrate/comedi_calibrate.c index 0cb4a94..f4e537f 100644 --- a/comedi_calibrate/comedi_calibrate.c +++ b/comedi_calibrate/comedi_calibrate.c @@ -70,6 +70,7 @@ struct option options[] = { { "verbose", 0, 0, 'v' }, { "quiet", 0, 0, 'q' }, { "file", 1, 0, 'f' }, + { "save-file", 1, 0, 's' }, { "help", 0, 0, 'h' }, { "driver-name", 1, 0, 0x1000 }, { "device-name", 1, 0, 0x1001 }, @@ -93,6 +94,7 @@ void help(void) printf(" --quiet, -q \n"); printf(" --help, -h \n"); printf(" --file, -f [/dev/comediN] \n"); + printf(" --save-file, -s [filepath] \n"); printf(" --driver-name [driver] \n"); printf(" --device-name [device] \n"); printf(" --[no-]reset \n"); @@ -118,6 +120,8 @@ int main(int argc, char *argv[]) int caldac_subdev; int retval; + memset( &setup, 0, sizeof( setup ) ); + fn = "/dev/comedi0"; while (1) { c = getopt_long(argc, argv, "f:vq", options, &index); @@ -132,6 +136,9 @@ int main(int argc, char *argv[]) case 'f': fn = optarg; break; + case 's': + setup.cal_save_file_path = optarg; + break; case 'v': verbose++; break; @@ -176,8 +183,6 @@ int main(int argc, char *argv[]) return 1; ok: - memset( &setup, 0, sizeof( setup ) ); - setup.dev = dev; setup.ad_subdev = ad_subdev; setup.da_subdev = da_subdev; diff --git a/comedi_calibrate/save_cal.c b/comedi_calibrate/save_cal.c index e35605c..3df0987 100644 --- a/comedi_calibrate/save_cal.c +++ b/comedi_calibrate/save_cal.c @@ -19,6 +19,8 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define _GNU_SOURCE + #include #include #include @@ -106,15 +108,15 @@ int write_calibration_perl_hash( FILE *file, comedi_t *dev, return 0; } -int write_calibration_file( comedi_t *dev, saved_calibration_t settings[], +int write_calibration_file( calibration_setup_t *setup, saved_calibration_t settings[], unsigned int num_settings ) { FILE *file; int retval; static const char *save_dir = "/etc/comedi/calibrations"; - char file_path[ 100 ]; char command[ 100 ]; struct stat file_stats; + comedi_t *dev = setup->dev; if( fstat( comedi_fileno( dev ), &file_stats ) < 0 ) { @@ -129,13 +131,17 @@ int write_calibration_file( comedi_t *dev, saved_calibration_t settings[], return -1; } - snprintf( file_path, sizeof( file_path ), "%s/%s_0x%lx", - save_dir, comedi_get_board_name( dev ), - ( unsigned long ) file_stats.st_ino ); - file = fopen( file_path, "w" ); + if( setup->cal_save_file_path == NULL ) + { + asprintf( &setup->cal_save_file_path, "%s/%s_0x%lx", + save_dir, comedi_get_board_name( dev ), + ( unsigned long ) file_stats.st_ino ); + } + file = fopen( setup->cal_save_file_path, "w" ); if( file == NULL ) { - fprintf( stderr, "failed to open file %s for writing\n", file_path ); + fprintf( stderr, "failed to open file %s for writing\n", + setup->cal_save_file_path ); return -1; } @@ -148,29 +154,62 @@ int write_calibration_file( comedi_t *dev, saved_calibration_t settings[], void sc_push_caldac( saved_calibration_t *saved_cal, caldac_t caldac ) { - assert( saved_cal->caldacs_length < N_CALDACS ); - + saved_cal->caldacs = realloc( saved_cal->caldacs, + ( saved_cal->caldacs_length + 1 ) * sizeof( caldac_t ) ); + if( saved_cal->caldacs == NULL ) + { + fprintf( stderr, "memory allocation failure\n" ); + abort(); + } saved_cal->caldacs[ saved_cal->caldacs_length++ ] = caldac; } void sc_push_channel( saved_calibration_t *saved_cal, int channel ) { - assert( saved_cal->channels_length < SC_MAX_CHANNELS_LENGTH ); - if( channel == SC_ALL_CHANNELS ) + { saved_cal->channels_length = 0; - else + if( saved_cal->channels ) + { + free( saved_cal->channels ); + saved_cal->channels = NULL; + } + }else + { + saved_cal->channels = realloc( saved_cal->channels, + ( saved_cal->channels_length + 1 ) * sizeof( int ) ); + if( saved_cal->channels == NULL ) + { + fprintf( stderr, "memory allocation failure\n" ); + abort(); + } saved_cal->channels[ saved_cal->channels_length++ ] = channel; + } } void sc_push_range( saved_calibration_t *saved_cal, int range ) { - assert( saved_cal->ranges_length < SC_MAX_RANGES_LENGTH ); - if( range == SC_ALL_RANGES ) + { saved_cal->ranges_length = 0; + if( saved_cal->ranges ) + { + free( saved_cal->ranges ); + saved_cal->ranges = NULL; + } + } else + { + saved_cal->ranges = realloc( saved_cal->ranges, + ( saved_cal->ranges_length + 1 ) * sizeof( int ) ); + if( saved_cal->ranges == NULL ) + { + fprintf( stderr, "memory allocation failure\n" ); + abort(); + } saved_cal->ranges[ saved_cal->ranges_length++ ] = range; + } + } void sc_push_aref( saved_calibration_t *saved_cal, int aref ) @@ -182,3 +221,25 @@ void sc_push_aref( saved_calibration_t *saved_cal, int aref ) else saved_cal->arefs[ saved_cal->arefs_length++ ] = aref; } + +void clear_saved_calibration( saved_calibration_t *saved_cal ) +{ + if( saved_cal->caldacs ) + { + free( saved_cal->caldacs ); + saved_cal->caldacs = NULL; + saved_cal->caldacs_length = 0; + } + if( saved_cal->channels ) + { + free( saved_cal->channels ); + saved_cal->channels = NULL; + saved_cal->channels_length = 0; + } + if( saved_cal->ranges ) + { + free( saved_cal->ranges ); + saved_cal->ranges = NULL; + saved_cal->ranges_length = 0; + } +} -- 2.26.2