TFT Displays

Introduction

A wide range of displays is available for use with the Arduino system. For the WeMos D1 mini system I found 3 display boards. Each of them is based a on different controller chip and has different resolution. Here they are are:

  • 64x48 pixel 0.66" TFT screen with SSD1306 controller. The SSD1306 uses the I2C bus
  • 128x128 pixel 1.4" display based on the ST7735 controller. The ST7735 uses the SPI bus
  • 320x240 pixel 2.4" display based on the ILI9341 display and the XPT2046 touch controller.
64x48 pixels with SSD1306 128x128 with ST7735 320x240 pixel touch screen with ILI9341
ssd1306.png tftScreen.png lolon2.4inch.png
Each of them has its advantages and disadvantages. The 0.66" display is really tiny and its resolution is very limited allowing to write just a few characters. In addition it is monochrome only. However, the reference display driver included in MicroPython uses this controller chip. There are also higher resolution and color screens using this controller but I did not find one with the WeMos D1 mini bus interface.

The 1.4" display is already quite a bit better even though 128x128 bit resolution is still not enough for sophisticated graphics. It is enough however to write a few lines of text and produce simple graphics, e.g. creating a graph of sensor readout results.

The 2.4" display is clearly the most powerful device, adding touch functionality. It is however so big that it is difficult to plug it onto the WeMos D1 bus, even if the connector is available. The better solution is an adapter board and a connecting cable.

Since the 1.4" display is enough for most our applications, it can be nicely plugged onto the bus just like any other sensor or actuator shield and it is substantially cheaper than the 2.4" solution, this is the device we chose for the course.

Preparing the hardware

Before using the display its solder jumpers on the back must be configured. There are 4 solder fields for configuration

  • TFT LED. his allows to control the display back light. I leave it unconfigured which results in the back light LED being permanently on
  • TFT_CS: D0. TFT_CS and TFT_DC can be selected onto D0, D3, D4 or D8 corresponding to GPIO 26, 17,16, 5. Since D3 and D4 cannot be used on the CPU with PSRAM the only selections possible are D0 and D8. I select D0 for CS and D8 for TFT_DC
  • TFT_DC: D8
  • TFT_RST is connected to the system reset
Here is a photo of the jumper settings:

tftJumpers.png

The driver software

Searching the Internet I found many display drivers, none of which really satisfied me.

  • The ssd1306 reference driver included in MicroPython. This driver is very basic and it relies very heavily on its framebuffer superclass.
  • A pure Python version of a ST7735 driver for the ESP32: https://github.com/boochow/MicroPython-ST7735. This a port of a ST7735 driver written by Guy Carver for stm32 processors (https://github.com/GuyCarver/MicroPython/blob/master/lib/ST7735.py). At first glance, this is exactly what I was looking for. However, Guy Carver himself was not too happy about its speed and, compared to other drivers, it has limited functionality.
    There is a demo program putting images up onto the screen and another one exercising the driver's graphic functions like
    • drawing line
    • drawing rectangles
    • drawing filled rectangles
    • drawing text
  • A C-module of the above driver also written by Guy. Finding this code was not so easy. It is hidden in a fork of MicroPython, made some years back, and it was again developed for stm32 processors. Since the method of integrating C-modules into MicroPython has been made substantially easier in recent versions of MicroPython quite a bit of work was needed to adapt this driver. When trying it, it was even slower than the Python code! I figured out that the reason was the way the SPI commands are sent to the controller. If you send commands and data in large chunks then you can speed up graphics considerably. This however is true for the Python and the C code.
  • A driver for displays based on the SSD1351 controller: https://github.com/rdagger/micropython-ssd1351 The display module used has 128x128 resolution just like the module we have. The driver comes with a big number of demo programs, some of which are rather sophisticated. It also provides many more functions than the ST7735 driver. A very nice tutorial with Youtube video is also available. Unfortunately loading fonts is extremely slow.
    I decided to try porting this library to the St7735 chip in such a way that the demo programs would run with a minimum of modification. Speeding up font loading and a port of the demo programs from the above ST7735 driver should be part of the process.

Developing our own driver

The first goal for our own driver was to be able to run all the demo programs on it. Since the calling sequences of the drivers are different (different method names, different way to pass parameters) some modifications to the demo code was of course necessary. In addition the method names and parameters defined in the framebuffer should be available.

Comparing the original ST7735 driver to the SSD1351 one we see the following differences

ST7735 SSD1351
clips drawing elements when they exceed display boundaries drops drawing elements if any pixel would exceed display boundaries
Provides 3 fonts as Python files Provides many different fonts as C files
These fonts must be loaded before use, which takes a long time
uses .bmp files for images uses binary rgb565 files for images
does not support sprites allows use of sprites
Comes with 2 demo programs for graphics primitives
and image display
Comes with man demo programs, some of which are rather sophisticated
Even supplies simple Arcade games
Finally I decided to come up with a mixture of both because I prefer drawing element clamping fro just dropping the element. In addition to the original naming convention of the methods additional names were introduced following the convention in the framebuffer driver.

Fonts

Fonts are normally loaded into RAM before use. However, this takes time and uses a big amount of RAM memory, a very scarce resource on the standard ESP32. (We are using an ESP32 with additional PSRAM where the problem is much less critical).

Since font loading in the SSD1351 driver takes ages (the demo-font program using 9 different fonts took ~ 30s before the first character could be written. After that text writing was fast) I searched for another method and ended up asking the MicroPython forum. I got a link to the micropython-font-to-py git repository explaining how the font data can be kept in flash and you can nevertheless get fast access to the them. The fonts are implemented as Python source files providing not only the font data but also access routines to the pixel data. These font files can be frozen into MicroPython and are thus stored in flash. When a character is written, only the pixel data for this character is loaded into RAM.

The problem was now how to convert the font files in C supplied with the SSD1351 driver to Python font files compatible with micropython-font-to-py. I wrote a Python script doing exactly this. The output is actually executable (on the PC) and will print the pixel data of the letter "A" for this font.

font.png

Finally I copied the fonts into the modules/fonts directory of the MicroPython sources and froze them into the binary.

You can use the fonts by importing the MicroPython module: from  ST7735 import Display, color565 import fonts/sysfont as sysfont sck = Pin(18)
miso= Pin(19)
mosi= Pin(23)
SPI_CS = 26
SPI_DC = 5
spi = SPI(2, baudrate=32000000, sck=sck, mosi=mosi, miso=miso) display=Display(spi,SPI_CS,SPI_DC)
display.draw_text(0, 0, 'Hello World!', sysfont, color565(255, 0, 0))  

-- Uli Raich - 2020-07-04

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng font.png r1 manage 2.7 K 2020-07-21 - 09:22 UliRaich  
PNGpng lolon2.4inch.png r1 manage 218.8 K 2020-07-04 - 09:45 UliRaich  
PNGpng ssd1306.png r1 manage 158.9 K 2020-07-04 - 09:44 UliRaich  
PNGpng tftJumpers.png r1 manage 626.4 K 2020-07-17 - 16:16 UliRaich  
PNGpng tftScreen.png r1 manage 144.4 K 2020-07-04 - 09:45 UliRaich  
Edit | Attach | Watch | Print version | History: r13 | r6 < r5 < r4 < r3 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r4 - 2020-07-21 - UliRaich
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2025 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback