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.
81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
from typing import List
|
|
import logging
|
|
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
from .screen import Screen
|
|
|
|
|
|
class Waveshare_233_SSD1305(Screen):
|
|
def __init__(self):
|
|
from drive import SSD1305
|
|
|
|
self.type = "Waveshare 2.33 SSD1305 OLED"
|
|
|
|
self.supports_custom_fonts = True
|
|
|
|
self.ssd1305 = SSD1305.SSD1305()
|
|
self.ssd1305.Init()
|
|
|
|
self.width = self.ssd1305.width
|
|
self.height = self.ssd1305.height
|
|
self.image = Image.new('1', (self.width, self.height)) # Create new image with 1 bit colour
|
|
self.draw = ImageDraw.Draw(self.image)
|
|
|
|
def setDefaultFont(self):
|
|
logging.debug("setting font to default font")
|
|
self.font = ImageFont.load_default()
|
|
|
|
def setFont(self, font, size):
|
|
self.font_size = size
|
|
self.font = ImageFont.truetype(font, size)
|
|
|
|
def clearBuffer(self):
|
|
self.draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
|
|
|
|
def displayOnScreen(self):
|
|
self.ssd1305.getbuffer(self.image)
|
|
self.ssd1305.ShowImage()
|
|
|
|
def displayLines(self, lines: List[str], x_margin: int = 2):
|
|
# displays text on the screen, the line that is first in the list is displayed on the top
|
|
self.clearBuffer()
|
|
self.clearScreen()
|
|
|
|
x_start = x_margin
|
|
y_start = 0
|
|
x_padding = 0
|
|
y_padding = 0
|
|
for i, line in enumerate(lines):
|
|
self.draw.text(
|
|
(
|
|
x_start+(i*x_padding),
|
|
y_start+y_padding
|
|
),
|
|
line,
|
|
font=self.font,
|
|
fill=255
|
|
)
|
|
bounding_box = self.draw.textbbox(
|
|
(
|
|
(x_start+x_padding),
|
|
y_start+y_padding,
|
|
),
|
|
line,
|
|
font=self.font,
|
|
)
|
|
bottom = bounding_box[3]
|
|
y_padding = bottom
|
|
self.displayOnScreen()
|
|
|
|
def clearScreen(self):
|
|
# Used by parent class Screen
|
|
buffer = Image.new('1', (self.width, self.height))
|
|
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)
|
|
self.ssd1305.getbuffer(buffer)
|
|
self.ssd1305.ShowImage()
|