Getting to Grips with the Adafruit Mini PiTFT

I bought an Adafruit Mini PiTFT a year or so ago, I’d played around with the sample Python scripts provided by Adafruit but had never really found a practical use for it…until now!

I recently created a script for my Raspberry Pi that checks the latency and speed of my Internet connection every 5 minutes and writes this to a CSV file – which at some point I’ll actually analyse and produce some pretty charts! I wrote about it here. This got me thinking – could I write the output of these tests to my shiny new-ish display? My idea being I can quickly take glance at the display to see the current-ish performance of my Internet connection whenever I have the urge.

I attached the display to my Raspberry Pi, cracked open Visual Studio Code and made a start on this.

My plan was to display the last line of the CSV output file that my Internet speed test script creates (which will contain the outcome of the previous test run), draw this to the display and then refresh every 5 minutes.

I knew that I could use the Linux tail command to read the last line of the CSV file, such as:

tail -n 1 Output.csv

I wasn’t sure however, how to call this from a Python script and store the output as a variable, it turns out that this can be performed using the following commands, which I found after doing some Bing-Fu here (Solution 4).

from subprocess import PIPE, run
output = run("tail -n 1 Output.csv", stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)

Inspecting the output variable yielded this:

CompletedProcess(args=’tail -n 1 Output.csv’, returncode=0, stdout=’01/07/2021 17:12:47,3.441,371.70,34.53\n’, stderr=”)

The next thing I needed to do was to extract the stdout and split this into separate variables that I can then draw to the display, I used a mix of the split and replace Python functions to do this (it’s not pretty but it works).

For reference in order the stdout contains the execution time, ping time, download, and then upload speed.

Ping = "Ping:" + str(output).split(",")[3]
Download = "DL:" + str(output).split(",")[4]
Upload = "UL:" + str(output).split(",")[5].replace("\\n'", "")

I then checked the newly-created variables to make sure they displayed correctly.

The final thing for me to do was to adapt the Python Stats Example and incorporate what I’d done above. This uses a while loop that runs continuously (with a 5-minute pause between executions), my modified while loop can be found below, this does the following:

  • Clears the current image on the display
  • Runs the tail command on the Output.csv file
  • Creates variables from the output of the tail command (Ping, Download and Upload)
  • Draws these variables to the display
  • Sleeps for 300 seconds
while True:
    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, width, height), outline=0, fill=0)
    # Obtain output from last line of CSV file and create variables for each value
    output = run("tail -n 1 Output.csv", stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
    Ping = "Ping:" + str(output).split(",")[3]
    Download = "DL:" + str(output).split(",")[4]
    Upload = "UL:" + str(output).split(",")[5].replace("\\n'", "")
    # Draw the variables defined above to the screen.
    y = 0
    draw.text((x, y), Ping, font=font, fill="#FFFFFF")
    y += font.getsize(Ping)[1]
    draw.text((x, y), Download, font=font, fill="#FFFF00")
    y += font.getsize(Download)[1]
    draw.text((x, y), Upload, font=font, fill="#00FF00")
    y += font.getsize(Upload)[1]
    # Display the image featuring the text.
    disp.image(image, rotation)
    time.sleep(300)

I ran the script and checked the display on my Raspberry Pi – success!

The full script in all its glory can be found below:

import time
import digitalio
import board
import adafruit_rgb_display.st7789 as st7789
import os
from subprocess import PIPE, run
from PIL import Image, ImageDraw, ImageFont
# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
# Config for display baudrate (default max is 24mhz):
BAUDRATE = 64000000
# Setup SPI bus using hardware SPI:
spi = board.SPI()
# Create the ST7789 display:
disp = st7789.ST7789(
    spi,
    cs=cs_pin,
    dc=dc_pin,
    rst=reset_pin,
    baudrate=BAUDRATE,
    width=240,
    height=240,
    x_offset=0,
    y_offset=80,
)
# Create blank image for drawing.
height = disp.width 
width = disp.height
image = Image.new("RGB", (width, height))
rotation = 180
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image, rotation)
x = 0
# Load a TTF font.
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 22)
# Turn on the backlight
backlight = digitalio.DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
while True:
    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, width, height), outline=0, fill=0)
    # Obtain output from last line of CSV file and create variables for each value
    output = run("tail -n 1 Output.csv", stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
    Ping = "Ping:" + str(output).split(",")[3]
    Download = "DL:" + str(output).split(",")[4]
    Upload = "UL:" + str(output).split(",")[5].replace("\\n'", "")
    # Draw the variables defined above to the screen.
    y = 0
    draw.text((x, y), Ping, font=font, fill="#FFFFFF")
    y += font.getsize(Ping)[1]
    draw.text((x, y), Download, font=font, fill="#FFFF00")
    y += font.getsize(Download)[1]
    draw.text((x, y), Upload, font=font, fill="#00FF00")
 
    # Display the image featuring the text.
    disp.image(image, rotation)
    time.sleep(300)

One thought on “Getting to Grips with the Adafruit Mini PiTFT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s