4 Panel key detection

From Banana Pi Wiki
Jump to: navigation, search
Overview: BPI-Bit

Panel key detection

Well, at least we've now created some code to get the board to do something. So, let's deal with device input, like keystroke input. First, we need to know two concepts: Output represents the Output from the device to the periphery, and Input represents some information received in the process of processing the device

So the most obvious input on the board is two buttons, two A and B buttons on the left and right of the light board. If we want to know whether the button on the board has been pressed, or whether it has been pressed, or how many times it has been pressed, how can we do that?

It's not so hard, it's easier said than done.

from microbit import *

This code will pause for two seconds before you start pressing the button, which will scroll to show how many times you have pressed A. It's that simple. While the code isn't very useful, it does provide some new ideas about how you can control your hardware

  • 1,The sleep function allows the board to pause for a few milliseconds, the number of milliseconds in the block. If you want to pause at some point in your program, write the sleep function as shown above.
  • 2,The button_a object allows you to get the number of times pressed in a time via the get_presses method

And then capturing the once get_presses values, pass it to the display. The sroll, this method can only accept character, so we need by STR function to convert the integer to a string in python principle so we made a hypothesis, for a deeper understanding if you within 10 seconds press the 10 times, how to perform the third line of code above? Because python executes code from the innermost layer. So:


Of course, this will display the number of times you pressed the button (a period of time, can be specified).


Now that python knows how many times to press, the next step is to turn the string inside into a string, and note that this must be the innermost priority, that is, executing STR ("10") first.


handle event

If you want the board to respond to a button press event, you need to use if to determine if the button is pressed, and this method is recommended in an infinite loop. Such as:

while True:
   # Do stuff

So we can build a very simple code

from microbit import *

while True:
   if button_a.is_pressed():

At this point, you can press the button A to display the Image we learned before, which is called putting what you have learned into practice