Button Functions - toggle_sym, change_settings

Cogent Tools Demo and Tutorials: Version 4.0
Prev | Chapter 8. Photon Functions (in `phsimple.g`) | Next

# 8.3. Button Functions - `toggle_sym`, `change_settings`

These functions connect the buttons in the Monitor GUI to the points in the Cascade DataHub. The first function is used to toggle the `AUTO_001` datahub point between values of `0` and `1`.

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

The Gamma `set` function is used to give control over the assignment that is not available when simply using the assignment operator. Used with the Gamma ternary operator, it allows us to check the current value of the symbol, and at the same time change the value of the symbol according to its existing value

The `change_settings` function is attached to each radio button (ie. Good, Poor, Oscillating, Out-of-control), and it assigns a value to each `PtNumeric` widget. Those changes are then written to the Cascade DataHub.

       /*--------------------------------------------------------------------
        * Function:    change_settings
        * Returns:     t or nil
        * Description: Used by the Monitor to change PID settings, and optionally
        *              unclick the Auto button if it is on.  The numeric widgets
        *              won't execute callbacks after they have been changed
        *              programmatically, so we have to write the point using
        *              write_point().
        *------------------------------------------------------------------*/
       function change_settings(auto, n1, v1, n2, v2, n3, v3, n4, v4,
                                n5, v5, n6, v6, msgno, autobutton?)
       {
         if (auto == nil)
           {
             if (AUTO_001 != 0)
               {
                 autobutton.onoff_state = 0;
                 write_point(#AUTO_001, 0);
               }
           }
         else
           {
             autobutton.onoff_state = 1;
             write_point(#AUTO_001, 1);
             n1.numeric_value = v1;
             write_point(#FREQ_001, v1);
           }

Due to an irregularity in the Photon 2 version of the `PtNumericFloat` widget, we are forced to use the `PtNumeric` widget for QNX 6. Thus we have to convert the values to integers before sending them to the numeric widgets. The original values are in hundredths, so we multiply by `100`.

         if (_os_ == "QNX4")
           {
             n2.numeric_value = v2;
             n3.numeric_value = v3;
             n4.numeric_value = v4;
             n5.numeric_value = v5;
             n6.numeric_value = v6;
           }
         else
           {
             n2.numeric_value = v2 * 100;
             n3.numeric_value = v3 * 100;
             n4.numeric_value = v4 * 100;
             n5.numeric_value = v5 * 100;
             n6.numeric_value = v6 * 100;
           }
         write_point(#PID1_Kp, v2);
         write_point(#PID1_Ki, v3);
         write_point(#PID1_Kd, v4);
         write_point(#PROP_001, v5);
         write_point(#INT_001, v6);
       }

The `PtNumeric` widget's value can be set programmatically, by assigning the `.numeric_value` resource, but doing so does not invoke the widget's callback. So after setting the value in the widget, we have to write the value to the Cascade DataHub using the Gamma function `write_point`.

Prev | Home | Next
The Monitor Window - `create_monitor` | Up | Callback Functions - `slider_callback`, `num_callback`

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.