Video Autoplay on Pi Zero W

Brett’s working on a new RC car featuring an entertainment center. The media’s powered by a Raspberry Pi Zero W. A Waveshare 2 inch LCD currently provides the display. The instructions from Waveshare at the link are actually very good, but the following has a few additional notes and collected links.

Connection

I’m trying to do all this setup without hooking up a keyboard or display. I spent a good while trying to configure networking via USB OTG but that’s still a work-in-progress. So instead I set up WiFi so I could SSH in.

First order of business is imaging the micro-SD card with Raspberry Pi OS using the Raspberry Pi Imager, available for Arch via the AUR. In this article I’m working with:

Linux raspberrypi 5.10.63+ #1459 Wed Oct 6 16:40:27 BST 2021 armv6l GNU/Linux

On the imaged micro-SD card you touch boot/ssh to enable SSH.

In boot on the micro-SD card create the file wpa_supplicant.conf with contents:

country=US # Your 2-digit country code
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
    ssid="YOUR_NETWORK_NAME"
    psk="YOUR_PASSWORD"
    key_mgmt=WPA-PSK
}

Display

Next is configuration to enable the display.

Add the following in boot/config.txt on the micro-SD card:

    hdmi_force_hotplug=1
    hdmi_cvt=640 480 60 1 0 0 0
    hdmi_group=2
    hdmi_mode=1
    hdmi_mode=87
    display_rotate=0

Also in boot/config.txt, comment out:

    dtoverlay=vc4-fkms-v3d
    max_framebuffers=2

Pop the micro-SD into the device and boot. If you don’t have Bonjour or Avahi setup to resolve raspberrypi.local, look on your router’s devices table to get the IP address for the Pi. The default login is pi/raspberry. Change the password with passwd to be extra safe.

Enable SPI via raspi-config, under the Interfaces menu, and reboot.

Install the BCM2835 library from AirSpayce.

Install wiringPi. It’s seemingly no longer included in the Pi OS default image or its repositories so you’ll have to download & install manually.

Install and run Waveshare’s demos to make sure the display’s wiring is correct.

Install framebuffer copy and start it running:

sudo apt-get install -y cmake p7zip-full
wget https://www.waveshare.com/w/upload/8/8d/Waveshare_fbcp-main.7z
7z x Waveshare_fbcp-main.7z
cd waveshare_fbcp-main
mkdir build
cd build
cmake -DSPI_BUS_CLOCK_DIVISOR=20 -DWAVESHARE_2INCH_LCD=ON -DBACKLIGHT_CONTROL=ON -DSTATISTICS=0 ..
make -j
sudo ./fbcp &

Install VLC using the vlc package.

In SSH, use cvlc -f movie.mp4 to run the video after uploading it to the device.

 

Autoplay

To have the video autoplay at boot, create the file /etc/xdg/autostart/movie-autoplay.desktop with contents:

  [Desktop Entry]
  Name=Movie Autoplay
  Exec=cvlc -f --loop --no-video-title /home/pi/Videos/autoplay.mp4

You also need to have fbcp start on boot, via adding it to /etc/rc.local (just before exit).

Video Crop

For what it’s worth, the video playing above is captured from YouTube. That video has black borders, which can be measured and cropped out quickly using ffmpeg. Use the cropdetect utility filter to determine the border size:

ffmpeg -i cowboy-bebop.mp4  -vf cropdetect -f null -

This will report a bunch of lines like the following:

[Parsed_cropdetect_0 @ 0x558f57561c00] x1:52 x2:373 y1:0 y2:239 w:320 h:240 x:54 y:0 pts:2156154 t:89.839750 crop=320:240:54:0

The reported crop parameters can then be passed to ffplay to test and ffmpeg to recut the video:

ffplay -vf crop=320:240:54:0 cowboy-bebop.mp4
ffmpeg -i cowboy-bebop.mp4 -vf crop=320:240:54:0 -c:a copy cowboy-bebop-320px.mp4

Splash Screen

To replace the bootloader splash screen, just replace /usr/share/plymouth/themes/pix/splash.png. The image will seemingly work best if it’s 1024×768 even though that’s not the display resolution.

AGPTEK A20 MP3 Player Notes

Recently I wanted a super cheap MP3 player to use in settings where it was likely to get messed up or lost: DJing at bike races, hiking in iffy weather, etc.. This was much more bewildering than I expected as there are hundreds of models from dozens of unknown manufacturers, all with cryptic names and minor differences.

I wound up with a AGPTEK A20 because it’s cheap and would take a MicroSD big enough to cover my collection, without paying for somewhat redundant larger internal storage. As it turned out, I was correct in my surmise that in these commodity players it would be a pain to have music divided across both the internal and removable cards. Big tradeoff for this player that knocks about $10–15 off is that it has no Bluetooth support. It does though have FM radio and a recording feature for taking notes.

Early impression is that the A20 is… somehow a bit less impressive than you would expect even for $28, but no doubt on par with all the other obscure players in this class.

Physically the device is fine, and the software is functional… but the emphasis is on functional. Just a little bit more polish would help a lot. A small example would be parsing song and artist names to present the song list, rather than just showing filenames… Welcome to 1999! Except, you know, you can only ready about 9 characters. A larger improvement would be unifying the internal memory with the MicroSD card to present a single collection, rather than somewhat implicitly requiring the user to mode switch between them.

In any event though, the player does have the basics covered. It also works reasonably well with my Linux laptop, in a barebones way. That’s ultimately why I’m posting this, to confirm compatibility and record a couple notes for other users searching around.

The internal drive mounts seamlessly on my (Arch) Linux laptop and exposes the MicroSD card as well, which is nice. So pulling over a collection is a simple matter of copying or rsyncing.

