from machine import Pin, SPI
import max7219
import time
# กำหนดขา
MATRIX_DIN, MATRIX_CS, MATRIX_CLK = 19, 20, 21
spi = SPI(1, baudrate=1000000, polarity=0, phase=0, sck=Pin(MATRIX_CLK), mosi=Pin(MATRIX_DIN))
display = max7219.Matrix8x8(spi, Pin(MATRIX_CS),8)
# ข้อความ
msg1 = "KMITL"
msg2 = "CHAYANADA SORNSITH"
length2 = len(msg2) * 8 #คำนวณความยาวพิกเซล (ตัวอักษรละ 8 พิกเซล)
# รหัสสำหรับรูปหัวใจ (8x8 pixels)
heart = bytearray([0x00, 0x66, 0x99, 0x81, 0x81, 0x42, 0x24, 0x18])
# ฟังก์ชันวาดหัวใจ
def show_heart(x_pos):
for col in range(8):
byte = heart[col]
for row in range(8):
if byte & (1 << row):
display.pixel(x_pos + col, row, 1)
while True:
# Message 1
# 1. msg1 (Scroll In) หยุดที่ x=7
for x in range(64, 7, -1):
display.fill(0)
display.text(msg1, x, 0, 1)
show_heart(x + 40)
display.show()
time.sleep(0.01)
time.sleep(0.5)
# 2. แสดงทีละตัวอักษร K M I T L heart
current_text = ""
for char in msg1:
current_text += char
display.fill(0)
display.text(current_text, 8, 0, 1)
display.show()
time.sleep(0.4) # ความเร็วในการปรากฏทีละตัว
# heart
display.fill(0)
display.text(msg1, 8, 0, 1)
show_heart(48)
display.show()
time.sleep(0.5)
# 3. กระพริบรวม 2 ที
for _ in range(2):
# ปิดไฟ
display.fill(0)
display.show()
time.sleep(0.3)
# เปิดไฟ
display.fill(0)
display.text(msg1, 8, 0, 1)
show_heart(48)
display.show()
time.sleep(0.3)
time.sleep(0.5)
# 4. msg1 (Scroll Out)
for x in range(8, -80, -1):
display.fill(0)
display.text(msg1, x, 0, 1)
show_heart(x + 40)
display.show()
time.sleep(0.01)
# Message 2
for x in range(64, -length2, -2):
display.fill(0)
display.text(msg2, x, 0, 1)
display.show()
time.sleep(0.01)