from machine import Pin, SPI
from time import sleep_ms
import ili9341
# =========================
# CONFIG DA TELA
# =========================
spi = SPI(
0,
baudrate=40000000,
sck=Pin(18),
mosi=Pin(19)
)
display = ili9341.ILI9341(
spi,
cs=Pin(17),
dc=Pin(16),
rst=Pin(20),
)
# =========================
# CORES RGB565
# =========================
def rgb565(r, g, b):
return ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
BLACK = rgb565(0, 0, 0)
WHITE = rgb565(255, 255, 255)
RED = rgb565(255, 0, 0)
GREEN = rgb565(0, 255, 0)
BLUE = rgb565(0, 0, 255)
YELLOW = rgb565(255, 255, 0)
CYAN = rgb565(0, 255, 255)
MAGENTA = rgb565(255, 0, 255)
ORANGE = rgb565(255, 140, 0)
PINK = rgb565(255, 80, 140)
BROWN = rgb565(139, 90, 43)
DARK_BROWN = rgb565(90, 55, 20)
BEIGE = rgb565(205, 170, 125)
colors = [RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, ORANGE, PINK, WHITE]
# =========================
# FUNCOES AUXILIARES
# =========================
def rect(x, y, w, h, color):
display.fill_rect(x, y, w, h, color)
def capivara(x, y, scale=2):
s = scale
# corpo
rect(x + 8*s, y + 18*s, 26*s, 12*s, BROWN)
# cabeça
rect(x + 2*s, y + 12*s, 14*s, 12*s, BROWN)
# focinho
rect(x + 0*s, y + 16*s, 6*s, 6*s, BEIGE)
# orelhas
rect(x + 5*s, y + 9*s, 3*s, 3*s, DARK_BROWN)
rect(x + 10*s, y + 9*s, 3*s, 3*s, DARK_BROWN)
# pernas
rect(x + 10*s, y + 30*s, 4*s, 8*s, DARK_BROWN)
rect(x + 18*s, y + 30*s, 4*s, 8*s, DARK_BROWN)
rect(x + 26*s, y + 30*s, 4*s, 8*s, DARK_BROWN)
rect(x + 32*s, y + 30*s, 4*s, 8*s, DARK_BROWN)
# olho
rect(x + 11*s, y + 15*s, 2*s, 2*s, BLACK)
# nariz
rect(x + 2*s, y + 18*s, 1*s, 1*s, BLACK)
rect(x + 4*s, y + 18*s, 1*s, 1*s, BLACK)
# rabo
rect(x + 34*s, y + 20*s, 2*s, 2*s, DARK_BROWN)
def draw_scene(text_color):
display.fill(BLACK)
# capivara esquerda
capivara(18, 110, 2)
# capivara direita
capivara(220, 110, 2)
# texto central
# fonte padrão do framebuf: 8x8
msg = "Hello word"
text_x = 110
text_y = 150
display.text(msg, text_x, text_y, text_color)
# =========================
# LOOP PRINCIPAL
# =========================
idx = 0
while True:
draw_scene(colors[idx])
idx = (idx + 1) % len(colors)
sleep_ms(350)