odroid_go:micropython:04_buttons

MicroPython for ODROID-GO - Buttons

We will learn how to read the status of the buttons with MicroPython.
A simple output will be shown on the LCD.

ODROID-GO has 10 buttons available.

  • 4 for direction pad.
  • 2 for an action.
  • and 4 functional buttons.

Thanks to the GO module, we can get the status of the buttons easily.

First of all, initialize the LCD with importing GO module.
Set text font to TT14 because the default text size is too small to read.

from odroid_go import GO
 
GO.lcd.set_font(GO.lcd.fonts.TT14)

Add an independent function to display the status, and name it display_buttons().
This function has 2 LCD functions:

  • GO.lcd.erase() to clear any LCD content previously shown.
  • GO.lcd.set_pos() to set the start point to print a string. The two parameters (0, 0) means top, left.
from odroid_go import GO
 
GO.lcd.set_font(GO.lcd.fonts.TT14)
 
 
def display_buttons():
    GO.lcd.erase()
    GO.lcd.set_pos(0, 0)

Fill display_buttons() out with the code below.

It displays whether any of the buttons are pressed or not. When a button is pressed, then a “Pressed” string appears beside the element that corresponds to the button.

All of the buttons ODROID-GO has are available as an instance of the Button class.
The Button class has some helpful functions to let us know the button's status.
Thus, we use is_axis_pressed() and is_pressed() functions to know if a button is currently pressed or not.

If the button is pressed, that functions returns not 0.

  • is_axis_pressed() function is only for the direction pad. If the button is pressed, it returns 1 or 2 to distinguish the direction.
  • is_pressed() function is for the other buttons. If the button is pressed, it returns 1.

Lastly, we have to add an infinite while loop to show the status on the LCD, continuously.
There are three functions needed in the loop:

  • GO.update() to update the buttons' states so that the is_pressed() functions works well.
  • display_buttons() to add result strings to the LCD.
  • time.sleep(1) to prevent the LCD from blinking too fast when GO.lcd.erase() function acts.
from odroid_go import GO
import time
 
GO.lcd.set_font(GO.lcd.fonts.TT14)
 
 
def display_buttons():
    GO.lcd.erase()
    GO.lcd.set_pos(0, 0)
 
    GO.lcd.print("/* Direction Pad */")
    GO.lcd.print("Joy-Y-Up: " + ("Pressed" if GO.btn_joy_y.is_axis_pressed() == 2 else ""))
    GO.lcd.print("Joy-Y-Down: " + ("Pressed" if GO.btn_joy_y.is_axis_pressed() == 1 else ""))
    GO.lcd.print("Joy-X-Left: " + ("Pressed" if GO.btn_joy_x.is_axis_pressed() == 2 else ""))
    GO.lcd.print("Joy-X-Right: " + ("Pressed" if GO.btn_joy_x.is_axis_pressed() == 1 else ""))
    GO.lcd.print("")
    GO.lcd.print("/* Function Key */")
    GO.lcd.print("Menu: " + ("Pressed" if GO.btn_menu.is_pressed() == 1 else ""))
    GO.lcd.print("Volume: " + ("Pressed" if GO.btn_volume.is_pressed() == 1 else ""))
    GO.lcd.print("Select: " + ("Pressed" if GO.btn_select.is_pressed() == 1 else ""))
    GO.lcd.print("Start: " + ("Pressed" if GO.btn_start.is_pressed() == 1 else ""))
    GO.lcd.print("")
    GO.lcd.print("/* Actions */")
    GO.lcd.print("B: " + ("Pressed" if GO.btn_b.is_pressed() == 1 else ""))
    GO.lcd.print("A: " + ("Pressed" if GO.btn_a.is_pressed() == 1 else ""))
 
 
while True:
    GO.update()
    display_buttons()
 
    time.sleep(1)

Save and overwrite this code as boot.py in the ODROID-GO module installation directory.

  • To execute this module properly, make sure you've uploaded ODROID-GO module. And you have to upload the written file called boot.py using rshell or ampy.
    • If you uploaded properly, MicroPython will execute boot.py when the device boots automatically.
    • Please refer to setup guide to further information: Install the ODROID-GO MicroPython module.
    • Or you also can do them in REPL. Write the codes line by line in order.

Upload the boot.py file using rshell or ampy, enter to REPL prompt, and restart ODROID-GO.

Then, press any button to show “Pressed” string besides that button.
As you noticed, you need to keep pressing it until the LCD is updated due to the time.sleep(1) function.

The complete example is available in following path:

  • odroid_go/examples/button/button.py

Copy and paste to try the example.

  • odroid_go/micropython/04_buttons.txt
  • Last modified: 2018/07/23 14:05
  • by joshua