Skip to content

Commit 99bcb44

Browse files
authored
Merge pull request #32 from makermelissa/master
Added Pillow ILI9341 Examples
2 parents 0f119ab + 1e4d5f4 commit 99bcb44

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import digitalio
2+
import board
3+
from PIL import Image, ImageDraw, ImageFont
4+
import adafruit_rgb_display.ili9341 as ili9341
5+
6+
# First define some constants to allow easy resizing of shapes.
7+
BORDER = 20
8+
FONTSIZE = 24
9+
10+
# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
11+
cs_pin = digitalio.DigitalInOut(board.CE0)
12+
dc_pin = digitalio.DigitalInOut(board.D25)
13+
reset_pin = digitalio.DigitalInOut(board.D24)
14+
15+
# Config for display baudrate (default max is 24mhz):
16+
BAUDRATE = 24000000
17+
18+
# Setup SPI bus using hardware SPI:
19+
spi = board.SPI()
20+
21+
# Create the ILI9341 display:
22+
disp = ili9341.ILI9341(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)
23+
24+
# Create blank image for drawing.
25+
# Make sure to create image with mode 'RGB' for full color.
26+
height = disp.width # we swap height/width to rotate it to landscape!
27+
width = disp.height
28+
image = Image.new('RGB', (width, height))
29+
rotation = 90
30+
31+
# Get drawing object to draw on image.
32+
draw = ImageDraw.Draw(image)
33+
34+
# Draw a green filled box as the background
35+
draw.rectangle((0, 0, width, height), fill=(0, 255, 0))
36+
disp.image(image, rotation)
37+
38+
# Draw a smaller inner purple rectangle
39+
draw.rectangle((BORDER, BORDER, width - BORDER - 1, height - BORDER - 1),
40+
fill=(170, 0, 136))
41+
42+
# Load a TTF Font
43+
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', FONTSIZE)
44+
45+
# Draw Some Text
46+
text = "Hello World!"
47+
(font_width, font_height) = font.getsize(text)
48+
draw.text((width//2 - font_width//2, height//2 - font_height//2),
49+
text, font=font, fill=(255, 255, 0))
50+
51+
# Display image.
52+
disp.image(image, rotation)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import digitalio
2+
import board
3+
from PIL import Image, ImageDraw
4+
import adafruit_rgb_display.ili9341 as ili9341
5+
6+
# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
7+
cs_pin = digitalio.DigitalInOut(board.CE0)
8+
dc_pin = digitalio.DigitalInOut(board.D25)
9+
reset_pin = digitalio.DigitalInOut(board.D24)
10+
11+
# Config for display baudrate (default max is 24mhz):
12+
BAUDRATE = 64000000
13+
14+
# Setup SPI bus using hardware SPI:
15+
spi = board.SPI()
16+
17+
# Create the ILI9341 display:
18+
disp = ili9341.ILI9341(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)
19+
20+
# Create blank image for drawing.
21+
# Make sure to create image with mode 'RGB' for full color.
22+
height = disp.width # we swap height/width to rotate it to landscape!
23+
width = disp.height
24+
image = Image.new('RGB', (width, height))
25+
rotation = 90
26+
27+
# Get drawing object to draw on image.
28+
draw = ImageDraw.Draw(image)
29+
30+
# Draw a black filled box to clear the image.
31+
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
32+
disp.image(image, rotation)
33+
34+
image = Image.open("blinka.png")
35+
36+
# Scale the image to the smaller screen dimension
37+
image_ratio = image.width / image.height
38+
screen_ratio = width / height
39+
if screen_ratio < image_ratio:
40+
scaled_width = image.width * height // image.height
41+
scaled_height = height
42+
else:
43+
scaled_width = width
44+
scaled_height = image.height * width // image.width
45+
image = image.resize((scaled_width, scaled_height), Image.BICUBIC)
46+
47+
# Crop and center the image
48+
x = scaled_width // 2 - width // 2
49+
y = scaled_height // 2 - height // 2
50+
image = image.crop((x, y, x + width, y + height))
51+
52+
# Display image.
53+
disp.image(image, rotation)
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import time
2+
import subprocess
3+
import digitalio
4+
import board
5+
from PIL import Image, ImageDraw, ImageFont
6+
import adafruit_rgb_display.ili9341 as ili9341
7+
8+
9+
# Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
10+
cs_pin = digitalio.DigitalInOut(board.CE0)
11+
dc_pin = digitalio.DigitalInOut(board.D25)
12+
reset_pin = digitalio.DigitalInOut(board.D24)
13+
14+
# Config for display baudrate (default max is 24mhz):
15+
BAUDRATE = 24000000
16+
17+
# Setup SPI bus using hardware SPI:
18+
spi = board.SPI()
19+
20+
# Create the ILI9341 display:
21+
disp = ili9341.ILI9341(spi, cs=cs_pin, dc=dc_pin, rst=reset_pin, baudrate=BAUDRATE)
22+
23+
# Create blank image for drawing.
24+
# Make sure to create image with mode 'RGB' for full color.
25+
height = disp.width # we swap height/width to rotate it to landscape!
26+
width = disp.height
27+
image = Image.new('RGB', (width, height))
28+
rotation = 90
29+
30+
# Get drawing object to draw on image.
31+
draw = ImageDraw.Draw(image)
32+
33+
# Draw a black filled box to clear the image.
34+
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
35+
disp.image(image, rotation)
36+
37+
# Draw some shapes.
38+
# First define some constants to allow easy resizing of shapes.
39+
padding = -2
40+
top = padding
41+
bottom = height-padding
42+
43+
# Move left to right keeping track of the current x position for drawing shapes.
44+
x = 0
45+
46+
47+
# Alternatively load a TTF font. Make sure the .ttf font file is in the
48+
# same directory as the python script!
49+
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
50+
font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 24)
51+
52+
while True:
53+
# Draw a black filled box to clear the image.
54+
draw.rectangle((0, 0, width, height), outline=0, fill=0)
55+
56+
# Shell scripts for system monitoring from here:
57+
# https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
58+
cmd = "hostname -I | cut -d\' \' -f1"
59+
IP = "IP: "+subprocess.check_output(cmd, shell=True).decode("utf-8")
60+
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
61+
CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
62+
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'"
63+
MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
64+
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%d GB %s\", $3,$2,$5}'"
65+
Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
66+
cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk \'{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}\'" # pylint: disable=line-too-long
67+
Temp = subprocess.check_output(cmd, shell=True).decode("utf-8")
68+
69+
# Write four lines of text.
70+
y = top
71+
draw.text((x, y), IP, font=font, fill="#FFFFFF")
72+
y += font.getsize(IP)[1]
73+
draw.text((x, y), CPU, font=font, fill="#FFFF00")
74+
y += font.getsize(CPU)[1]
75+
draw.text((x, y), MemUsage, font=font, fill="#00FF00")
76+
y += font.getsize(MemUsage)[1]
77+
draw.text((x, y), Disk, font=font, fill="#0000FF")
78+
y += font.getsize(Disk)[1]
79+
draw.text((x, y), Temp, font=font, fill="#FF00FF")
80+
81+
# Display image.
82+
disp.image(image, rotation)
83+
time.sleep(.1)

0 commit comments

Comments
 (0)