Skip to content

Instantly share code, notes, and snippets.

@rosscm
Last active December 4, 2022 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rosscm/393d090367df82497ceaca301e51e7cc to your computer and use it in GitHub Desktop.
Save rosscm/393d090367df82497ceaca301e51e7cc to your computer and use it in GitHub Desktop.
Display Adafruit_Python_SSD1306 OLED stats using CircuitPython libraries
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
This code modifies the Adafruit_Python_SSD1306/examples/stats.py
script to be compliant with the newer CircuitPython libraries.
This has been tested on a 4GB RPI4B running Raspbian (bullseye) GNU/Linux 11 armv7.
Before attempting to execute this script, make sure to follow the
'Python Setup' instructions listed here:
https://learn.adafruit.com/monochrome-oled-breakouts/python-setup.
Additional steps to update the final display output taken from:
https://www.the-diy-life.com/connect-and-program-an-oled-stats-display-for-your-raspberry-pi.
This is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
"""
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
import subprocess
import time
# Define the Reset Pin
oled_reset = digitalio.DigitalInOut(board.D4)
# Change these
# to the right size for your display!
WIDTH = 128
# HEIGHT = 32 # Change to 64 if needed
HEIGHT = 64
BORDER = 5
# Use for I2C.
i2c = board.I2C() # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr = 0x3C, reset = oled_reset)
# Use for SPI
# spi = board.SPI()
# oled_cs = digitalio.DigitalInOut(board.D5)
# oled_dc = digitalio.DigitalInOut(board.D6)
# oled = adafruit_ssd1306.SSD1306_SPI(WIDTH, HEIGHT, spi, oled_dc, oled_reset, oled_cs)
# Clear display.
oled.fill(0)
oled.show()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new("1", (oled.width, oled.height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, oled.width, oled.height), outline = 0, fill = 0)
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = oled.height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0
# Load default font.
# font = ImageFont.load_default()
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
font = ImageFont.truetype('PixelOperator.ttf', 16)
while True:
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, oled.width, oled.height), outline = 0, fill = 0)
# Shell scripts for system monitoring from here : https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
cmd = "hostname -I | cut -d\' \' -f1"
IP = subprocess.check_output(cmd, shell = True )
cmd = "top -bn1 | grep load | awk '{printf \"CPU Load: %.2f\", $(NF-2)}'"
CPU = subprocess.check_output(cmd, shell = True )
cmd = "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'"
MemUsage = subprocess.check_output(cmd, shell = True )
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
Disk = subprocess.check_output(cmd, shell = True )
cmd = "vcgencmd measure_temp |cut -f 2 -d '='"
temp = subprocess.check_output(cmd, shell = True )
# Write two lines of text.
draw.text((x, top+2), "IP: " + str(IP,'utf-8'), font = font, fill = 255)
draw.text((x, top+18), str(CPU,'utf-8') + " " + str(temp,'utf-8'), font = font, fill = 255)
draw.text((x, top+34), str(MemUsage,'utf-8'), font = font, fill = 255)
draw.text((x, top+50), str(Disk,'utf-8'), font = font, fill = 255)
# Display image.
oled.image(image)
oled.show()
time.sleep(.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment