lancaster-university / microbit-dal

http://lancaster-university.github.io/microbit-docs
Other
259 stars 131 forks source link

buttonWasPressed, pinWasPressed #164

Open pelikhan opened 8 years ago

pelikhan commented 8 years ago

It would be great to have the same API as micropython for button presses:

# returns True or False to indicate if the button was pressed since the device
# started or the last time this method was called.
button.was_pressed()
# returns the running total of button presses, and resets this counter to zero
button.get_presses()

It's particularly usefull in a tight loop when button presses are missed/.

http://microbit-micropython.readthedocs.io/en/latest/microbit_micropython_api.html?highlight=was_pressed

finneyj commented 8 years ago

thanks @pelikhan

Sure, that would be a trivial but useful enhancement. I hate to introduce duplication though: @dpgeorge - would upstreaming this API be a welcome move, assuming we preserve semantics with your API of course?

dpgeorge commented 8 years ago

I thought the point of having the message bus with primitives like BUTTON_EVT_DOWN was so that we could easily implement such semantics ourselves (which is what we do, we listen for EVT_DOWN and use that to update the state about button presses). Adding wasPressed would complicate the button API in the DAL.

finneyj commented 8 years ago

@dpgeorge @pelikhan

For sure, the aim is to allow folks to use the building blocks here to create the best programming experience possible for higher level languages. It also makes sense to consider sharing best practice round too though, where it make sense to do so to reduce effort and duplication.

For most drivers we support calls for basic synchronous and asynchronous use, and for microBitButton we already have isPressed(), so I'm happy either way on this one.