Controlling the default tty / framebuffer on a raspberry pi zero w.
When you power on a raspberry pi, after a lot of status messages etc scroll through the pi presents a login prompt:
This screen is tty1
. You can find it in the /dev/tty1
device on the filesystem.
A super user can write directly to this tty1
device, being that tty devices are text-mode terminals in a simple sense.
So, it is possible to write text to the device.
Using terminal escape codes it is possible to send commands to tty1
much as you would on the tty you are currently using.
The escape code \033c
will clear the screen. So we can execute a simple command to clear the screen before it says hello:
It is possible to log in through ssh and drive the monitor connected to a raspberry pi remotely. This is excellent for presentations - for example we could use catimg to send ascii art to the monitor connected to the raspberry pi (tty1
) - sometimes it looks cool, sometimes not so much - YMMV ;)
sudo -- sh -c 'echo "\033c" > /dev/tty1 ; catimg Pictures/yoda.jpg > /dev/tty1'
Ok so now it's pretty straight forward to write a little script that pushes a bunch of ascii to tty1
from a file in the (say,) Pictures
folder.
That way, we can make a slideshow type script:
- clear the screen
- blast some ascii
- wait x seconds
The example script proves that we can do that.
#!/bin/sh
echo "\033c" > /dev/tty1
catimg /home/user/Pictures/yoda.jpg > /dev/tty1
sleep 5
echo "\033c" > /dev/tty1
catimg /home/user/Pictures/darth-vader.jpg > /dev/tty1
The Raspberry Pi OS Lite linux comes with the watch
command which is highly useful for running something over and over again.
Given the script above, the "slideshow" we have created will last for ~5 seconds. This means we can tell watch
to run once every, say, 10 seconds and have a functional continuous slideshow controlled by basic tools.
I have a theory that if the interval
given to watch
is less than the time it takes to get through the script it will create a second running copy of the script - over time, the number of processes and scripts and images being decoded into ascii and whatnot will likely consume all the RAM on the pi and cause problems.
Remember to make sure your script is finished before
watch
runs it again.
I am sure there is wizardry that could be applied here, to avoid this but for the sake of keeping it as basic as possible I just recommend this:
- add up the number of
sleep
seconds (5 in the script above) - give
watch
an interval a few seconds more, just in case
watch --interval 10 sudo ./show-ascii-image-on-tty1.sh
htop
is a good way to keep an eye on your pi's resource use. You can use it to check (by eye) whether or not your continuously looping screenshow script is eating memory or whatnot.
Above, htop
is showing there is a call to sudo
to execute the script, and below the script itself running.