Search code examples
cflip-flop

T-Flip Flop in C - How to compact


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.


Solution

  • 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.