/ examples / ssd1305_stats.py
ssd1305_stats.py
  1  # Copyright (c) 2020 Adafruit Industries
  2  # Author: Tony DiCola, James DeVito, Melissa LeBlanc-Williams
  3  #
  4  # Permission is hereby granted, free of charge, to any person obtaining a copy
  5  # of this software and associated documentation files (the "Software"), to deal
  6  # in the Software without restriction, including without limitation the rights
  7  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8  # copies of the Software, and to permit persons to whom the Software is
  9  # furnished to do so, subject to the following conditions:
 10  #
 11  # The above copyright notice and this permission notice shall be included in
 12  # all copies or substantial portions of the Software.
 13  #
 14  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 17  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 18  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 19  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 20  # THE SOFTWARE.
 21  
 22  
 23  # This example is for use on (Linux) computers that are using CPython with
 24  # Adafruit Blinka to support CircuitPython libraries. CircuitPython does
 25  # not support PIL/pillow (python imaging library)!
 26  
 27  import time
 28  import subprocess
 29  from board import SCL, SDA, D4
 30  import busio
 31  import digitalio
 32  from PIL import Image, ImageDraw, ImageFont
 33  import adafruit_ssd1305
 34  
 35  # Define the Reset Pin
 36  oled_reset = digitalio.DigitalInOut(D4)
 37  
 38  # Create the I2C interface.
 39  i2c = busio.I2C(SCL, SDA)
 40  
 41  # Create the SSD1305 OLED class.
 42  # The first two parameters are the pixel width and pixel height.  Change these
 43  # to the right size for your display!
 44  disp = adafruit_ssd1305.SSD1305_I2C(128, 32, i2c, reset=oled_reset)
 45  
 46  # Clear display.
 47  disp.fill(0)
 48  disp.show()
 49  
 50  # Create blank image for drawing.
 51  # Make sure to create image with mode '1' for 1-bit color.
 52  width = disp.width
 53  height = disp.height
 54  image = Image.new("1", (width, height))
 55  
 56  # Get drawing object to draw on image.
 57  draw = ImageDraw.Draw(image)
 58  
 59  # Draw a black filled box to clear the image.
 60  draw.rectangle((0, 0, width, height), outline=0, fill=0)
 61  
 62  # Draw some shapes.
 63  # First define some constants to allow easy resizing of shapes.
 64  padding = -2
 65  top = padding
 66  bottom = height - padding
 67  # Move left to right keeping track of the current x position for drawing shapes.
 68  x = 0
 69  
 70  
 71  # Load default font.
 72  font = ImageFont.load_default()
 73  
 74  # Alternatively load a TTF font.  Make sure the .ttf font file is in the
 75  # same directory as the python script!
 76  # Some other nice fonts to try: http://www.dafont.com/bitmap.php
 77  # font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 9)
 78  
 79  while True:
 80  
 81      # Draw a black filled box to clear the image.
 82      draw.rectangle((0, 0, width, height), outline=0, fill=0)
 83  
 84      # Shell scripts for system monitoring from here:
 85      # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
 86      cmd = "hostname -I | cut -d' ' -f1"
 87      IP = subprocess.check_output(cmd, shell=True).decode("utf-8")
 88      cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
 89      CPU = subprocess.check_output(cmd, shell=True).decode("utf-8")
 90      cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB  %.2f%%\", $3,$2,$3*100/$2 }'"
 91      MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8")
 92      cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB  %s", $3,$2,$5}\''
 93      Disk = subprocess.check_output(cmd, shell=True).decode("utf-8")
 94  
 95      # Write four lines of text.
 96  
 97      draw.text((x, top + 0), "IP: " + IP, font=font, fill=255)
 98      draw.text((x, top + 8), CPU, font=font, fill=255)
 99      draw.text((x, top + 16), MemUsage, font=font, fill=255)
100      draw.text((x, top + 25), Disk, font=font, fill=255)
101  
102      # Display image.
103      disp.image(image)
104      disp.show()
105      time.sleep(0.1)