From 7ba3bdcda128041c1027400739696504b6281157 Mon Sep 17 00:00:00 2001 From: Xargana Date: Fri, 16 May 2025 19:28:37 +0300 Subject: [PATCH] a --- __init__.py | 1 - display/font_manager.py | 22 ++++++----------- display/{screen.py => oled_display.py} | 17 ++++++------- main.py | 30 +++++++++++++++-------- modules/__init__.py | 1 - modules/example_module.py | 2 ++ modules/hello_world.py | 2 -- utils/__init__.py | 0 modules/ip_address.py => utils/network.py | 4 +-- 9 files changed, 40 insertions(+), 39 deletions(-) delete mode 100644 __init__.py rename display/{screen.py => oled_display.py} (61%) create mode 100644 modules/example_module.py delete mode 100644 modules/hello_world.py create mode 100644 utils/__init__.py rename modules/ip_address.py => utils/network.py (79%) diff --git a/__init__.py b/__init__.py deleted file mode 100644 index 28bbb07..0000000 --- a/__init__.py +++ /dev/null @@ -1 +0,0 @@ -#a \ No newline at end of file diff --git a/display/font_manager.py b/display/font_manager.py index 882829a..606f67a 100644 --- a/display/font_manager.py +++ b/display/font_manager.py @@ -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) diff --git a/display/screen.py b/display/oled_display.py similarity index 61% rename from display/screen.py rename to display/oled_display.py index 1d1abb8..5d01e05 100644 --- a/display/screen.py +++ b/display/oled_display.py @@ -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() diff --git a/main.py b/main.py index 57edf36..d4e26ed 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,29 @@ -from display.screen import OLEDDisplay -from display.font_manager import FontManager import time - -# Import modules -from modules import ip_address, hello_world +from display.oled_display import OLEDDisplay +from display.font_manager import FontManager +from utils.network import get_ip +from modules import example_module def main(): oled = OLEDDisplay() - font_mgr = FontManager(size=14) + font_mgr = FontManager(size=10) while True: - message = f"{ip_address.get_ip()}\n{hello_world.get_text()}" - font_mgr.draw_multiline_text(oled.draw, message, 0, 0, oled.display.width, oled.display.height) - oled.show_image() - time.sleep(5) + oled.clear() + + # Top HUD info (IP) + 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__": main() diff --git a/modules/__init__.py b/modules/__init__.py index 28bbb07..e69de29 100644 --- a/modules/__init__.py +++ b/modules/__init__.py @@ -1 +0,0 @@ -#a \ No newline at end of file diff --git a/modules/example_module.py b/modules/example_module.py new file mode 100644 index 0000000..7d3b8a2 --- /dev/null +++ b/modules/example_module.py @@ -0,0 +1,2 @@ +def get_output(): + return "Hello from module!" diff --git a/modules/hello_world.py b/modules/hello_world.py deleted file mode 100644 index a98d1ba..0000000 --- a/modules/hello_world.py +++ /dev/null @@ -1,2 +0,0 @@ -def get_text(): - return "Hello, world!" diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/modules/ip_address.py b/utils/network.py similarity index 79% rename from modules/ip_address.py rename to utils/network.py index fa5eb7b..06569fb 100644 --- a/modules/ip_address.py +++ b/utils/network.py @@ -6,6 +6,6 @@ def get_ip(): s.connect(("8.8.8.8", 80)) ip = s.getsockname()[0] s.close() + return ip except Exception: - ip = "No Connection" - return f"IP: {ip}" + return "No IP"