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

#--------------------------------------------------------------------------------------------------
import time
from math import floor

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

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

#--------------------------------------------------------------------------------------------------
couleurs_menus = ["#FFFFFF", "#00FF00", "#FFFF00", "#FF7F00", "#FF0000", "#00FFFF", "#FF00FF"] ;
#                 0 blanc    1 vert     2 jaune    3 orange   4 rouge    5 cyan     6 magenta

WARNING_EMOTICONS = [ "alerte_0.jpg", "alerte_1.jpg", "alerte_2.jpg", "alerte_3.jpg", "alerte_4.jpg", "alerte_5.jpg" ]
couleurs_alertes  = [      "#30FF80",      "#FFFF00",      "#FFBF00",      "#FF0000",      "#C000C0",      "#FF80FF" ]

NB_GRAPHES_TFT     = 10
GRAPHES_TFT        = [ "TFT_T.png", "TFT_HR.png", "TFT_CO2.png", "TFT_P_mer.png", "TFT_COV_IAQ.png", "TFT_PM25.png", "TFT_HCHO.png", "TFT_LUX.png", "TFT_SPL.png", "TFT_NOX_IAQ.png" ]
CHEMIN_GRAPHES_TFT = "/var/www/html/graphiques_jpgraph/"

#--------------------------------------------------------------------------------------------------
def TFT9341_backlight_off():
	try :
		BuzzerPoussoir.TFT_backlight_off()
		return True
		
	except :
		print("TFT9341_backlight_off : exception ! ")
		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_mesures,font_menus,font_menus_tft,font_titres_tft,font_alerts,font_big_1,font_big_2,font_big_3,font_big_4,image
	global image_IAQ, draw_IAQ
	global image_DBA, draw_DBA
	global image_NETWORK, draw_NETWORK, font_network
	global image_WEDI_0, draw_WEDI_0
	global image_WEDI_1, draw_WEDI_1
	global image_WEDI_2, draw_WEDI_2
	global image_WEDI_3, draw_WEDI_3
	global image_WEDI_4, draw_WEDI_4
	global image_WEDI_5, draw_WEDI_5
	global image_WEDI_6, draw_WEDI_6
	global image_WEDI_7, draw_WEDI_7
	global image_INFO, draw_INFO
	global image_menus, draw_menus
	
	try:
		
		TFT9341_backlight_off()

		font         = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
		font_mesures = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 19)
		font_menus   = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 22)
		font_titres_tft	= ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 24)
		font_menus_tft	= ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
		font_alerts  = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
		font_network = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
		
		font_big_1   = ImageFont.truetype("/usr/share/fonts/truetype/seven-segment.ttf", 30)
		font_big_2   = ImageFont.truetype("/usr/share/fonts/truetype/seven-segment.ttf", 90)
		font_big_3   = ImageFont.truetype("/usr/share/fonts/truetype/seven-segment.ttf", 45)
		font_big_4   = ImageFont.truetype("/usr/share/fonts/truetype/seven-segment.ttf", 75)

		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_IAQ = Image.open("/home/scripts_python_tests_capteurs/TFT9341/TFT_IAQ.jpg")
		draw_IAQ  = ImageDraw.Draw(image_IAQ)

		#------------------------------------------------------------------------------------------
		image_DBA = Image.open("/home/scripts_python_tests_capteurs/TFT9341/TFT_DBA_LUX.jpg")
		draw_DBA  = ImageDraw.Draw(image_DBA)

		#------------------------------------------------------------------------------------------
		image_NETWORK = Image.open("/home/scripts_python_tests_capteurs/TFT9341/network.jpg")
		draw_NETWORK  = ImageDraw.Draw(image_NETWORK)

		#------------------------------------------------------------------------------------------
		image_WEDI_0 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_0.jpg")
		draw_WEDI_0  = ImageDraw.Draw(image_WEDI_0)
		image_WEDI_1 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_1.jpg")
		draw_WEDI_1  = ImageDraw.Draw(image_WEDI_1)
		image_WEDI_2 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_2.jpg")
		draw_WEDI_2  = ImageDraw.Draw(image_WEDI_2)
		image_WEDI_3 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_3.jpg")
		draw_WEDI_3  = ImageDraw.Draw(image_WEDI_3)
		image_WEDI_4 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_4.jpg")
		draw_WEDI_4  = ImageDraw.Draw(image_WEDI_4)
		image_WEDI_5 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_5.jpg")
		draw_WEDI_5  = ImageDraw.Draw(image_WEDI_5)
		image_WEDI_6 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_6.jpg")
		draw_WEDI_6  = ImageDraw.Draw(image_WEDI_6)
		image_WEDI_7 = Image.open("/home/scripts_python_tests_capteurs/TFT9341/WEDI_7.jpg")
		draw_WEDI_7  = ImageDraw.Draw(image_WEDI_7)

		#------------------------------------------------------------------------------------------
		image_INFO = Image.open("/home/scripts_python_tests_capteurs/TFT9341/INFO.jpg")
		draw_INFO  = ImageDraw.Draw(image_INFO)

		#------------------------------------------------------------------------------------------
		image_menus = Image.open("/home/scripts_python_tests_capteurs/TFT9341/AQCM05_back_menus.jpg")
		draw_menus  = ImageDraw.Draw(image_menus)



		return True
		
	except Exception as except_str :
		print("TFT9341_init : exception  -> " + str(except_str) )

		return False

#--------------------------------------------------------------------------------------------------
'''
def TFT9341_display_blue():	
	try :
		# 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)
		
		return True
	
	except :
		print("TFT9341_display_blue : exception ! ")
		return False
'''

#--------------------------------------------------------------------------------------------------
def TFT9341_display_logo():	
	try :
		image = Image.open("/home/scripts_python_tests_capteurs/TFT9341/AQCM05_logo_OK.jpg")
		display.image(image)
		TFT9341_backlight_on()
		time.sleep(3)
		
		return True
	
	except :
		print("TFT9341_display_logo : exception ! ")
		return False

#--------------------------------------------------------------------------------------------------
'''
def TFT9341_set_menus_backscreen():
	global image_menus, draw_menus
	
	try :
		image_menus = Image.open("/home/scripts_python_tests_capteurs/TFT9341/AQCM05_back_menus.jpg")
		display.image(image_menus)
		draw_menus = ImageDraw.Draw(image_menus)
		return True
	
	except :
		print("TFT9341_set_menus_backscreen : exception ! ")
		return False
'''
#--------------------------------------------------------------------------------------------------
'''
def TFT9341_set_IAQ_backscreen():
	global image_IAQ, draw_IAQ
	
	try :
		image_IAQ = Image.open("/home/scripts_python_tests_capteurs/TFT9341/TFT_IAQ.jpg")
		display.image(image_IAQ)
		draw_IAQ = ImageDraw.Draw(image_IAQ)
		return True
	
	except :
		print("TFT9341_set_IAQ_backscreen : exception ! ")
		return False
'''
#--------------------------------------------------------------------------------------------------
'''
def TFT9341_set_backscreen(jpg_file):
	global image_menus, draw_menus
	
	try :
		image_menus = Image.open(jpg_file)
		display.image(image_menus)
		draw_menus = ImageDraw.Draw(image_menus)
		return True
	
	except :
		print("TFT9341_set_menus_backscreen : exception ! ")
		return False
'''

#--------------------------------------------------------------------------------------------------
def TFT9341_set_alerts_backscreen():
	global image_alert_0, image_alert_1, image_alert_2, image_alert_3, image_alert_4, image_alert_5  
	global  draw_alert_0,  draw_alert_1,  draw_alert_2,  draw_alert_3,  draw_alert_4,  draw_alert_5 
	
	try :
		image_alert_0 = Image.open("/home/scripts_python/" + WARNING_EMOTICONS[0])
		draw_alert_0  = ImageDraw.Draw(image_alert_0)
		display.image(image_alert_0)

		image_alert_1 = Image.open("/home/scripts_python/" + WARNING_EMOTICONS[1])
		draw_alert_1  = ImageDraw.Draw(image_alert_1)
		display.image(image_alert_1)

		image_alert_2 = Image.open("/home/scripts_python/" + WARNING_EMOTICONS[2])
		draw_alert_2  = ImageDraw.Draw(image_alert_2)
		display.image(image_alert_2)

		image_alert_3 = Image.open("/home/scripts_python/" + WARNING_EMOTICONS[3])
		draw_alert_3  = ImageDraw.Draw(image_alert_3)
		display.image(image_alert_3)

		image_alert_4 = Image.open("/home/scripts_python/" + WARNING_EMOTICONS[4])
		draw_alert_4  = ImageDraw.Draw(image_alert_4)
		display.image(image_alert_4)

		image_alert_5 = Image.open("/home/scripts_python/" + WARNING_EMOTICONS[5])
		draw_alert_5  = ImageDraw.Draw(image_alert_5)
		display.image(image_alert_5)
		
		return True

	except :
		print("TFT9341_set_alerts_backscreen : exception ! ")
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_send_alert(index, text_string):
	try :
		bandeau_Y1 = 215
		bandeau_Y2 = 239
		alerte_x   =  44
		alerte_y   = 217
		
		if ( index == 0 ) :
			draw_alert_0.rectangle( (0, bandeau_Y1, 319, bandeau_Y2), fill="#000000" )
			draw_alert_0.text( (alerte_x,alerte_y), text_string, font=font_alerts, fill=couleurs_alertes[index] )
			display.image(image_alert_0)
		if ( index == 1 ) :
			draw_alert_1.rectangle( (0, bandeau_Y1, 319, bandeau_Y2), fill="#000000" )
			draw_alert_1.text( (alerte_x,alerte_y), text_string, font=font_alerts, fill=couleurs_alertes[index] )
			display.image(image_alert_1)
		if ( index == 2 ) :
			draw_alert_2.rectangle( (0, bandeau_Y1, 319, bandeau_Y2), fill="#000000" )
			draw_alert_2.text( (alerte_x,alerte_y), text_string, font=font_alerts, fill=couleurs_alertes[index] )
			display.image(image_alert_2)
		if ( index == 3 ) :
			draw_alert_3.rectangle( (0, bandeau_Y1, 319, bandeau_Y2), fill="#000000" )
			draw_alert_3.text( (alerte_x,alerte_y), text_string, font=font_alerts, fill=couleurs_alertes[index] )
			display.image(image_alert_3)
		if ( index == 4 ) :
			draw_alert_4.rectangle( (0, bandeau_Y1, 319, bandeau_Y2), fill="#000000" )
			draw_alert_4.text( (alerte_x,alerte_y), text_string, font=font_alerts, fill=couleurs_alertes[index] )
			display.image(image_alert_4)
		if ( index == 5 ) :
			draw_alert_5.rectangle( (0, bandeau_Y1, 319, bandeau_Y2), fill="#000000" )
			draw_alert_5.text( (alerte_x,alerte_y), text_string, font=font_alerts, fill=couleurs_alertes[index] )
			display.image(image_alert_5)
		
		return True
	
	except :
		print("TFT9341_send_alert : exception ! ")
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_display_graphes(index):
	try :
		
		if ( index >= 0 ) and ( index < NB_GRAPHES_TFT ) :
			graph_filename = CHEMIN_GRAPHES_TFT + GRAPHES_TFT[index]
			print("graphique : " + graph_filename )
			image_graphe = Image.open(graph_filename)
			draw_graphe  = ImageDraw.Draw(image_graphe)
			display.image(image_graphe)
			return True
			
		else :
			return False
		
	except :
		print("TFT9341_display_graphes : exception ! ")
		return False

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

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

		draw_menus.rectangle( (48, 30, 319-48, 110), fill="#000000" )
		draw_menus.text( (54,50), msg0, font=font_menus, fill="#FFD766" )
		draw_menus.text( (54,80), msg1, font=font_menus, fill="#FFD766" )
		display.image(image_menus)
		
		return True
	
	except :
		print("TFT9341_update_TFT_12 : exception ! ")
		return False
			
