📄 scope_vert.c
字号:
/** This file, 'halsc_vert.c', contains the portion of halscope that deals with vertical stuff - signal sources, scaling, position and such.*//** 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 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 <ctype.h>#include <string.h>#include "rtapi.h" /* RTAPI realtime OS API */#include "hal.h" /* HAL public API decls */#include "../hal_priv.h" /* private HAL decls */#include <gtk/gtk.h>#include <gdk/gdkkeysyms.h>#include "miscgtk.h" /* generic GTK stuff */#include "scope_usr.h" /* scope related declarations */#define BUFLEN 80 /* length for sprintf buffers *//************************************************************************ GLOBAL VARIABLES DECLARATIONS *************************************************************************/#define VERT_POS_RESOLUTION 100.0/* The channel select buttons sometimes need to be toggled by the code rather than the user, without causing any action. This global is used for that */static int ignore_click = 0;/************************************************************************ LOCAL FUNCTION PROTOTYPES *************************************************************************/struct offset_data { char buf[BUFLEN]; int ac_coupled;};static void init_chan_sel_window(void);static void init_chan_info_window(void);static void init_vert_info_window(void);static gboolean dialog_select_source(int chan_num);static void selection_made(GtkWidget * clist, gint row, gint column, GdkEventButton * event, dialog_generic_t * dptr);static void change_source_button(GtkWidget * widget, gpointer gdata);static void channel_off_button(GtkWidget * widget, gpointer gdata);static void offset_button(GtkWidget * widget, gpointer gdata);static gboolean dialog_set_offset(int chan_num);static void scale_changed(GtkAdjustment * adj, gpointer gdata);static void offset_changed(GtkEditable * editable, struct offset_data *);static void offset_activated(GtkEditable * editable, gchar * button);static void pos_changed(GtkAdjustment * adj, gpointer gdata);static void chan_sel_button(GtkWidget * widget, gpointer gdata);/* helper functions */static void write_chan_config(FILE *fp, scope_chan_t *chan);/************************************************************************ PUBLIC FUNCTIONS *************************************************************************/void init_vert(void){ scope_vert_t *vert; scope_chan_t *chan; int n; /* stop sampling */ ctrl_shm->state = IDLE; /* make a pointer to the vert structure */ vert = &(ctrl_usr->vert); /* init non-zero members of the vertical structure */ invalidate_all_channels(); /* init non-zero members of the channel structures */ for (n = 1; n <= 16; n++) { chan = &(ctrl_usr->chan[n - 1]); chan->position = 0.5; } /* set up the windows */ init_chan_sel_window(); init_chan_info_window(); init_vert_info_window();}int set_active_channel(int chan_num){ int n, count; scope_vert_t *vert; scope_chan_t *chan; if (( chan_num < 1 ) || ( chan_num > 16 )) { return -1; } vert = &(ctrl_usr->vert); chan = &(ctrl_usr->chan[chan_num - 1]); if (vert->chan_enabled[chan_num - 1] == 0 ) { /* channel is disabled, want to enable it */ if (ctrl_shm->state != IDLE) { /* acquisition in progress, must restart it */ prepare_scope_restart(); } count = 0; for (n = 0; n < 16; n++) { if (vert->chan_enabled[n]) { count++; } } if (count >= ctrl_shm->sample_len) { /* max number of channels already enabled */ return -2; } if (chan->name == NULL) { /* no signal source */ return -3; } /* "push" the button in to indicate enabled channel */ ignore_click = 1; gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vert->chan_sel_buttons[chan_num-1]), TRUE); vert->chan_enabled[chan_num - 1] = 1; } if (vert->selected != chan_num) { /* make chan_num the selected channel */ vert->selected = chan_num; channel_changed(); } return 0;}int set_channel_off(int chan_num){ scope_vert_t *vert; int n; if ((chan_num < 1) || (chan_num > 16)) { return -1; } vert = &(ctrl_usr->vert); if ( vert->chan_enabled[chan_num - 1] == 0 ) { /* channel is already off, nothing to do */ return -1; } vert->chan_enabled[chan_num - 1] = 0; /* force the button to pop out */ ignore_click = 1; gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(vert-> chan_sel_buttons[chan_num - 1]), FALSE); if ( chan_num == vert->selected ) { /* channel was selected, pick new selected channel */ n = 0; vert->selected = 0; do { chan_num++; if (chan_num > 16) { chan_num = 1; } if (vert->chan_enabled[chan_num - 1] != 0) { vert->selected = chan_num; } } while ((++n < 16) && (vert->selected == 0)); } channel_changed(); return 0;}int set_channel_source(int chan_num, int type, char *name){ scope_vert_t *vert; scope_chan_t *chan; hal_pin_t *pin; hal_sig_t *sig; hal_param_t *param; vert = &(ctrl_usr->vert); chan = &(ctrl_usr->chan[chan_num - 1]); /* locate the selected item in the HAL */ if (type == 0) { /* search the pin list */ pin = halpr_find_pin_by_name(name); if (pin == NULL) { /* pin not found */ return -1; } chan->data_source_type = 0; chan->data_source = SHMOFF(pin); chan->data_type = pin->type; chan->name = pin->name; } else if (type == 1) { /* search the signal list */ sig = halpr_find_sig_by_name(name); if (sig == NULL) { /* signal not found */ return -1; } chan->data_source_type = 1; chan->data_source = SHMOFF(sig); chan->data_type = sig->type; chan->name = sig->name; } else if (type == 2) { /* search the parameter list */ param = halpr_find_param_by_name(name); if (param == NULL) { /* parameter not found */ return -1; } chan->data_source_type = 2; chan->data_source = SHMOFF(param); chan->data_type = param->type; chan->name = param->name; } switch (chan->data_type) { case HAL_BIT: chan->data_len = sizeof(hal_bit_t); chan->min_index = -2; chan->max_index = 2; break; case HAL_FLOAT: chan->data_len = sizeof(hal_float_t); chan->min_index = -36; chan->max_index = 36; break; case HAL_S32: chan->data_len = sizeof(hal_s32_t); chan->min_index = -2; chan->max_index = 30; break; case HAL_U32: chan->data_len = sizeof(hal_u32_t); chan->min_index = -2; chan->max_index = 30; break; default: /* Shouldn't get here, but just in case... */ chan->data_len = 0; chan->min_index = -1; chan->max_index = 1; } /* invalidate any data in the buffer for this channel */ vert->data_offset[chan_num - 1] = -1; /* set scale and offset to nominal values */ chan->vert_offset = 0.0; chan->scale_index = 0; /* return success */ return 0;}int set_vert_scale(int setting){ scope_vert_t *vert; scope_chan_t *chan; GtkAdjustment *adj; int chan_num, index; float scale; gchar buf[BUFLEN]; vert = &(ctrl_usr->vert); chan_num = vert->selected; if ((chan_num < 1) || (chan_num > 16)) { return -1; } chan = &(ctrl_usr->chan[chan_num - 1]); if ((setting > chan->max_index) || (setting < chan->min_index)) { /* value out of range for this data type */ return -1; } /* save new index */ chan->scale_index = setting; /* set scale slider based on new setting */ adj = GTK_ADJUSTMENT(vert->scale_adj); gtk_adjustment_set_value(adj, setting); /* compute scale factor */ scale = 1.0; index = chan->scale_index; while (index >= 3) { scale *= 10.0; index -= 3; } while (index <= -3) { scale *= 0.1; index += 3; } switch (index) { case 2: scale *= 5.0; break; case 1: scale *= 2.0; break; case -1: scale *= 0.5; break; case -2: scale *= 0.2; break; default: break; } chan->scale = scale; format_scale_value(buf, BUFLEN - 1, scale); gtk_label_set_text_if(vert->scale_label, buf); if (chan_num == ctrl_shm->trig_chan) { refresh_trigger(); } request_display_refresh(1); return 0;}int set_vert_pos(double setting){ scope_vert_t *vert; scope_chan_t *chan; int chan_num; GtkAdjustment *adj; /* range check setting */ if (( setting < 0.0 ) || ( setting > 1.0 )) { return -1; } /* point to data */ vert = &(ctrl_usr->vert); chan_num = vert->selected; if ((chan_num < 1) || (chan_num > 16)) { return -1; } chan = &(ctrl_usr->chan[chan_num - 1]); chan->position = setting; /* set position slider based on new setting */ adj = GTK_ADJUSTMENT(vert->pos_adj); gtk_adjustment_set_value(adj, chan->position * VERT_POS_RESOLUTION); /* refresh other stuff */ if (chan_num == ctrl_shm->trig_chan) { refresh_trigger(); } request_display_refresh(1); return 0;}int set_vert_offset(double setting, int ac_coupled){ scope_vert_t *vert; scope_chan_t *chan; int chan_num; gchar buf1[BUFLEN + 1], buf2[BUFLEN + 1]; /* point to data */ vert = &(ctrl_usr->vert); chan_num = vert->selected; if ((chan_num < 1) || (chan_num > 16)) { return -1; } chan = &(ctrl_usr->chan[chan_num - 1]); /* set the new offset */ chan->vert_offset = setting; chan->ac_offset = ac_coupled; /* update the offset display */ if (chan->data_type == HAL_BIT) { snprintf(buf1, BUFLEN, "----"); } else { if(chan->ac_offset) { snprintf(buf1, BUFLEN, "(AC)"); } else { format_signal_value(buf1, BUFLEN, chan->vert_offset); } } snprintf(buf2, BUFLEN, "Offset\n%s", buf1); gtk_label_set_text_if(vert->offset_label, buf2); /* refresh other stuff */ if (chan_num == ctrl_shm->trig_chan) { refresh_trigger(); } request_display_refresh(1); return 0;}void format_signal_value(char *buf, int buflen, float value){ char *units; int decimals; char sign, symbols[] = "pnum KMGT"; if (value < 0) { value = -value; sign = '-'; } else { sign = '+'; } if (value <= 1.0e-24) { /* pretty damn small, call it zero */ snprintf(buf, buflen, "0.000"); return; } if (value <= 1.0e-12) { /* less than pico units, use scientific notation */ snprintf(buf, buflen, "%c%10.3e", sign, value); return; } if (value >= 1.0e+12) { /* greater than tera-units, use scientific notation */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -