# My Pi 400 CPU info display

Pi 400, stock no overclock.
Custom modified Pimorni pHat Stack that plugs directly into the Pi 400’s GPIO header.
Pimoorni Proto Zero with an RV3028 RTC and Trackball Breakout soldered to it.
Pimoorni Proto Zero with two SPI sockets setup side by side.
Two 1.54 Color SPI 240x240 LCD breakouts.
I borrowed the custom changes over time background code from an Enviro+ example. Strip that out for a simple background and the file gets a lot smaller.

``````#!/usr/bin/env python3

import os
import sys
import time
import numpy
import colorsys
import psutil
import ST7789
import pytz

from PIL import Image, ImageDraw, ImageFont, ImageFilter
from fonts.ttf import RobotoMedium as UserFont
from pytz import timezone
from astral.geocoder import database, lookup
from astral.sun import sun
from datetime import datetime, timedelta
from os import popen

def calculate_y_pos(x, centre):
"""Calculates the y-coordinate on a parabolic curve, given x."""
centre = 80
y = 1 / centre * (x - centre) ** 2

return int(y)

"""Calculates the bounds of a circle, given centre and radius."""

x1 = x - radius  # Left
x2 = x + radius  # Right
y1 = y - radius  # Bottom
y2 = y + radius  # Top

return (x1, y1, x2, y2)

def map_colour(x, centre, start_hue, end_hue, day):
"""Given an x coordinate and a centre point, a start and end hue (in degrees),
and a Boolean for day or night (day is True, night False), calculate a colour
hue representing the 'colour' of that time of day."""

start_hue = start_hue / 360  # Rescale to between 0 and 1
end_hue = end_hue / 360

sat = 1.0

# Dim the brightness as you move from the centre to the edges
val = 1 - (abs(centre - x) / (2 * centre))

# Ramp up towards centre, then back down
if x > centre:
x = (2 * centre) - x

# Calculate the hue
hue = start_hue + ((x / centre) * (end_hue - start_hue))

# At night, move towards purple/blue hues and reverse dimming
if not day:
hue = 1 - hue
val = 1 - val

r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, sat, val)]

return (r, g, b)

def x_from_sun_moon_time(progress, period, x_range):
"""Recalculate/rescale an amount of progress through a time period."""

x = int((progress / period) * x_range)

return x

def sun_moon_time(city_name, time_zone):
"""Calculate the progress through the current sun/moon period (i.e day or
night) from the last sunrise or sunset, given a datetime object 't'."""

city = lookup(city_name, database())

# Datetime objects for yesterday, today, tomorrow
utc = pytz.utc
utc_dt = datetime.now(tz=utc)
local_dt = utc_dt.astimezone(pytz.timezone(time_zone))
today = local_dt.date()
yesterday = today - timedelta(1)
tomorrow = today + timedelta(1)

# Sun objects for yesterday, today, tomorrow
sun_yesterday = sun(city.observer, date=yesterday)
sun_today = sun(city.observer, date=today)
sun_tomorrow = sun(city.observer, date=tomorrow)

# Work out sunset yesterday, sunrise/sunset today, and sunrise tomorrow
sunset_yesterday = sun_yesterday["sunset"]
sunrise_today = sun_today["sunrise"]
sunset_today = sun_today["sunset"]
sunrise_tomorrow = sun_tomorrow["sunrise"]

# Work out lengths of day or night period and progress through period
if sunrise_today < local_dt < sunset_today:
day = True
period = sunset_today - sunrise_today
# mid = sunrise_today + (period / 2)
progress = local_dt - sunrise_today

elif local_dt > sunset_today:
day = False
period = sunrise_tomorrow - sunset_today
# mid = sunset_today + (period / 2)
progress = local_dt - sunset_today

else:
day = False
period = sunrise_today - sunset_yesterday
# mid = sunset_yesterday + (period / 2)
progress = local_dt - sunset_yesterday

# Convert time deltas to seconds
progress = progress.total_seconds()
period = period.total_seconds()

return (progress, period, day, local_dt)

def draw_background_left(progress, period, day):
"""Given an amount of progress through the day or night, draw the
background colour and overlay a blurred sun/moon."""

# x-coordinate for sun/moon
x = x_from_sun_moon_time(progress, period, WIDTH)

# If it's day, then move right to left
if day:
x = WIDTH - x

# Calculate position on sun/moon's curve
centre = WIDTH / 2
y = calculate_y_pos(x, centre)

# Background colour
background_left = map_colour(x, 80, mid_hue, day_hue, day)

# New image for background colour
img_left = Image.new('RGBA', (WIDTH, HEIGHT), color=background_left)

# New image for sun/moon overlay
overlay = Image.new('RGBA', (WIDTH, HEIGHT), color=(0, 0, 0, 0))
overlay_draw = ImageDraw.Draw(overlay)

# Draw the sun/moon
overlay_draw.ellipse(circle, fill=(200, 200, 50, opacity))

# Overlay the sun/moon on the background as an alpha matte

return composite_left

def draw_background_right(progress, period, day):
"""Given an amount of progress through the day or night, draw the
background colour and overlay a blurred sun/moon."""

