#! /usr/bin/env python3
#-*- coding: utf-8 -*-

# python3 /home/scripts_python_tests_capteurs/TFT9341/test_00.py


import time
import digitalio
import board

from adafruit_rgb_display import color565
import adafruit_rgb_display.ili9341 as ili9341

import RPi.GPIO as GPIO_BZP

POUSSOIR_BZP = 4
BUZZER_BZP   = 5
TFT_BACKLIGHT = 13

GPIO_BZP.setmode(GPIO_BZP.BCM)
GPIO_BZP.setwarnings(False)
GPIO_BZP.setup(POUSSOIR_BZP, GPIO_BZP.IN,  pull_up_down=GPIO_BZP.PUD_UP)
GPIO_BZP.setup(BUZZER_BZP,   GPIO_BZP.OUT, initial=GPIO_BZP.LOW)
GPIO_BZP.setup(TFT_BACKLIGHT,   GPIO_BZP.OUT, initial=GPIO_BZP.LOW)



cs_pin = digitalio.DigitalInOut(board.D8)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D7)

BAUDRATE = 24000000
spi = board.SPI()

display = ili9341.ILI9341(
	spi,
	rotation=90,
	cs=cs_pin,
	dc=dc_pin,
	rst=reset_pin,
	baudrate=BAUDRATE,
)


# Main loop:
try :
	while True:
		GPIO_BZP.output(TFT_BACKLIGHT, GPIO_BZP.HIGH)

		# Clear the display
		display.fill(0)
		# Draw a red pixel in the center.
		display.pixel(120, 160, color565(255, 0, 0))
		# Pause 2 seconds.
		time.sleep(2)
		# Clear the screen blue.
		display.fill(color565(0, 0, 255))
		# Pause 2 seconds.
		time.sleep(2)
		
except :
	GPIO_BZP.output(TFT_BACKLIGHT, GPIO_BZP.LOW)
