You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.1 KiB
Python

import os
import logging
from typing import List
import psutil
from PIL import Image, ImageDraw, ImageFont
from .screen import Screen
class DebugScreen(Screen):
def __init__(self, width: int, height: int, number_of_colour_bits: str):
self.type = "Debug Screen"
self.supports_custom_fonts = True
self.width = width
self.height = height
self.image = Image.new(number_of_colour_bits, (self.width, self.height)) # Create a new image with 1 bit colour
self.draw = ImageDraw.Draw(self.image)
def setDefaultFont(self):
logging.debug("setting font to default font")
ImageFont.load_default()
def setFont(self, font: str, size: int):
logging.debug(f"setting font to {font} with size, {size}")
self.font = font
self.font_size = size
ImageFont.truetype(font, size)
def clearBuffer(self):
self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
def displayOnScreen(self):
self.image.show()
def displayLines(self, lines: List[str], x_padding: int = 0, y_padding: int = 4):
# displays text on the screen, the line that is first in the list is displayed on the top
self.clearBuffer()
self.clearScreen()
x_start = 0
y_start = 0
for i, line in enumerate(lines):
self.draw.text((x_start+(i*x_padding), y_start+int(i*self.font_size)+(i*y_padding)), line, fill=255)
self.displayOnScreen()
def clearScreen(self):
xdg_open_pid = -1
for p in psutil.process_iter():
if p.ppid() == os.getpid() and p.name() == "xdg-open":
logging.debug(f"found xdg-open process with a process id {p.pid}")
xdg_open_pid = p.pid
elif xdg_open_pid != -1 and p.ppid() == xdg_open_pid:
logging.debug(f"killing process, {p.name()} {p.cmdline()}")
p.terminate()
p.kill()
self.clearBuffer()
draw = ImageDraw.Draw(self.image)
# black rectangle to clear screen, without clearing buffer
draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)