From 8ae8280e1d5e5af4f4f538a380535cd5048a7558 Mon Sep 17 00:00:00 2001 From: Xiao Pan Date: Wed, 30 Apr 2025 03:08:12 -0700 Subject: cleaner code, return and continue instead of put inside a large if --- remote_plot.c | 173 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 86 insertions(+), 87 deletions(-) diff --git a/remote_plot.c b/remote_plot.c index 512e40c..9fe3b1d 100644 --- a/remote_plot.c +++ b/remote_plot.c @@ -474,108 +474,107 @@ static void draw_function (GtkDrawingArea *area, gpointer user_data) { DATA *data=user_data; - if(!ListIsEmpty(&(data->cans))) - { - //printf("draw begin\n"); - //PLFLT xmin=data->cans.head->item.t; - //PLFLT xmax=data->cans.end->item.t; - PLFLT xmin,xmax; - PLFLT ymin, ymax; - int plot_counts=0; - gboolean active_checkbutton[VOLTLEN+TEMPLEN]; - - if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->live_toggle))) - xmax=data->cans.end->item.t; - else - xmax=data->to_time; + if(ListIsEmpty(&data->cans)) + return; + + //printf("draw begin\n"); + //PLFLT xmin=data->cans.head->item.t; + //PLFLT xmax=data->cans.end->item.t; + PLFLT xmin,xmax; + PLFLT ymin, ymax; + int plot_counts=0; + gboolean active_checkbutton[VOLTLEN+TEMPLEN]; + + if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->live_toggle))) + xmax=data->cans.end->item.t; + else + xmax=data->to_time; - if(data->from_time_entered) - xmin=data->from_time; - else - xmin=data->cans.head->item.t; + if(data->from_time_entered) + xmin=data->from_time; + else + xmin=data->cans.head->item.t; - for(int i=0;i<(VOLTLEN+TEMPLEN);i++) - if((active_checkbutton[i]=gtk_check_button_get_active(GTK_CHECK_BUTTON(data->checkbutton[i])))) - plot_counts++; + for(int i=0;i<(VOLTLEN+TEMPLEN);i++) + if((active_checkbutton[i]=gtk_check_button_get_active(GTK_CHECK_BUTTON(data->checkbutton[i])))) + plot_counts++; - plsdev( "extcairo" ); + plsdev( "extcairo" ); + { + char str[12]; + sprintf(str,"%dx%d", + gtk_widget_get_width(data->area), + gtk_widget_get_height(data->area)); + plsetopt("geometry", str); + } + + // change to white background and black foreground, and gray color palette + // http://plplot.sourceforge.net/docbook-manual/plplot-html-5.15.0/color.html + // seems should be put before plinit; or need pladv(), plvpor(), plwind() for a new picture? + // more see example 16 + // http://plplot.sourceforge.net/examples.php?demo=16 + // /usr/share/plplot5.15.0/examples/c/x16c.c + plspal0("cmap0_black_on_white.pal"); + plspal1("cmap1_gray.pal", 1); + + // Initialize plplot + if(plot_counts>3) + plstar(3,(plot_counts+2)/3); + else + plstar(1,plot_counts); + pl_cmd( PLESC_DEVINIT, cr ); + + // 10 char unix time: 1745925459 + // set numbers of digits to display x axis labels + plsxax(10,0); + + for(int i=0;i<(VOLTLEN+TEMPLEN);i++) + { + if(!active_checkbutton[i]) + continue; + if(iarea), - gtk_widget_get_height(data->area)); - plsetopt("geometry", str); + // according to .dbc file + ymin=0; + ymax=6.; } - - // change to white background and black foreground, and gray color palette - // http://plplot.sourceforge.net/docbook-manual/plplot-html-5.15.0/color.html - // seems should be put before plinit; or need pladv(), plvpor(), plwind() for a new picture? - // more see example 16 - // http://plplot.sourceforge.net/examples.php?demo=16 - // /usr/share/plplot5.15.0/examples/c/x16c.c - plspal0("cmap0_black_on_white.pal"); - plspal1("cmap1_gray.pal", 1); - - // Initialize plplot - if(plot_counts>3) - plstar(3,(plot_counts+2)/3); else - plstar(1,plot_counts); - pl_cmd( PLESC_DEVINIT, cr ); - - // 10 char unix time: 1745925459 - // set numbers of digits to display x axis labels - plsxax(10,0); + { + // according to .dbc file + ymin=-50; + ymax=100; + } - for(int i=0;i<(VOLTLEN+TEMPLEN);i++) + // Create a labelled box to hold the plot. + plenv(xmin, xmax, ymin, ymax, 0, 0); + if(icans.head; + while(pnode!=NULL && ((pnode->item.t <= xmax) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->live_toggle))) ) { - if(iitem.t >= xmin) { - // according to .dbc file - ymin=0; - ymax=6.; - } - else - { - // according to .dbc file - ymin=-50; - ymax=100; - } - - // Create a labelled box to hold the plot. - plenv(xmin, xmax, ymin, ymax, 0, 0); - if(icans.head; - while(pnode!=NULL && ((pnode->item.t <= xmax) || gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->live_toggle))) ) - { - if(pnode->item.t >= xmin) - { - if(iitem.t,pnode->item.volt[i],1.,0.,0.5,"#(727)"); - else - plptex(pnode->item.t,pnode->item.temp[i-VOLTLEN],1.,0.,0.5,"#(727)"); - } - pnode=pnode->next; - } + if(iitem.t,pnode->item.volt[i],1.,0.,0.5,"#(727)"); + else + plptex(pnode->item.t,pnode->item.temp[i-VOLTLEN],1.,0.,0.5,"#(727)"); } + pnode=pnode->next; } } - - // Close PLplot library - plend(); } + + // Close PLplot library + plend(); } static void print_data (GtkWidget *widget, gpointer user_data) -- cgit v1.2.3-70-g09d2