boochow / MicroPython-ST7735

ST7735 TFT LCD driver for MicroPython
200 stars 64 forks source link

Flashing updates, no buffer #9

Closed DedFishy closed 1 year ago

DedFishy commented 1 year ago

Hello, I'm using this with the intent of creating a game that utilizes the screen. However, there's no update method or anything to utilize a buffer, so every operation is immediately applied. This causes the screen to flicker and show things loading in, which is not ideal. Is there a way to fix this?

boochow commented 1 year ago

Hi, You want to use an off-screen buffer for drawing something and then transport the buffer to the screen, right? Here is an example. ( I tested this code on RPi Pico + ST7735. )

from machine import SPI,Pin
from ST7735 import TFT
spi = SPI(0, baudrate=20000000, polarity=0, phase=0, sck=Pin(2), mosi=Pin(3), miso=Pin(0))
tft=TFT(spi,4,5,1)
tft.initr()
tft.rgb(True)
tft.fill(TFT.BLACK)

from framebuf import FrameBuffer, RGB565
buf = bytearray(128*160*2)
fb = FrameBuffer(buf, 128, 160, RGB565)

tft._setwindowloc((0,0),(127,159))

size=20
(xmax, ymax) = (128-size, 160-size)
(x, y) = (size, size)
(vx, vy) = (1, 1)

while True:
    fb.fill(0)
    fb.ellipse(x, y, size, size, 0xffff, True)
    x += vx
    if x == xmax or x == size:
        vx = -vx
    y += vy
    if y == ymax or y == size:
        vy = -vy
    tft._writedata(buf)

pico-spi-st7735

DedFishy commented 1 year ago

This code works very well! Thank you so much! Maybe you should put this in as a separate script as an example, because it's a lot smoother this way.