#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_12_colored(msg0, msg1, index_1, index_2):
	try :

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

		draw_menus.rectangle( (48, 30, 319-48, 110), fill="#000000" )
		draw_menus.text( (61,50), msg0, font=font_menus, fill="#FFD766" )
		draw_menus.text( (61,80), msg1, font=font_menus, fill="#FFD766" )
		display.image(image_menus)
		
		return True
	
	except :
		print("TFT9341_update_TFT_12 : exception ! ")
		return False
			
#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_lite(date, heure, co2, temp, hr, couleur_CO2, couleur_temp, couleur_hr):
	try :

		draw_IAQ.rectangle( (16, 12, 25+280, 12+32), fill="#000000" )
		draw_IAQ.text( ( 30, 13), date,  font=font_big_1, fill="#00FFFF" )
		draw_IAQ.text( (200, 13), heure, font=font_big_1, fill="#00FFFF" )

		if ( len(co2) < 4 ) :
			co2 = " " + co2
		nb_of_ones = co2.count('1')
		draw_IAQ.rectangle( (25, 60, 25+200, 60+85), fill="#000000" )
		draw_IAQ.text( ( 30 + nb_of_ones*20, 56 ), co2, 	font=font_big_2, fill=couleur_CO2 )
		
		if ( len(temp) < 4 ) :
			temp = " " + temp
		nb_of_ones = temp.count('1')
		draw_IAQ.rectangle( (16, 158, 16+90, 158+45), fill="#000000" )
		draw_IAQ.text( ( 27 + nb_of_ones*8,158), temp, 	font=font_big_3, fill=couleur_temp )
		
		nb_of_ones = hr.count('1')
		draw_IAQ.rectangle( (170, 158, 170+92, 158+45), fill="#000000" )
		draw_IAQ.text( (200 + nb_of_ones*10,158), hr, 	font=font_big_3, fill=couleur_hr )
		
		display.image(image_IAQ)
		
		return True
	
	except :
		print("TFT9341_update_TFT_lite : exception ! ")
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_update_date_heure_IAQ(date, heure):
	try :

		#------------------------------------------------------------------------------------------
		splitted_date = date.split(" ")
		
		draw_IAQ.rectangle( (15, 12, 320-15, 12+34), fill="#000000" )
		draw_IAQ.text( ( 17, 13), splitted_date[0],  font=font_big_1, fill="#FF0000" )
		draw_IAQ.text( ( 80, 13), splitted_date[1],  font=font_big_1, fill="#00FF00" )
		draw_IAQ.text( (237, 13), heure,             font=font_big_1, fill="#0000FF" )

		#------------------------------------------------------------------------------------------
		display.image(image_IAQ)
		
		return True
	
	except :
		print("TFT9341_update_date_heure_IAQ : exception ! ")
		return False
			
			
