📄 scope_files.c
字号:
/** This file, 'scope_files.c', handles file I/O for halscope. It includes code to save and restore front panel setups, and a clunky way to save captured scope data.*//** Copyright (C) 2003 John Kasunich <jmkasunich AT users DOT sourceforge DOT net>*//** This program is free software; you can redistribute it and/or modify it under the terms of version 2.1 of the GNU General Public License as published by the Free Software Foundation. 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR ANY HARM OR LOSS RESULTING FROM ITS USE. IT IS _EXTREMELY_ UNWISE TO RELY ON SOFTWARE ALONE FOR SAFETY. Any machinery capable of harming persons must have provisions for completely removing power from all motors, etc, before persons enter any danger area. All machinery must be designed to comply with local and national safety codes, and the authors of this software can not, and do not, take any responsibility for such compliance. This code was written as part of the EMC HAL project. For more information, go to www.linuxcnc.org.*/#ifndef ULAPI#error This is a user mode component only!#endif#include <sys/types.h>#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <signal.h>#include <ctype.h>#include <string.h>#include "rtapi.h" /* RTAPI realtime OS API */#include "hal.h" /* HAL public API decls */#include <gtk/gtk.h>#include "miscgtk.h" /* generic GTK stuff */#include "scope_usr.h" /* scope related declarations *//************************************************************************ DOCUMENTATION *************************************************************************//* Scope setup is stored in the form of a script containing commands that set various parameters. Each command consists of a keyword followed by one or more values. Keywords are not case sensitive.*//* THREAD <string> name of thread to sample in MAXCHAN <int> 1,2,4,8,16, maxumum channel count HMULT <int> multiplier, sample every N runs of thread HZOOM <int> 1-9, horizontal zoom setting HPOS <float> 0.0-1.0, horizontal position setting CHAN <int> sets channel for subsequent commands PIN <string> named pin becomes source for channel PARAM <string> named parameter becomes source for channel SIG <string> named signal becomes source for channel CHOFF disables selected channel VSCALE <int> vertical scaling VPOS <float> 0.0-1.0, vertical position setting VOFF <float> vertical offset TSOURCE <int> channel number for trigger source TLEVEL <float> 0.0-1.0, trigger level setting TPOS <float> 0.0-1.0, trigger position setting TPOLAR <enum> triger polarity, RISE or FALL TMODE <int> 0 = normal trigger, 1 = auto trigger RMODE <int> 0 = stop, 1 = norm, 2 = single, 3 = roll *//************************************************************************ TYPEDEFS AND DEFINES *************************************************************************/typedef enum { INT, FLOAT, STRING} arg_type_t;typedef struct { char* name; arg_type_t arg_type; char * (*handler)(void *arg);} cmd_lut_entry_t;/************************************************************************ GLOBAL VARIABLES *************************************************************************//************************************************************************ LOCAL FUNCTION PROTOTYPES *************************************************************************/static int parse_command(char *in);/* the following functions implement halscope config items each is called with a pointer to a single argument (the parser used here allows only one arg per command) and returns NULL if the command succeeded, or an error message if it failed.*/static char *dummy_cmd(void * arg);static char *thread_cmd(void * arg);static char *maxchan_cmd(void * arg);static char *hzoom_cmd(void * arg);static char *hpos_cmd(void * arg);static char *hmult_cmd(void * arg);static char *chan_cmd(void * arg);static char *choff_cmd(void * arg);static char *pin_cmd(void * arg);static char *sig_cmd(void * arg);static char *param_cmd(void * arg);static char *vscale_cmd(void * arg);static char *vpos_cmd(void * arg);static char *voff_cmd(void * arg);static char *tsource_cmd(void * arg);static char *tlevel_cmd(void * arg);static char *tpos_cmd(void * arg);static char *tpolar_cmd(void * arg);static char *tmode_cmd(void * arg);static char *rmode_cmd(void * arg);/************************************************************************ LOCAL VARIABLES *************************************************************************/static const cmd_lut_entry_t cmd_lut[25] = { { "thread", STRING, thread_cmd }, { "maxchan", INT, maxchan_cmd }, { "hmult", INT, hmult_cmd }, { "hzoom", INT, hzoom_cmd }, { "hpos", FLOAT, hpos_cmd }, { "chan", INT, chan_cmd }, { "choff", INT, choff_cmd }, { "pin", STRING, pin_cmd }, { "sig", STRING, sig_cmd }, { "param", STRING, param_cmd }, { "vscale", INT, vscale_cmd }, { "vpos", FLOAT, vpos_cmd }, { "voff", FLOAT, voff_cmd }, { "tsource", INT, tsource_cmd }, { "tlevel", FLOAT, tlevel_cmd }, { "tpos", FLOAT, tpos_cmd }, { "tpolar", INT, tpolar_cmd }, { "tmode", INT, tmode_cmd }, { "rmode", INT, rmode_cmd }, { "", 0, dummy_cmd }};static int deferred_channel; /************************************************************************ PUBLIC FUNCTION CODE *************************************************************************/int read_config_file (char *filename){ FILE *fp; char cmd_buf[100]; char *cp; int retval; deferred_channel = 0; fp = fopen(filename, "r"); if ( fp == NULL ) { fprintf(stderr, "halscope: config file '%s' could not be opened\n", filename ); return -1; } retval = 0; while ( fgets(cmd_buf, 99, fp) != NULL ) { /* remove trailing newline if present */ cp = cmd_buf; while (( *cp != '\n' ) && ( *cp != '\0' )) { cp++; } *cp = '\0'; /* parse and execute the command */ retval += parse_command(cmd_buf); } fclose(fp); if ( retval < 0 ) { fprintf(stderr, "halscope: config file '%s' caused %d warnings\n", filename, -retval ); return -1; } return 0;}void write_config_file (char *filename){ FILE *fp; fp = fopen(filename, "w"); if ( fp == NULL ) { fprintf(stderr, "halscope: config file '%s' could not be created\n", filename ); return; } write_horiz_config(fp); write_vert_config(fp); write_trig_config(fp); /* write run mode */ if (ctrl_usr->run_mode == NORMAL ) { fprintf(fp, "RMODE 1\n" ); } else if ( ctrl_usr->run_mode == SINGLE ) { fprintf(fp, "RMODE 2\n" );#if 0 /* FIXME - role mode not implemented yet */ } else if ( ctrl_usr->run_mode == ROLL ) { fprintf(fp, "RMODE 3\n" );#endif } else { /* stop mode */ fprintf(fp, "RMODE 0\n" ); } fclose(fp);}/* writes captured data to disk */void write_log_file (char *filename){ scope_data_t *dptr, *start; scope_horiz_t *horiz; int sample_len, chan_num, sample_period_ns, samples, n; char *label[16]; //scope_disp_t *disp; scope_log_t *log; scope_chan_t *chan; hal_type_t type[16]; FILE *fp; fp = fopen(filename, "w"); if ( fp == NULL ) { fprintf(stderr, "ERROR: log file '%s' could not be created\n", filename ); return; } /* fill in local variables */ for (chan_num=0; chan_num<16; chan_num++) { chan = &(ctrl_usr->chan[chan_num]); label[chan_num] = chan->name; type[chan_num] = chan->data_type; } /* sample_len is really the number of channels, don't let it fool you */ sample_len = ctrl_shm->sample_len; //disp = &(ctrl_usr->disp); n=0; samples = ctrl_usr->samples*sample_len ; //fprintf(stderr, "maxsamples = %p \n", maxsamples); log = &(ctrl_usr->log); horiz = &(ctrl_usr->horiz); sample_period_ns = horiz->thread_period_ns * ctrl_shm->mult; //for testing, this will be a check box or something eventually log->order=INTERLACED; /* write data */ fprintf(fp, "Sampling period is %i nSec \n", sample_period_ns ); /* point to the first sample in the display buffer */ start = ctrl_usr->disp_buf ; switch (log->order) { case INTERLACED: while (n <= samples) { for (chan_num=0; chan_num<sample_len; chan_num++) { dptr=start+n; if ((n%sample_len)==0){ fprintf( fp, "\n"); } write_sample( fp, label[chan_num], dptr, type[chan_num]); /* point to next sample */ n++; } } break; case NOT_INTERLACED: for (chan_num=0; chan_num<sample_len; chan_num++) { n=chan_num; while (n <= samples) { dptr=start+n; write_sample( fp, label[chan_num], dptr, type[chan_num]); fprintf( fp, "\n"); /* point to next sample */ n += sample_len; } } break; } fclose(fp); fprintf(stderr, "Log file '%s' written.\n", filename );}/* format the data and print it */void write_sample(FILE *fp, char *label, scope_data_t *dptr, hal_type_t type){ double data_value; switch (type) { case HAL_BIT: if (dptr->d_u8) { data_value = 1.0; } else { data_value = 0.0; }; break; case HAL_FLOAT: data_value = dptr->d_float; break; case HAL_S8: data_value = dptr->d_s8; break; case HAL_U8: data_value = dptr->d_u8; break; case HAL_S16: data_value = dptr->d_s16; break; case HAL_U16: data_value = dptr->d_u16; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -