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

import board
import terminalio
import displayio
from adafruit_display_text import label
import time

import adafruit_ili9341

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 splash, display, font
	
	try:
		
		displayio.release_displays()

		spi       = board.SPI()
		tft_cs    = board.D8
		tft_dc    = board.D25
		tft_reset = board.D7

		display_bus = displayio.FourWire(
			spi, command=tft_dc, chip_select=tft_cs, reset=tft_reset
		)

		display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240, rotation=180)

		font = terminalio.FONT
		
		return True
		
	except:
	
		return False

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

		print(msg0)
		print(msg1)
		print(" ")

		# Make the display context
		splash = displayio.Group(max_size=10)
		display.show(splash)

		# Draw a smaller inner rectangle
		inner_bitmap = displayio.Bitmap(240, 80, 1)
		inner_palette = displayio.Palette(1)
		inner_palette[0] = 0xffffff
		inner_sprite = displayio.TileGrid(inner_bitmap, pixel_shader=inner_palette, x=40, y=35)
		splash.append(inner_sprite)

		text_group = displayio.Group(max_size=10, scale=2, x=64, y=60)
		text_area = label.Label(font, text=msg0, color=0x000000)
		text_group.append(text_area)
		splash.append(text_group)

		text_group_2 = displayio.Group(max_size=10, scale=2, x=64, y=90)
		text_area_2 = label.Label(font, text=msg1, color=0x000000)
		text_group_2.append(text_area_2)
		splash.append(text_group_2)
		
		return True
	
	except :
		return False
			
#--------------------------------------------------------------------------------------------------
def TFT9341_display_TFT_mesures(mesures, couleurs):
	try :

		nb_mesures = len(mesures)
		
		# Make the display context
		splash_mesures = displayio.Group(max_size=10)
		display.show(splash_mesures)

		# Draw a rectangle
		inner_bitmap_mesures = displayio.Bitmap(320, 240, 1)
		inner_palette_mesures = displayio.Palette(1)
		inner_palette_mesures[0] = 0x000000
		inner_sprite = displayio.TileGrid(inner_bitmap_mesures, pixel_shader=inner_palette_mesures, x=0, y=0)
		splash_mesures.append(inner_sprite)

		for i in range (0, nb_mesures):
			text_group = displayio.Group(max_size=10, scale=2, x=20, y=10+30*i)
			text_area  = label.Label( font, text=mesures[i], color=couleurs[i] )
			text_group.append(text_area)
			splash_mesures.append(text_group)

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