Skip to content

Instantly share code, notes, and snippets.

@samneggs
Created September 10, 2022 03:07
Show Gist options
  • Save samneggs/8d0f9932e0320bccae3026450ea723b1 to your computer and use it in GitHub Desktop.
Save samneggs/8d0f9932e0320bccae3026450ea723b1 to your computer and use it in GitHub Desktop.
MicroPython function to display variable size big text on a screen
from machine import Pin, SPI, WDT
import gc9a01
import framebuf, array
from micropython import const
buffer = bytearray(240*240*2)
MAXSCREEN_X = const(240)
@micropython.viper
def big_text(s,x1:int,y1:int,h_size:int,w_size:int,c:int,screen):
screen_addr = ptr16(screen)
s_width = int(len(s))*8
big_text = bytearray(s_width*8*2)
text_fb = framebuf.FrameBuffer(big_text,s_width,8,framebuf.RGB565)
text_fb.text(s,0,0,c)
text_addr=ptr16(big_text)
offset = y1*MAXSCREEN_X+x1
for y in range(8):
for x in range(s_width):
color=text_addr[y*s_width+x]
for ly in range(h_size):
for lx in range(w_size):
screen_addr[(ly+h_size*y)*MAXSCREEN_X+(w_size*x)+lx+offset]=color
spi = SPI(1, baudrate=80_000_000, sck=Pin(10), mosi=Pin(11))
tft = gc9a01.GC9A01(
spi,
240,
240,
reset=Pin(12, Pin.OUT),
cs=Pin(9, Pin.OUT),
dc=Pin(8, Pin.OUT),
backlight=Pin(13, Pin.OUT),
rotation=0)
tft.init()
tft.rotation(0)
tft.fill(0)
# text, xpos, ypos, height, width, color, screen buffer
big_text('Hello',10,50,10,4,0xff00,buffer)
tft.blit_buffer(buffer, 0, 0, 240,240)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment