#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import sys 
import time
import logging
import st7796
import hr2046
from PIL import Image, ImageDraw, ImageFont
import RPi.GPIO as GPIO

if __name__ == '__main__':
    disp = st7796.st7796()  # Initialize the st7796 display
    disp.clear()  # Clear the display
    touch = hr2046.hr2046()  # Initialize the hr2046 touch controller
    
    logging.info("show image")
    ImagePath = ["./pic/img_4.jpg", "./pic/img_5.jpg", "./pic/img_6.jpg", "./pic/img_7.jpg"]
    for i in range(0, 4):
        image = Image.open(ImagePath[i])  # Open the image file
        disp.show_image(image)  # Display the image
        time.sleep(0.5)  # Wait for 0.5 seconds
    disp.clear()  # Clear the display after showing images

    try:
        while True:
            time.sleep(0.1)  # Delay
            if GPIO.input(17) == GPIO.LOW:  # Check if the interrupt pin is low (indicating a touch event)
                print("Touch detected!")
                x, y = touch.read_touch_data()  # Get the touch coordinates
                disp.dre_rectangle(  # Draw a rectangle at the touch position
                    y + 2, x + 2,
                    y + 10, x + 10,
                    0x00fA  # Color
                )
                print(f"Touch Coordinates: X = {x}, Y = {y}")
                y = 0
                x = 0
            else:
                print("No touch detected")

    except KeyboardInterrupt:
        print("Program terminated")
        GPIO.cleanup()  # Clean up GPIO settings
