Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

gtkdaq.c

Go to the documentation of this file.
00001 
00010 #include <stdio.h>
00011 #include <gtk/gtk.h>
00012 #include <gtkdatabox.h>
00013 #include <math.h>
00014 
00015 #include "di194.h"
00016 
00019 static gfloat *analog = NULL;
00020 static gfloat *digital_0 = NULL;
00021 static gfloat *digital_1 = NULL;
00022 static GdkColor analog_color = 
00023 {
00024     0, 0, 65535
00025 };
00026 
00027 static GdkColor d0_color = 
00028 {
00029     65535, 0, 0
00030 };
00031 static GdkColor d1_color = 
00032 {
00033     0, 65535, 0
00034 };
00035 
00036 static gfloat *x_axis = NULL;
00037 
00038 static gint     bars_idle=0;
00039 static GtkWidget *bars_label=NULL;
00040 static guint    bars_counter=0;
00041 
00042 static int COMM_FD;
00043 const static int DRAW_TYPE = GTK_DATABOX_LINES;
00044 const static int PT_SIZE = 1;
00045 const static int NUM_PTS = 240;
00046 volatile bool di_paused;
00047 
00048 static void di_pause(GtkWidget *widget, GtkWidget **window)
00049 {
00050     int    rc;
00051 
00052     if(di_paused)
00053         rc = di_start(COMM_FD);
00054     else
00055         rc = di_stop(COMM_FD);
00056 
00057     di_paused = !di_paused;
00058     fprintf(stderr, "\npause called, pause now %d", di_paused);
00059 }
00060         
00061 static void bars_destroy(GtkWidget *widget, GtkWidget **window)
00062 {
00063     if (bars_idle) 
00064     {
00065         gtk_idle_remove(bars_idle);
00066         bars_idle=0;
00067     }
00068 
00069     *window=NULL;  
00070 }
00071 
00072 static void do_exit (GtkWidget *widget, GtkWidget *window)
00073 {
00074     di_close(COMM_FD);
00075     // free x_axis, y1, y2?
00076     gtk_widget_destroy (window);
00077     gtk_main_quit ();
00078 }
00079 
00080 // Idle function, used to read data
00081 static gboolean my_bars_idle_func(GtkDatabox *box)
00082 {
00083     daq_reading_t  data;
00084     int            rc;
00085     char           label[128] = "";
00086     int            idx;
00087     
00088 
00089     if(di_paused)
00090     {
00091         gtk_entry_set_text(GTK_ENTRY(bars_label), "Paused");
00092         
00093         return FALSE;
00094     }
00095 
00096     // Read the data from the device
00097     rc = di_read(COMM_FD, &data);
00098     if(rc != 0)
00099     {
00100         sprintf(label, "Error %d on data read", rc);
00101         gtk_entry_set_text(GTK_ENTRY(bars_label), label);
00102         
00103         return TRUE;
00104     }
00105     
00106     // Hack for now!
00107     //    sprintf(label, "%5.2f", data.analog[0]);
00108     //    gtk_entry_set_text(GTK_ENTRY(bars_label), label);
00109 
00110     for(idx = 0; idx < 4; idx++, bars_counter++)
00111     {
00112         analog[bars_counter] = data.analog[idx];
00113 
00114         // For now, just read bit D0
00115         digital_0[bars_counter] = data.digital[0][idx];
00116         digital_1[bars_counter] = data.digital[1][idx];
00117     }
00118 
00119     // Scope wraparound - clear screen
00120     if(bars_counter >= NUM_PTS)
00121         bars_counter = 0;
00122 
00123     gtk_databox_redraw(GTK_DATABOX(box));
00124     
00125     return TRUE;
00126 }
00127 
00128 static void create_bars(void)
00129 {
00130     static GtkWidget *window = NULL;
00131     GtkWidget  *box1;
00132     GtkWidget *box2;
00133     GtkWidget *close_button;
00134     GtkWidget *pause_button;
00135     GtkWidget *box;
00136     GtkWidget *label;
00137     GtkWidget *separator;
00138     gint i;
00139     GtkDataboxValue  disp_min, disp_max;
00140     
00141 
00142     if (!window)
00143     {
00144         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
00145         gtk_widget_set_usize (window, 640, 480);  
00146         
00147         gtk_signal_connect (GTK_OBJECT (window), "destroy",
00148                             GTK_SIGNAL_FUNC(bars_destroy),
00149                             &window);
00150         
00151         gtk_window_set_title (GTK_WINDOW (window), "Databox Bars");
00152         gtk_container_border_width (GTK_CONTAINER (window), 0);
00153         
00154         box1 = gtk_vbox_new (FALSE, 0);
00155         gtk_container_add (GTK_CONTAINER (window), box1);
00156         
00157         label=gtk_label_new("DI-194 oscilloscope");
00158         
00159         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
00160         separator=gtk_hseparator_new();
00161         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
00162         bars_label=gtk_entry_new();
00163         gtk_entry_set_text(GTK_ENTRY(bars_label), "0");
00164         gtk_box_pack_start (GTK_BOX (box1), bars_label, FALSE, FALSE, 0);
00165         separator=gtk_hseparator_new();
00166         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, FALSE, 0);
00167         
00168         bars_idle=0;
00169         bars_counter=0;
00170         
00171         box=gtk_databox_new();
00172         gtk_signal_connect (GTK_OBJECT (box), "destroy",
00173                             GTK_SIGNAL_FUNC(gtk_databox_data_destroy_all),
00174                             NULL);
00175         
00176         // Allocate vectors for X, Y, Y2
00177         x_axis = g_new0(gfloat, NUM_PTS);
00178         analog = g_new0(gfloat, NUM_PTS);
00179         digital_0 = g_new0(gfloat, NUM_PTS);
00180         digital_1 = g_new0(gfloat, NUM_PTS);
00181 
00182         // Initialize x axis - others set to zero by new0
00183         for (i = 0; i < NUM_PTS; i++ ) 
00184             x_axis[i] = i;
00185         
00186         gtk_databox_data_add_x_y(GTK_DATABOX(box), NUM_PTS, 
00187                                  x_axis, analog, analog_color, 
00188                                  DRAW_TYPE, PT_SIZE);
00189         gtk_databox_data_add_x_y(GTK_DATABOX(box), NUM_PTS, 
00190                                  x_axis, digital_0, d0_color, 
00191                                  DRAW_TYPE, PT_SIZE);
00192         gtk_databox_data_add_x_y(GTK_DATABOX(box), NUM_PTS, 
00193                                  x_axis, digital_1, d1_color, 
00194                                  DRAW_TYPE, PT_SIZE);
00195         // Set viewing range
00196         disp_min.x = 0;
00197         disp_min.y = -10.0;
00198         disp_max.x = NUM_PTS;
00199         disp_max.y = 10.0;
00200 
00201         gtk_databox_rescale_with_values(GTK_DATABOX(box),
00202                                         disp_min, disp_max);
00203         
00204         gtk_box_pack_start (GTK_BOX (box1), box, TRUE, TRUE, 0);
00205         
00206         separator = gtk_hseparator_new();
00207         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
00208         
00209         box2 = gtk_vbox_new (FALSE, 10);
00210         gtk_container_border_width (GTK_CONTAINER (box2), 10);
00211         gtk_box_pack_end (GTK_BOX (box1), box2, FALSE, TRUE, 0);
00212         close_button = gtk_button_new_with_label("close");
00213         pause_button = gtk_button_new_with_label("pause");
00214         
00215         gtk_signal_connect_object_after (GTK_OBJECT (close_button), "clicked",
00216                                          GTK_SIGNAL_FUNC(do_exit),
00217                                          GTK_OBJECT (window));
00218         
00219         gtk_signal_connect_object_after (GTK_OBJECT (pause_button), "clicked",
00220                                          GTK_SIGNAL_FUNC(di_pause),
00221                                          GTK_OBJECT (window));
00222         
00223         gtk_box_pack_start (GTK_BOX (box2), close_button, TRUE, TRUE, 0);
00224         gtk_box_pack_start (GTK_BOX (box2), pause_button, TRUE, TRUE, 0);
00225 
00226         GTK_WIDGET_SET_FLAGS (close_button, GTK_CAN_DEFAULT);
00227         GTK_WIDGET_SET_FLAGS (pause_button, GTK_CAN_DEFAULT);
00228         gtk_widget_grab_default (pause_button);
00229         
00230         // Hook in idle function
00231         bars_idle=gtk_idle_add((GtkFunction) my_bars_idle_func, box);
00232         
00233         // Set initial label
00234         gtk_entry_set_text(GTK_ENTRY(bars_label), "Running");
00235     }
00236     
00237     if (!GTK_WIDGET_VISIBLE (window))
00238         gtk_widget_show_all (window);
00239     else 
00240     {
00241         gtk_widget_destroy (window);
00242     }
00243 }
00244 
00245 gint main(gint argc, char *argv[])
00246 {
00247     char     me[] = "main";
00248     int      rc;
00249     
00250     // Now seems a good time to open comms
00251     rc = di_open(NULL);
00252     if(rc <= 0)
00253     {
00254         fprintf(stderr, "\n%s: Error %d opening DI-194\n",
00255                 me, rc);
00256         return(1);
00257     }
00258     else
00259         COMM_FD = rc;
00260         
00261     // Start DAQ rollingt
00262     rc = di_start(COMM_FD);
00263     if(rc != 0)
00264     {
00265         fprintf(stderr, "\nError %d starting DAQ\n", rc);
00266         return(2);
00267     }
00268     
00269     gtk_init(&argc, &argv);
00270     
00271     create_bars();
00272     
00273     gtk_main();
00274     
00275     return 0;
00276 }

Generated on Thu May 1 11:31:46 2003 for DI-194 driver by doxygen1.3