micropython-ili9341
micropython-ili9341 copied to clipboard
Blit bitbuffer
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?
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)
- 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]
- 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)
- test
draw_image():display.draw_image("RaspberryPiWB128x128.raw", 0, 0, 128, 128)
image dimensions: 128x128 pixels image(s) from Github @rdagger.
For details:
- see Github @rdagger https://github.com/rdagger/micropython-ili9341/tree/master driver code ili9341.py, demo code demo_images.py, and images folder for images
- Rototron: https://www.rototron.info/projects/esp32-pwned-password-checker/
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
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