diff --git a/tutorial/9.oled_test.py b/tutorial/9.oled_test.py new file mode 100644 index 0000000..44ec94d --- /dev/null +++ b/tutorial/9.oled_test.py @@ -0,0 +1,73 @@ +import time +from datetime import datetime +from luma.core.interface.serial import i2c +from luma.core.render import canvas +from luma.oled.device import ssd1306, ssd1309, ssd1325, ssd1331, sh1106 + +# --- 1. OLED 장치 설정 --- +# 0x3C는 일반적인 0.96인치 SSD1306 OLED의 I2C 주소입니다. +# 만약 i2cdetect로 0x3D가 나왔다면 address=0x3D로 변경하세요. +# SDA 핀: GPIO 2 (물리적 핀 3), SCL 핀: GPIO 3 (물리적 핀 5) +serial = i2c(port=1, address=0x3C) + +# SSD1306 128x64 해상도 OLED 디바이스 생성 +# 다른 해상도나 컨트롤러 칩을 사용한다면 ssd1306 대신 다른 클래스 사용 +device = ssd1306(serial, rotate=0) # rotate=0, 1, 2, 3으로 디스플레이 방향 조절 가능 + +print("OLED 디스플레이 초기화 완료.") + +# --- 2. 텍스트 표시 예제 --- +def display_text_example(): + print("텍스트 표시 예제 실행 중...") + with canvas(device) as draw: + # 텍스트 그리기 + draw.text((0, 0), "Hello, Raspberry Pi!", fill="white") + draw.text((0, 15), "OLED Display Test", fill="white") + draw.text((0, 30), "Python Control", fill="white") + draw.text((0, 45), "I2C with luma.oled", fill="white") + time.sleep(3) # 3초간 표시 + + with canvas(device) as draw: + draw.text((0, 0), "Current Time:", fill="white") + # 실시간 시간 표시 (반복문 안에서 사용하면 계속 업데이트) + now = datetime.now() + current_time = now.strftime("%H:%M:%S") + draw.text((0, 20), current_time, fill="white") + draw.text((0, 40), f"Date: {now.strftime('%Y-%m-%d')}", fill="white") + time.sleep(3) # 3초간 표시 + +# --- 3. 화면 지우기 함수 --- +def clear_display(): + print("화면 지우기...") + with canvas(device) as draw: + draw.rectangle(device.bounding_box, outline="black", fill="black") # 전체를 검정색으로 채움 + time.sleep(1) + +# --- 메인 실행 흐름 --- +try: + clear_display() # 시작 전 화면 지우기 + display_text_example() + + # 무한 루프를 돌면서 시간 업데이트 (원하면 주석 해제하여 사용) + print("실시간 시간 업데이트 시작 (Ctrl+C로 종료)...") + while True: + with canvas(device) as draw: + # 배경을 검정으로 지우고 텍스트 그리기 + draw.rectangle(device.bounding_box, outline="black", fill="black") + now = datetime.now() + current_time = now.strftime("%H:%M:%S") + current_date = now.strftime("%Y-%m-%d") + + draw.text((0, 0), "Raspi Time:", fill="white") + draw.text((0, 20), current_time, fill="white") + draw.text((0, 40), current_date, fill="white") + + time.sleep(1) # 1초마다 업데이트 + +except KeyboardInterrupt: + print("\n프로그램 종료. OLED 화면을 끄고 정리합니다.") +finally: + # 프로그램 종료 시 OLED 화면 끄고 GPIO 정리 + clear_display() + # luma.oled는 device 객체가 소멸될 때 자동으로 정리합니다. + # 명시적인 GPIO.cleanup()은 RPi.GPIO 사용 시에만 필요합니다.