# x-coordinate for sun/moon
x = x_from_sun_moon_time(progress, period, WIDTH)

# If it's day, then move right to left
if day:
x = WIDTH - x

# Calculate position on sun/moon's curve
centre = WIDTH / 2
y = calculate_y_pos(x, centre)

# Background colour
background_right = map_colour(x, 80, mid_hue, day_hue, day)

# New image for background colour
img_right = Image.new('RGBA', (WIDTH, HEIGHT), color=background_right)

# New image for sun/moon overlay
overlay = Image.new('RGBA', (WIDTH, HEIGHT), color=(0, 0, 0, 0))
overlay_draw = ImageDraw.Draw(overlay)

# Draw the sun/moon
overlay_draw.ellipse(circle, fill=(200, 200, 50, opacity))

# Overlay the sun/moon on the background as an alpha matte

return composite_right

def overlay_text_left(img_left, position, text, font, align_right=False, rectangle=False):
draw = ImageDraw.Draw(img_left)
w, h = font.getsize(text)
if align_right:
x, y = position
x -= w
position = (x, y)
if rectangle:
x += 1
y += 1
position = (x, y)
border = 1
rect = (x - border, y, x + w, y + h + border)
rect_img = Image.new('RGBA', (WIDTH, HEIGHT), color=(0, 0, 0, 0))
rect_draw = ImageDraw.Draw(rect_img)
rect_draw.rectangle(rect, (255, 255, 255))
rect_draw.text(position, text, font=font, fill=(0, 0, 0, 0))
img_left = Image.alpha_composite(img_left, rect_img)
else:
draw.text(position, text, font=font, fill=(255, 255, 255))
return img_left

def overlay_text_right(img_right, position, text, font, align_right=False, rectangle=False):
draw = ImageDraw.Draw(img_right)
w, h = font.getsize(text)
if align_right:
x, y = position
x -= w
position = (x, y)
if rectangle:
x += 1
y += 1
position = (x, y)
border = 1
rect = (x - border, y, x + w, y + h + border)
rect_img = Image.new('RGBA', (WIDTH, HEIGHT), color=(0, 0, 0, 0))
rect_draw = ImageDraw.Draw(rect_img)
rect_draw.rectangle(rect, (255, 255, 255))
rect_draw.text(position, text, font=font, fill=(0, 0, 0, 0))
img_right = Image.alpha_composite(img_right, rect_img)
else:
draw.text(position, text, font=font, fill=(255, 255, 255))
return img_right

def get_cpu_temp():
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
temp = int(temp) / 1000.0
return temp

cpu_temps = [get_cpu_temp()] * 5

# Initialise the left LCD
disp_left = ST7789.ST7789(
port=0,
cs=0,
dc=9,
#backlight=19,
rotation=90,
spi_speed_hz=80 * 1000 * 1000,
)

disp_left.begin()

# Initialise the right LCD
disp_right = ST7789.ST7789(
port=0,
cs=1,
dc=9,
#backlight=19,
rotation=90,
spi_speed_hz=80 * 1000 * 1000,
)

disp_right.begin()

WIDTH = 240
HEIGHT = 240

# The city and timezone that you want to display.
city_name = "Halifax"

# Values that alter the look of the background
blur = 50
opacity = 125

mid_hue = 0
day_hue = 25

# Fonts
font_sm = ImageFont.truetype(UserFont, 27)
font_md = ImageFont.truetype(UserFont, 38)
font_lg = ImageFont.truetype(UserFont, 55)

while True:

path = os.path.dirname(os.path.realpath(__file__))
progress, period, day, local_dt = sun_moon_time(city_name, time_zone)
background_left = draw_background_left(progress, period, day)
background_right = draw_background_right(progress, period, day)

date_string = local_dt.strftime("%B %-d")
time_string = local_dt.strftime("%-I:%M%p")

img_left = overlay_text_left(background_left, (60, 5), date_string, font_md)
img_left = overlay_text_left(img_left, (225, 50), time_string, font_lg, align_right=True)

img_left = overlay_text_left(img_left, (10, 120), ("CPU Temperature"), font_sm)
cpu_temp = get_cpu_temp()
temp_string = f"{cpu_temp:.0f}°C"
img_left = overlay_text_left(img_left, (180, 155), temp_string, font_lg, align_right=True)

img_right = overlay_text_right(background_right, (28, 15), ("CPU Frequency"), font_sm)
str = str[str.find("=")+1:-7]
freq_string = f"{str}mhz"
img_right = overlay_text_right(img_right, (233, 50), freq_string, font_lg, align_right=True)

img_right = overlay_text_right(img_right, (53, 120), ("CPU Usage"), font_sm)
str = psutil.cpu_percent()
usage_string = f"{str}%"
img_right = overlay_text_right(img_right, (195, 155), usage_string, font_lg, align_right=True)

# Display image
disp_left.display(img_left)
disp_right.display(img_right)
time.sleep(1)
'''
sudp pip3 install st7789
sudo pip3 install pytz
sudo pip3 install astral
sudo pip3 install fonts
sudo pip3 install font-roboto
run crontab -e