import pygame
import serial
import sys

PORT = '/dev/cu.usbserial-10'
BAUDRATE = 9600

try:
    ser = serial.Serial(PORT,BAUDRATE,timeout=1)
except:
    print(f"ポート{PORT}　に接続できません。")
    sys.exit(1)

# Pygameの初期化

pygame.init()
screen = pygame.display.set_mode((400,400))
pygame.display.set_caption("Arduino Button Monitor")
clock = pygame.time.Clock()

show_circle = False

running = True
while running:
    screen.fill((255,255,255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    
    # シリアルからのデータを受信
    if ser.in_waiting:
        line = ser.readline().decode('utf-8').strip()
        if line == 'down':
            show_circle = True
        elif line == 'up':
            show_circle = False
    
    # 円を表示するかどうか
    if show_circle:
        pygame.draw.circle(screen, (100,150,255),(200,200),50)
    
    pygame.display.flip()
    clock.tick(30)


ser.close()
pygame.quit()
