⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 meter.c

📁 Source code for an Numeric Cmputer
💻 C
📖 第 1 页 / 共 2 页
字号:
/** This file, 'meter.c', is a GUI program that serves as a simple    meter to look at HAL signals.  It is a user space component and    uses GTK 1.2 for the GUI code.  It allows you to view one pin,    signal, or parameter, and updates its display about 10 times    per second.  (It is not a realtime program, and heavy loading    can temporarily slow or stop the update.)  Clicking on the 'Select'    button pops up a dialog that allows you to select what pin/signal/    parameter you want to monitor.  Multiple instances of the program    can be started if you want to monitor more than one item.  If you    add "pin|sig|par[am] <name>" to the command line, the meter will    initially display the pin/signal/parameter <name>, otherwise it    will initially display nothing.*//** 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 "../hal_priv.h"	/* private HAL decls */#include <gtk/gtk.h>#include "miscgtk.h"		/* generic GTK stuff *//************************************************************************                            TYPEDEFS                                  *************************************************************************//** a 'probe' is an object that references a HAL pin, signal, or    parameter.  The user may select the item that is to be probed.*/#define PROBE_NAME_LEN 63typedef struct {    int listnum;		/* 0 = pin, 1 = signal, 2 = parameter */    char *pickname;		/* name from list, not validated */    hal_pin_t *pin;		/* metadata (if it's a pin) */    hal_sig_t *sig;		/* metadata (if it's a signal) */    hal_param_t *param;		/* metadata (if it's a parameter) */    GtkWidget *window;		/* selection dialog window */    GtkWidget *notebook;	/* pointer to the notebook */    GtkWidget *lists[3];	/* lists for pins, sigs, and params */    char probe_name[PROBE_NAME_LEN + 1];	/* name of this probe */} probe_t;typedef struct {    probe_t *probe;		/* probe that locates the data */    GtkWidget *value_label;	/* label object to display value */    GtkWidget *name_label;	/* label object to display name */} meter_t;/************************************************************************                  GLOBAL VARIABLES DECLARATIONS                       *************************************************************************/int comp_id;			/* HAL component ID */GtkWidget *main_window;meter_t *meter;int small;/************************************************************************                  LOCAL FUNCTION PROTOTYPES                           *************************************************************************/static meter_t *meter_new(void);/** 'probe_new()' creates a new probe structure.  It also creates    a dialog window for the probe that allows the user to pick the    pin, signal, or parameter that the probe will attach to.  It    should be called during the init phase of the program, before    the main event loop is started.*/static probe_t *probe_new(char *probe_name);/** 'popup_probe_window()' is an event handler function that opens    the selection dialog for a probe.  'data' must be a pointer to    a probe_t structure that was allocated by 'probe_new'.*/static void popup_probe_window(GtkWidget * widget, gpointer data);static void quit(int sig);static void exit_from_hal(void);static int refresh_value(gpointer data);static char *data_value(int type, void *valptr);static void create_probe_window(probe_t * probe);static void apply_selection(GtkWidget * widget, gpointer data);static void apply_selection_and_close(GtkWidget * widget, gpointer data);static void close_selection(GtkWidget * widget, gpointer data);static void selection_made(GtkWidget * clist, gint row, gint column,    GdkEventButton * event, gpointer data);/************************************************************************                        MAIN() FUNCTION                               *************************************************************************/int main(int argc, gchar * argv[]){    GtkWidget *vbox, *hbox;    GtkWidget *button_select, *button_exit;    char buf[30];    int initial_type, n, height;    char *initial_name, *win_name;    /* process and remove any GTK specific command line args */    gtk_init(&argc, &argv);    /* process my own command line args (if any) here */    small = 0;    n = 1;    if ( argc > n ) {	if ( strcmp (argv[n], "-s") == 0 ) {	    small = 1;	    n++;	}    }    if (argc > n) {	/* check for user specified initial probe point */	if (strncmp(argv[n], "pin", 3) == 0) {	    /* initial probe is a pin */	    initial_type = 0;	} else if (strncmp(argv[n], "sig", 3) == 0) {	    /* initial probe is a signal */	    initial_type = 1;	} else if (strncmp(argv[n], "par", 3) == 0) {	    /* initial probe is a parameter */	    initial_type = 2;	} else {	    printf("ERROR: '%s' is not a valid probe type\n", argv[n]);	    return -1;	}	n++;	if ( argc > n ) {	    initial_name = argv[n];	} else {	    printf("ERROR: no pin/signal/parameter name\n");	    return -1;	}    } else {	initial_type = 0;	initial_name = NULL;    }    /* create a unique module name */    snprintf(buf, 29, "halmeter-%d", getpid());    /* connect to the HAL */    comp_id = hal_init(buf);    if (comp_id < 0) {	return -1;    }    /* register an exit function to disconnect from the HAL */    g_atexit(exit_from_hal);    /* capture INT (ctrl-C) and TERM signals */    signal(SIGINT, quit);    signal(SIGTERM, quit);    /* create main window, set it's size, and lock the size */    main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);    /* ideally this wouldn't be fixed size in pixels */    if ( small ) {	height = 22;	win_name = initial_name;    } else {	height = 80;	win_name = "Hal Meter";    }    gtk_widget_set_usize(GTK_WIDGET(main_window), 270, height);    gtk_window_set_policy(GTK_WINDOW(main_window), FALSE, FALSE, FALSE);    /* set main window title */    gtk_window_set_title(GTK_WINDOW(main_window), win_name);    /* this makes the application exit when the window is closed */    gtk_signal_connect(GTK_OBJECT(main_window), "destroy",	GTK_SIGNAL_FUNC(gtk_main_quit), NULL);    /* a vbox to hold the displayed value and the pin/sig/param name */    vbox = gtk_vbox_new(FALSE, 3);    gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);    /* add the vbox to the main window */    gtk_container_add(GTK_CONTAINER(main_window), vbox);    gtk_widget_show(vbox);    /* create a meter object */    meter = meter_new();    if (meter == NULL) {	exit(-1);    }    /* set up for initial probe, if any */    if (initial_name != NULL) {	meter->probe->pickname = initial_name;	meter->probe->listnum = initial_type;	apply_selection(NULL, meter->probe);    }    /* add the meter's value label to the vbox */    gtk_box_pack_start(GTK_BOX(vbox), meter->value_label, TRUE, TRUE, 0);    gtk_widget_show(meter->value_label);    /* add the meter's name label to the vbox */    if ( !small ) {	gtk_box_pack_start(GTK_BOX(vbox), meter->name_label, TRUE, TRUE, 0);	gtk_widget_show(meter->name_label);    }    /* arrange for periodic refresh of the value */    gtk_timeout_add(100, refresh_value, meter);    /* an hbox to hold the select and exit buttons */    if ( !small ) {	hbox = gtk_hbox_new_in_box(FALSE, 0, 0, vbox, FALSE, TRUE, 0);	/* create the buttons and add them to the hbox */	button_select = gtk_button_new_with_label("Select");	button_exit = gtk_button_new_with_label("Exit");	gtk_box_pack_start(GTK_BOX(hbox), button_select, TRUE, TRUE, 4);	gtk_box_pack_start(GTK_BOX(hbox), button_exit, TRUE, TRUE, 4);	/* make the application exit when the 'exit' button is clicked */	gtk_signal_connect(GTK_OBJECT(button_exit), "clicked",	    GTK_SIGNAL_FUNC(gtk_main_quit), NULL);	/* activate selection window when the 'select' button is clicked */	gtk_signal_connect(GTK_OBJECT(button_select), "clicked",	    GTK_SIGNAL_FUNC(popup_probe_window), meter->probe);	gtk_widget_show(button_select);	gtk_widget_show(button_exit);    }    /* The interface is now set up so we show the window and       enter the gtk_main loop. */    gtk_widget_show(main_window);    gtk_main();    return (0);}/************************************************************************                         LOCAL FUNCTION CODE                          *************************************************************************/static meter_t *meter_new(void){    meter_t *new;    /* allocate a meter object for the display */    new = malloc(sizeof(meter_t));    if (new == NULL) {	return NULL;    }    /* define a probe for the display item */    new->probe = probe_new("Select item to display");    if (new->probe == NULL) {	free(new);	return NULL;    }    /* create a label widget to hold the value */    new->value_label = gtk_label_new("----");    /* center justify text, no wordwrap */    gtk_label_set_justify(GTK_LABEL(new->value_label), GTK_JUSTIFY_CENTER);    gtk_label_set_line_wrap(GTK_LABEL(new->value_label), FALSE);    /* create a label widget to hold the name */    if ( !small ) {	new->name_label = gtk_label_new("------");	/* center justify text, no wordwrap */	gtk_label_set_justify(GTK_LABEL(new->name_label),	    GTK_JUSTIFY_CENTER);	gtk_label_set_line_wrap(GTK_LABEL(new->name_label), FALSE);    }    return new;}probe_t *probe_new(char *probe_name){    probe_t *new;    if (probe_name != NULL) {	/* no name specified, fake it */	probe_name = "Select Item to Probe";    }    /* allocate a new probe structure */    new = malloc(sizeof(probe_t));    if (new == NULL) {	return NULL;    }    /* init the fields */    new->pickname = NULL;    new->listnum = -1;    new->pin = NULL;    new->sig = NULL;    new->param = NULL;    strncpy(new->probe_name, probe_name, HAL_NAME_LEN);    /* window will be created just before it is displayed */    new->window = NULL;    /* done */    return new;}void popup_probe_window(GtkWidget * widget, gpointer data){    probe_t *probe;    int next, row;    hal_pin_t *pin;    hal_sig_t *sig;    hal_param_t *param;    gchar *name;    /* get a pointer to the probe data structure */    probe = (probe_t *) data;    /* create window if needed */    if (probe->window == NULL) {	create_probe_window(probe);    }    /* this selects the page holding the current selected probe */    if (probe->listnum >= 0) {	gtk_notebook_set_page((GtkNotebook *)probe->notebook, probe->listnum);    }    gtk_clist_clear(GTK_CLIST(probe->lists[0]));    gtk_clist_clear(GTK_CLIST(probe->lists[1]));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -