jeffmer / micropython-ili9341

Micropython Driver for ILI9341 display
MIT License
124 stars 43 forks source link

Blit bitbuffer #11

Open TecDroiD opened 2 years ago

TecDroiD commented 2 years ago

Hi, i try to load a raw image (60x60 pixels, rgb565) using the following code:

from machine import Pin, SPI
from ili934xnew import *
import tt24 as font
from framebuf import *

spi = SPI(1, baudrate=45000000)
d = ILI9341(spi, Pin('X5'), Pin('X4'), Pin('X3'),w=320,h=240,r=2)

bg=color565(100,100,100)
fg=color565(0,0,0)
d.set_color(fg,bg)
d.erase()

with open('prism.raw', 'rb') as fp:
    b = FrameBuffer(bytearray(fp.read()),60,60,framebuf.RGB565)
    d.blit(b, 30,30,120,120)

and get this error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "ili934xnew.py", line 247, in blit
IndexError: bytearray index out of range

is it my fault or some issue in the blit source?

flashypepo commented 1 year ago

After some trial-and-error experiments, I got images properly on the TFT-display. It is a kind of a fix for the IndexError - do not ask why it works and there might be a much better fix! (Note: colormap seems to be a B/W-array and might therefore be too small, c is a 16-bit color value)

  1. Changes in method blit(): after the line: c = bitbuff.pixel(ix,iy)

    self._buf[index*2] = c    #self._colormap[c*2]
    self._buf[index*2+1] = c  #self._colormap[c*2+1]
  2. add method draw_image() to ili94xnew.py:

    # draw_image - partial from rdagger/Rototron, and @TecDroID issue Blit bitbuffer #11
    def draw_image(self, path, x=0, y=0, w=320, h=240):
    """ Draw image from flash.
        Args:
            path (string): Image file path.
            x (int): X coordinate of image left.  Default is 0.
            y (int): Y coordinate of image top.  Default is 0.
            w (int): Width of image.  Default is 320.
            h (int): Height of image.  Default is 240.
        """
    # read file contents in FrameBuffer
    with open(path, "rb") as fp:
        buf = framebuf.FrameBuffer(bytearray(fp.read()), w, h, framebuf.RGB565)
        self.blit(buf, x, y, w, h)
  3. test draw_image(): display.draw_image("RaspberryPiWB128x128.raw", 0, 0, 128, 128)

image dimensions: 128x128 pixels image(s) from Github @rdagger.

For details:

Hope someone would like this, and/or might do a proper fix.

PS. Thanks @Jeffmer for the driver, I like it due to the simple font handling (and uses Peter Hinch font_to_py).

Peter

flashypepo commented 1 year ago

After some more tests, it turns out text is not displayed on the TFT-display with above fix in blit().

Modification in blit():

  if (c == 0 | c == 1) :     # I'll assume text
      self._buf[index*2] = self._colormap[c*2]
      self._buf[index*2+1] = self._colormap[c*2+1]
  else:      # I'll assume an image
       self._buf[index*2] = c   
       self._buf[index*2+1] = c

It would be interesting to see how it goes. Peter