rdagger / micropython-ili9341 Goto Github PK
View Code? Open in Web Editor NEWMicroPython ILI9341Display & XPT2046 Touch Screen Driver
License: MIT License
MicroPython ILI9341Display & XPT2046 Touch Screen Driver
License: MIT License
Can i suggest a mod to the clear method of the driver to allow the number of rows that are cleared at a time to be controlled.
Currently depending on the orientation of the screen the number of bytes allocated is either 3840 (w=240, 0 or 180 degrees) or 5120 (w=320, 90 or 270 degrees). This can cause memory allocation issues
Perhaps enabling blanking of 4 or 2 rows at a time would alleviate memory issues eg
def clear(self, color=0, n=2):
"""Clear display.
Args:
color (Optional int): RGB565 color value (Default: 0 = Black).
"""
w = self.width
h = self.height
# Clear display in 1024 byte blocks ?
# not sure this is true - if w = 240 (the default orientation of 0 or 180 degrees)
# then it's 3840 bytes and if w = 320 (if rotation is 90 or 270) it's 5120
# so add a parameter to enable it to be done in smaller chunks due to memory
# allocation issues. with n = 4 it seems faster than when n = 8 which is what the
# original code had set. other n values that could be used 1,2,4,5,8,10,16,20,40,80
# but probably best to stick to low single digits
if color:
line = color.to_bytes(2, 'big') * (w * n)
else:
line = bytearray(2 * w * n)
for y in range(0, h, n):
self.block(0, y, w - 1, y + n - 1, line)
Hi,
I have a problem when using your library. I want to draw custom .raw image on my display and when i am using one from your examles it works flolessly but when i want to use my image it just display some weird random pixels and i dont know what to do with that. In attachment i have placed what images i want to display (I know it has to be raw type but github does not support it). Please help!
edit: afetr looking for it you mentioned img2rgb565.py but i cannot find it anywhere
Howdy. Really great Library! Already used it easily.
I am using the ili9341 with rotation=90
I see that there's about a 1/2 inch of usable space on the "left" (i.e 'bottom' of the ili9341 with rotation=0)
I have tried to manipulate VSCRDEF but it seems like I just end up squish the text all together.
Any ideas on how to achieve filling up the full screen using the rotation=90?
For reference:
spi = SPI(2, baudrate=51200000, sck=Pin(18), mosi=Pin(23))
display = Display(spi, dc=Pin(2), cs=Pin(15), rst=Pin(4),width=320, height=240, rotation=90)
Hi, i'm trying to run the demo_shapes.py
with an Raspberry PI Zero W, but everytime i try to run, it give me this error:
Traceback (most recent call last):
File "demo_shapes.py", line 4, in <module>
from machine import Pin, SPI
ModuleNotFoundError: No module named 'machine'
I already tried to install using pip3 install machine
, but no success. What can i do?
First, much respect for your efforts on this repository.
We use the ili9341 code set on the STM32F429 Discovery board as part of a course at our university. The board has a large flash (2M), but because of STM32 paging issues, the user MicroPython flash is relatively small (~160k). Thus, we freeze all the utility modules as bytecode into the firmware, which includes fonts, to free up program store for the students and their projects.
What follows is how we decided to implement that relative to your xglcd_font.py file. The question for you: Would you like us to do a Pull request to include this as something your other users might want, or would you like us to break into a separate branch? We are also open to counter proposals.
Rough code snippets to show the implementation: xglcd_font.py
def __init__(self, path, width, height, start_letter=32, letter_count=96, load_frozen=False):
...
if load_frozen:
self.letters = bytearray(self.bytes_per_letter * self.letter_count) # allocate space for subclass to fill.
else:
self.__load_xglcd_font(path)
Next, the font Bally7x9_froze.py (This is a .py variant of your .c font file. The .py allows it to easily be frozen as bytecode in MicroPython)
from xglcd_font_froze import XglcdFont
class Bally7x9 (XglcdFont):
def __init__(self):
super().__init__('', 7, 9, 32, 96, True) # 7x9 font, "standard" range of letters, frozen font loaded
font_data = [
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, # Code for char
...
0x02, 0x7C, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 # Code for char �
]
self.letters = bytearray(font_data)
The user can use both options as they desire. Traditional .c font files or .py font files (either frozen, or unfrozen in firmware).
from xglcd_font_froze import XglcdFont
from Bally7x9_froze import Bally7x9
arcadepix = XglcdFont('fonts/ArcadePix9x11.c', 9, 11)
bally = Bally7x9()
I've been tinkering with your driver to allow me to drive an ILI9488 and along the way found some nice (I think, but I'm new to all this python and microcontroller stuff) ways to enhance the performance of your driver for the ILI9341 which I also use and find to be excellent !
I concentrated on the block() function as it's the workhorse and pre-allocated some bytearrays to hold the page and column coordinates as well bytearrays for the command bytes themselves. I also moved away from calls to write_cmd() and write_data() and instead switched to direct spi.write() calls
Some numbers
Filled / unfilled rectangle drawing - before 2330ms, after 1688
Filled circles (192) - before 6451ms, after 2223
Filled squares (192) - before 456, after 323
Fairly decent improvements
I'm not to familiar with how GitHub works but I think I can attach the code to this ticket so you can have a look and decide if it's worth including in your driver
ili9341.txt
Hi, THank you for great work! I just interesting, how to draw some symbols, like "°"?
Thanks
Anyone knows how I can use this with an external SD Card?
I need it for my school project and it's the only thing that doesn't work. I want to simply load an image from the micro-sd card (not the tft's) when touching the screen at a specified coordinate. I can load it from the esp32 no problem, but the moment I initialize the sd card, the screen turns black.
I use micropython, ESP32 and an sd card library: https://github.com/micropython/micropython-lib/blob/master/micropython/drivers/storage/sdcard/sdcard.py
If I don't initialize it, it works fine.
Anyone knows something? I tried controlling high and low, even disconnecting the sd card and it would still give me a black screen.
Hi there,
I thought I'd pop you a message and let you know that I forked your code and made a minimum version of the library code for the lolin s2 mini with the lolin tft 2.4 shield.
Thanks for making this!
https://github.com/wolfen351/micropython-s2mini-wemos-tft-shield-ili9341-xpt2046
Hi,
I was looking into using the libraries to implement in rpi pico but I have tried everything but it just did not work.
Any ideas or any test code that you might have to implement this?
The library is working but my backlight remains off. It's not a hardware fault as the demo software worked.
The board I have is "ESP32 Arduino LVGL WIFI&Bluetooth Development Board 2.8" 240*320 Smart Display Screen 2.8inch LCD TFT Module With Touch WROOM" purchased from AliExpress.
I've tried setting each of the GPIO pins high and low in case that's how it's done. Any tips would be most welcome.
Hey, sorry to bother you with this but I was wondering if you could help me with this because I can’t find a way to fix it. On my screen (a ROHS 2.4” TFT SPI 240x320 V1.3) the colors appear inverted. 255,0,0 for example comes out as blue. Do you have any idea what it might be and how I could fix it?
Thank you so much for sharing your great work man!
The colors in draw_text8x8 are incorrect. Example...
display.clear(color565(0, 255, 0))
display.draw_text8x8(0, 0, "foo bar", color565(0, 0, 0), background=color565(0, 255, 0))
The background color of the text will differ from the rest of the background.
This occurs because draw_text8x8 uses the Framebuffer to draw the text, and the endianness of the FrameBuffer differs from that of the ILI9341. The current code attempts to work around this by extracting the RGB values from the color, then recreating it in BRG order using color565(). However, this does not result in correct colors.
A better solution would be to create the color with bytes order swapped.
In ili9341.py, replace lines 630 - 631...
fbuf.fill(color565(bg_b, bg_r, bg_g))
fbuf.text(text, 0, 0, color565(b, r, g))
...with...
bg_color = (bg_r & 0xf8) | bg_g >> 5 | (bg_b & 0xf8) << 5 | (bg_g & 0x1c) << 11
fbuf.fill(bg_color)
fb_color = (r & 0xf8) | g >> 5 | (b & 0xf8) << 5 | (g & 0x1c) << 11
fbuf.text(text, 0, 0, fb_color)
Tested in on my device and it appears to provide correct colors now.
Not exactly an issue, more a question. What image formats does the draw_image method support? I checked the images folder and found only raw files, but I could not open them as raw on my PC. Windows did not recognize them as raw.
I am using a Raspberry Pi Pico W with a 128x160 pixel LCD display from AZ-Delivery.
In most cases, the mirrored display isn't a problem, because I can just code everything mirrored.
But as far as I can see, there is no way to do this, when trying to display text.
Am I doing something wrong? Is this a bug? Is there a way to just mirror the display again so that it is correct?
I'd love to here from you. 😄
Here is a minimal working example:
import ili9341
from machine import Pin, SPI
spi = SPI(1, baudrate=15625000, sck=Pin(10), mosi=Pin(11))
display = ili9341.Display(spi, dc=Pin(15), cs=Pin(13), rst=Pin(14), width=128, height=160)
display.draw_text8x8(0, 0, "Hello World", ili9341.color565(255, 255, 255))
Hello,
I would like to use this library on Raspberry pi pico but as i have noticed in the tutorial it is for ESP32 and the pinout also for ESP32, Could you guide me for any documentation or anything so i can adapt it for the Raspberry pi which uses micropython?
Using rotation 90 or 270, does not maintain across the code the correct new Height and Width, not allowing to use full display.
Regards,
Pinnchus
Missing byte in WHITE color code defining.
WHITE = const(0XFFF) # (255, 255, 255)
--> WHITE = const(0XFFFF) # (255, 255, 255)
Hi,
I'm trying to perform a couple of simple actions using this library on a Raspberry Pi Pico running MicroPython, but I've encountered some issues:
See the video:
This is the code I'm running:
from ili9341 import Display, color565
from xpt2046 import Touch
from machine import Pin, SPI
from xglcd_font import XglcdFont
touch = None
def touch_handler(x, y):
global touch
display.draw_circle(x, y, 3, color565(255, 255, 255))
print(f"Touch at ({x}, {y})")
spi = SPI(0, baudrate=51200000, sck=Pin(18), mosi=Pin(19))
cs_pin = Pin(17, Pin.OUT)
dc_pin = Pin(16, Pin.OUT)
reset_pin = Pin(20, Pin.OUT)
display = Display(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, rotation = 0, bgr=False, width=240, height=320)
display.clear()
display.display_on()
unispace = XglcdFont('fonts/Robotron7x11.c', 7, 11)
display.draw_text8x8(0, 0, "Hello World", color565(255, 255, 255))
display.draw_hline(10, 10, 230, color565(255, 255, 255))
spi1 = SPI(1, baudrate=1000000, sck=Pin(14), mosi=Pin(11), miso=Pin(12))
touch_cs_pin = Pin(13, Pin.OUT)
touch_int_pin = Pin(9, Pin.IN)
touch = Touch(spi1, cs=touch_cs_pin, int_pin=touch_int_pin, int_handler=touch_handler)
while True:
pass
Hey,
i know this is quite old code. But i was wondering... could I use the framebuf library with this?
Specifically: I want to have a "start-up-animation" like every gaming console has. But for the framebuffer to use, i would need bitmap images.
Your library needs the raw rgb565... so I don't know where to go from here.
Maybe a little help for some micro-controlling-newbie?
TamTam
Hello, any chance you could help me figure out how to load a new font? I'm on linux so not using glcd-font-creator, I found a different utility that make a .c font file and I was able to load it after some struggles (the font has a different bytes per letter than specified in the load code from the xglcd_font script. Now I am getting an error in the get_letters function (in the xglcd_font scrip) when i try to draw letters on the screen. the error is on line 144 of the xglcd script, and I do not have much experience dealing with raw memmory. If you are willing to take a look I can share the font file etc. Thank you in advance, Brian
Howdy. I noticed the esp32_s3_box has an ili9342c based display:
Clearly, that is different from an ili9341 and was not intended for that usage.
But, being curious, I tried.
I can use this repo and write to the screen but it comes out as a mirror image and write down the screen instead of across.
What I am uncertain of is there would be support for the ili9342c/esp32_s3_box?
I've tried other repos w/o success using the esp32_s3_box and micropyton, so figured I'd toss the Q out there.
Thanks in advance!
In this python code: img2rgb565.py
I am puzzled why the RGB conversion is like this:
#r = (pix[0] >> 3) & 0x1F
#g = (pix[1] >> 2) & 0x3F
#b = (pix[2] >> 3) & 0x1F
Is this a typo?
I changed the code to this and the colors look way much better on my CYD display.
r = (pix[0] >> 3) & 0x1F
g = (pix[1] >> 3) & 0x1F
b = (pix[2] >> 3) & 0x1F
Hello and thanks for this project. I'm using it on my Pi Pico and it is way faster than the others I tried. I think I found a small error where the bottom edge of the screen is detected incorrectly since it calls is_off_grid without subtracting one from the line height.
It is on line 577 and should read,
if self.is_off_grid(x, y, x, y + h - 1):
Then it matches the block call following like hline does which was how I found it since the horizontal lines of the rectangle were drawing just fine at the bottom of the screen.
Hi folks,
Firstly, this library is fantastic, and thanks so much for developing it. I've got it working on a standard ESP 32 dev board and separate display, but just can't get it working on the Lilygo T4 https://www.lilygo.cc/products/t4. Nothing displays at all. Sorry for posting if this is not the right forum.
I am quite new to Micro python, been playing around with it for the last two weeks . I have a ILI9341 with XPT2046 touch controller and been using your library. The board has 11pins (GND) (VCC) (CLK) (MOSI) (RES) (DC) (BLK) (MISO) (CS1 [Screen Selector]) (CS2 [Touch Selector]) (Pen [Touch interrupt : 3v3 normally when I touch the screen goes to 0v and vice versa]) Here is my Settings
from ili9341 import Display
from machine import Pin, SPI
from xpt2046 import Touch
TFT_CLK_PIN = const(6)
TFT_MOSI_PIN = const(7)
#TFT_MISO_PIN = const(4)
TFT_CS_PIN = const(13)
TFT_RST_PIN = const(14)
TFT_DC_PIN = const(15)
XPT_CLK_PIN = const(10)
XPT_MOSI_PIN = const(11)
XPT_MISO_PIN = const(8)
XPT_CS_PIN = const(12)
XPT_INT = const(0)
def createMyDisplay():
spiTFT = SPI(0, baudrate=1000000, sck=Pin(TFT_CLK_PIN), mosi=Pin(TFT_MOSI_PIN))
display = Display(spiTFT,dc=Pin(TFT_DC_PIN), cs=Pin(TFT_CS_PIN), rst=Pin(TFT_RST_PIN),width=240, height=320, rotation=180)
return display
def createXPT(touch_handler):
spiXPT = SPI(1, baudrate=1000000,sck=Pin(XPT_CLK_PIN), mosi=Pin(XPT_MOSI_PIN), miso=Pin(XPT_MISO_PIN))
xpt = Touch(spiXPT, cs=Pin(XPT_CS_PIN), int_pin=Pin(XPT_INT),int_handler=touch_handler)
return xpt
I can either use the screen or use the touch screen, I cannot seem to get both working on the same SPI Bus. I have used a variety of settings ! Any help would be appreciated ?
Hi,
I don't know what I'm doing wrong, but whenever I try to write text, it's either all mirrored, or the font itself seems messed up.
I get the same result in real life as what you can see in this simulation (it takes a few seconds to initialize):
https://wokwi.com/projects/389429329585761281
Picture: https://ibb.co/F4cnqDy
Thanks for any help possible.
I am working with the ILI9341 TFT display and successfully loading images in the .raw format using the images from the images folder provided by this project. However, I am struggling to understand how to create my own .raw files, I looked everywhere and found nothing. They always end up loading wrong.
Some of the fonts like IBMPlexMono12x24.c, UbuntuMono12x24.c and UnispaceExt12x24.c have errors and cannot be used. Please advise. Thank you.
Hello,
I can mirror touch axes in the file xpt2046.py
With kind regards
ili9341.py in micropython, how to support png image display, thank you
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.