whaleygeek/bitio

Can't detect dual button press

Closed this issue · 2 comments

When trying to run the following:

if microbit.button_a.was_pressed() and microbit.button_a.was_pressed(): print("both")
elif microbit.button_a.was_pressed():   print("A")
elif microbit.button_b.was_pressed():   print("B")

Things kind of breakdown.

Pressing only button_a does nothing, pressing both buttons does nothing, pressing button_b works.

I remember reading somewhere that internally the microbit stores three different states for button presses. So pressing both buttons results in both button_a.was_pressed() and button_b.was_pressed() resolving to False.

Looking at the bitio code, it is only trying the separate button checks. So that first line above will always be False if both buttons are pressed, which in turn resets the was_pressed flag on button_a, causing the first elif to fail. The second elif only works, because and is lazy in python, and button_b isn't checked in that first line.

I'm not sure how you could resolve this and keep the syntax the same, but maybe a new command (class?) could be introduced: microbit.both_buttons and that could be queried using the same syntax (was_pressed(), is_pressed(), etc) - although grammatically that would look poor...

Actually, one thing that could be introduced, would be to always run:

r = self.parent.cmd("print(button_a.was_pressed() and button_b.was_pressed()")
r = eval(r)

In the Button class's was_pressed() function.
If it resolves to True then don't run the individual checks.

That would stop the counter being reset.

Then potentially True could be returned, allowing the original and to work.

But the side affect of that would be that True would be returned when a single button is queried and both are being pressed (which doesn't happen in the micro:bit) - but as that already happens in bitio, it wouldn't change the interface...

Ignore all of this! There's an obvious typo in my original code - that's what you get for deciding to try something out late on a Friday night!