starcontrol/screen.py

100 lines
2.8 KiB
Python
Raw Normal View History

2024-11-04 19:38:22 -08:00
# Copyright © 2024 Nicole O'Connor
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
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)