Skip to content

Instantly share code, notes, and snippets.

@gragib
Last active December 26, 2022 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gragib/5c2bd759f1fff258604becd755c0bfef to your computer and use it in GitHub Desktop.
Save gragib/5c2bd759f1fff258604becd755c0bfef to your computer and use it in GitHub Desktop.
Pimoroni Display Hat Mini + Raspberry Pi Zero 2W

Pimoroni Display Hat Mini + Raspberry Pi Zero 2W

$ uname -a
# Linux raspberrypi 5.15.76-v7+ #1597 SMP Fri Nov 4 12:13:17 GMT 2022 armv7l GNU/Linux

$ lsb_release -a
# No LSB modules are available.
# Distributor ID: Raspbian
# Description:    Raspbian GNU/Linux 11 (bullseye)
# Release:        11
# Codename:       bullseye
$ sudo wget -O /boot/overlays/fbtft.dtbo https://github.com/raspberrypi/firmware/raw/1.20221104/boot/overlays/fbtft.dtbo
# /boot/config.txt
dtparam=spi=on
dtoverlay=fbtft,spi0-1,st7789v,dc_pin=9,width=240,height=320,led_pin=13,rotate=90
$ sudo apt install -yqq python3-pygame
$ sudo python pygame_demo.py
import colorsys
import contextlib
import math
import os
import signal
import sys
import time
with contextlib.redirect_stdout(None):
import pygame
print("""Display HAT Mini: Pygame Demo""")
class PygameDemo:
def __init__(self):
os.putenv('SDL_FBDEV', '/dev/fb0')
os.putenv('SDL_AUDIODRIVER', 'dsp')
self.hue_to_rgb = tuple(map(
lambda hue: colorsys.hsv_to_rgb(hue / 255.0, 1, 1),
range(0, 255),
))
pygame.display.init()
pygame.mouse.set_visible(False)
self.screen = pygame.display.set_mode((320, 240))
self.screen.fill((0, 0, 0))
pygame.display.update()
self._running = False
def _exit(self, sig, frame):
self._running = False
print("\nExiting!...\n")
def __del__(self):
"Destructor to make sure pygame shuts down, etc."
def run(self):
self._running = True
signal.signal(signal.SIGINT, self._exit)
while self._running:
for event in pygame.event.get():
if any((
event.type == pygame.QUIT,
event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE,
)):
self._running = False
break
self.screen.fill((0, 0, 0))
t = int(time.time() * 40)
for x in range(21):
for y in range(15):
r, g, b = self.tunnel(x, y, t)
r = min(255, int(r))
g = min(255, int(g))
b = min(255, int(b))
pygame.draw.circle(
self.screen,
(r, g, b),
(
(x * 15) + 7,
(y * 15) + 6 + 7,
),
7,
)
pygame.display.update()
pygame.quit()
sys.exit(0)
def tunnel(self, x, y, step):
u_width = 32
u_height = 32
speed = step / 100.0
x -= (u_width / 2)
y -= (u_height / 2)
xo = math.sin(step / 27.0) * 2
yo = math.cos(step / 18.0) * 2
x += xo
y += yo
if y == 0:
if x < 0:
angle = -(math.pi / 2)
else:
angle = (math.pi / 2)
else:
angle = math.atan(x / y)
if y > 0:
angle += math.pi
angle /= 2 * math.pi # convert angle to 0...1 range
hyp = math.sqrt(math.pow(x, 2) + math.pow(y, 2))
shade = hyp / 2.1
shade = 1 if shade > 1 else shade
angle += speed
depth = speed + (hyp / 10)
col1 = self.hue_to_rgb[step % 255]
col1 = (col1[0] * 0.8, col1[1] * 0.8, col1[2] * 0.8)
col2 = self.hue_to_rgb[step % 255]
col2 = (col2[0] * 0.3, col2[1] * 0.3, col2[2] * 0.3)
col = col1 if int(abs(angle * 6.0)) % 2 == 0 else col2
td = .3 if int(abs(depth * 3.0)) % 2 == 0 else 0
col = (col[0] + td, col[1] + td, col[2] + td)
col = (col[0] * shade, col[1] * shade, col[2] * shade)
return (col[0] * 255, col[1] * 255, col[2] * 255)
PygameDemo().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment