7.5. Button Functions - toggle_sym, change_settings

These two functions provide features for the buttons in the program.

The first, toggle_sym, is used to give toggle functionality to the Auto mode button. We can't use a regular toggle button for this because the automatic mode can be changed by any program that has access to the datahub (such as dhview and the Windows PID Monitor.) Having the program toggle or untoggle a toggle button to reflect the status in the datahub would invoke the toggle button callback, and change the value back to its original value. So, we give toggling functionality to a regular button here, and give it a special label created by set_label that toggles each time the function is called.

   /*--------------------------------------------------------------------
    * Function:    toggle_sym
    * Returns:     t or nil
    * Description: Toggles a database point between 0 and 1 when a button
    *              is pressed, and calls set_label() to change the label
    *              accordingly.
    *------------------------------------------------------------------*/
   function toggle_sym(button, sym, onvalue, offvalue)
   {
     set (sym, eval(sym) == 0 ? 1 : 0);
     write_point (sym, eval(sym));
     set_label(button, eval(sym), onvalue, offvalue);
   }
   

We use the Gamma ternary operator to switch 0 to 1 and to switch any non-zero number to 0. The Gamma set function assigns the new value to the sym variable. Then we write the value to the datahub, and call set_label to toggle the label.

The second function, change_settings, assigns new values to all of the spin buttons, as well as optionally untoggling the automatic mode button. This function is called by the four PID Loop: radio buttons at the bottom of the Monitor to set predefined values for all parameters.

[Note]

This function is an adaptation of the abstracted function anyos_change_settings explained in Tutorial Two.

   /*--------------------------------------------------------------------
    * Function:    change_settings
    * Returns:     t or nil
    * Description: Changes PID settings in the Monitor, and optionally
    *              unclicks the Auto button if it is on.
    * Note:        In the Demo, this function is a Library function, found
    *              in lib/linux.g.
    *------------------------------------------------------------------*/
   function change_settings(button, auto, sp1, v1, sp2, v2, sp3, v3, sp4,
                            v4, sp5, v5, sp6, v6, msgno, autobutton?)
   {
     if (button.get_active() == TRUE)
       {
         if (auto == nil)
           {
             if (AUTO_001 != 0)
               autobutton.clicked();
           }
         else
           sp1.set_value(v1);
         sp2.set_value(v2);
         sp3.set_value(v3);
         sp4.set_value(v4);
         sp5.set_value(v5);
         sp6.set_value(v6);
       }
   }