joukos / PaperTTY
- вторник, 4 сентября 2018 г. в 00:15:25
Python
PaperTTY - Python module to render a TTY on e-ink
This is an experimental command-line driven Python module to render the contents of a Linux virtual terminal (/dev/tty[1-63]
) or standard input onto a Waveshare e-Paper display. See list of supported displays.
Note: Testing has been minimal and I probably forgot something, so 'caveat utilitor'.
Note: I am also not affiliated with Waveshare in any way.
vim
, tmux
, irssi
, nethack
...) and display whatever you want easily with scripts.systemd
service unit to start the service early at boot and gracefully stop it.It isn't perfect and has only been tested with the monochrome 2.13" HAT, but it might work for other models too, and allows you to at least try.
drivers/
) is GPL 3.0 licensed, because it is based on Waveshare's GPL code - you still run it at your own risk.Collage of running various programs in tmux
Running Nethack outside in the noon sun, powered directly by a solar panel, connected to a Bluetooth keyboard
Action video - terminal usage (Raspberry Pi Zero W)
Showcasing input feedback.
Action video 2 - cacafire (Raspberry Pi 3)
The RPi3 is noticeably faster - cacafire
is 3x slower on the Zero. Typical terminal usage works pretty well.
All of the code was written for Raspbian Stretch and Python 3.5+. These instructions assume you're going to run this on a Raspberry Pi, otherwise you're on your own.
The code includes a reimplementation/refactoring of the Waveshare reference drivers - unlike the rest of the code which is CC0, the drivers have the GPL 3.0 license, because that's what Waveshare used. The drivers for models that aren't in the repo have been acquired from their Wiki's demo code packages.
See the driver page for details and the supported models.
The earlier, initial version of PaperTTY (tag: v0.01
) did not have instructions for using virtualenv (though it would work) - you can still run it as before using the system packages and alongside this new version. Using the virtualenv means that PIL and Pillow can also coexist on the same system.
sudo raspi-config
)
Interfacing Options -> SPI -> Yes
git clone https://github.com/joukos/PaperTTY.git
cd PaperTTY
sudo apt install virtualenvwrapper python3-virtualenv libopenjp2-7
mkvirtualenv
(you may want to add this to ~/.bashrc
)
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh
requirements.txt
mkvirtualenv -p /usr/bin/python3 -r requirements.txt papertty
~/.virtualenvs/papertty
which contains the required environment(papertty)
on your prompt
sudo
in the typical case, so you need to explicitly start the interpreter within the virtualenv - otherwise the program attempts to import system packages insteadsudo ~/.virtualenvs/papertty/bin/python3 ./papertty.py list
to see the available drivers and start using the software~/.virtualenvs/papertty/bin/activate
- activate the virtualenv
workon papertty
if you have sourced virtualenvwrapper.sh
deactivate
- deactivate the virtualenvsudo apt install python3-rpi.gpio python3-spidev python3-pil python3-click
sudo ./papertty.py list
You can use TrueType fonts or bitmap fonts, but the bitmap fonts need to be in the right format. With bitmap fonts the --size
option is ignored.
Included as default is a very small bitmap font called Tom Thumb, it is fairly readable for its tiny size and fits 20 rows with 62 columns on the 2.13". Thanks go to Brian Swetland and Robey Pointer for their work on the font and for releasing it under CC0.
Another included font is the nanofont, which is an extremely tiny (3x4 pixels) font and also released under CC0. Thanks go to the author, Michael Pohoreski. The conversion was done by generating the BMP, then transformed it with Pillow so that everything was on one line, then used Fony to save a BDF and converted that to PIL.
Why would you use such a microscopic font, I hear you ask? One good reason is that some programs refuse to start unless the terminal size is big enough, and using this font will allow you to get things theoretically readable and run those programs even on the smaller displays. One example being Dungeon Crawl Stone Soup which wouldn't otherwise start on the 2.13" display (hooray!):
Playing the game like this would be quite challenging, however...
Unless you're happy with the awesome default font, find a nice monospaced TrueType or bitmap font: Andale Mono (sudo apt install ttf-mscorefonts-installer
) is pretty great for very small sizes and on the 2.13" (128x250 pixels) can fit 17 rows and 50 columns
Pillow includes a utility called pilfont.py
, you can use this to convert a BDF/PCF font file into a .pil
and a .pbm
(I didn't have luck with some fonts - remember to use the pilfont.py
version that's on your Pi):
# convert Terminus
gunzip -c /usr/share/fonts/X11/misc/ter-u12b_unicode.pcf.gz > terminus-12.pcf
pilfont.py terminus-12.pcf
# you should get terminus-12.pil that you can pass with the --font option
All font options expect a path to the font file - the system font directories are not searched for them.
Remember to activate the virtualenv, then run sudo ./papertty.py
to get help.
sudo
unless you've set it up so that SPI works without and you've given read access to /dev/vcsa*
To do anything, you'll need to tell the script which model you're using - in my case this would be epd2in13
. Use the top-level option --driver
to set the desired driver.
Append --help
with the subcommands to get help with their parameters.
You can just edit papertty.py
to your liking - the code is very simple and commented.
Top-level options
Option | Description | Default |
---|---|---|
--driver NAME |
Select driver to use - required | no default |
--nopartial |
Disable partial refresh even if the display supported it | disabled |
--encoding NAME |
Select encoding to use | utf-8 |
Note: The encoding settings are a bit questionable right now - encoding/decoding is done explicitly to have ignore
on any errors, but I think this needs some more work as it's not an entirely trivial issue. If you feel like there's a big dum-dum in the code regarding these, a PR is very appreciated.
Note 2: To get scandinavian accents to show (ä
,ö
etc.), try --encoding cp852
.
list
- List display drivers# Example
sudo ./papertty.py list
scrub
- Scrub displayThis command mostly makes sense with the partial refresh models, although you can run it with full refresh too - it's just going to take a pretty long time to run. I needed this because my own unit can't handle a full refresh so it was the only way to clear the screen properly!
If you're left with "burn-in" or the display doesn't seem to work properly, this usually helps to even it out (may even need to run it twice sometimes if the display is not in a steady state).
This will slowly fill the screen with bands of black, then white.
Option | Description | Default |
---|---|---|
--size N |
Chunk width (pixels) to fill with (valid values: 8-32 ) |
16 |
# Example
sudo ./papertty.py --driver epd2in13 scrub
stdin
- Render standard inputRender stdin
on the display, simple as that. Leaves the image on the display until something else overwrites it. Very useful for showing script output or just about anything that updates irregularly.
Option | Description | Default |
---|---|---|
--font FILENAME |
Path to a TrueType or PIL font to use - strongly recommended to use monospaced | tom-thumb.pil |
--size N |
Font size | 8 |
--width N |
Fit to a particular width (characters) | display width / font width |
--portrait |
Enable portrait mode | disabled |
--nofold |
Disable folding (ie. don't wrap to width) | disabled |
--spacing |
Set line spacing | 0 |
# Example
cowsay "Hello World" | sudo ./papertty.py --driver epd2in13 stdin --nofold
terminal
- Render a virtual terminalThe most prominent feature.
This requires read permission to the virtual console device (/dev/vcsa[1-63]
) and optionally write permission to the associated terminal device (/dev/tty[1-63]
) if you want to set the TTY size via ioctl
s.
If you're going to use terminal
with a display that doesn't support partial refresh, you probably want to set --sleep
a bit larger than the default, such as a few seconds, unless you enjoy blinking.
The process handles two signals:
SIGINT
- stop and clear the screen (unless --noclear
was given), same as pressing Ctrl-C
sudo pkill -INT -f papertty.py
systemd
service unit attempts to stop the process using SIGINTSIGUSR1
- apply scrub and keep running
sudo pkill -USR1 -f papertty.py
See details on how all of this works further down this document.
Option | Description | Default |
---|---|---|
--vcsa FILENAME |
Virtual console device (/dev/vcsa[1-63] ) |
/dev/vcsa1 |
--font FILENAME |
Path to a TrueType or PIL font to use - strongly recommended to use monospaced | tom-thumb.pil |
--size N |
Font size | 8 |
--noclear |
Leave display content on exit | disabled |
--nocursor |
Don't draw cursor | disabled |
--sleep |
Minimum delay between screen updates (seconds) | 0.1 |
--rows |
Set TTY rows (--cols required too) |
no default |
--cols |
Set TTY columns (--rows required too) |
no default |
--portrait |
Enable portrait mode | disabled |
--flipx |
Mirror X axis (experimental / buggy) | disabled |
--flipy |
Mirror Y axis (experimental / buggy) | disabled |
--spacing |
Set line spacing | 0 |
--scrub |
Apply scrub when starting | disabled |
--autofit |
Try to automatically set terminal rows/cols for the font | disabled |
# Examples
# by default the first virtual terminal (/dev/vcsa1 == /dev/tty1) is displayed
sudo ./papertty.py --driver epd2in13 terminal
# set font size to 16, update every 10 seconds, set terminal rows/cols to 10x20
sudo ./papertty.py --driver epd2in13 terminal --size 16 --sleep 10 --rows 10 --cols 20
# auto-fit terminal rows/cols for the font and use a bitmap font
# (fitting may not work for very small fonts in portrait mode because of terminal restrictions)
sudo ./papertty.py --driver epd2in13 terminal --autofit --font myfont.pil
After you've gotten the terminal to render, you'll want to run something there.
As the program mirrors the system virtual terminals, you can either attach a keyboard to the Pi and simply log in or use the openvt
program to start something there without messing around with cables, if you already have SSH access.
The following commands are run over SSH.
For example, to start htop
for user pi
on tty1
(via sudo
, twice):
# "as a sudoer, start sudo forcibly on VT 1 (tty1) to run 'htop' as the user 'pi'"
sudo openvt -fc 1 -- sudo -u pi htop
After you exit the process, agetty
may go haywire though (hogging CPU). Give it a nudge to fix it:
sudo pkill agetty
And you should have the login prompt there again.
In practice, you'll want to use tmux
(or screen
, if you prefer) to have the most flexible control over the terminal (these are terminal multiplexers, and if you haven't used one before, now is the time to start):
# start a new tmux session (or just run 'tmux' with a connected keyboard)
sudo openvt -fc 1 -- sudo -u pi tmux
# (see the session starting up on the display)
# now, attach to the session
tmux attach
Lo and behold! You should now be attached to the tiny session visible on the display.
You can kill the papertty.py
process at any time - the stuff that runs in the TTY will be unaffected (unless they react badly to console resizing) and you can just restart the terminal
to get the display back and play around with the settings.
A simple systemd
service unit file is included with the package, called papertty.service
. It calls start.sh
so that instead of editing the service file, you can edit the start script (and easily add whatever you need) without needing to run systemctl daemon-reload
all the time.
sudo chown root:root start.sh; sudo chmod 700 start.sh
python3
command from within the virtualenv's bin
directory - this will ensure the environment is correctTo have the display turn on at boot, first edit the command you're happy with into start.sh
:
# Remember: you probably want to set rows and cols here, because at reboot they're reset.
# Also, when booting up after a power cycle the display may have some artifacts on it, so
# you may want to add --scrub to get a clean display (during boot it's a bit slower than usual)
VENV="/home/pi/.virtualenvs/papertty/bin/python3"
${VENV} papertty.py --driver epd2in13 terminal --autofit
Then make sure you have the right paths set in the service file:
...
### Change the paths below to match yours
WorkingDirectory=/home/pi/code/PaperTTY
ExecStart=/home/pi/code/PaperTTY/start.sh
###
...
Then (read the unit file more carefully and) do the following steps:
sudo cp papertty.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable papertty
# To disable the service:
# sudo systemctl disable papertty
# sudo systemctl stop papertty
This will incorporate the service with systemd
and enables it. Before rebooting and trying it out, you may want to stop any other instances of the papertty.py
and then see if the service works:
sudo systemctl start papertty
# (the service should start and the terminal should appear on the display,
# if you need to edit any settings, run 'systemctl daemon-reload' again after
# saving the service file)
sudo systemctl stop papertty
# (the service should stop and the display should be cleared, unless you used --noclear)
If the service seemed to work, try rebooting and enjoy watching the bootup. If you need to scrub the display while the service is running, you can send the SIGUSR1
signal to the process.
If the service didn't work, check that the paths are correct and that start.sh
has the execute bit set.
Kindles and the like have been around for a long time already, but there have been very few attempts at a general purpose e-ink display. General purpose meaning that I can use the programs I'm used to using and can display them on the e-ink display.
Why would anyone want such a thing, anyway? Here are some reasons:
Aside from digital price tags and similar special markets, there are some viable commercial offerings for mainstream computing on e-ink, such as the Onyx Boox Max2 that not only boasts a proper tablet form factor with an e-ink display, but also an HDMI input for using it as a secondary display (squee!). While it seems really cool, it's quite expensive, rare and more than just a simple display unit (and those cost just as much).
The display modules sold by Waveshare are exceptional in that they are very affordable (~15-90 USD), offer a wide range of sizes (1.54" up to 7.5") and even have "color" models (black/white/red). Earlier such offerings simply weren't there and people used to hack Kindles in very complex ways to get any of the fun.
So now that anyone can buy cheap e-ink, there is but one problem: how to get your content on it?
The display looks really cool and nifty but all you'll get in the package is just that and some code examples to draw something on it - with a program you need to write yourself. After unboxing, how does someone browse the Internet with it? Sadly, they don't.
I've had a Waveshare 2.13" HAT for the Raspberry Pi for a while now, and from time to time I've tried to find if someone had already implemented something like this since it sounds simple enough, but at the time of writing I don't know of any programs that mirror the terminal onto an e-ink, so I had a go at it.
For my purposes I just need proper terminal program support. The next step might be implementing a VNC client which should naturally translate quite well to e-ink's partial updating, but I don't have the time.
The principle of operation is deceptively simple:
/dev/vcsa*
(see man vcsa
)
/dev/tty1
(that you get with Ctrl-Alt-F1) is available at /dev/vcsa1
ioctl
s (requires write access to the /dev/ttyX
device)screendump
utility that reads from /dev/tty*
, reading from a vcsa*
does not include newlines)Image
objectSome notes:
scrub
command a couple of times first and wait for it to finish - powering off and disconnecting the module completely ought to help as a last resortscrub
feature may be entirely unnecessary for normally functioning units/dev/vcsu*
which would rectify this, but it's not yet in the mainline kernel - option to use a pseudo TTY would be welcome in the mean timeioctl
s - it would be better to use some pseudo terminal for this but then again, sometimes you specifically want tty1
(imagine server crashing and having the kernel log imprinted on the e-ink)spacing
keyword argument for drawing text - this may be a problem in some environments but I didn't think much of itEven with all the caveats in mind, I still think the program is very useful and fills a niche. I wish I could have tested it with more than one display model, but that's why I'm releasing it as public domain, so anyone can try it out and hopefully turn it into something better.
- Jouko Strömmer