a
This commit is contained in:
parent
54e78333dd
commit
7ba3bdcda1
|
@ -1 +0,0 @@
|
|||
#a
|
|
@ -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)
|
||||
|
|
|
@ -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()
|
30
main.py
30
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()
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
#a
|
2
modules/example_module.py
Normal file
2
modules/example_module.py
Normal file
|
@ -0,0 +1,2 @@
|
|||
def get_output():
|
||||
return "Hello from module!"
|
|
@ -1,2 +0,0 @@
|
|||
def get_text():
|
||||
return "Hello, world!"
|
0
utils/__init__.py
Normal file
0
utils/__init__.py
Normal file
|
@ -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"
|
Loading…
Reference in a new issue