englishtokorea/apa102.py
2025-05-28 14:26:49 +09:00

100 lines
3.0 KiB
Python

import time
import random
from PIL import Image, ImageDraw
from luma.led_matrix.device import max7219
from luma.core.interface.serial import spi, noop
# SPI 인터페이스 설정
serial = spi(port=0, device=0, gpio=noop())
device = max7219(serial, width=32, height=16, block_orientation=-90, blocks_arranged_in_reverse_order=False)
# 매트릭스 크기
matrix_width = 32
matrix_height = 16
# 눈동자 이미지 생성
eye_width = 8
eye_height = 8
eye_color = 255 # 흰색
eye_image = Image.new("1", (matrix_width, matrix_height), color=0) # 도트 매트릭스 크기로 이미지 생성
eye_draw = ImageDraw.Draw(eye_image)
eye_draw.rectangle([0, 0, eye_width, eye_height], fill=eye_color)
# 시작 위치
current_position = [matrix_width // 2 - eye_width // 2, matrix_height // 2 - eye_height // 2]
# 눈동자 이동 속도 (초 단위)
movement_speed = 0.1
try:
while True:
# 눈동자 위치 랜덤으로 이동
new_position = [random.randint(0, matrix_width - eye_width), random.randint(0, matrix_height - eye_height)]
print(type(new_position))
# 이동 방향 계산
dx = 1 if new_position[0] > current_position[0] else -1
dy = 1 if new_position[1] > current_position[1] else -1
# 눈동자 부드럽게 이동
while current_position != new_position:
# 이동 방향으로 한 픽셀씩 이동
current_position[0] += dx
current_position[1] += dy
print(current_position)
# 매트릭스에 이미지 표시
device.clear()
device.display(eye_image)
device.show()
time.sleep(movement_speed)
# 잠시 멈춤
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
device.clear()
#타이머
# import time
# from datetime import datetime, timedelta
# from PIL import Image, ImageDraw, ImageFont
# from luma.led_matrix.device import max7219
# from luma.core.interface.serial import spi, noop
#
# # SPI 인터페이스 설정
# serial = spi(port=0, device=0, gpio=noop())
# device = max7219(serial,width=32, height=16,block_orientation=-90, blocks_arranged_in_reverse_order=False)
#
# # 타이머 설정 (초 단위)
# timer_duration = 300 # 5분 타이머 예시
# end_time = datetime.now() + timedelta(seconds=timer_duration)
#
# try:
# while datetime.now() < end_time:
# remaining_time = end_time - datetime.now()
# seconds = int(remaining_time.total_seconds())
#
# # 분과 초 분리
# minutes, seconds = divmod(seconds, 60)
#
# # 이미지 생성
# image = Image.new("1", (device.width, device.height), color=0)
# draw = ImageDraw.Draw(image)
# font = ImageFont.load_default()
#
# # 텍스트 그리기
# text = f"{minutes:02d}:{seconds:02d}"
# draw.text((2, 2), text, font=font, fill=255)
#
# # 매트릭스에 이미지 표시
# device.display(image)
# time.sleep(1) # 1초마다 갱신
#
# finally:
# device.clear()