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

#--------------------------------------------------------------------------------------------------
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341

import time

#--------------------------------------------------------------------------------------------------
import BuzzerPoussoir

#--------------------------------------------------------------------------------------------------
def TFT9341_backlight_off():
	try :
		BuzzerPoussoir.TFT_backlight_off()
		return True
		
	except :
		return False
	
#--------------------------------------------------------------------------------------------------
def TFT9341_backlight_on():
	try :
		BuzzerPoussoir.TFT_backlight_on()
		return True
		
	except :
		return False
	
#--------------------------------------------------------------------------------------------------
def TFT9341_init():
	global display,width,height,font,font_menus,image
	
	try:
		
		font       = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
		font_menus = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 26)

		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,
		)
		
		width  = 320
		height = 240

		image = Image.new("RGB", (width, height))
		
		draw = ImageDraw.Draw(image)
		draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
		display.image(image)
		
		image = Image.open("/home/scripts_python/AQCM05_logo_OK.jpg")
		display.image(image)
		
		time.sleep(3)

		return True
		
	except:
	
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_12(msg0, msg1):
	try :

		print( msg0 + " ; " + msg1 )
		print( " " )

		x_offset = 22
		y_offset = 50
		y_interligne = 30

		draw = ImageDraw.Draw(image)
		draw.rectangle((0, 0, width, height), outline=0, fill=0)
		
		y  = y_offset
		draw.text((x_offset, y), msg0, font=font_menus, fill="#24def0")
		y += y_interligne
		draw.text((x_offset, y), msg1, font=font_menus, fill="#24def0")

		display.image(image)
		
		return True
	
	except :
		print("TFT9341_update_TFT_12 : exception ! ")
		return False
			
#--------------------------------------------------------------------------------------------------
def TFT9341_display_TFT_mesures(mesures, couleurs):
	try :

		x_offset     = 0
		y_offset     = 10
		y_interligne = 26

		nb_mesures = len(mesures)

		draw = ImageDraw.Draw(image)
		draw.rectangle((0, 0, width, height), outline=0, fill=0)
		
		y  = y_offset
		for i in range (0, nb_mesures):
			draw.text((x_offset, y), mesures[i], font=font, fill=couleurs[i])
			y += y_interligne

		display.image(image)

		return True
	
	except :
		return False
			
#--------------------------------------------------------------------------------------------------
def TFT9341_close_TFT():
	try :
		return True
	
	except :
		return False
			
#--------------------------------------------------------------------------------------------------
