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

# python3 /home/scripts_python_TFT_display/test_03.py

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

# Configuration for CS and DC pins (these are PiTFT defaults):
cs_pin = digitalio.DigitalInOut(board.D8)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D7)

# Config for display baudrate (default max is 24mhz):
BAUDRATE = 24000000

# Setup SPI bus using hardware SPI:
spi = board.SPI()

disp = ili9341.ILI9341(
    spi,
    rotation=90,  # 2.2", 2.4", 2.8", 3.2" ILI9341
    cs=cs_pin,
    dc=dc_pin,
    rst=reset_pin,
    baudrate=BAUDRATE,
)

backlight = digitalio.DigitalInOut(board.D18)
backlight.switch_to_output()
backlight.value = True


# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
if disp.rotation % 180 == 90:
    height = disp.width  # we swap height/width to rotate it to landscape!
    width = disp.height
else:
    width = disp.width  # we swap height/width to rotate it to landscape!
    height = disp.height
image = Image.new("RGB", (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image)

image = Image.open("/home/scripts_python_TFT_display/blinka_320_240.jpg")

'''
# Scale the image to the smaller screen dimension
image_ratio = image.width / image.height
screen_ratio = width / height
if screen_ratio < image_ratio:
    scaled_width = image.width * height // image.height
    scaled_height = height
else:
    scaled_width = width
    scaled_height = image.height * width // image.width
image = image.resize((scaled_width, scaled_height), Image.BICUBIC)

# Crop and center the image
x = scaled_width // 2 - width // 2
y = scaled_height // 2 - height // 2
image = image.crop((x, y, x + width, y + height))
'''

# Display image.
disp.image(image)


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

# python3 /home/scripts_python_TFT_display/test_05.py

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""
This will show some Linux Statistics on the attached display. Be sure to adjust
to the display you have connected. Be sure to check the learn guides for more
usage information.

This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
"""

import time
import subprocess
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.ili9341 as ili9341

# Configuration for CS and DC pins (these are PiTFT defaults):
cs_pin = digitalio.DigitalInOut(board.D8)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = digitalio.DigitalInOut(board.D7)

# Config for display baudrate (default max is 24mhz):
BAUDRATE = 24000000

# Setup SPI bus using hardware SPI:
spi = board.SPI()

# pylint: disable=line-too-long
# Create the display:
# disp = st7789.ST7789(spi, rotation=90,							# 2.0" ST7789
# disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180,  # 1.3", 1.54" ST7789
# disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53, y_offset=40, # 1.14" ST7789
# disp = hx8357.HX8357(spi, rotation=180,						   # 3.5" HX8357
# disp = st7735.ST7735R(spi, rotation=90,						   # 1.8" ST7735R
# disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3,   # 1.44" ST7735R
# disp = st7735.ST7735R(spi, rotation=90, bgr=True,				 # 0.96" MiniTFT ST7735R
# disp = ssd1351.SSD1351(spi, rotation=180,						 # 1.5" SSD1351
# disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351
# disp = ssd1331.SSD1331(spi, rotation=180,						 # 0.96" SSD1331
disp = ili9341.ILI9341(
	spi,
	rotation=90,  # 2.2", 2.4", 2.8", 3.2" ILI9341
	cs=cs_pin,
	dc=dc_pin,
	rst=reset_pin,
	baudrate=BAUDRATE,
)
# pylint: enable=line-too-long

# Create blank image for drawing.
# Make sure to create image with mode 'RGB' for full color.
if disp.rotation % 180 == 90:
	height = disp.width  # we swap height/width to rotate it to landscape!
	width = disp.height
else:
	width = disp.width  # we swap height/width to rotate it to landscape!
	height = disp.height

image = Image.new("RGB", (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image)

# First define some constants to allow easy positioning of text.
padding = -2
x = 0

# Load a TTF font.  Make sure the .ttf font file is in the
# same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 16)

compteur = 1234

while True:
	# Draw a black filled box to clear the image.
	draw.rectangle((0, 0, width, height), outline=0, fill=0)

	# Shell scripts for system monitoring from here:
	# https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
	cmd = "hostname -I | cut -d' ' -f1"
	IP = "IP: " + subprocess.check_output(cmd, shell=True).decode("utf-8")
	cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
	CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
	cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB  %.2f%%\", $3,$2,$3*100/$2 }'"
	MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
	cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB  %s", $3,$2,$5}\''
	Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
	cmd = "cat /sys/class/thermal/thermal_zone0/temp |  awk '{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}'"  # pylint: disable=line-too-long
	Temp = subprocess.check_output(cmd, shell=True).decode("utf-8")
	
	compteur += 17
	if ( compteur >= 1500 ) :
		compteur = 1225
	compteur_string = str(compteur)

	# Write four lines of text.
	y = padding
	draw.text((x, y), IP, font=font, fill="#FFFFFF")
	y += font.getsize(IP)[1]
	draw.text((x, y), CPU, font=font, fill="#FFFF00")
	y += font.getsize(CPU)[1]
	draw.text((x, y), MemUsage, font=font, fill="#00FF00")
	y += font.getsize(MemUsage)[1]
	draw.text((x, y), Disk, font=font, fill="#0000FF")
	y += font.getsize(Disk)[1]
	draw.text((x, y), Temp, font=font, fill="#FF00FF")
	y += font.getsize(Temp)[1]
	draw.text((x, y), compteur_string, font=font, fill="#FF8000")

	# Display image.
	disp.image(image)
	time.sleep(0.5)

