Skip to content

Commit

Permalink
Add resolution parameter to InkyMockImpression.
Browse files Browse the repository at this point in the history
Copied from #169

Co-authored-by: Phil Howard <[email protected]>
  • Loading branch information
corranwebster and Gadgetoid committed Nov 20, 2023
1 parent 0865247 commit fe70476
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions inky/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
class InkyMock(inky.Inky):
"""Base simulator class for Inky."""

def __init__(self, colour, h_flip=False, v_flip=False):
def __init__(self, colour, h_flip=False, v_flip=False, resolution=None):
"""Initialise an Inky pHAT Display.
:param colour: one of red, black or yellow, default: black
:param resolution: (width, height) in pixels
"""
global tkinter, ImageTk, Image
Expand All @@ -25,7 +26,8 @@ def __init__(self, colour, h_flip=False, v_flip=False):
except ImportError:
raise ImportError('Simulation requires PIL ImageTk and Image')

resolution = (self.WIDTH, self.HEIGHT)
if resolution is None:
resolution = (self.WIDTH, self.HEIGHT)

if resolution not in inky._RESOLUTION.keys():
raise ValueError('Resolution {}x{} not supported!'.format(*resolution))
Expand Down Expand Up @@ -74,12 +76,12 @@ def __init__(self, colour, h_flip=False, v_flip=False):
self._tk_done = False
self.tk_root = tkinter.Tk()
self.tk_root.title('Inky Preview')
self.tk_root.geometry('{}x{}'.format(self.WIDTH, self.HEIGHT))
self.tk_root.aspect(self.WIDTH, self.HEIGHT, self.WIDTH, self.HEIGHT)
self.tk_root.geometry('{}x{}'.format(self.width, self.height))
self.tk_root.aspect(self.width, self.height, self.width, self.height)
self.tk_root.protocol('WM_DELETE_WINDOW', self._close_window)
self.cv = None
self.cvh = self.HEIGHT
self.cvw = self.WIDTH
self.cvh = self.height
self.cvw = self.width

def wait_for_window_close(self):
"""Wait until the Tkinter window has closed."""
Expand Down Expand Up @@ -118,7 +120,7 @@ def _display(self, region):
image = self.disp_img_copy.resize([self.cvw, self.cvh])
self.photo = ImageTk.PhotoImage(image)
if self.cv is None:
self.cv = tkinter.Canvas(self.tk_root, width=self.WIDTH, height=self.HEIGHT)
self.cv = tkinter.Canvas(self.tk_root, width=self.width, height=self.height)
self.cv.pack(side='top', fill='both', expand='yes')
self.cvhandle = self.cv.create_image(0, 0, image=self.photo, anchor='nw')
self.cv.bind('<Configure>', self.resize)
Expand All @@ -130,7 +132,7 @@ def show(self, busy_wait=True):
:param busy_wait: Ignored. Updates are simulated and instant.
"""
print('>> Simulating {} {}x{}...'.format(self.colour, self.WIDTH, self.HEIGHT))
print('>> Simulating {} {}x{}...'.format(self.colour, self.width, self.height))

region = self.buf

Expand Down Expand Up @@ -234,9 +236,13 @@ class InkyMockImpression(InkyMock):
[177, 106, 73],
[255, 255, 255]]

def __init__(self):
"""Initialize a new mock Inky Impression."""
InkyMock.__init__(self, 'multi')
def __init__(self, resolution=None):
"""Initialize a new mock Inky Impression.
:param resolution: (width, height) in pixels, default: (600, 448)
"""
InkyMock.__init__(self, 'multi', resolution=resolution)

def _simulate(self, region):
self._display(region)
Expand Down

0 comments on commit fe70476

Please sign in to comment.