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