|
| 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