From 723db82ce5bb47388480396e458b06aba834a25f Mon Sep 17 00:00:00 2001 From: Frank Mori Hess Date: Mon, 24 Feb 2003 22:30:43 +0000 Subject: [PATCH] beginnings of comedi_set_calibration() --- lib/Makefile | 2 +- lib/calib.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 lib/calib.c diff --git a/lib/Makefile b/lib/Makefile index ef07fb7..4ca23d4 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -5,7 +5,7 @@ include ../version CFLAGS += -fPIC -I../include -I. OBJS=comedi.o timer.o sv.o range.o ioctl.o filler.o timed.o error.o \ - dio.o data.o get.o cmd.o buffer.o + dio.o data.o get.o cmd.o buffer.o calib.o SONAME=libcomedi$(SONAME_SUFFIX).so.0 diff --git a/lib/calib.c b/lib/calib.c new file mode 100644 index 0000000..fd8126a --- /dev/null +++ b/lib/calib.c @@ -0,0 +1,104 @@ +/* + lib/calib.c + functions for setting calibration + + Copyright (C) 2003 Frank Mori Hess +#include +#include + +static int ph_extract_element( const char *file_path, const char *element, + char *result, unsigned int result_size ) +{ + char perl_prog[ 1024 ]; + FILE *perl_stdout; + int retval; + + snprintf( perl_prog, sizeof( perl_prog ), + "perl -e '" + "use strict;" + "use warnings;" + "my $hash;" + "my $cal;" + "$hash = `cat %s`;" + "eval \"\\$cal = $hash;\";" + "print $cal->%s;" + "'", + file_path, element ); + + perl_stdout = popen( perl_prog, "r"); + if( perl_stdout == NULL ) + { + fprintf( stderr, "popen() failed in ph_extract_element()\n" ); + return -1; + } + + if( fgets( result, result_size, perl_stdout ) == NULL ) + { + fprintf( stderr, "fgets() returned NULL in ph_extract_element()\n" ); + return -1; + } + + retval = pclose( perl_stdout ); + if( retval ) + { + fprintf( stderr, "perl returned error %i\n in ph_extract_element()", retval ); + return -1; + } + + return 0; +} + +//EXPORT_SYMBOL(comedi_set_calibration,0.7.20); +int comedi_set_calibration( comedi_t *dev, const char *cal_file_path, + unsigned int subdev, unsigned int channel, unsigned int range, unsigned int aref ) +{ + struct stat file_stats; + FILE *cal_file; + + if( cal_file_path ) + cal_file = fopen( cal_file_path, "r" ); + else + { + char *file_path; + + if( fstat( comedi_fileno( dev ), &file_stats ) < 0 ) + { + fprintf( stderr, "failed to get file stats of comedi device file\n" ); + return -1; + } + + asprintf( &file_path, "/etc/comedi/calibrations/%s_0x%lx", + comedi_get_board_name( dev ), + ( unsigned long ) file_stats.st_ino ); + cal_file = fopen( file_path, "r" ); + free( file_path ); + } + + if( cal_file == NULL ) + { + fprintf( stderr, "failed to open calibration file\n" ); + return -1; + } + + return 0; +} -- 2.26.2