rgb_display_pillow_stats.py
1 """ 2 This will show some Linux Statistics on the attached display. Be sure to adjust 3 to the display you have connected. Be sure to check the learn guides for more 4 usage information. 5 6 This example is for use on (Linux) computers that are using CPython with 7 Adafruit Blinka to support CircuitPython libraries. CircuitPython does 8 not support PIL/pillow (python imaging library)! 9 """ 10 11 import time 12 import subprocess 13 import digitalio 14 import board 15 from PIL import Image, ImageDraw, ImageFont 16 import adafruit_rgb_display.ili9341 as ili9341 17 import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import 18 import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import 19 import adafruit_rgb_display.st7735 as st7735 # pylint: disable=unused-import 20 import adafruit_rgb_display.ssd1351 as ssd1351 # pylint: disable=unused-import 21 import adafruit_rgb_display.ssd1331 as ssd1331 # pylint: disable=unused-import 22 23 # Configuration for CS and DC pins (these are PiTFT defaults): 24 cs_pin = digitalio.DigitalInOut(board.CE0) 25 dc_pin = digitalio.DigitalInOut(board.D25) 26 reset_pin = digitalio.DigitalInOut(board.D24) 27 28 # Config for display baudrate (default max is 24mhz): 29 BAUDRATE = 24000000 30 31 # Setup SPI bus using hardware SPI: 32 spi = board.SPI() 33 34 # pylint: disable=line-too-long 35 # Create the display: 36 # disp = st7789.ST7789(spi, rotation=90, # 2.0" ST7789 37 # disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180, # 1.3", 1.54" ST7789 38 # disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53, y_offset=40, # 1.14" ST7789 39 # disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357 40 # disp = st7735.ST7735R(spi, rotation=90, # 1.8" ST7735R 41 # disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3, # 1.44" ST7735R 42 # disp = st7735.ST7735R(spi, rotation=90, bgr=True, # 0.96" MiniTFT ST7735R 43 # disp = ssd1351.SSD1351(spi, rotation=180, # 1.5" SSD1351 44 # disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351 45 # disp = ssd1331.SSD1331(spi, rotation=180, # 0.96" SSD1331 46 disp = ili9341.ILI9341( 47 spi, 48 rotation=90, # 2.2", 2.4", 2.8", 3.2" ILI9341 49 cs=cs_pin, 50 dc=dc_pin, 51 rst=reset_pin, 52 baudrate=BAUDRATE, 53 ) 54 # pylint: enable=line-too-long 55 56 # Create blank image for drawing. 57 # Make sure to create image with mode 'RGB' for full color. 58 if disp.rotation % 180 == 90: 59 height = disp.width # we swap height/width to rotate it to landscape! 60 width = disp.height 61 else: 62 width = disp.width # we swap height/width to rotate it to landscape! 63 height = disp.height 64 65 image = Image.new("RGB", (width, height)) 66 67 # Get drawing object to draw on image. 68 draw = ImageDraw.Draw(image) 69 70 # Draw a black filled box to clear the image. 71 draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0)) 72 disp.image(image) 73 74 # First define some constants to allow easy positioning of text. 75 padding = -2 76 x = 0 77 78 # Load a TTF font. Make sure the .ttf font file is in the 79 # same directory as the python script! 80 # Some other nice fonts to try: http://www.dafont.com/bitmap.php 81 font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 24) 82 83 while True: 84 # Draw a black filled box to clear the image. 85 draw.rectangle((0, 0, width, height), outline=0, fill=0) 86 87 # Shell scripts for system monitoring from here: 88 # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load 89 cmd = "hostname -I | cut -d' ' -f1" 90 IP = "IP: " + subprocess.check_output(cmd, shell=True).decode("utf-8") 91 cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'" 92 CPU = subprocess.check_output(cmd, shell=True).decode("utf-8") 93 cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'" 94 MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8") 95 cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB %s", $3,$2,$5}\'' 96 Disk = subprocess.check_output(cmd, shell=True).decode("utf-8") 97 cmd = "cat /sys/class/thermal/thermal_zone0/temp | awk '{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}'" # pylint: disable=line-too-long 98 Temp = subprocess.check_output(cmd, shell=True).decode("utf-8") 99 100 # Write four lines of text. 101 y = padding 102 draw.text((x, y), IP, font=font, fill="#FFFFFF") 103 y += font.getsize(IP)[1] 104 draw.text((x, y), CPU, font=font, fill="#FFFF00") 105 y += font.getsize(CPU)[1] 106 draw.text((x, y), MemUsage, font=font, fill="#00FF00") 107 y += font.getsize(MemUsage)[1] 108 draw.text((x, y), Disk, font=font, fill="#0000FF") 109 y += font.getsize(Disk)[1] 110 draw.text((x, y), Temp, font=font, fill="#FF00FF") 111 112 # Display image. 113 disp.image(image) 114 time.sleep(0.1)