"""
# Professor Cristiano Teixeira
# Sob Licença Apache 2.0
Display RGB 16x16 - Capetinha dando risada (desenho animado)
Wokwi: ESP32 DevKit v1 + wokwi-led-matrix 16x16
Conexao:
matrix:DIN -> esp:D2 (GPIO 2)
matrix:VDD -> esp:3V3
matrix:VSS -> esp:GND
IMPORTANTE: no Wokwi, defina o wokwi-led-matrix com:
{ "rows": "16", "cols": "16" }
"""
import time
from machine import Pin
import neopixel
# -------------------- Configuracao --------------------
WIDTH = 16
HEIGHT = 16
PIN = 2
NUM_LEDS = WIDTH * HEIGHT # 256
np = neopixel.NeoPixel(Pin(PIN), NUM_LEDS)
# Cores
PRETO = (0, 0, 0)
VERMELHO= (255, 0, 0) # pele do capeta
LARANJA = (255, 100, 0) # chifres e rabo
AMARELO = (255, 220, 0) # olhos
BRANCO = (255, 255, 255) # dentes / boca
# -------------------- Funcoes de matriz --------------------
def idx(x, y):
return y * WIDTH + x
def set_pixel(x, y, cor):
if 0 <= x < WIDTH and 0 <= y < HEIGHT:
np[idx(x, y)] = cor
def clear():
np.fill(PRETO)
def show():
np.write()
def sleep_ms(ms):
time.sleep_ms(ms)
# -------------------- Desenho do Capetinha (16x16) --------------------
# Corpo: ovalo vermelho centrado em (8, 8)
corpo = []
for y in range(3, 13):
for x in range(4, 13):
dx = x - 8
dy = y - 8
if dx * dx + dy * dy <= 22:
corpo.append((x, y, VERMELHO))
# Chifres (esquerdo e direito)
chifre_esq = [(4, 4), (5, 3), (4, 3), (3, 4)]
chifre_dir = [(12, 4), (11, 3), (12, 3), (13, 4)]
# Rabinho (3 frames para animar balancando)
rabo_frames = [
[(13, 11), (14, 10), (15, 9)],
[(13, 11), (14, 11), (15, 10)],
[(13, 11), (14, 12), (15, 11)],
]
# Olhos
olho_esq = [(6, 6), (7, 6), (6, 7), (7, 7)]
pupila_esq = [(6, 6)]
olho_dir = [(9, 6), (10, 6), (9, 7), (10, 7)]
pupila_dir = [(9, 6)]
# Boca aberta (risada)
boca_aberta = [
(6, 11), (7, 11), (8, 11), (9, 11), (10, 11),
(6, 12), (7, 12), (8, 12), (9, 12), (10, 12),
]
dentes = [(7, 12), (8, 12), (9, 12)]
# Boca fechada (sorriso pequeno)
boca_fechada = [(7, 12), (8, 12), (9, 12)]
# -------------------- Funcoes de desenho --------------------
def desenhar_corpo():
for x, y, c in corpo:
set_pixel(x, y, c)
def desenhar_chifres():
for x, y in chifre_esq + chifre_dir:
set_pixel(x, y, LARANJA)
def desenhar_olhos(abertos=True):
if abertos:
for x, y in olho_esq:
set_pixel(x, y, AMARELO)
for x, y in pupila_esq:
set_pixel(x, y, PRETO)
for x, y in olho_dir:
set_pixel(x, y, AMARELO)
for x, y in pupila_dir:
set_pixel(x, y, PRETO)
else:
# Olhos fechados de tanto rir
set_pixel(6, 6, PRETO); set_pixel(7, 6, PRETO)
set_pixel(9, 6, PRETO); set_pixel(10, 6, PRETO)
def desenhar_boca(aberta=False):
if aberta:
for x, y in boca_aberta:
set_pixel(x, y, BRANCO)
for x, y in dentes:
set_pixel(x, y, BRANCO)
else:
for x, y in boca_fechada:
set_pixel(x, y, BRANCO)
def desenhar_rabo(frame_idx):
for x, y in rabo_frames[frame_idx % 3]:
set_pixel(x, y, LARANJA)
# -------------------- Animacao: Capetinha rindo --------------------
def rindo():
frames = [
dict(olhos=True, boca=False, rabo=0), # normal
dict(olhos=True, boca=True, rabo=1), # abre sorrindo
dict(olhos=False, boca=True, rabo=2), # fecha os olhos rindo
dict(olhos=False, boca=True, rabo=0), # continua rindo
dict(olhos=True, boca=True, rabo=1), # abre os olhos
dict(olhos=True, boca=False, rabo=2), # fecha a boca
]
for f in frames:
clear()
desenhar_corpo()
desenhar_chifres()
desenhar_olhos(f["olhos"])
desenhar_boca(f["boca"])
desenhar_rabo(f["rabo"])
show()
sleep_ms(250)
# -------------------- Loop principal --------------------
print("Capetinha rindo - 16x16 ...")
print("Ajuste o wokwi-led-matrix para rows=16, cols=16")
clear()
show()
while True:
rindo()
sleep_ms(500) # pausa entre risadas