View Single Post
  #6   Spotlight this post!  
Unread 18-12-2013, 01:14
mikets's Avatar
mikets mikets is offline
Software Engineer
FRC #0492 (Titan Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2008
Location: Bellevue, WA
Posts: 667
mikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of lightmikets is a glorious beacon of light
Re: C++ Toggle Button

Sure, how about this? It is basically the same as your Posedge_toggle class except that it allows the update method to also return the switch state so you don't have to make the get call.
Code:
class ToggleSwitch
{
private:
    bool switchValue;
    bool prevInput;

public:
    ToggleSwitch(void)
        : switchValue(false)
        , prevInput(false)
    {
    }

    bool GetSwitchValue(void)
    {
        return switchValue;
    }

    bool UpdateSwitchValue(bool currInput)
    {
        if (currInput != prevInput)
        {
            if (currInput)
            {
                switchValue = !switchValue;
            }

            prevInput = currInput;
        }

        return switchValue;
    }
};

...
...
ToggleSwitch tankMode;

while (IsOperatorControl())
{
    //
    // Check for button 6 press.
    //
    if (tankMode.UpdateSwitchValue(secondaryController->GetRawButton(6)))
    {
        //
        // Do tank drive.
        //
    }
    else
    {
        //
        // Do arcade drive.
        //
    }
    wait(0.02);
}
__________________

Last edited by mikets : 18-12-2013 at 01:19.
Reply With Quote