from machine import Pin,SPI, ADC
import utime
import time
#
# Función para escribir en un registro
#
def Send(RegAddr, data):
buff = [0, 0]
buff[0] = RegAddr
buff[1] = data
CS.value(0)
spi.write(bytearray(buff))
CS.value(1)
#
# Registros empleados en el CI MAX7219
#
DECODE_MODE = 0x09
INTENSIDAD = 0x0A
SCAN_LIM = 0x0B
SHUTDOWN = 0x0C
# Valores para los registros
NOP_OPR = 0x01
NO_DECODE = 0x00
#
# Función para inicializar la matriz de LED's
#
def Configure():
Send(SHUTDOWN, NOP_OPR) # Modo Normal al encender
Send(DECODE_MODE,NO_DECODE) # Sin Decodificador
Send(INTENSIDAD, 0x07) # Intensidad de la matriz al 50 % aprox.
Send(SCAN_LIM, 0x07) # Se manejaran los 7 digitos
#
# Función que envia una imagen a la matriz
#
def Paint(Data_in):
for i in range(0, 7):
Send(i+1, Data_in[i])
Datos0 = [0x3C, 0x42, 0xA5, 0x81, 0xA5, 0x99, 0x42, 0x3C] # Carita Feliz
Datos1 = [0x38, 0x7C, 0x92, 0x92, 0xFE, 0xFE, 0xAA, 0xAA] # Fantasmita
Datos2 = [0x48, 0x30, 0x7E, 0x87, 0x85, 0x87, 0xFD, 0x7E] # TV
spi_sck = Pin(2) # pin SCK en GP2
spi_tx = Pin(3) # pin TX en GP3
spi_rx = Pin(0) # pin RX en GP0 (no es usado)
spi = SPI(0,sck=spi_sck,mosi=spi_tx,miso=spi_rx,baudrate=500000)
matriz = [0, 0, 0, 0, 0, 0, 0, 0]
px = 3
py = 3
painting = 0
debounce_time=0
def callback(boton):
global painting, px, py, matriz, debounce_time
if (time.ticks_ms()-debounce_time) > 500:
if painting == 0:
painting = 1
else:
aux = 0x80 >> py
matriz[px] = matriz[px] ^ aux
debounce_time=time.ticks_ms()
boton = machine.Pin(22, machine.Pin.IN, machine.Pin.PULL_UP)
boton.irq(trigger=Pin.IRQ_FALLING, handler=callback)
Pot_X = ADC(0)
Pot_Y = ADC(1)
CS = Pin(16, Pin.OUT) # CS
CS.value(1) # Deshabilita la matriz de LED's
Configure()
#
# Función que prende o apaga un LED
#
def SetLED(row, column):
val = 0x80 >> column
status = matriz[row]
status = status | val
Send(row+1, status)
# Muestra imágenes al inicio, mientras no se presione un botón
while painting == 0:
#Configure()
Paint(Datos0)
utime.sleep(1) # espera 1 segundos
#Configure()
Paint(Datos1)
utime.sleep(1) # espera 1 segundos
#Configure()
Paint(Datos2)
utime.sleep(1) # espera 1 segundos
SetLED(3, 3)
while 1:
sx = Pot_X.read_u16()
if sx < 21854:
if px > 0:
px = px - 1
else:
px = 7
if sx > 43690:
if px < 7:
px = px + 1
else:
px = 0
sy = Pot_Y.read_u16()
if sy < 21854:
if py < 7:
py = py + 1
else:
py = 0
if sy > 43690:
if py > 0:
py = py - 1
else:
py = 7
Paint(matriz)
SetLED(px, py)
utime.sleep(0.2) # espera 200 mili-segundos