Console

class ev3dev2.console.Console(font='Lat15-TerminusBold24x12')

A class that represents the EV3 LCD console, which implements ANSI codes for cursor positioning, text color, and resetting the screen. Supports changing the console font using standard system fonts.

columns

Return (int) number of columns on the EV3 LCD console supported by the current font.

rows

Return (int) number of rows on the EV3 LCD console supported by the current font.

echo

Return (bool) whether the console echo mode is enabled.

cursor

Return (bool) whether the console cursor is visible.

text_at(text, column=1, row=1, reset_console=False, inverse=False, alignment='L')

Display text (string) at grid position (column, row). Note that the grid locations are 1-based (not 0-based).

Depending on the font, the number of columns and rows supported by the EV3 LCD console can vary. Large fonts support as few as 11 columns and 4 rows, while small fonts support 44 columns and 21 rows. The default font for the Console() class results in a grid that is 14 columns and 5 rows.

Using the inverse=True parameter will display the text with more emphasis and contrast, as the background of the text will be black, and the foreground is white. Using inverse can help in certain situations, such as to indicate when a color sensor senses black, or the gyro sensor is pointing to zero.

Use the alignment parameter to enable the function to align the text differently to the column/row values passed-in. Use L for left-alignment (default), where the first character in the text will show at the column/row position. Use R for right-alignment, where the last character will show at the column/row position. Use C for center-alignment, where the text string will centered at the column/row position (as close as possible using integer division–odd-length text string will center better than even-length).

Parameters:

  • text (string): Text to display
  • column (int): LCD column position to start the text (1 = left column); text will wrap when it reaches the right edge
  • row (int): LCD row position to start the text (1 = top row)
  • reset_console (bool): True to reset the EV3 LCD console before showing the text; default is False
  • inverse (bool): True for white on black, otherwise black on white; default is False
  • alignment (string): Align the text horizontally. Use L for left-alignment (default), R for right-alignment, or C for center-alignment
set_font(font='Lat15-TerminusBold24x12', reset_console=True)

Set the EV3 LCD console font and optionally reset the EV3 LCD console to clear it and turn off the cursor.

Parameters:

  • font (string): Font name, as found in /usr/share/consolefonts/
  • reset_console (bool): True to reset the EV3 LCD console after the font change; default is True
clear_to_eol(column=None, row=None)

Clear to the end of line from the column and row position on the EV3 LCD console. Default to current cursor position.

Parameters:

  • column (int): LCD column position to move to before clearing
  • row (int): LCD row position to move to before clearing
reset_console()

Clear the EV3 LCD console using ANSI codes, and move the cursor to 1,1

Examples:

#!/usr/bin/env micropython
from ev3dev2.console import Console

# create a Console instance, which uses the default font
console = Console()

# reset the console to clear it, home the cursor at 1,1, and then turn off the cursor
console.reset_console()

# display 'Hello World!' at row 5, column 1 in inverse, but reset the EV3 LCD console first
console.text_at('Hello World!', column=1, row=5, reset_console=True, inverse=True)
#!/usr/bin/env micropython
from time import sleep
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3
from ev3dev2.console import Console
from ev3dev2.sensor.lego import GyroSensor, ColorSensor

console = Console()
gyro = GyroSensor(INPUT_1)
gyro.mode = GyroSensor.MODE_GYRO_ANG
color_sensor_left = ColorSensor(INPUT_2)
color_sensor_right = ColorSensor(INPUT_3)

# show the gyro angle and reflected light intensity for both of our color sensors
while True:
    angle = gyro.angle
    left = color_sensor_left.reflected_light_intensity
    right = color_sensor_right.reflected_light_intensity

    # show angle; in inverse color when pointing at 0
    console.text_at("G: %03d" % (angle), column=5, row=1, reset_console=True, inverse=(angle == 0))

    # show light intensity values; in inverse when 'dark'
    console.text_at("L: %02d" % (left), column=0, row=3, reset_console=False, inverse=(left < 10))
    console.text_at("R: %02d" % (right), column=10, row=3, reset_console=False, inverse=(right < 10))

    sleep(0.5)

Console fonts

The ev3dev2.console.Console class displays text on the LCD console using ANSI codes in various system console fonts. The system console fonts are located in /usr/share/consolefonts.

Font filenames consist of the codeset, font face and font size. The codeset specifies the characters supported. The font face determines the look of the font. Each font face is available in multiple sizes.

For Codeset information, see <https://www.systutorials.com/docs/linux/man/5-console-setup/#lbAP>.

Note: Terminus fonts are “thinner”; TerminusBold and VGA offer more contrast on the LCD console and are thus more readable; the TomThumb font is too small to read!

Depending on the font used, the EV3 LCD console will support various maximum rows and columns, as follows for the Lat15 fonts. See utils/console_fonts.py to discover fonts and their resulting rows/columns. These fonts are listed in larger-to-smaller size order:

LCD Rows LCD Columns Font
4 11 Lat15-Terminus32x16.psf.gz
4 11 Lat15-TerminusBold32x16.psf.gz
4 11 Lat15-VGA28x16.psf.gz
4 11 Lat15-VGA32x16.psf.gz
4 12 Lat15-Terminus28x14.psf.gz
4 12 Lat15-TerminusBold28x14.psf.gz
5 14 Lat15-Terminus24x12.psf.gz
5 14 Lat15-TerminusBold24x12.psf.gz
5 16 Lat15-Terminus22x11.psf.gz
5 16 Lat15-TerminusBold22x11.psf.gz
6 17 Lat15-Terminus20x10.psf.gz
6 17 Lat15-TerminusBold20x10.psf.gz
7 22 Lat15-Fixed18.psf.gz
8 22 Lat15-Fixed15.psf.gz
8 22 Lat15-Fixed16.psf.gz
8 22 Lat15-Terminus16.psf.gz
8 22 Lat15-TerminusBold16.psf.gz
8 22 Lat15-TerminusBoldVGA16.psf.gz
8 22 Lat15-VGA16.psf.gz
9 22 Lat15-Fixed13.psf.gz
9 22 Lat15-Fixed14.psf.gz
9 22 Lat15-Terminus14.psf.gz
9 22 Lat15-TerminusBold14.psf.gz
9 22 Lat15-TerminusBoldVGA14.psf.gz
9 22 Lat15-VGA14.psf.gz
10 29 Lat15-Terminus12x6.psf.gz
16 22 Lat15-VGA8.psf.gz
21 44 Lat15-TomThumb4x6.psf.gz

Example:

#!/usr/bin/env micropython
from ev3dev2.console import Console

# create a Console instance, which uses the default font
console = Console()

# change the console font and reset the console to clear it and turn off the cursor
console.set_font('Lat15-TerminusBold16.psf.gz', True)

# compute the middle of the console
mid_col = console.columns // 2
mid_row = console.rows // 2

# display 'Hello World!' in the center of the LCD console
console.text_at('Hello World!', column=mid_col, row=mid_row, alignment="C")