14.11. Photon: 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.

   RECORD_TIME = 5;
   
   /*--------------------------------------------------------------------
    * Function:    record_data
    * Returns:     an integer (timer number), or nil
    * Description: Records data for the specified time, then sends a query.
    *------------------------------------------------------------------*/
   function record_data(but1, but2, iset, text, window, e1, e2, e3, e4, e5, e6)
   {
     if (but1.switched_on())
       {
         local time = clock() + RECORD_TIME;
         text.text_string = string("Recording,please wait.\nSeconds remaining: ",
                                   RECORD_TIME);
         numtimer = every (1, `((@text).text_string = string(substr((@text).text_string, 0, 42),
                                                             string((countdown(@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.text_string = string(RECORD_TIME + 1);
         iset.set_defaults(but1, e2);
         send_hs_command(but1, "enable", e1.text_string, nil);

Once the time elapses, we have to pop the Record button out, stop the recording, and cancel the timer. Then we refresh the display with the query_refresh function. 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, `((@but1).flags = cons(Pt_SET, nil)));
         after((RECORD_TIME + .1), `(send_hs_command(@but1, "enable", (@e1).text_string, nil)));
         after((RECORD_TIME + .1), `(cancel(numtimer)));
         after((RECORD_TIME + .15), `(query_refresh(nil, nil)));
         after((RECORD_TIME + .25), `PtFlush());
       }
   }
   

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

   /*--------------------------------------------------------------------
    * Function:    countdown
    * Returns:     an integer
    * Description: Called by record_data to facilitate the time count-down.
    *              This functionality had to be separated out from the callback.
    *------------------------------------------------------------------*/
   function countdown(starttime)
   {
     starttime - clock();
   }