#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_DBA(date, heure, DBA_moy, DBA_max, Lux, couleur_DBA_moy, couleur_DBA_max, couleur_Lux):
	try :

		#------------------------------------------------------------------------------------------
		X1  = 14
		Y1  = 62
		L1  = 94
		H1  = 72

		XX1 = -14
		SP1 = 20
		YY1 = 3
		
		X2 = 135
		XX2 = -15
		L2  = 96
		
		#------------------------------------------------------------------------------------------
		splitted_date = date.split(" ")
		print(splitted_date)
		color_date_heure = "#288868"
		
		draw_DBA.rectangle( (15, 12, 320-15, 12+34), fill="#000000" )
		draw_DBA.text( ( 17, 13), splitted_date[0],  font=font_big_1, fill=color_date_heure )
		draw_DBA.text( ( 76, 13), splitted_date[1],  font=font_big_1, fill=color_date_heure )
		draw_DBA.text( (237, 13), heure,             font=font_big_1, fill=color_date_heure )


		#------------------------------------------------------------------------------------------
		if ( len(DBA_moy) < 3 ) :
			DBA_moy = " " + DBA_moy
			XX1 = -20
		nb_of_ones = DBA_moy.count('1')
		draw_DBA.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#000000" )
		draw_DBA.text( ( X1 + XX1 + nb_of_ones*SP1, Y1 - YY1 ), DBA_moy, font=font_big_4, fill=couleur_DBA_moy )
		
		if ( len(DBA_max) < 3 ) :
			DBA_max = " " + DBA_max
			XX2 = -25
		nb_of_ones = DBA_max.count('1')
		draw_DBA.rectangle( (X2, Y1, X2+L2, Y1+H1), fill="#000000" )
		draw_DBA.text( ( X2 + XX2 + nb_of_ones*SP1, Y1 - YY1 ), DBA_max, font=font_big_4, fill=couleur_DBA_max )
		
		#------------------------------------------------------------------------------------------
		X3  = 18
		Y3  = 150
		L3  = 194
		H3  = 76

		XX3 = 10
		SP3 = 20
		YY3 = 3

		while ( len(Lux) < 5 ) :
			Lux = " " + Lux
			
		nb_of_ones = Lux.count('1')
		draw_DBA.rectangle( (X3, Y3, X3+L3, Y3+H3), fill="#000000" )
		draw_DBA.text( ( X3 + XX3 + nb_of_ones*SP3, Y3 - YY3 ), Lux, font=font_big_4, fill=couleur_Lux )
		
		display.image(image_DBA)
		
		return True
	
	except Exception as except_str :
		print("TFT9341_update_TFT_DBA : exception  -> " + str(except_str) )
		return False
			
#--------------------------------------------------------------------------------------------------
def TFT9341_display_TFT_mesures(mesures, couleurs, enabled_strings):
	try :
		
		x_offset     = 4
		y_offset     = 2
		nb_lignes    = 0
		for i in range (0, 10):
			if ( enabled_strings[i] ) :
				nb_lignes += 1
		y_interligne = floor( 240 / nb_lignes )
		
		draw = ImageDraw.Draw(image)
		draw.rectangle((0, 0, width, height), outline=0, fill=0)
		
		y  = y_offset
		for i in range (0, 10):
			if ( enabled_strings[i] ) :
				draw.text((x_offset, y), mesures[i], font=font, fill=couleurs[i])
				y += y_interligne

		display.image(image)

		return True
	
	except :
		print("TFT9341_display_TFT_mesures : exception ! ")
		return False
			
