14.4. GTK: Recording Data - record_data, countdown

The record_data function gets called whenever the Record button is pressed. It sets up a timer to record data for the specified number of seconds, and then calls other History functions to send a query and prepare a plot of the data.

   /* Assign a global variable for recording time, and a font. */
   RECORD_TIME = 5;
   FONT = gdk_font_load ("-adobe-courier-medium-r-normal--*-120-*-*-*-*-*-*");
   
   /*--------------------------------------------------------------------
    * Function:    record_data
    * Returns:     t or nil
    * Description: Records data for the specified time, then sends a query.
    * Arguments:   button:       The Record button
    *              iset:         An InterpolatorSettings instance
    *              e1 - e6:      The six Interpolator options entry widgets
    *              e7:           The "Number of points from last query" display
    *              but_db1 - 3:  The three Deadband options buttons
    *              b1 - b4:      The four Interpolator options buttons
    *              b6:           The Update display button
    *------------------------------------------------------------------*/
   function record_data(button, iset, but_db1, but_db2, but_db3, text,
                        e1, e2, e3, e4, e5, e6, e7, b1, b2, b3, b4, b6)
   {
     if (button.switched_on())
       {
         local time = clock() + RECORD_TIME;
         text.backward_delete(text.get_length());
         text.insert(FONT, nil, nil,
                     (string("Recording,please wait.\nSeconds remaining: ", RECORD_TIME)),
                     -1);
         numtimer = every (1, `countdown(@text, @time));
         

The first step is to set up the text display with a timed countdown feature. We use Gamma's every timer function and the countdown function (below) to create the effect of a second-by-second countdown until completion of the data recording.

The next step is to prepare for and send an enable command to the Cascade Historian to start recording data, using the InterpolatorSettings.set_defaults method and the send_hs_command function.

         e3.set_text(string(RECORD_TIME + 1));
         iset.set_defaults(button, e2);
         send_hs_command(button, "enable", e1.get_text(), nil);
         

Once the time elapses, we have to pop the Record button out, stop the recording, and cancel the timer. Then we prepare the display with the reset_deadbands function. This has the effect of selecting only the first deadband option. The reset_deadbands function calls the plot_prep function to prepare the plot, which in turn calls the send_query function to make the query. The tasks in this part of the record_data function are performed at the right time, and in proper sequence with the help of the Gamma after timer function.

         after(RECORD_TIME, `(@button).set_active(FALSE));
         after((RECORD_TIME + .1), `(send_hs_command(@button, "enable",
                                                     (@e1).get_text(), nil)));
         after((RECORD_TIME + .1), `(cancel(numtimer)));
         after((RECORD_TIME + .15),
               `(reset_deadbands(nil, @iset, @but_db1, @but_db2, @but_db3, @text,
                                 @e1, @e2, @e3, @e4, @e5, @e6, @e7, @b1,
                                 @b2, @b3, @b4, @button, @b6)));
       }
   }
   

The countdown function is used exclusively by the record_data function to do the timer countdown in the text display.

   /*--------------------------------------------------------------------
    * Function:    countdown
    * Returns:     t or nil
    * Description: Called by record_data() to do the time count-down.
    *------------------------------------------------------------------*/
   function countdown(txt_wgt, time)
   {
     txt_wgt.set_point(42);
     txt_wgt.forward_delete(txt_wgt.get_length() - 42);
     txt_wgt.insert(FONT, nil, nil, string(time - clock()), -1);
   }