Skip to content

Commit ac82fbe

Browse files
bpmerkelWasabiFan
authored andcommitted
Console() rows/columns, echo, and cursor properties; doc corrections (#654)
* implement rows/columns, echo, and cursor properties * improved console_fonts utility to use rows/columns from the console class; improved documentation for console and motors * disable cursor and turn off echo only at construction, not in every reset_console * corrected README link; added console_menu utility sample * corrected setter attributes; simplified echo logic * correct end-paren typo * simplified and aligned inline docs * corrected type reference in docs for sphinx rendering * Delete menu demo (to be re-added later in modified form)
1 parent be85d8c commit ac82fbe

6 files changed

Lines changed: 86 additions & 99 deletions

File tree

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ features your project needs.
191191
Library Documentation
192192
---------------------
193193

194-
**Class documentation for this library can be found on
195-
** `our Read the Docs page`_ **.** You can always go there to get
194+
Class documentation for this library can be found on
195+
`our Read the Docs page`_. You can always go there to get
196196
information on how you can use this library's functionality.
197197

198198

docs/console.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,5 +145,9 @@ Example:
145145
# change the console font and reset the console to clear it and turn off the cursor
146146
console.set_font('Lat15-TerminusBold16.psf.gz', True)
147147
148-
# display 'Hello World!' at column 1, row 5
149-
console.text_at('Hello World!', column=1, row=5)
148+
# compute the middle of the console
149+
mid_col = console.columns // 2
150+
mid_row = console.rows // 2
151+
152+
# display 'Hello World!' in the center of the LCD console
153+
console.text_at('Hello World!', column=mid_col, row=mid_row, alignment="C")

docs/motors.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ Move Joystick
121121
:members:
122122
:show-inheritance:
123123

124-
Move MoveDifferential
125-
~~~~~~~~~~~~~~~~~~~~~
124+
Move Differential
125+
~~~~~~~~~~~~~~~~~
126126

127127
.. autoclass:: MoveDifferential
128128
:members:

ev3dev2/console.py

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,59 @@ def __init__(self, font="Lat15-TerminusBold24x12"):
3838
3939
"""
4040
self._font = None
41-
self.set_font(font, False) # don't reset the screen during construction
41+
self._columns = 0
42+
self._rows = 0
43+
self._echo = False
44+
self._cursor = False
45+
self.set_font(font, reset_console=False) # don't reset the screen during construction
46+
self.cursor = False
47+
self.echo = False
48+
49+
@property
50+
def columns(self):
51+
"""
52+
Return (int) number of columns on the EV3 LCD console supported by the current font.
53+
"""
54+
return self._columns
55+
56+
@property
57+
def rows(self):
58+
"""
59+
Return (int) number of rows on the EV3 LCD console supported by the current font.
60+
"""
61+
return self._rows
62+
63+
@property
64+
def echo(self):
65+
"""
66+
Return (bool) whether the console echo mode is enabled.
67+
"""
68+
return self._echo
69+
70+
@echo.setter
71+
def echo(self, value):
72+
"""
73+
Enable/disable console echo (so that EV3 button presses do not show the escape characters on
74+
the LCD console). Set to True to show the button codes, or False to hide them.
75+
"""
76+
self._echo = value
77+
os.system("stty {}".format("echo" if value else "-echo"))
78+
79+
@property
80+
def cursor(self):
81+
"""
82+
Return (bool) whether the console cursor is visible.
83+
"""
84+
return self._cursor
85+
86+
@cursor.setter
87+
def cursor(self, value):
88+
"""
89+
Enable/disable console cursor (to hide the cursor on the LCD).
90+
Set to True to show the cursor, or False to hide it.
91+
"""
92+
self._cursor = value
93+
print("\x1b[?25{}".format('h' if value else 'l'), end='')
4294

4395
def text_at(self, text, column=1, row=1, reset_console=False, inverse=False, alignment="L"):
4496
"""
@@ -90,7 +142,7 @@ def text_at(self, text, column=1, row=1, reset_console=False, inverse=False, ali
90142

91143
print("\x1b[{};{}H{}".format(row, column, text), end='')
92144

93-
def set_font(self, font, reset_console=True):
145+
def set_font(self, font="Lat15-TerminusBold24x12", reset_console=True):
94146
"""
95147
Set the EV3 LCD console font and optionally reset the EV3 LCD console
96148
to clear it and turn off the cursor.
@@ -105,21 +157,13 @@ def set_font(self, font, reset_console=True):
105157
if font is not None and font != self._font:
106158
self._font = font
107159
os.system("setfont {}".format(font))
160+
rows, columns = os.popen('stty size').read().strip().split(" ")
161+
self._rows = int(rows)
162+
self._columns = int(columns)
108163

109164
if reset_console:
110165
self.reset_console()
111166

112-
def show_cursor(self, on=False):
113-
"""
114-
Use ANSI codes to turn the EV3 LCD console cursor on or off.
115-
116-
Parameter:
117-
118-
- `on` (bool): ``True`` to turn on the cursor; default is ``False``
119-
120-
"""
121-
print("\x1b[?25{}".format('h' if on else 'l'), end='')
122-
123167
def clear_to_eol(self, column=None, row=None):
124168
"""
125169
Clear to the end of line from the `column` and `row` position
@@ -137,7 +181,6 @@ def clear_to_eol(self, column=None, row=None):
137181

138182
def reset_console(self):
139183
"""
140-
Use ANSI codes to clear the EV3 LCD console, move the cursor to 1,1, then turn off the cursor.
184+
Clear the EV3 LCD console using ANSI codes, and move the cursor to 1,1
141185
"""
142186
print("\x1b[2J\x1b[H", end='')
143-
self.show_cursor(False)

ev3dev2/sound.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ def play_song(self, song, tempo=120, delay=0.05):
455455
456456
Only 4/4 signature songs are supported with respect to note durations.
457457
458-
:param iterable[tuple(string, string)] song: the song
458+
:param iterable[tuple(string,string)] song: the song
459459
:param int tempo: the song tempo, given in quarters per minute
460460
:param float delay: delay between notes (in seconds)
461461

utils/console_fonts.py

Lines changed: 17 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#!/usr/bin/env micropython
22
from time import sleep
3-
from sys import stderr, stdin
3+
from sys import stderr
44
from os import listdir
55
from ev3dev2.console import Console
66

77
"""
8-
Used to iterate over the system console fonts (in /usr/share/consolefonts) and calculate the max row/col
9-
position by moving the cursor to 50, 50 and asking the LCD for the actual cursor position
10-
where it ends up (the EV3 LCD console driver prevents the cursor from positioning off-screen).
8+
Used to iterate over the system console fonts (in /usr/share/consolefonts) and show the max row/col.
119
12-
The font specification consists of three parameters - codeset, font face and font size. The codeset specifies
10+
Font names consist of three parameters - codeset, font face and font size. The codeset specifies
1311
what characters will be supported by the font. The font face determines the general look of the font. Each
1412
font face is available in certain possible sizes.
1513
@@ -18,97 +16,39 @@
1816
"""
1917

2018

21-
def calc_fonts():
19+
def show_fonts():
2220
"""
23-
Iterate through all the Latin "1 & 5" fonts, and use ANSI escape sequences to see how many rows/columns
24-
the EV3 LCD console can accommodate for each font
21+
Iterate through all the Latin "1 & 5" fonts, and see how many rows/columns
22+
the EV3 LCD console can accommodate for each font.
23+
Note: `Terminus` fonts are "thinner"; `TerminusBold` and `VGA` offer more contrast on the LCD console
24+
and are thus more readable; the `TomThumb` font is waaaaay too small to read!
2525
"""
2626
console = Console()
27-
2827
files = [f for f in listdir("/usr/share/consolefonts/") if f.startswith("Lat15") and f.endswith(".psf.gz")]
2928
files.sort()
29+
fonts = []
3030
for font in files:
3131
console.set_font(font, True)
32-
33-
# position cursor at 50, 50, and ask the console to report its actual cursor position
34-
console.text_at("\x1b[6n", 50, 50, False)
3532
console.text_at(font, 1, 1, False, True)
3633
console.clear_to_eol()
34+
console.text_at("{}, {}".format(console.columns, console.rows),
35+
column=2, row=4, reset_console=False, inverse=False)
36+
print("{}, {}, \"{}\"".format(console.columns, console.rows, font), file=stderr)
37+
fonts.append((console.columns, console.rows, font))
3738

38-
# now, read the console response of the actual cursor position, in the form of esc[rr;ccR
39-
# requires pressing the center button on the EV3 for each read
40-
dims = ''
41-
while True:
42-
ch = stdin.read(1)
43-
if ch == '\x1b' or ch == '[' or ch == '\r' or ch == '\n':
44-
continue
45-
if ch == 'R':
46-
break
47-
dims += str(ch)
48-
(rows, cols) = dims.split(";")
49-
print("({}, {}, \"{}\"),".format(rows, cols, font), file=stderr)
50-
sleep(.5)
51-
52-
53-
def show_fonts():
54-
"""
55-
Iterate over the known Latin "1 & 5" fonts and display each on the EV3 LCD console.
56-
Note: `Terminus` fonts are "thinner"; `TerminusBold` and `VGA` offer more contrast on the LCD console
57-
and are thus more readable; the `TomThumb` font is waaaaay too small to read!
58-
"""
59-
# Create a list of tuples with calulated rows, columns, font filename
60-
fonts = [
61-
(4, 11, "Lat15-Terminus32x16.psf.gz"),
62-
(4, 11, "Lat15-TerminusBold32x16.psf.gz"),
63-
(4, 11, "Lat15-VGA28x16.psf.gz"),
64-
(4, 11, "Lat15-VGA32x16.psf.gz"),
65-
(4, 12, "Lat15-Terminus28x14.psf.gz"),
66-
(4, 12, "Lat15-TerminusBold28x14.psf.gz"),
67-
(5, 14, "Lat15-Terminus24x12.psf.gz"),
68-
(5, 14, "Lat15-TerminusBold24x12.psf.gz"),
69-
(5, 16, "Lat15-Terminus22x11.psf.gz"),
70-
(5, 16, "Lat15-TerminusBold22x11.psf.gz"),
71-
(6, 17, "Lat15-Terminus20x10.psf.gz"),
72-
(6, 17, "Lat15-TerminusBold20x10.psf.gz"),
73-
(7, 22, "Lat15-Fixed18.psf.gz"),
74-
(8, 22, "Lat15-Fixed15.psf.gz"),
75-
(8, 22, "Lat15-Fixed16.psf.gz"),
76-
(8, 22, "Lat15-Terminus16.psf.gz"),
77-
(8, 22, "Lat15-TerminusBold16.psf.gz"),
78-
(8, 22, "Lat15-TerminusBoldVGA16.psf.gz"),
79-
(8, 22, "Lat15-VGA16.psf.gz"),
80-
(9, 22, "Lat15-Fixed13.psf.gz"),
81-
(9, 22, "Lat15-Fixed14.psf.gz"),
82-
(9, 22, "Lat15-Terminus14.psf.gz"),
83-
(9, 22, "Lat15-TerminusBold14.psf.gz"),
84-
(9, 22, "Lat15-TerminusBoldVGA14.psf.gz"),
85-
(9, 22, "Lat15-VGA14.psf.gz"),
86-
(10, 29, "Lat15-Terminus12x6.psf.gz"),
87-
(16, 22, "Lat15-VGA8.psf.gz"),
88-
(21, 44, "Lat15-TomThumb4x6.psf.gz")
89-
]
39+
fonts.sort(key=lambda f: (f[0], f[1], f[2]))
9040

9141
# Paint the screen full of numbers that represent the column number, reversing the even rows
92-
console = Console()
93-
for rows, cols, font in fonts:
94-
print(rows, cols, font, file=stderr)
42+
for cols, rows, font in fonts:
43+
print(cols, rows, font, file=stderr)
9544
console.set_font(font, True)
9645
for row in range(1, rows+1):
9746
for col in range(1, cols+1):
9847
console.text_at("{}".format(col % 10), col, row, False, (row % 2 == 0))
9948
console.text_at(font.split(".")[0], 1, 1, False, True)
10049
console.clear_to_eol()
101-
sleep(.5)
102-
103-
104-
# Uncomment the calc_fonts() call to iterate through each system font
105-
# and use ANSI codes to find the max row/column the screen will accommodate for
106-
# each font. Remember to press the center EV3 button for each font.
107-
# Also, you may want to adjust the `startswith` filter to show other codesets.
108-
# calc_fonts()
109-
11050

111-
# show the fonts
51+
# Show the fonts; you may want to adjust the `startswith` filter to show other codesets.
11252
show_fonts()
11353

11454
sleep(5)

0 commit comments

Comments
 (0)