Skip to content

Commit

Permalink
Added set_image for gfxhat
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasBurgess2000 authored and Gadgetoid committed Nov 7, 2024
1 parent 012c5cd commit fc3055d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gfxhat/lcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def contrast(value):
"""Change GFX HAT LCD contrast."""
st7567.contrast(value)

def set_image(image):
"""Set the display buffer from a PIL Image and update the display."""
st7567.set_image(image)

def rotation(r=0):
"""Set the display rotation.
Expand Down
27 changes: 27 additions & 0 deletions gfxhat/st7567.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,33 @@ def contrast(self, value):
self.setup()
self._command([ST7567_SETCONTRAST, value])

def set_image(self, image):
"""Set the display buffer from a PIL Image and update the display.
:param image: PIL Image object to display
"""
# Ensure the image is in 1-bit mode and correct size
image = image.convert('1').resize((WIDTH, HEIGHT))
pixels = image.load()

for y in range(HEIGHT):
for x in range(WIDTH):
pixel = pixels[x, y]
value = 1 if pixel == 1 else 0
if self.rotated:
x_display = (WIDTH - 1) - x
y_display = (HEIGHT - 1) - y
else:
x_display = x
y_display = y
page = y_display // 8
bit = y_display % 8
offset = (page * WIDTH) + x_display
if value:
self.buf[offset] |= (1 << bit)
else:
self.buf[offset] &= ~(1 << bit)

if __name__ == '__main__': # pragma: no cover
st7567 = ST7567()
Expand Down

0 comments on commit fc3055d

Please sign in to comment.