The A20 will also play M3U playlists exported from any of a number of tools. The catch here is that you have to apply Windows conventions to those M3U files: It’s looking for CRLF line terminators, and backward slash (‘\’) path separators. Spaces, other special characters in the song names seem to be fine, and both the listing and playlist features work with songs buried several folders deep.

So, as long as you don’t want to do too much on the device itself other than hit play, and can prepare playlists and convert them appropriately, this seems like a reasonable super cheap MP3 option for Linux users.

Importing TTF Fonts Into LaTeX With Kerning & Ligatures

This past weekend I spent some time revamping the styling of some of my work and gaming documents. Generally I write all of these in LaTeX. Long story short, for the uninitiated, it’s a typesetting markup language. Somewhat similar to HTML, you write a text document peppered with commands and then run it through a compiler which produces a beautiful PDF or similar output. Even a marginally trained eye can identify the difference versus output from a word processor or web page, but most people will instinctively feel that the document is more professional.

Some of the foundations of LaTeX are a antiquated though.  The initial release of the underlying engine (TeX) dates to 1978. I wasn’t even born yet, and I’m an old man with a child of my own now! An oft cited trouble area remains fonts. The compiler doesn’t just hook into your system fonts, and you can’t just dump any old font file into a folder and have it work.

Fortunately working with fonts has gotten vastly better in the ~16 years I’ve been using LaTeX. The needed supporting programs are included in the major TeX/LaTeX distributions now, and a few short notes are around for using TrueType Fonts (TTF) that will mostly work with pdflatex, such as this post. If you’re using XeTeX or LuaTeX then you probably have an easier ride, but are on your own.

Kerning & Ligatures

Sadly though, even in this utopic typesetting future of ours, there’s still always a gotcha! It’s not widely remarked, but that commonly cited process does not carry over kerning and ligatures.

Ligatures are letter and symbol combos that are handled specially by a font, e.g., an ‘f’ followed by an ‘i’ combined to have the dot of the ‘i’ (formally the tittle of the ‘i’) be the drop tail of the ‘f’ (formally the hood).

Commonly cited and used ligatures. (image from Wikipedia)

Commonly cited and used ligatures. (image from Wikipedia)

Kerning is adjustments to the inter-symbol spacing between characters, e.g., a ‘V’ followed by an ‘A’ should be pushed together to avoid creating a large visual space.

Commonly cited and used kerning examples. (image from Wikipedia)

Commonly cited and used kerning examples. (image from Wikipedia)

That’s exactly how I realized the kerning of my fancy TTF fonts was not being carried through into LaTeX, when I saw this untidily spaced output:

Kerning fail!

Kerning fail!

At first I thought maybe the font, having gone through the wringer of one shady font website after another, didn’t include kerning and ligatures. But a quick check in FontForge revealed all was well with the font.

Inspecting kerning in FontForge.

Inspecting kerning in FontForge.

Conversion

Long story short, in the still awful machinery that is TeX fonts, the compiler can only read shapes from the TTF font files. The metrics—height, width, baseline, etc.—need to be extracted out to a separate TFM file.

Font metrics. Oh hell yeah. (image from Apple Developer)

Font metrics. Oh hell yeah. (image from Apple Developer)

That TFM format can capture kerning and ligatures, but the standard conversion tools don’t include them. Instead you need to:

  • From the actual original .TTF font, generate one kind of virtual font file, a .VPL, along with the font metrics, .TFM.
  • Generate another kind of virtual font, .VF, and a second set of font metrics, .TFM, from that virtual font .VPL.
  • Provide a proper file index pointing LaTeX to the original .TTF, both .TFM files, and the .VF, as a .FD font definition file.
  • Don’t screw up along the way.

Really easy ways to screw up include breaking underlying assumptions about filenames. The suite of tools involved in that font conversion process and LaTeX itself employ a bunch of conventions about extensions, precedence, and lowercasing. So it seems best just to keep your filenames really simple, without capitalization, spaces, special characters, etc.. And don’t leave any extraneous files laying around, e.g., font metrics from the virtual file with the same name as the original file, because that will be used in the wrong place and not work…

In any event, here are the detailed steps. Throughout, you should replace ‘custom’ with whatever simple name you want to give your font. All of this is simply operating from the working directory for simplicity.

Make sure the filename has no special characters, or uppercase letters.

cp MyCrazyFont_v7..ttf custom.ttf

Download or locate T1-WGL4.enc (here, or possibly already on your machine at /usr/share/texmf-dist/fonts/enc/ttf2pk/base/T1-WGL4.enc or similar) and use it to generate a TFM and a VPL:

ttf2tfm custom.ttf -p T1-WGL4.enc -v custom.vpl

Generate another TFM and a VF, noting that these are now using a different file basename so the original TFM remains accessible:

vptovf custom.vpl virtcustom.vf virtcustom.tfm

Copy the following mapping into t1custom.fd, changing the references in the code and filename itself to whatever you’re using instead of custom, but keeping the t1 filename prefix derived from the encoding type:

\ProvidesFile{t1custom.fd}
\DeclareFontFamily{T1}{custom}{}
\DeclareFontShape{T1}{custom}{m}{n}{ <-> virtcustom}{}
\pdfmapline{+custom\space <custom.ttf\space <T1-WGL4.enc}

Note that this mapping points to the original shape file (custom), but the second virtual font & metrics (virtcustom)!

Use the following command in your LaTeX to switch to the font:

\usefont{T1}{custom}{m}{n}

After all that, the kernings will be done up nice and proper. Good luck!

Kerning success!

Kerning success!