beginnings of comedi_set_calibration()
authorFrank Mori Hess <fmhess@speakeasy.net>
Mon, 24 Feb 2003 22:30:43 +0000 (22:30 +0000)
committerFrank Mori Hess <fmhess@speakeasy.net>
Mon, 24 Feb 2003 22:30:43 +0000 (22:30 +0000)
lib/Makefile
lib/calib.c [new file with mode: 0644]

index ef07fb798f7323f1b50c4fde71fcd4e8305ad4f3..4ca23d434e540bc4baedfacdc2c4f0dae267b86a 100644 (file)
@@ -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 (file)
index 0000000..fd8126a
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+    lib/calib.c
+    functions for setting calibration
+
+    Copyright (C) 2003 Frank Mori Hess <fmhess@users.sourceforge.net
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation, version 2.1
+    of the License.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
+    USA.
+*/
+
+#define _GNU_SOURCE
+
+#include <stdlib.h>
+#include <comedilib.h>
+#include <libinternal.h>
+
+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;
+}