n0ukf
Posts: 15
Joined: Fri Mar 10, 2017 3:27 am

More-exclusive OR test

Sun Mar 19, 2017 5:02 am

When polling GPIO input pins, is there some quick way to check whether one and only one has gone high (or low, depending on pull-up or pull-down setup)? I was thinking about exclusive-OR but from checking into it, any odd-number of trues will give a true. I could check them one by one but was hoping for some existing test for exclusivity.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: More-exclusive OR test

Sun Mar 19, 2017 12:41 pm

The best way of doing this really depends on why you want to do it! If the circumstances are relatively infrequent then using a callback function to check all the GPIO if any of them change state is probably the best option. If you need to check them often then the python list comprehension loop is pretty fast. Or if speed is more important look at jean's pigpio module where there are read_bank_x() functions that do what you want
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: More-exclusive OR test

Sun Mar 19, 2017 12:51 pm

n0ukf wrote:When polling GPIO input pins, is there some quick way to check whether one and only one has gone high (or low, depending on pull-up or pull-down setup)? I was thinking about exclusive-OR but from checking into it, any odd-number of trues will give a true. I could check them one by one but was hoping for some existing test for exclusivity.
Add them all together and see if the sum equals 1? True == 1, False == 0 when doing arithmetic.
She who travels light — forgot something.

ghp
Posts: 1498
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: More-exclusive OR test

Sun Mar 19, 2017 2:33 pm

Hello,
there are functions to read a complete bank of GPIO at a time in pigpiod. This avoids the timing problem. Then look through the relevant bits and build the result.
Regards,
Gerhard

klintkrossa
Posts: 81
Joined: Tue Nov 10, 2015 3:06 pm

Re: More-exclusive OR test

Sun Mar 19, 2017 5:56 pm

Hello,
Now I'm interested. My thought was how do the first ring in game work, like Jeopardy.
I have no nee just now I like using the xor gates. and not-xor.
Would a not-xor work?
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.

n0ukf
Posts: 15
Joined: Fri Mar 10, 2017 3:27 am

Re: More-exclusive OR test

Fri Mar 24, 2017 10:46 pm

The application for this question is for making a game buzzer, first contestant to press their button gets their light lit. It needs to be fast enough to prevent reading a tie, and if there is a tie, some indication of that condition (if the buttons are sequentially tested and that test exits on the first true, 1 will always win over 2 if tied).

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: More-exclusive OR test

Sat Mar 25, 2017 12:10 am

n0ukf wrote:The application for this question is for making a game buzzer, first contestant to press their button gets their light lit. It needs to be fast enough to prevent reading a tie, and if there is a tie, some indication of that condition (if the buttons are sequentially tested and that test exits on the first true, 1 will always win over 2 if tied).
In that case using what paddyg suggested and read a bank of GPIOs in one go, as long as all the GPIOs are in the same bank you get them in 1 read (and there are only 2 banks, GPIO 0-31 and GPIO 32-53), you just look at the individual bitsof the value read relating to the GPIOs you are interested in, if more than one is set then the buttons will have been detected at the same time.
She who travels light — forgot something.

n0ukf
Posts: 15
Joined: Fri Mar 10, 2017 3:27 am

Re: More-exclusive OR test

Thu Mar 30, 2017 2:04 pm

That NOT is just going to invert the output, which will still be true or false based on an even or odd number of trues at the input.

User avatar
paddyg
Posts: 2541
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: More-exclusive OR test

Thu Mar 30, 2017 4:22 pm

hi, it sounded from your description of what you were doing that it would be sufficient to read a bank of inputs and check that
v != 0 or 2**32, else check which ones were triggered.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”