From e1d0e39b7ef011f7a285a7546f268290f5583130 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Tue, 8 Oct 2019 09:02:31 -0700 Subject: [PATCH 1/3] Added Pillow ILI9341 Examples --- examples/rgb_display_pillow_ili9341_demo.py | 52 ++++++++++++ examples/rgb_display_pillow_ili9341_image.py | 55 +++++++++++++ examples/rgb_display_pillow_ili9341_stats.py | 83 ++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 examples/rgb_display_pillow_ili9341_demo.py create mode 100644 examples/rgb_display_pillow_ili9341_image.py create mode 100644 examples/rgb_display_pillow_ili9341_stats.py diff --git a/examples/rgb_display_pillow_ili9341_demo.py b/examples/rgb_display_pillow_ili9341_demo.py new file mode 100644 index 0000000..807fb07 --- /dev/null +++ b/examples/rgb_display_pillow_ili9341_demo.py @@ -0,0 +1,52 @@ +import digitalio +import board +from PIL import Image, ImageDraw, ImageFont +import adafruit_rgb_display.ili9341 as ili9341 + +# First define some constants to allow easy resizing of shapes. +BORDER = 20 +FONTSIZE = 24 + +# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4): +cs_pin = digitalio.DigitalInOut(board.CE0) +dc_pin = digitalio.DigitalInOut(board.D25) +reset_pin = digitalio.DigitalInOut(board.D24) + +# Config for display baudrate (default max is 24mhz): +BAUDRATE = 24000000 + +# Setup SPI bus using hardware SPI: +spi = board.SPI() + +# Create the ILI9341 display: +disp = ili9341.ILI9341(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE) + +# Create blank image for drawing. +# Make sure to create image with mode 'RGB' for full color. +height = disp.width # we swap height/width to rotate it to landscape! +width = disp.height +image = Image.new('RGB', (width, height)) +rotation = 90 + +# Get drawing object to draw on image. +draw = ImageDraw.Draw(image) + +# Draw a green filled box as the background +draw.rectangle((0, 0, width, height), fill=(0, 255, 0)) +disp.image(image, rotation) + +# Draw a smaller inner purple rectangle +draw.rectangle((BORDER, BORDER, width - BORDER - 1, height - BORDER - 1), + fill=(170,0, 136)) + +# Load a TTF Font +font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', FONTSIZE) + +# Draw Some Text +text = "Hello World!" +(font_width, font_height) = font.getsize(text) +draw.text((width//2 - font_width//2, height//2 - font_height//2), + text, font=font, fill=(255, 255, 0)) + +# Display image. +disp.image(image, rotation) diff --git a/examples/rgb_display_pillow_ili9341_image.py b/examples/rgb_display_pillow_ili9341_image.py new file mode 100644 index 0000000..86aea15 --- /dev/null +++ b/examples/rgb_display_pillow_ili9341_image.py @@ -0,0 +1,55 @@ +import time +import subprocess +import digitalio +import board +from PIL import Image, ImageDraw +import adafruit_rgb_display.ili9341 as ili9341 + +# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4): +cs_pin = digitalio.DigitalInOut(board.CE0) +dc_pin = digitalio.DigitalInOut(board.D25) +reset_pin = digitalio.DigitalInOut(board.D24) + +# Config for display baudrate (default max is 24mhz): +BAUDRATE = 64000000 + +# Setup SPI bus using hardware SPI: +spi = board.SPI() + +# Create the ILI9341 display: +disp = ili9341.ILI9341(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE) + +# Create blank image for drawing. +# Make sure to create image with mode 'RGB' for full color. +height = disp.width # we swap height/width to rotate it to landscape! +width = disp.height +image = Image.new('RGB', (width, height)) +rotation = 90 + +# 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, rotation) + +image = Image.open("blinka.png") + +# 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, rotation) diff --git a/examples/rgb_display_pillow_ili9341_stats.py b/examples/rgb_display_pillow_ili9341_stats.py new file mode 100644 index 0000000..a9e2fb9 --- /dev/null +++ b/examples/rgb_display_pillow_ili9341_stats.py @@ -0,0 +1,83 @@ +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 FeatherWing defaults on M0/M4): +cs_pin = digitalio.DigitalInOut(board.CE0) +dc_pin = digitalio.DigitalInOut(board.D25) +reset_pin = digitalio.DigitalInOut(board.D24) + +# Config for display baudrate (default max is 24mhz): +BAUDRATE = 24000000 + +# Setup SPI bus using hardware SPI: +spi = board.SPI() + +# Create the ILI9341 display: +disp = ili9341.ILI9341(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE) + +# Create blank image for drawing. +# Make sure to create image with mode 'RGB' for full color. +height = disp.width # we swap height/width to rotate it to landscape! +width = disp.height +image = Image.new('RGB', (width, height)) +rotation = 90 + +# 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, rotation) + +# Draw some shapes. +# First define some constants to allow easy resizing of shapes. +padding = -2 +top = padding +bottom = height-padding + +# Move left to right keeping track of the current x position for drawing shapes. +x = 0 + + +# Alternatively 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', 24) + +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") + + # Write four lines of text. + y = top + 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") + + # Display image. + disp.image(image, rotation) + time.sleep(.1) From c95332cb5c32eca814fb8d893c76edc433446d36 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Tue, 8 Oct 2019 09:15:31 -0700 Subject: [PATCH 2/3] Removing unused imports --- examples/rgb_display_pillow_ili9341_image.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/rgb_display_pillow_ili9341_image.py b/examples/rgb_display_pillow_ili9341_image.py index 86aea15..130729f 100644 --- a/examples/rgb_display_pillow_ili9341_image.py +++ b/examples/rgb_display_pillow_ili9341_image.py @@ -1,5 +1,3 @@ -import time -import subprocess import digitalio import board from PIL import Image, ImageDraw From 1e4d5f4c935aefcb5a11bd7d1b98746f5370c32b Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Tue, 8 Oct 2019 09:19:01 -0700 Subject: [PATCH 3/3] Fixed whitespace issues --- examples/rgb_display_pillow_ili9341_demo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/rgb_display_pillow_ili9341_demo.py b/examples/rgb_display_pillow_ili9341_demo.py index 807fb07..3aebf85 100644 --- a/examples/rgb_display_pillow_ili9341_demo.py +++ b/examples/rgb_display_pillow_ili9341_demo.py @@ -32,12 +32,12 @@ draw = ImageDraw.Draw(image) # Draw a green filled box as the background -draw.rectangle((0, 0, width, height), fill=(0, 255, 0)) +draw.rectangle((0, 0, width, height), fill=(0, 255, 0)) disp.image(image, rotation) # Draw a smaller inner purple rectangle draw.rectangle((BORDER, BORDER, width - BORDER - 1, height - BORDER - 1), - fill=(170,0, 136)) + fill=(170, 0, 136)) # Load a TTF Font font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', FONTSIZE)