import os
import machine
import utime
import dht
from ili9341 import ILI9341, color565
from xpt2046_syn import XPT2046
spi = machine.SPI(1, baudrate=40000000, sck=machine.Pin(10), mosi=machine.Pin(11), miso=machine.Pin(12))
cs = machine.Pin(9)
dc = machine.Pin(8)
rst = machine.Pin(15)
bl = machine.Pin(17, machine.Pin.OUT)
display = ILI9341(spi, cs=cs, dc=dc, rst=rst)
display.fill(color565(255, 0, 0))
print("Display test completed successfully.")
bl.value(1)
touch = XPT2046(spi, cs=machine.Pin(14))
dht_sensor = dht.DHT22(machine.Pin(2))
red = machine.Pin(0, machine.Pin.OUT)
green = machine.Pin(1, machine.Pin.OUT)
blue = machine.Pin(16, machine.Pin.OUT)
def display_temp_humidity():
try:
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
display.fill_rectangle(0, 200, 320, 40, color565(0, 0, 0))
display.text('Temp: {:.1f}C'.format(temp), 10, 210, color565(255, 255, 255))
display.text('Hum: {:.1f}%'.format(hum), 160, 210, color565(255, 255, 255))
except Exception as e:
print("Error reading from DHT22:", e)
def handle_touch():
if touch.touched():
x, y = touch.get_touch()
if y < 80:
if x < 106:
red.value(not red.value())
update_led_status()
elif x < 212:
green.value(not green.value())
update_led_status()
else:
blue.value(not blue.value())
update_led_status()
def update_led_status():
display.fill_rectangle(0, 100, 320, 100, color565(0, 0, 0))
display.text('LED Status:', 10, 110, color565(255, 255, 255))
display.text('R: {}'.format('ON' if red.value() else 'OFF'), 10, 130, color565(255, 0, 0))
display.text('G: {}'.format('ON' if green.value() else 'OFF'), 110, 130, color565(0, 255, 0))
display.text('B: {}'.format('ON' if blue.value() else 'OFF'), 210, 130, color565(0, 0, 255))
def main():
display.text('Touch areas to toggle LEDs', 10, 50, color565(255, 255, 0))
while True:
handle_touch()
display_temp_humidity()
utime.sleep(2)
if __name__ == '__main__':
main()