0

Flip Flop Fun

I've been trying to code some functions with a gamepad in c for some time. When a button is held down on the gamepad, calling vexRT[Btn4D] (the '4D' just means the fourth set of buttons in the down direction) will return either true or false. If you have a boolean, say x, and you wanted to turn 'x' on with a single press and release of the button and have it stay on, there's no simple method/function predefined. So I spent some time and came up with a simple flip-flop that can be seen here:

if (vexRT[Btn7D]) {
        if (y)
            x = false;
        else
            x = true;
    }
    else if (counter == 0) {
        counter = 25;
        if (x)
            y = true;
        else
            y = false;
    }
    else
        counter--;

Then simply, you could just say if (x) or if (!x) and do whatever you want to do.

The problem is that I have 8 buttons on the gamepad and I want them each to have their own ability to 'flip'. I could simply copy out the code eight times, making a new variable such as x2 and counter2, however this seems redundant. I tried making a method that would shorten the total code but I know it doesn't work. Here it is anyway:

bool tFlip(bool x) {
bool y = !x;
if (y)
    return true;
else
    return false;
}

Long story short, if anyone knows how to properly code these things I'd love to know how. I've tried googling however I can't find anything on it. Thanks.

TLDR: Anyone know how to code a Flip-Flop?

EDIT: This is all inside a loop so I can't simply do

x = !x

as it will continually be called when the button is held down; the value of x would essentially be 'random' when you release. The y is there to act as a 'wait until key release' as there is no native key pressed/key pressed detection method.

Community
  • 1
  • 1
  • What in the world are `counter` and `y` in your first code? What's wrong with `x = y` and / or `x = !y` that you feel the need for clunky `if`/`else` statements? Have you never heard of arrays? – John Bollinger Nov 19 '15 at 19:32
  • Sorry, I guess it's hard to explain the problems that appear in real life. Counter is just a delay of 25 miliseconds because the gamepad is garbage and can't handle pressing the button too fast. I can't do that because this is all placed inside a while loop. It would mean that every milisecond the value of x would switch. When the joystick is pressed it needs to switch the value once and then not switch again until the button has been released and pressed again. If there was any way to detect key release/key press on the joystick this would be a thousand times easier – Matt Walker Nov 19 '15 at 19:37
  • So to achieve a 25 millisecond delay you are somehow, elsewhere, ensuring that the loop iterates exactly once per millisecond? – John Bollinger Nov 19 '15 at 19:53
  • `x = !y;` and `y = !x;`? – user253751 Nov 19 '15 at 19:59
  • What does being in a loop have to do with using `x = !x` ? – John Bollinger Nov 19 '15 at 20:10

1 Answers1

0

You need two pieces of information per button.

  1. On the last sample, was the button up or down?
  2. Is the state of the flip-flop 0 or 1?

You need the first, button state, to detect an edge from up to down.

You need the second, flip-flop state, so you can toggle the state when you see an edge.

You can represent the state for eight buttons in a number of ways, e.g., using 16 variables, using a bit vector, using an array of structs, etc. Here I'll use two arrays:

bool button[8];
bool flipflop[8];

You'll also need to associate each of your flip flops with a gamepad button. E.g.,

int btnID[8] = { ..., Btn4D, ...};

You'll need to replace the ... with the gamepad buttons you're interested in.

Now, each millisecond, do this

#define DOWN (0)  /* as appropriate for your gamepad */

for (i = 0; i < 8, i++)
{
    if (button[i] != vexRT[btnID[i]])
    {
        button[i] = !button[i];
        if (button[i] == DOWN) /* edge */ flipflop[i] = !flipflop[i];
    }
}

To get the flipflop state just read flipflop[n] for the button of interest, n.

All of this ignores debouncing the buttons; hopefully your gamepad library does that for you.

Doug Currie
  • 40,708
  • 1
  • 95
  • 119