Buttons

The buttons API allows you to read the state of the buttons on a badge. This API encapsulates the drivers for different button types.

Badge support

This API is currently supported on the following badges:

  • SHA2017
  • Hackerhotel 2019
  • Disobey 2019
  • CampZone 2019
  • Disobey 2020
  • Fri3dcamp 2018

Support for GPIO buttons and touch-buttons via the MPR121 touch controller IC are supported. Touch buttons using the touch button features of the ESP32 can not be used (yet).

Reference

CommandParametersDescription
attachbutton, callback functionAttach a callback to a button
detachbuttonDetach a callback from a button
valuebuttonGet the current value of a button
getCallbackbuttonGet the current callback of a button
pushMapping[mapping]Switch to a new button mapping
popMappingnoneSwitch back to the previous button mapping
rotatedegreesAdapt the button layout to an orientation. Accepts 0, 90, 180 and 270 as values.

Button availability per badge

NameSHA2017Hackerhotel 2019Disobey 2019CampZone 2019Disobey 2020Fri3dCamp 2018OpenHardwareSummit 2018
AYesYesYesYesYes
BYesYesYesYesYes
SELECTYesYesNoNoYes
STARTYesYesNoNoYes
UPYesYesYesYesYes
DOWNYesYesYesYesYes
LEFTYesYesYesYesYes
RIGHTYesYesYesYesYes

Default callback per button

NameSHA2017Hackerhotel 2019Disobey 2019CampZone 2019Disobey 2020Fri3dCamp 2018OpenHardwareSummit 2018
A
BExit appExit app
SELECT
STARTExit appExit appExit app
UP
DOWN
LEFT
RIGHT

Callback implementation:

to use the buttons, you need to implement a callback function:


import buttons, display # Imports 2 libraries to use the buttons, the display library, as well as the buttons library
def on_action_btn(pressed): # Defines a function on_action_btn with the required parameter pressed
    if pressed: # Uses an if statement to check if the button has been pressed
        display.drawFill(display.BLACK) # If the button is pressed, sets the screen to black
        display.drawText(10,10,"Hack The Planet!!!", display.GREEN, "roboto_regular18") # Draws text if the button is pressed
        display.flush() # Flushes the screen to draw the text and color onto the screen
buttons.attach(buttons.BTN_A, on_action_btn) # Assigns the function on_action_btn to the A button