def __init__(self, device=None, framebuffer=None, bgr=False, **kwargs): super(linux_framebuffer, self).__init__(serial_interface=noop()) self.id = self.__get_display_id(device) (width, height) = self.__config("virtual_size") self.bits_per_pixel = next(self.__config("bits_per_pixel")) # Lookup table of (target bit-depth, bgr) -> function, where the # function takes an RGB image and converts it into a stream of # bytes for the target bit-depth and RGB/BGR orientation. image_converters = { (16, False): self.__toRGB565, (24, False): self.__toRGB, (24, True): self.__toBGR, (32, False): self.__toRGBA, (32, True): self.__toBGRA, } assert ( self.bits_per_pixel, bgr ) in image_converters, f"Unsupported bit-depth: {self.bits_per_pixel}" self.__image_converter = image_converters[(self.bits_per_pixel, bgr)] self.framebuffer = framebuffer or diff_to_previous(num_segments=16) self.capabilities(width, height, rotate=0, mode="RGB") # This file handle is closed in self.cleanup() (usually invoked # automatically via a registered `atexit` hook) self.__file_handle = open(f"/dev/fb{self.id}", "wb")
def test_custom_full(): """ Auto-create feature runs out of custom character space """ device = hd44780(interface, bitmode=8, gpio=gpio, framebuffer=diff_to_previous(num_segments=1)) # Consume 8 special character positions img = Image.new('1', (80, 16), 0) drw = ImageDraw.Draw(img) for i in range(8): drw.rectangle((i * 5, 0, (i + 1) * 5, i), fill='white', outline='white') device.display(img) interface.reset_mock() # Consume one more (on the last char position on screen) drw.line((75, 8, 79, 15), fill='white') device.display(img) interface.assert_has_calls([ call.command(0x40), call.data([0x10, 0x08, 0x08, 0x04, 0x04, 0x02, 0x02, 0x01]), call.command(0xcf), call.data([0x0]) ])
def test_diff_to_previous(): framebuffer = diff_to_previous(num_segments=4) redraws = list(framebuffer.redraw(im1)) # First redraw should be the full image assert len(redraws) == 1 assert redraws[0][0] == im1 assert redraws[0][1] == im1.getbbox() assert redraws[0][1] is not None # Redraw of same image should return empty changeset redraws = list(framebuffer.redraw(im1)) assert len(redraws) == 0 # Redraw of new image should return two changesets redraws = list(framebuffer.redraw(im2)) assert len(redraws) == 2 assert redraws[0][0] == im2.crop((0, 20, 20, 40)) assert redraws[0][1] == (20, 0, 40, 20) assert redraws[1][0] == im2.crop((20, 0, 40, 20)) assert redraws[1][1] == (0, 20, 20, 40) # Redraw of original image should return two changesets redraws = list(framebuffer.redraw(im1)) assert len(redraws) == 2 assert redraws[0][0] == im1.crop((20, 0, 40, 20)) assert redraws[0][1] == (20, 0, 40, 20) assert redraws[1][0] == im1.crop((0, 20, 20, 40)) assert redraws[1][1] == (0, 20, 20, 40)
def test_diff_to_previous(): framebuffer = diff_to_previous(device) assert framebuffer.redraw_required(im1) pix1 = list(framebuffer.getdata()) assert len(pix1) == 12 assert pix1 == [0x00] * 12 assert framebuffer.bounding_box == (0, 1, 4, 4) assert framebuffer.inflate_bbox() == (0, 1, 4, 4) assert framebuffer.redraw_required(im2) pix2 = list(framebuffer.getdata()) assert len(pix2) == 3 assert pix2 == [0xFF] * 3 assert framebuffer.bounding_box == (0, 1, 1, 4) assert framebuffer.inflate_bbox() == (0, 1, 4, 4) assert not framebuffer.redraw_required(im2) assert framebuffer.getdata() is None assert framebuffer.bounding_box is None
def test_diff_to_previous_debug(): framebuffer = diff_to_previous(num_segments=4, debug=True) redraws = list(framebuffer.redraw(im1)) # First redraw should be the full image unchanged assert len(redraws) == 1 assert redraws[0][0] == im1 # Redraw of new image should return two changesets with red borders around them redraws = list(framebuffer.redraw(im2)) assert len(redraws) == 2 first_changeset_image = im2.copy().crop((20, 0, 40, 20)) draw = ImageDraw.Draw(first_changeset_image) draw.rectangle((0, 0, 19, 19), outline="red") del draw assert redraws[0][0] == first_changeset_image second_changeset_image = im2.copy().crop((0, 20, 20, 40)) draw = ImageDraw.Draw(second_changeset_image) draw.rectangle((0, 0, 19, 19), outline="red") del draw assert redraws[1][0] == second_changeset_image
def test_framebuffer_override(): """ Reproduce https://github.com/rm-hull/luma.examples/issues/95 """ ssd1327(serial, mode="1", framebuffer=diff_to_previous())