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 +0,0 @@
#a

View file

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

View file

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

30
main.py
View file

@ -1,19 +1,29 @@
from display.screen import OLEDDisplay
from display.font_manager import FontManager
import time import time
from display.oled_display import OLEDDisplay
# Import modules from display.font_manager import FontManager
from modules import ip_address, hello_world from utils.network import get_ip
from modules import example_module
def main(): def main():
oled = OLEDDisplay() oled = OLEDDisplay()
font_mgr = FontManager(size=14) font_mgr = FontManager(size=10)
while True: while True:
message = f"{ip_address.get_ip()}\n{hello_world.get_text()}" oled.clear()
font_mgr.draw_multiline_text(oled.draw, message, 0, 0, oled.display.width, oled.display.height)
oled.show_image() # Top HUD info (IP)
time.sleep(5) ip = f"IP: {get_ip()}"
font_mgr.draw_multiline_text(oled.draw, ip, 0, 0, oled.display.width, 10)
# Divider line
oled.draw.line((0, 14, oled.display.width, 14), fill=255)
# Main module output
output = example_module.get_output()
font_mgr.draw_multiline_text(oled.draw, output, 0, 18, oled.display.width, oled.display.height - 18)
oled.render()
time.sleep(2)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -1 +0,0 @@
#a

View file

@ -0,0 +1,2 @@
def get_output():
return "Hello from module!"

View file

@ -1,2 +0,0 @@
def get_text():
return "Hello, world!"

0
utils/__init__.py Normal file
View file

View file

@ -6,6 +6,6 @@ def get_ip():
s.connect(("8.8.8.8", 80)) s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0] ip = s.getsockname()[0]
s.close() s.close()
return ip
except Exception: except Exception:
ip = "No Connection" return "No IP"
return f"IP: {ip}"