#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_NETWORK(mac, ip, gw, dns, ssid):
	try :

		#------------------------------------------------------------------------------------------
		interligne = 32
		
		X1  = 82
		Y1  = 86-21
		L1  = 222
		H1  = 22

		X2  = 60
		Y2  = Y1 + interligne
		L2  = 245
		H2  = 22

		X3  = 85
		Y3  = Y2 + interligne
		L3  = 220
		H3  = 22

		X4  = 88
		Y4  = Y3 + interligne
		L4  = 218
		H4  = 22

		X5  = 75
		Y5  = Y4 + interligne
		L5  = 230
		H5  = 22
		
		#------------------------------------------------------------------------------------------
		color_network = "#00ff00"
		
		draw_NETWORK.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#000000" )
		draw_NETWORK.text( ( X1+1, Y1+1), mac,  font=font_network, fill=color_network )
		
		draw_NETWORK.rectangle( (X2, Y2, X2+L2, Y2+H2), fill="#000000" )
		draw_NETWORK.text( ( X2+1, Y2+1), ip,  font=font_network, fill=color_network )
		
		draw_NETWORK.rectangle( (X3, Y3, X3+L3, Y3+H3), fill="#000000" )
		draw_NETWORK.text( ( X3+1, Y3+1), gw,  font=font_network, fill=color_network )
		
		draw_NETWORK.rectangle( (X4, Y4, X4+L4, Y4+H4), fill="#000000" )
		draw_NETWORK.text( ( X4+1, Y4+1), dns,  font=font_network, fill=color_network )
		
		draw_NETWORK.rectangle( (X5, Y5, X5+L5, Y5+H5), fill="#000000" )
		draw_NETWORK.text( ( X5+1, Y5+1), ssid,  font=font_network, fill=color_network )
		
		display.image(image_NETWORK)

		return True
	
	except Exception as except_str :
		print("TFT9341_update_TFT_NETWORK : exception  -> " + str(except_str) )
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_MENUS(lignes, index_cible):
	try :

		#------------------------------------------------------------------------------------------
		interligne = 30
		
		X1  = 20
		L1  = 320 - 40
		Y1  = 82-21
		H1  = 26

		Y2  = Y1 + interligne
		Y3  = Y2 + interligne
		Y4  = Y3 + interligne
		
		#------------------------------------------------------------------------------------------
		color_TFT_MENUS = [ "#BBBBBB", "#BBBBBB", "#BBBBBB", "#BBBBBB", "#BBBBBB", "#BBBBBB" ]
		
		if   ( index_cible == 0 ) :
			draw_WEDI_0.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
			draw_WEDI_0.text( ( X1+1, Y1+1), lignes[0],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_0.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
			draw_WEDI_0.text( ( X1+1, Y2+1), lignes[1],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_0.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
			draw_WEDI_0.text( ( X1+1, Y3+1), lignes[2],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_0.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
			draw_WEDI_0.text( ( X1+1, Y4+1), lignes[3],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			display.image(image_WEDI_0)
		
		elif ( index_cible == 1 ) :
			draw_WEDI_1.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
			draw_WEDI_1.text( ( X1+1, Y1+1), lignes[0],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_1.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
			draw_WEDI_1.text( ( X1+1, Y2+1), lignes[1],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_1.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
			draw_WEDI_1.text( ( X1+1, Y3+1), lignes[2],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_1.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
			draw_WEDI_1.text( ( X1+1, Y4+1), lignes[3],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			display.image(image_WEDI_1)
		
		elif ( index_cible == 2 ) :
			draw_WEDI_2.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
			draw_WEDI_2.text( ( X1+1, Y1+1), lignes[0],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_2.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
			draw_WEDI_2.text( ( X1+1, Y2+1), lignes[1],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_2.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
			draw_WEDI_2.text( ( X1+1, Y3+1), lignes[2],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_2.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
			draw_WEDI_2.text( ( X1+1, Y4+1), lignes[3],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			display.image(image_WEDI_2)
		
		elif ( index_cible == 3 ) :
			draw_WEDI_3.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
			draw_WEDI_3.text( ( X1+1, Y1+1), lignes[0],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_3.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
			draw_WEDI_3.text( ( X1+1, Y2+1), lignes[1],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_3.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
			draw_WEDI_3.text( ( X1+1, Y3+1), lignes[2],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_3.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
			draw_WEDI_3.text( ( X1+1, Y4+1), lignes[3],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			display.image(image_WEDI_3)
		
		elif ( index_cible == 4 ) :
			draw_WEDI_4.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
			draw_WEDI_4.text( ( X1+1, Y1+1), lignes[0],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_4.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
			draw_WEDI_4.text( ( X1+1, Y2+1), lignes[1],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_4.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
			draw_WEDI_4.text( ( X1+1, Y3+1), lignes[2],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_4.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
			draw_WEDI_4.text( ( X1+1, Y4+1), lignes[3],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			display.image(image_WEDI_4)
		
		elif ( index_cible == 5 ) :
			draw_WEDI_5.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
			draw_WEDI_5.text( ( X1+1, Y1+1), lignes[0],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_5.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
			draw_WEDI_5.text( ( X1+1, Y2+1), lignes[1],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_5.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
			draw_WEDI_5.text( ( X1+1, Y3+1), lignes[2],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			draw_WEDI_5.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
			draw_WEDI_5.text( ( X1+1, Y4+1), lignes[3],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
			display.image(image_WEDI_5)
		

		return True
	
	except Exception as except_str :
		print("TFT9341_update_TFT_MENUS : exception  -> " + str(except_str) )
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_INFO(lignes):
	try :

		#------------------------------------------------------------------------------------------
		interligne = 30
		
		K0  = 6.0
		Y0  = 14
		H0  = 26
		
		X1  = 20
		L1  = 320 - 40
		Y1  = 90-21
		H1  = 26

		Y2  = Y1 + interligne
		Y3  = Y2 + interligne
		Y4  = Y3 + interligne
		Y5  = Y4 + interligne
		
		#------------------------------------------------------------------------------------------
		color_TFT_INFO = "#BBBBBB"
		
		X00 = int( 160 - K0 * len(lignes[0]) )
		draw_INFO.rectangle( (52, 12, 320-52, 12+34), fill="#800000" )
		draw_INFO.text( ( X00, Y0+1), lignes[0],  font=font_titres_tft, fill=color_TFT_INFO )
		
		draw_INFO.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
		draw_INFO.text( ( X1+1, Y1+1), lignes[1],  font=font_menus_tft, fill=color_TFT_INFO )
		draw_INFO.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
		draw_INFO.text( ( X1+1, Y2+1), lignes[2],  font=font_menus_tft, fill=color_TFT_INFO )
		draw_INFO.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
		draw_INFO.text( ( X1+1, Y3+1), lignes[3],  font=font_menus_tft, fill=color_TFT_INFO )
		draw_INFO.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
		draw_INFO.text( ( X1+1, Y4+1), lignes[4],  font=font_menus_tft, fill=color_TFT_INFO )
		draw_INFO.rectangle( (X1, Y5, X1+L1, Y5+H1), fill="#800000" )
		draw_INFO.text( ( X1+1, Y5+1), lignes[5],  font=font_menus_tft, fill=color_TFT_INFO )
		display.image(image_INFO)
		
		return True
	
	except Exception as except_str :
		print("TFT9341_update_TFT_MENUS : exception  -> " + str(except_str) )
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_CONFIRMATION(lignes, index_cible):
	try :

		#------------------------------------------------------------------------------------------
		interligne = 30
		
		X1  = 20
		L1  = 320 - 40
		Y1  = 82-21
		H1  = 26

		Y2  = Y1 + interligne
		Y3  = Y2 + interligne
		Y4  = Y3 + interligne
		
		#------------------------------------------------------------------------------------------
		color_TFT_MENUS = [ "#BBBBBB", "#BBBBBB", "#BBBBBB", "#BBBBBB", "#BBBBBB", "#BBBBBB" ]
		
		draw_WEDI_7.rectangle( (X1, Y1, X1+L1, Y1+H1), fill="#800000" )
		draw_WEDI_7.text( ( X1+1, Y1+1), lignes[0],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
		draw_WEDI_7.rectangle( (X1, Y2, X1+L1, Y2+H1), fill="#800000" )
		draw_WEDI_7.text( ( X1+1, Y2+1), lignes[1],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
		draw_WEDI_7.rectangle( (X1, Y3, X1+L1, Y3+H1), fill="#800000" )
		draw_WEDI_7.text( ( X1+1, Y3+1), lignes[2],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
		draw_WEDI_7.rectangle( (X1, Y4, X1+L1, Y4+H1), fill="#800000" )
		draw_WEDI_7.text( ( X1+1, Y4+1), lignes[3],  font=font_menus_tft, fill=color_TFT_MENUS[index_cible] )
		display.image(image_WEDI_7)

		return True
	
	except Exception as except_str :
		print("TFT9341_update_TFT_CONFIRMATION : exception  -> " + str(except_str) )
		return False

#--------------------------------------------------------------------------------------------------
def TFT9341_update_TFT_SABLIER():
	try :

		#------------------------------------------------------------------------------------------
		display.image(image_WEDI_6)

		return True
	
	except Exception as except_str :
		print("TFT9341_update_TFT_SABLIER : exception  -> " + str(except_str) )
		return False

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