starcontrol/screen.py

86 lines
2.2 KiB
Python
Raw Normal View History

import atexit
import math
import time
import board
import digitalio
from adafruit_rgb_display import rgb, st7789
from PIL import Image, ImageDraw, ImageOps
running = True
scr = None
scr_light = None
btn_upper = None
btn_lower = None
def screen_init():
global scr
global scr_light
global btn_upper
global btn_lower
scr_light = digitalio.DigitalInOut(board.D22)
scr_light.switch_to_output()
scr_light.value = True
# Configuration for CS and DC pins for Raspberry Pi
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
BAUDRATE = 64000000 # The pi can be very fast!
# Create the ST7789 display:
scr = st7789.ST7789(
board.SPI(),
cs=cs_pin,
dc=dc_pin,
rst=reset_pin,
baudrate=BAUDRATE,
width=135,
height=240,
x_offset=53,
y_offset=40,
)
scr.fill(rgb.color565(0,0,0))
btn_upper = digitalio.DigitalInOut(board.D23)
btn_lower = digitalio.DigitalInOut(board.D24)
btn_upper.switch_to_input()
btn_lower.switch_to_input()
@atexit.register
def screen_deinit():
global running
running = False # stops update thread
scr.fill(rgb.color565(0,0,0))
scr_light.value = False
current_icon = None
wifi_state = 0 # -1 error, 0 disconnected, 1 connected
dns_state = 0 # -1 error, 0 off, 1 running
download_state = 0 # yes/no
screen_on = True
def screen_update():
global current_icon
if wifi_state < 1 or dns_state < 1:
current_icon = Image.open("res/image/problem.png") # ⚠️
else:
current_icon = Image.open("res/image/okay.png") # ✅
#scr.fill(rgb.color565(0,0,0)) # blank
img_canvas = Image.new("RGB", (135, 240))
hnd_canvas = ImageDraw.Draw(img_canvas)
hnd_canvas.rectangle([(0,0), (135,240)], fill=(0,0,0))
hnd_canvas.text((math.floor(135/2), 0), time.strftime("%H:%M"), # top center of screen, "16:20"
fill=(50,200,255), anchor="mt", # cyan, middle-top
font_size=16)
img_canvas.paste(current_icon, (math.floor(135/2) - 32, math.floor(240/2) - 32))
scr.image(img_canvas)
def screen_thread():
while running:
screen_update()
time.sleep(1)