added command line option for specifiying name of file you want to write
authorFrank Mori Hess <fmhess@speakeasy.net>
Mon, 24 Feb 2003 19:20:12 +0000 (19:20 +0000)
committerFrank Mori Hess <fmhess@speakeasy.net>
Mon, 24 Feb 2003 19:20:12 +0000 (19:20 +0000)
calibration to

comedi_calibrate/calib.h
comedi_calibrate/cb.c
comedi_calibrate/comedi_calibrate.c
comedi_calibrate/save_cal.c

index 5efa2d2b087dd88464f460abfe9398bd9e975429..39c1afa9c54bb2abf1ce7de3fe9120cca325a99f 100644 (file)
@@ -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
 
index 9bc872971a2b7536e4461cca251e1ca6f3756b1c..03eb17b8ef0bb5a92198df940a3cbfa3709ae500 100644 (file)
@@ -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;
 }
index 0cb4a9433001264c0a05afcb76bfbe4c227cf8eb..f4e537ff833ccaf94b52bed7f145dd5cde0bebe5 100644 (file)
@@ -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;
index e35605c3dad928642f5e75e0041dfe7fb5f43916..3df09879e814809d7718f1e1f2e7f2d0dc8390fe 100644 (file)
@@ -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 <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -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;
+       }
+}