This commit is contained in:
Xargana 2025-05-16 19:28:37 +03:00
parent 54e78333dd
commit 7ba3bdcda1
9 changed files with 40 additions and 39 deletions

View file

@ -1,27 +1,21 @@
from PIL import ImageFont
class FontManager:
def __init__(self, size=12):
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size)
def set_font_size(self, size):
def __init__(self, size=10):
self.font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size)
def draw_multiline_text(self, draw, text, x, y, width, height):
lines = []
words = text.split(" ")
words = text.split()
line = ""
for word in words:
test = f"{line} {word}".strip()
bbox = self.font.getbbox(test)
test_width = bbox[2] - bbox[0]
if test_width <= width:
line = test
test_line = line + word + " "
if draw.textlength(test_line, font=self.font) <= width:
line = test_line
else:
lines.append(line)
line = word
line = word + " "
lines.append(line)
draw.rectangle((0, 0, width, height), outline=0, fill=0)
for idx, line in enumerate(lines):
draw.text((x, y + idx * self.font.size), line, font=self.font, fill=255)
for i, l in enumerate(lines):
draw.text((x, y + i * (self.font.size + 2)), l, font=self.font, fill=255)

View file

@ -1,21 +1,20 @@
from PIL import Image, ImageDraw
import adafruit_ssd1306
import board
import busio
import adafruit_ssd1306
from PIL import Image, ImageDraw
class OLEDDisplay:
def __init__(self, width=128, height=64):
def __init__(self):
i2c = busio.I2C(board.SCL, board.SDA)
self.display = adafruit_ssd1306.SSD1306_I2C(width, height, i2c)
self.image = Image.new("1", (width, height))
self.display = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
self.display.fill(0)
self.display.show()
self.image = Image.new("1", (self.display.width, self.display.height))
self.draw = ImageDraw.Draw(self.image)
self.clear()
def clear(self):
self.draw.rectangle((0, 0, self.display.width, self.display.height), outline=0, fill=0)
self.display.image(self.image)
self.display.show()
def show_image(self):
def render(self):
self.display.image(self.image)
self.display.show()