- tft.py: ILI9341 SPI 드라이버 + 320x240 대시보드(온도·습도·토양·릴레이 5칸·1시간 추세선), 바뀐 프레임만 전송 - oled.py: SSD1306/SH1106 I2C 상태 표시(smbus2+Pillow, 추가 설치 없음) - greenhouse.py: display 설정(목록)이 있을 때만 표시 스레드 시작. 표시 오류는 제어 루프에 영향 없음 - config.yaml: display 블록(TFT 기본, OLED 주석), 릴레이 짧은 이름(short) - oled_test.py / ili9341_test.py / display_demo.py: 배선 확인·시연용(MQTT·HA 전송 없음) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
110 lines
4.3 KiB
Python
110 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""2.4" ILI9341 SPI TFT(240x320) 배선 테스트. spidev + gpiozero + Pillow만 사용.
|
|
|
|
배선(BCM): MOSI=10 SCK=11 CS=8(CE0) DC=25 RESET=5, VCC/LED=3.3V, GND
|
|
python3 ili9341_test.py # 단색 R,G,B,흰,검 → 텍스트 화면
|
|
python3 ili9341_test.py --rot 0 # 세로(240x320). 기본은 가로(320x240)
|
|
python3 ili9341_test.py --bgr # 빨강/파랑이 바뀌어 보이면
|
|
"""
|
|
import argparse
|
|
import time
|
|
|
|
import spidev
|
|
from gpiozero import OutputDevice
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
DC, RST = 25, 5
|
|
|
|
|
|
class ILI9341:
|
|
def __init__(self, rot=1, bgr=False, speed=16_000_000):
|
|
self.dc = OutputDevice(DC)
|
|
self.rst = OutputDevice(RST, initial_value=True)
|
|
self.spi = spidev.SpiDev()
|
|
self.spi.open(0, 0)
|
|
self.spi.max_speed_hz = speed
|
|
self.spi.mode = 0
|
|
self.rot = rot
|
|
self.w, self.h = (320, 240) if rot else (240, 320)
|
|
self.madctl = (0x28 if rot else 0x48) ^ (0x08 if bgr else 0) # 기본 BGR 비트 켬
|
|
self._init()
|
|
|
|
def _cmd(self, c, data=()):
|
|
self.dc.off()
|
|
self.spi.xfer2([c])
|
|
if data:
|
|
self.dc.on()
|
|
self.spi.xfer2(list(data))
|
|
|
|
def _init(self):
|
|
self.rst.on(); time.sleep(0.05); self.rst.off(); time.sleep(0.05); self.rst.on(); time.sleep(0.15)
|
|
self._cmd(0x01); time.sleep(0.15)
|
|
for c, d in [(0xCF, (0x00, 0xC1, 0x30)), (0xED, (0x64, 0x03, 0x12, 0x81)), (0xE8, (0x85, 0x00, 0x78)),
|
|
(0xCB, (0x39, 0x2C, 0x00, 0x34, 0x02)), (0xF7, (0x20,)), (0xEA, (0x00, 0x00)),
|
|
(0xC0, (0x23,)), (0xC1, (0x10,)), (0xC5, (0x3E, 0x28)), (0xC7, (0x86,)),
|
|
(0x36, (self.madctl,)), (0x3A, (0x55,)), (0xB1, (0x00, 0x18)), (0xB6, (0x08, 0x82, 0x27)),
|
|
(0xF2, (0x00,)), (0x26, (0x01,)),
|
|
(0xE0, (0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00)),
|
|
(0xE1, (0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F))]:
|
|
self._cmd(c, d)
|
|
self._cmd(0x11); time.sleep(0.12)
|
|
self._cmd(0x29); time.sleep(0.05)
|
|
|
|
def _window(self):
|
|
w, h = self.w, self.h
|
|
self._cmd(0x2A, (0, 0, (w - 1) >> 8, (w - 1) & 0xFF))
|
|
self._cmd(0x2B, (0, 0, (h - 1) >> 8, (h - 1) & 0xFF))
|
|
self._cmd(0x2C)
|
|
|
|
def _write(self, buf):
|
|
self.dc.on()
|
|
for i in range(0, len(buf), 4096):
|
|
self.spi.xfer2(buf[i:i + 4096])
|
|
|
|
def fill(self, rgb):
|
|
r, g, b = rgb
|
|
v = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
|
|
self._window()
|
|
self._write([v >> 8, v & 0xFF] * (self.w * self.h))
|
|
|
|
def show(self, im):
|
|
d = im.convert("RGB").resize((self.w, self.h)).tobytes()
|
|
buf = bytearray(self.w * self.h * 2)
|
|
r, g, b = d[0::3], d[1::3], d[2::3]
|
|
for i in range(self.w * self.h):
|
|
v = ((r[i] & 0xF8) << 8) | ((g[i] & 0xFC) << 3) | (b[i] >> 3)
|
|
buf[2 * i] = v >> 8
|
|
buf[2 * i + 1] = v & 0xFF
|
|
self._window()
|
|
self._write(list(buf))
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--rot", type=int, default=1)
|
|
ap.add_argument("--bgr", action="store_true")
|
|
ap.add_argument("--speed", type=int, default=16_000_000)
|
|
a = ap.parse_args()
|
|
d = ILI9341(a.rot, a.bgr, a.speed)
|
|
for name, c in [("RED", (255, 0, 0)), ("GREEN", (0, 255, 0)), ("BLUE", (0, 0, 255)),
|
|
("WHITE", (255, 255, 255)), ("BLACK", (0, 0, 0))]:
|
|
print(name, flush=True)
|
|
d.fill(c)
|
|
time.sleep(0.8)
|
|
f = ImageFont.truetype("/usr/share/fonts/truetype/nanum/NanumGothicBold.ttf", 28)
|
|
big = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", 56)
|
|
im = Image.new("RGB", (d.w, d.h), (0, 0, 0))
|
|
g = ImageDraw.Draw(im)
|
|
g.rectangle((0, 0, d.w - 1, d.h - 1), outline=(255, 255, 255), width=2)
|
|
g.text((12, 10), "온실 Pi2 TFT 테스트", font=f, fill=(255, 255, 0))
|
|
g.text((12, 70), "24.3°C", font=big, fill=(80, 220, 255))
|
|
g.text((12, 150), "습도 61% 토양 42%", font=f, fill=(120, 255, 120))
|
|
for i, c in enumerate([(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 255)]):
|
|
g.rectangle((12 + i * 60, 195, 62 + i * 60, 225), fill=c)
|
|
d.show(im)
|
|
print("done")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|