import time from rpi_ws281x import PixelStrip, Color # --- LED 스트립 설정 --- LED_COUNT = 6 # LED 개수 LED_PIN = 12 # WS2812B 데이터 핀이 연결된 GPIO 핀 (BCM 모드) LED_FREQ_HZ = 800000 # LED 신호 주파수 (800kHz) LED_DMA = 10 # DMA 채널 (0-14 중 사용 가능한 채널, 10이 일반적) LED_BRIGHTNESS = 255 # 밝기 (0-255) LED_INVERT = False # True로 설정하면 신호를 반전 (일부 스트립에서 필요) LED_CHANNEL = 0 # 0 또는 1 (PWM 채널, GPIO 12는 보통 채널 0) # PixelStrip 객체 생성 strip = PixelStrip(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS, LED_CHANNEL) # LED 스트립 초기화 strip.begin() print("WS2812B LED 스트립 초기화 완료.") # --- LED 제어 함수 --- def color_wipe(strip_obj, color, wait_ms=50): """지정된 색상으로 모든 LED를 순차적으로 켜는 애니메이션""" print(f"모든 LED를 {color}로 채우기...") for i in range(strip_obj.numPixels()): strip_obj.setPixelColor(i, color) strip_obj.show() time.sleep(wait_ms / 1000.0) def theater_chase(strip_obj, color, wait_ms=50): """극장의 불빛처럼 깜빡이는 애니메이션""" print(f"극장 불빛 애니메이션 ({color})...") for q in range(3): # 3번 반복 for i in range(0, strip_obj.numPixels(), 3): strip_obj.setPixelColor(i + q, color) strip_obj.show() time.sleep(wait_ms / 1000.0) for i in range(0, strip_obj.numPixels(), 3): strip_obj.setPixelColor(i + q, Color(0, 0, 0)) # LED 끄기 def wheel(pos): """색상 휠 함수 (0-255 사이 값으로 무지개 색상 반환)""" if pos < 85: return Color(pos * 3, 255 - pos * 3, 0) elif pos < 170: pos -= 85 return Color(255 - pos * 3, 0, pos * 3) else: pos -= 170 return Color(0, pos * 3, 255 - pos * 3) def rainbow(strip_obj, wait_ms=20, iterations=1): """무지개 색상 애니메이션""" print("무지개 애니메이션...") for j in range(256 * iterations): for i in range(strip_obj.numPixels()): strip_obj.setPixelColor(i, wheel((i + j) & 255)) strip_obj.show() time.sleep(wait_ms / 1000.0) # --- 메인 실행 루프 --- try: while True: # 모든 LED를 빨간색으로 채우기 color_wipe(strip, Color(255, 0, 0)) # Red time.sleep(1) # 모든 LED를 초록색으로 채우기 color_wipe(strip, Color(0, 255, 0)) # Green time.sleep(1) # 모든 LED를 파란색으로 채우기 color_wipe(strip, Color(0, 0, 255)) # Blue time.sleep(1) # 극장 불빛 애니메이션 (빨간색) theater_chase(strip, Color(127, 127, 127), wait_ms=50) # Half white time.sleep(1) # 무지개 애니메이션 rainbow(strip, wait_ms=20, iterations=1) time.sleep(1) except KeyboardInterrupt: print("\n프로그램 종료. 모든 LED 끄기.") color_wipe(strip, Color(0, 0, 0), 10) # 모든 LED 끄기 strip.setBrightness(0) # 밝기를 0으로 설정하여 완전히 끔 strip.show()