|
| 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 | + |
| 30 | +from board import SCL, SDA |
| 31 | +import busio |
| 32 | +from PIL import Image, ImageDraw, ImageFont |
| 33 | +import adafruit_ssd1305 |
| 34 | + |
| 35 | + |
| 36 | +# Create the I2C interface. |
| 37 | +i2c = busio.I2C(SCL, SDA) |
| 38 | + |
| 39 | +# Create the SSD1305 OLED class. |
| 40 | +# The first two parameters are the pixel width and pixel height. Change these |
| 41 | +# to the right size for your display! |
| 42 | +disp = adafruit_ssd1305.SSD1305_I2C(128, 32, i2c) |
| 43 | + |
| 44 | +# Clear display. |
| 45 | +disp.fill(0) |
| 46 | +disp.show() |
| 47 | + |
| 48 | +# Create blank image for drawing. |
| 49 | +# Make sure to create image with mode '1' for 1-bit color. |
| 50 | +width = disp.width |
| 51 | +height = disp.height |
| 52 | +image = Image.new("1", (width, height)) |
| 53 | + |
| 54 | +# Get drawing object to draw on image. |
| 55 | +draw = ImageDraw.Draw(image) |
| 56 | + |
| 57 | +# Draw a black filled box to clear the image. |
| 58 | +draw.rectangle((0, 0, width, height), outline=0, fill=0) |
| 59 | + |
| 60 | +# Draw some shapes. |
| 61 | +# First define some constants to allow easy resizing of shapes. |
| 62 | +padding = -2 |
| 63 | +top = padding |
| 64 | +bottom = height - padding |
| 65 | +# Move left to right keeping track of the current x position for drawing shapes. |
| 66 | +x = 0 |
| 67 | + |
| 68 | + |
| 69 | +# Load default font. |
| 70 | +font = ImageFont.load_default() |
| 71 | + |
| 72 | +# Alternatively load a TTF font. Make sure the .ttf font file is in the |
| 73 | +# same directory as the python script! |
| 74 | +# Some other nice fonts to try: http://www.dafont.com/bitmap.php |
| 75 | +# font = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 9) |
| 76 | + |
| 77 | +while True: |
| 78 | + |
| 79 | + # Draw a black filled box to clear the image. |
| 80 | + draw.rectangle((0, 0, width, height), outline=0, fill=0) |
| 81 | + |
| 82 | + # Shell scripts for system monitoring from here: |
| 83 | + # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load |
| 84 | + cmd = "hostname -I | cut -d' ' -f1" |
| 85 | + IP = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 86 | + cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'" |
| 87 | + CPU = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 88 | + cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%s MB %.2f%%\", $3,$2,$3*100/$2 }'" |
| 89 | + MemUsage = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 90 | + cmd = 'df -h | awk \'$NF=="/"{printf "Disk: %d/%d GB %s", $3,$2,$5}\'' |
| 91 | + Disk = subprocess.check_output(cmd, shell=True).decode("utf-8") |
| 92 | + |
| 93 | + # Write four lines of text. |
| 94 | + |
| 95 | + draw.text((x, top + 0), "IP: " + IP, font=font, fill=255) |
| 96 | + draw.text((x, top + 8), CPU, font=font, fill=255) |
| 97 | + draw.text((x, top + 16), MemUsage, font=font, fill=255) |
| 98 | + draw.text((x, top + 25), Disk, font=font, fill=255) |
| 99 | + |
| 100 | + # Display image. |
| 101 | + disp.image(image) |
| 102 | + disp.show() |
| 103 | + time.sleep(0.1) |
0 commit comments