No announcement yet.

Toggling a WinButton using a Momentary switch on GPIO pin input

  • Filter
  • Time
  • Show
Clear All
new posts

  • Toggling a WinButton using a Momentary switch on GPIO pin input

    I have done a fair bit of searching and did not find any guides that would show me how to use a momentary switch to toggle a on-screen object so I thought I would share what I came up with.

    The reason for doing this is to prevent wear on a screen that will be used in a dirty environment where the controls will be switched a great number of times.
    I want the user to be able to use either the WinButton or the physical button to toggle the state of the WinButton, which will then be output on a PinOutput for an indicator LED as well as send a ReportEvent to the host.

    My test Magic Event is triggered by PinInput0 and executes a toggle of WinButton0, only when PinInput0 is low (otherwise it toggles 2 times at every button press, rising and falling) and then output the state of WinButton on PinOutput. ReportMessage function is not included here.

    Note that PinInput is debounced with a 10uF capacitor.
    func MagicEvent0(var newval)
        var buttonval :=0;
        if (newval !=0)     // when input switch rises, do nothing
        else if (newval==0)  // if input switch returns to zero
        //get value of winbutton
            buttonval :=(img_GetWord(hndl, iWinbutton0, IMAGE_INDEX));
       //invert value of WinButton
            buttonval:= !buttonval;
            WriteObject(tWinButton, 0, buttonval);
        //outputs state of winbutton0 to Pin0
            WriteObject(tPinOutput, 0, img_GetWord(hndl, iWinbutton0, IMAGE_INDEX)) ;
    Does this seem a reasonable approach or is there a simpler / more elegant way to do this?
    Last edited by Sterno; 13th September 2017, 04:16 AM. Reason: Misposted, should have posted in Workshop Pro sub-section

  • #2

    Thank you for sharing your code on the forum, I am sure it will help others.

    I find that a very reasonable approach and it works, which is the main thing. Nice work.