| 1 |
/* |
| 2 |
comedi_calibrate/save_cal.c - stuff for saving calibration info to |
| 3 |
a file. |
| 4 |
|
| 5 |
Copyright (C) 2003 Frank Mori Hess <[email protected]> |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as |
| 9 |
published by the Free Software Foundation; either version 2 of the |
| 10 |
license, or (at your option) any later version. |
| 11 |
|
| 12 |
This program is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with this program; if not, write to the Free Software |
| 19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#define _GNU_SOURCE |
| 23 |
|
| 24 |
#include "comedi_calibrate_shared.h" |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/stat.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <assert.h> |
| 32 |
#include <time.h> |
| 33 |
#include <string.h> |
| 34 |
|
| 35 |
static void indent(FILE *file, unsigned numTabs) |
| 36 |
{ |
| 37 |
while (numTabs--) |
| 38 |
fputc('\t', file); |
| 39 |
} |
| 40 |
|
| 41 |
static void write_caldac( FILE *file, comedi_caldac_t caldac ) |
| 42 |
{ |
| 43 |
const unsigned baseNumTabs = 4; |
| 44 |
indent(file, baseNumTabs); |
| 45 |
fprintf( file, "{\n" ); |
| 46 |
indent(file, baseNumTabs + 1); |
| 47 |
fprintf( file, "subdevice => %i,\n", caldac.subdevice ); |
| 48 |
indent(file, baseNumTabs + 1); |
| 49 |
fprintf( file, "channel => %i,\n", caldac.channel ); |
| 50 |
indent(file, baseNumTabs + 1); |
| 51 |
fprintf( file, "value => %i,\n", caldac.value ); |
| 52 |
indent(file, baseNumTabs); |
| 53 |
fprintf( file, "}" ); |
| 54 |
} |
| 55 |
|
| 56 |
static void write_polynomial(FILE *file, const comedi_polynomial_t *polynomial) |
| 57 |
{ |
| 58 |
const unsigned baseNumTabs = 3; |
| 59 |
unsigned i; |
| 60 |
|
| 61 |
indent(file, baseNumTabs); |
| 62 |
fprintf(file, "{\n"); |
| 63 |
indent(file, baseNumTabs + 1); |
| 64 |
fprintf(file, "expansion_origin => %.20e,\n", polynomial->expansion_origin); |
| 65 |
indent(file, baseNumTabs + 1); |
| 66 |
fprintf(file, "coefficients => ["); |
| 67 |
for(i = 0; i <= polynomial->order; ++i) |
| 68 |
{ |
| 69 |
assert(i < COMEDI_MAX_NUM_POLYNOMIAL_COEFFICIENTS); |
| 70 |
fprintf(file, "%.20e,", polynomial->coefficients[i]); |
| 71 |
} |
| 72 |
fprintf(file, "],\n"); |
| 73 |
indent(file, baseNumTabs); |
| 74 |
fprintf(file, "}"); |
| 75 |
} |
| 76 |
|
| 77 |
void write_calibration_setting( FILE *file, comedi_calibration_setting_t setting ) |
| 78 |
{ |
| 79 |
unsigned baseNumTabs = 2; |
| 80 |
int i; |
| 81 |
|
| 82 |
indent(file, baseNumTabs); |
| 83 |
fprintf( file, "{\n" ); |
| 84 |
indent(file, baseNumTabs + 1); |
| 85 |
fprintf( file, "subdevice => %i,\n", setting.subdevice ); |
| 86 |
indent(file, baseNumTabs + 1); |
| 87 |
fprintf( file, "channels => [" ); |
| 88 |
for( i = 0; i < setting.num_channels; i++ ) |
| 89 |
fprintf( file, "%i,", setting.channels[ i ] ); |
| 90 |
fprintf( file, "],\n" ); |
| 91 |
indent(file, baseNumTabs + 1); |
| 92 |
fprintf( file, "ranges => [" ); |
| 93 |
for( i = 0; i < setting.num_ranges; i++ ) |
| 94 |
fprintf( file, "%i,", setting.ranges[ i ] ); |
| 95 |
fprintf( file, "],\n" ); |
| 96 |
indent(file, baseNumTabs + 1); |
| 97 |
fprintf( file, "arefs => [" ); |
| 98 |
for( i = 0; i < setting.num_arefs; i++ ) |
| 99 |
fprintf( file, "%i,", setting.arefs[ i ] ); |
| 100 |
fprintf( file, "],\n" ); |
| 101 |
indent(file, baseNumTabs + 1); |
| 102 |
fprintf( file, "caldacs =>\n" ); |
| 103 |
indent(file, baseNumTabs + 1); |
| 104 |
fprintf( file, "[\n" ); |
| 105 |
for( i = 0; i < setting.num_caldacs; i++ ) |
| 106 |
{ |
| 107 |
write_caldac( file, setting.caldacs[ i ] ); |
| 108 |
fprintf( file, ",\n" ); |
| 109 |
} |
| 110 |
indent(file, baseNumTabs + 1); |
| 111 |
fprintf( file, "],\n" ); |
| 112 |
if(setting.soft_calibration.to_phys) |
| 113 |
{ |
| 114 |
indent(file, baseNumTabs + 1); |
| 115 |
fprintf(file, "softcal_to_phys =>\n"); |
| 116 |
write_polynomial(file, setting.soft_calibration.to_phys); |
| 117 |
fprintf( file, ",\n" ); |
| 118 |
} |
| 119 |
if(setting.soft_calibration.from_phys) |
| 120 |
{ |
| 121 |
indent(file, baseNumTabs + 1); |
| 122 |
fprintf(file, "softcal_from_phys =>\n"); |
| 123 |
write_polynomial(file, setting.soft_calibration.from_phys); |
| 124 |
fprintf( file, ",\n" ); |
| 125 |
} |
| 126 |
indent(file, baseNumTabs); |
| 127 |
fprintf( file, "}" ); |
| 128 |
} |
| 129 |
|
| 130 |
int write_calibration_perl_hash( FILE *file, const comedi_calibration_t *calibration ) |
| 131 |
{ |
| 132 |
int i; |
| 133 |
time_t now; |
| 134 |
|
| 135 |
now = time( NULL ); |
| 136 |
fprintf( file, "#calibration file generated by comedi_calibrate\n" |
| 137 |
"#%s\n", ctime( &now ) ); |
| 138 |
fprintf( file, "{\n" ); |
| 139 |
fprintf( file, "\tdriver_name => \"%s\",\n", calibration->driver_name ); |
| 140 |
fprintf( file, "\tboard_name => \"%s\",\n", calibration->board_name ); |
| 141 |
fprintf( file, "\tcalibrations =>\n" |
| 142 |
"\t[\n" ); |
| 143 |
for( i = 0; i < calibration->num_settings; i++ ) |
| 144 |
{ |
| 145 |
write_calibration_setting( file, calibration->settings[ i ] ); |
| 146 |
fprintf( file, ",\n" ); |
| 147 |
} |
| 148 |
fprintf( file, "\t],\n" |
| 149 |
"}\n"); |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
int write_calibration_file(const char *file_path, const comedi_calibration_t *calibration) |
| 155 |
{ |
| 156 |
FILE *file; |
| 157 |
int retval; |
| 158 |
|
| 159 |
assert(calibration); |
| 160 |
assert(file_path); |
| 161 |
|
| 162 |
file = fopen(file_path, "w"); |
| 163 |
if(file == NULL) |
| 164 |
{ |
| 165 |
fprintf( stderr, "failed to open file %s for writing\n", |
| 166 |
file_path ); |
| 167 |
perror( "fopen" ); |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
// DPRINT( 0, "writing calibration to %s\n", setup->cal_save_file_path ); |
| 172 |
retval = write_calibration_perl_hash(file, calibration); |
| 173 |
|
| 174 |
fclose(file); |
| 175 |
|
| 176 |
return retval; |
| 177 |
} |
| 178 |
|
| 179 |
comedi_calibration_setting_t* sc_alloc_calibration_setting(comedi_calibration_t *calibration) |
| 180 |
{ |
| 181 |
comedi_calibration_setting_t *temp; |
| 182 |
|
| 183 |
temp = realloc(calibration->settings, |
| 184 |
(calibration->num_settings + 1) * sizeof(comedi_calibration_setting_t)); |
| 185 |
assert(temp); |
| 186 |
calibration->settings = temp; |
| 187 |
memset(&calibration->settings[calibration->num_settings], |
| 188 |
0, sizeof(comedi_calibration_setting_t)); |
| 189 |
|
| 190 |
calibration->num_settings++; |
| 191 |
|
| 192 |
return &calibration->settings[calibration->num_settings - 1]; |
| 193 |
} |
| 194 |
|
| 195 |
void sc_push_caldac(comedi_calibration_setting_t *saved_cal, comedi_caldac_t caldac) |
| 196 |
{ |
| 197 |
int i; |
| 198 |
|
| 199 |
/* check if caldac is already listed, in which case we just update */ |
| 200 |
for( i = 0; i < saved_cal->num_caldacs; i++ ) |
| 201 |
{ |
| 202 |
if( saved_cal->caldacs[ i ].subdevice != caldac.subdevice ) continue; |
| 203 |
if( saved_cal->caldacs[ i ].channel != caldac.channel ) continue; |
| 204 |
break; |
| 205 |
} |
| 206 |
if( i < saved_cal->num_caldacs ) |
| 207 |
{ |
| 208 |
saved_cal->caldacs[ i ].value = caldac.value; |
| 209 |
return; |
| 210 |
} |
| 211 |
saved_cal->caldacs = realloc(saved_cal->caldacs, |
| 212 |
(saved_cal->num_caldacs + 1) * sizeof(comedi_caldac_t)); |
| 213 |
if(saved_cal->caldacs == NULL) |
| 214 |
{ |
| 215 |
fprintf( stderr, "memory allocation failure\n" ); |
| 216 |
abort(); |
| 217 |
} |
| 218 |
saved_cal->caldacs[ saved_cal->num_caldacs ] = caldac; |
| 219 |
saved_cal->num_caldacs++; |
| 220 |
} |
| 221 |
|
| 222 |
void sc_push_channel( comedi_calibration_setting_t *saved_cal, int channel ) |
| 223 |
{ |
| 224 |
if( channel == SC_ALL_CHANNELS ) |
| 225 |
{ |
| 226 |
saved_cal->num_channels = 0; |
| 227 |
if( saved_cal->channels ) |
| 228 |
{ |
| 229 |
free( saved_cal->channels ); |
| 230 |
saved_cal->channels = NULL; |
| 231 |
} |
| 232 |
}else |
| 233 |
{ |
| 234 |
saved_cal->channels = realloc( saved_cal->channels, |
| 235 |
( saved_cal->num_channels + 1 ) * sizeof( int ) ); |
| 236 |
if( saved_cal->channels == NULL ) |
| 237 |
{ |
| 238 |
fprintf( stderr, "memory allocation failure\n" ); |
| 239 |
abort(); |
| 240 |
} |
| 241 |
saved_cal->channels[ saved_cal->num_channels++ ] = channel; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
void sc_push_range( comedi_calibration_setting_t *saved_cal, int range ) |
| 246 |
{ |
| 247 |
if( range == SC_ALL_RANGES ) |
| 248 |
{ |
| 249 |
saved_cal->num_ranges = 0; |
| 250 |
if( saved_cal->ranges ) |
| 251 |
{ |
| 252 |
free( saved_cal->ranges ); |
| 253 |
saved_cal->ranges = NULL; |
| 254 |
} |
| 255 |
} |
| 256 |
else |
| 257 |
{ |
| 258 |
saved_cal->ranges = realloc( saved_cal->ranges, |
| 259 |
( saved_cal->num_ranges + 1 ) * sizeof( int ) ); |
| 260 |
if( saved_cal->ranges == NULL ) |
| 261 |
{ |
| 262 |
fprintf( stderr, "memory allocation failure\n" ); |
| 263 |
abort(); |
| 264 |
} |
| 265 |
saved_cal->ranges[ saved_cal->num_ranges++ ] = range; |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
|
| 270 |
void sc_push_aref( comedi_calibration_setting_t *saved_cal, int aref ) |
| 271 |
{ |
| 272 |
assert( saved_cal->num_arefs < CS_MAX_AREFS_LENGTH ); |
| 273 |
|
| 274 |
if( aref == SC_ALL_AREFS ) |
| 275 |
saved_cal->num_arefs = 0; |
| 276 |
else |
| 277 |
saved_cal->arefs[ saved_cal->num_arefs++ ] = aref; |
| 278 |
} |