Skip to content

Commit be85d8c

Browse files
bpmerkelWasabiFan
authored andcommitted
Console() implementation (ev3dev#649)
Related to ev3dev#635. Micropython support for Display This is a text-only implementation using the EV3 LCD console ANSI code support to position the cursor, clear the screen, show text with different fonts, and use reverse (white text on black background, versus the default black text on white background). See the file utils/console_fonts.py to see the fonts supported with sample calls to the ev3dev2.Console() class. I also included a Makefile so folks can git clone, build and install the latest ev3dev2 modules to the /usr/lib/micropython directory.
1 parent 6030987 commit be85d8c

9 files changed

Lines changed: 503 additions & 11 deletions

File tree

‎CONTRIBUTING.rst‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The ``ev3dev-stretch`` branch
1313
-----------------------------
1414

1515
This is where the latest version of our library lives. It targets
16-
`ev3dev-stretch`, which is currently considered a beta. Nonetheless,
16+
``ev3dev-stretch``, which is currently considered a beta. Nonetheless,
1717
it is very stable and isn't expected to have significant breaking
1818
changes. We publish releases from this branch.
1919

@@ -23,13 +23,16 @@ Before you issue a Pull Request
2323
Sometimes, it isn't easy for us to pull your suggested change and run
2424
rigorous testing on it. So please help us out by validating your changes
2525
and mentioning what kinds of testing you did when you open your PR.
26-
Please also consider adding relevant tests to `api_tests.py`.
26+
Please also consider adding relevant tests to ``api_tests.py`` and documentation
27+
changes within the ``docs`` directory.
2728

2829
If your change breaks or changes an API
2930
---------------------------------------
3031

3132
Breaking changes are discouraged, but sometimes they are necessary. A
3233
more common change is to add a new function or property to a class.
34+
If you add a new parameter to an existing function, give it a default value
35+
so as not to break existing code that calls the function.
3336

3437
Either way, if it's more than a bug fix, please add enough text to the
3538
comments in the pull request message so that we know what was updated
@@ -47,4 +50,48 @@ note of the format ``Fixes #24`` in the PR so that the PR links back
4750
to its relevant issue and will automatically close the issue when the
4851
PR is merged.
4952

50-
.. _ev3dev: http://ev3dev.org
53+
Building and testing changes on the EV3
54+
---------------------------------------
55+
56+
In an SSH terminal window with an EV3 with Internet access,
57+
run the following commands:
58+
(recall that the default ``sudo`` password is ``maker``)
59+
60+
```shell
61+
git clone https://github.com/ev3dev/ev3dev-lang-python.git
62+
cd ev3dev-lang-python
63+
sudo make install
64+
```
65+
66+
To update the module, use the following commands:
67+
68+
```shell
69+
cd ev3dev-lang-python
70+
git pull
71+
sudo make install
72+
```
73+
74+
If you are developing micropython support, you can take a shortcut
75+
and use the following command to build and deploy the micropython
76+
files only:
77+
78+
```shell
79+
cd ev3dev-lang-python
80+
sudo make micropython-install
81+
```
82+
83+
To re-install the latest release, use the following command:
84+
85+
```shell
86+
sudo apt-get --reinstall install python3-ev3dev2
87+
```
88+
89+
Or, to update your current ev3dev2 to the latest release, use the
90+
following commands:
91+
92+
```shell
93+
sudo apt update
94+
sudo apt install --only-upgrade micropython-ev3dev2
95+
```
96+
97+
.. _ev3dev: http://ev3dev.org

‎Makefile‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Makefile to assist developers while modifying and testing changes before release
2+
# Note: to re-install a release of EV3DEV2, use `sudo apt-get --reinstall install python3-ev3dev2`
3+
OUT := build
4+
MPYCROSS := /usr/bin/mpy-cross
5+
MPYC_FLAGS := -v -v -mcache-lookup-bc
6+
PYS := $(shell find ./ev3dev2 -type f \( -iname "*.py" ! -iname "setup.py" \))
7+
MPYS := $(PYS:./%.py=${OUT}/%.mpy)
8+
vpath %.py .
9+
10+
${OUT}/%.mpy: %.py
11+
@mkdir -p $(dir $@)
12+
${MPYCROSS} ${MPYC_FLAGS} -o $@ $<
13+
14+
install:
15+
python3 setup.py install
16+
17+
micropython-install: ${MPYS}
18+
cp -R $(OUT)/* /usr/lib/micropython/

‎debian/changelog‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ python-ev3dev2 (2.0.0~beta4) UNRELEASED; urgency=medium
88
* StopWatch class
99
* Avoid race condition due to poll(None)
1010
* MoveDifferential odometry support, tracks robot's (x,y) position
11-
* Display support via raspberry pi HDMI
11+
* Display support via raspberry pi HDMI
1212
* Update tests/README to include sphinx-build instructions
1313
* LED animation support, brickpi3 LED corrected from BLUE to AMBER
1414
* Sound: fallback to regular case of 'note' if uppercase is not found
@@ -17,6 +17,16 @@ python-ev3dev2 (2.0.0~beta4) UNRELEASED; urgency=medium
1717
[David Lechner]
1818
* Fix and document ev3dev2.sensors.lego.GyroSensor.reset()
1919

20+
[Brady Merkel]
21+
* Added the Console() class for positional text display and font support
22+
on the EV3 LCD console
23+
* Added a utility program to demonstrate the Console() functionality and
24+
show the various system fonts
25+
* Added documentation for the Console() class
26+
* Updated the micropython documentation to reflect the Console() class
27+
* Updated the CONTRIBUTING documentation with guidance using the makefile
28+
to build and install the module while developing enancements
29+
2030
-- Kaelin Laundry <wasabifan@outlook.com> Sun, 24 Mar 2019 00:25:00 -0800
2131

2232
python-ev3dev2 (2.0.0~beta3) stable; urgency=medium
@@ -36,7 +46,7 @@ python-ev3dev2 (2.0.0~beta3) stable; urgency=medium
3646
* brickpi(3) raise exception if LargeMotor not used for a motor
3747
* MoveJoyStick should use SpeedPercent
3848
* renamed GyroSensor rate_and_angle to angle_and_rate
39-
49+
4050
[Kaelin Laundry]
4151
* Added new binary package for Micropython
4252

‎docs/console.rst‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
Console
2+
=======
3+
4+
.. autoclass:: ev3dev2.console.Console
5+
:members:
6+
7+
Examples:
8+
9+
.. code-block:: py
10+
11+
#!/usr/bin/env micropython
12+
from ev3dev2.console import Console
13+
14+
# create a Console instance, which uses the default font
15+
console = Console()
16+
17+
# reset the console to clear it, home the cursor at 1,1, and then turn off the cursor
18+
console.reset_console()
19+
20+
# display 'Hello World!' at row 5, column 1 in inverse, but reset the EV3 LCD console first
21+
console.text_at('Hello World!', column=1, row=5, reset_console=True, inverse=True)
22+
23+
.. code-block:: py
24+
25+
#!/usr/bin/env micropython
26+
from time import sleep
27+
from ev3dev2.sensor import INPUT_1, INPUT_2, INPUT_3
28+
from ev3dev2.console import Console
29+
from ev3dev2.sensor.lego import GyroSensor, ColorSensor
30+
31+
console = Console()
32+
gyro = GyroSensor(INPUT_1)
33+
gyro.mode = GyroSensor.MODE_GYRO_ANG
34+
color_sensor_left = ColorSensor(INPUT_2)
35+
color_sensor_right = ColorSensor(INPUT_3)
36+
37+
# show the gyro angle and reflected light intensity for both of our color sensors
38+
while True:
39+
angle = gyro.angle
40+
left = color_sensor_left.reflected_light_intensity
41+
right = color_sensor_right.reflected_light_intensity
42+
43+
# show angle; in inverse color when pointing at 0
44+
console.text_at("G: %03d" % (angle), column=5, row=1, reset_console=True, inverse=(angle == 0))
45+
46+
# show light intensity values; in inverse when 'dark'
47+
console.text_at("L: %02d" % (left), column=0, row=3, reset_console=False, inverse=(left < 10))
48+
console.text_at("R: %02d" % (right), column=10, row=3, reset_console=False, inverse=(right < 10))
49+
50+
sleep(0.5)
51+
52+
Console fonts
53+
-------------
54+
55+
The :py:class:`ev3dev2.console.Console` class displays text on the LCD console
56+
using ANSI codes in various system console fonts. The system console fonts are
57+
located in `/usr/share/consolefonts`.
58+
59+
Font filenames consist of the codeset, font face and font size. The codeset
60+
specifies the characters supported. The font face determines the look of the
61+
font. Each font face is available in multiple sizes.
62+
63+
For Codeset information, see
64+
`<https://www.systutorials.com/docs/linux/man/5-console-setup/#lbAP>`.
65+
66+
Note: `Terminus` fonts are "thinner"; `TerminusBold` and `VGA` offer more
67+
contrast on the LCD console and are thus more readable; the `TomThumb` font is
68+
too small to read!
69+
70+
Depending on the font used, the EV3 LCD console will support various maximum
71+
rows and columns, as follows for the `Lat15` fonts. See
72+
`utils/console_fonts.py` to discover fonts and their resulting rows/columns.
73+
These fonts are listed in larger-to-smaller size order:
74+
75+
+----------+------------+--------------------------------+
76+
| LCD Rows | LCD Columns| Font |
77+
+==========+============+================================+
78+
| 4 | 11 | Lat15-Terminus32x16.psf.gz |
79+
+----------+------------+--------------------------------+
80+
| 4 | 11 | Lat15-TerminusBold32x16.psf.gz |
81+
+----------+------------+--------------------------------+
82+
| 4 | 11 | Lat15-VGA28x16.psf.gz |
83+
+----------+------------+--------------------------------+
84+
| 4 | 11 | Lat15-VGA32x16.psf.gz |
85+
+----------+------------+--------------------------------+
86+
| 4 | 12 | Lat15-Terminus28x14.psf.gz |
87+
+----------+------------+--------------------------------+
88+
| 4 | 12 | Lat15-TerminusBold28x14.psf.gz |
89+
+----------+------------+--------------------------------+
90+
| 5 | 14 | Lat15-Terminus24x12.psf.gz |
91+
+----------+------------+--------------------------------+
92+
| 5 | 14 | Lat15-TerminusBold24x12.psf.gz |
93+
+----------+------------+--------------------------------+
94+
| 5 | 16 | Lat15-Terminus22x11.psf.gz |
95+
+----------+------------+--------------------------------+
96+
| 5 | 16 | Lat15-TerminusBold22x11.psf.gz |
97+
+----------+------------+--------------------------------+
98+
| 6 | 17 | Lat15-Terminus20x10.psf.gz |
99+
+----------+------------+--------------------------------+
100+
| 6 | 17 | Lat15-TerminusBold20x10.psf.gz |
101+
+----------+------------+--------------------------------+
102+
| 7 | 22 | Lat15-Fixed18.psf.gz |
103+
+----------+------------+--------------------------------+
104+
| 8 | 22 | Lat15-Fixed15.psf.gz |
105+
+----------+------------+--------------------------------+
106+
| 8 | 22 | Lat15-Fixed16.psf.gz |
107+
+----------+------------+--------------------------------+
108+
| 8 | 22 | Lat15-Terminus16.psf.gz |
109+
+----------+------------+--------------------------------+
110+
| 8 | 22 | Lat15-TerminusBold16.psf.gz |
111+
+----------+------------+--------------------------------+
112+
| 8 | 22 | Lat15-TerminusBoldVGA16.psf.gz |
113+
+----------+------------+--------------------------------+
114+
| 8 | 22 | Lat15-VGA16.psf.gz |
115+
+----------+------------+--------------------------------+
116+
| 9 | 22 | Lat15-Fixed13.psf.gz |
117+
+----------+------------+--------------------------------+
118+
| 9 | 22 | Lat15-Fixed14.psf.gz |
119+
+----------+------------+--------------------------------+
120+
| 9 | 22 | Lat15-Terminus14.psf.gz |
121+
+----------+------------+--------------------------------+
122+
| 9 | 22 | Lat15-TerminusBold14.psf.gz |
123+
+----------+------------+--------------------------------+
124+
| 9 | 22 | Lat15-TerminusBoldVGA14.psf.gz |
125+
+----------+------------+--------------------------------+
126+
| 9 | 22 | Lat15-VGA14.psf.gz |
127+
+----------+------------+--------------------------------+
128+
| 10 | 29 | Lat15-Terminus12x6.psf.gz |
129+
+----------+------------+--------------------------------+
130+
| 16 | 22 | Lat15-VGA8.psf.gz |
131+
+----------+------------+--------------------------------+
132+
| 21 | 44 | Lat15-TomThumb4x6.psf.gz |
133+
+----------+------------+--------------------------------+
134+
135+
Example:
136+
137+
.. code-block:: py
138+
139+
#!/usr/bin/env micropython
140+
from ev3dev2.console import Console
141+
142+
# create a Console instance, which uses the default font
143+
console = Console()
144+
145+
# change the console font and reset the console to clear it and turn off the cursor
146+
console.set_font('Lat15-TerminusBold16.psf.gz', True)
147+
148+
# display 'Hello World!' at column 1, row 5
149+
console.text_at('Hello World!', column=1, row=5)

‎docs/micropython.md‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ MicroPython, we recommend you run your code with it for improved performance.
1111
Module Support status
1212
============================== =================
1313
`ev3dev2.button` ️️✔️
14+
`ev3dev2.console` ✔️️
1415
`ev3dev2.control` [1]_ ⚠️
15-
`ev3dev2.display` ❌
16-
`ev3dev2.fonts` [2]_ ⚠️
16+
`ev3dev2.display` [2]_ ❌
17+
`ev3dev2.fonts` [3]_ ❌
1718
`ev3dev2.led` ✔️
1819
`ev3dev2.motor` ✔️
1920
`ev3dev2.port` ✔️
@@ -25,7 +26,8 @@ Module Support status
2526
============================== =================
2627
2728
.. [1] Untested/low-priority, but some of it might work.
28-
.. [2] It might work, but isn't useful without ``ev3dev2.display``.
29+
.. [2] ``ev3dev2.display`` isn't implemented. Use ``ev3dev2.console`` for text-only, using ANSI codes to the EV3 LCD console.
30+
.. [3] ``ev3dev2.console`` supports the system fonts, but the fonts for ``ev3dev2.display`` do not work.
2931
```
3032

3133
## Differences from standard Python (CPython)
@@ -36,10 +38,17 @@ See [the MicroPython differences page](http://docs.micropython.org/en/latest/gen
3638

3739
You should modify the first line of your scripts to replace "python3" with "micropython":
3840

39-
```
41+
```python
4042
#!/usr/bin/env micropython
4143
```
4244

4345
### Running from the command line
4446

45-
If you previously would have typed `python3 foo.py`, you should now type `micropython foo.py`.
47+
If you previously would have typed `python3 foo.py`, you should now type `micropython foo.py`.
48+
49+
If you are running programs via an SSH shell to your EV3, use the following command line to
50+
prevent Brickman from interfering:
51+
52+
```shell
53+
brickrun -- ./program.py
54+
```

‎docs/spec.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Device interfaces
1616
power-supply
1717
sound
1818
display
19+
console
1920
ports
2021
port-names
2122
wheels
@@ -34,4 +35,4 @@ Each class in ev3dev module inherits from the base :py:class:`ev3dev2.Device` cl
3435

3536
.. autofunction:: ev3dev2.motor.list_motors
3637

37-
.. autofunction:: ev3dev2.sensor.list_sensors
38+
.. autofunction:: ev3dev2.sensor.list_sensors

‎ev3dev2/auto.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
raise Exception("Unsupported platform '%s'" % platform)
3737

3838
from ev3dev2.button import *
39+
from ev3dev2.console import *
3940
from ev3dev2.display import *
4041
from ev3dev2.fonts import *
4142
from ev3dev2.led import *

0 commit comments

Comments
 (0)