Skip to content

Instantly share code, notes, and snippets.

@blech
Created March 11, 2024 21:01
Show Gist options
  • Select an option

  • Save blech/b37522de2b24c94a64c65885152f6368 to your computer and use it in GitHub Desktop.

Select an option

Save blech/b37522de2b24c94a64c65885152f6368 to your computer and use it in GitHub Desktop.

Revisions

  1. blech created this gist Mar 11, 2024.
    101 changes: 101 additions & 0 deletions oled_stats.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    # oled stats - for 1.44" square Adafruit miniTFT
    # based on code by Adafruit and Michael Klements

    import time
    import platform
    import subprocess

    import signal
    import sys

    import board
    import busio
    import digitalio

    from PIL import Image, ImageDraw, ImageFont
    from adafruit_rgb_display import st7789


    # Display Refresh
    LOOPTIME = 1.0

    # Configuration for CS and DC pins (these are FeatherWing defaults on M0/M4):
    cs_pin = digitalio.DigitalInOut(board.CE0)
    dc_pin = digitalio.DigitalInOut(board.D25)
    reset_pin = None

    # Config for display baudrate (default max is 24mhz):
    BAUDRATE = 64000000

    # Setup SPI bus using hardware SPI:
    spi = board.SPI()

    # Use for I2C.
    i2c = board.I2C()
    disp = st7789.ST7789(
    spi,
    cs=cs_pin,
    dc=dc_pin,
    rst=reset_pin,
    baudrate=BAUDRATE,
    width=240,
    height=240,
    x_offset=0,
    y_offset=80,
    )

    # Create blank image for drawing.
    # Make sure to create image with mode 'RGB' for full color.
    image = Image.new("RGB", (disp.width, disp.height))
    rotation = 180

    # Get drawing object to draw on image and font to draw with
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype('PixelOperator.ttf', 32)

    # register a keyboard signal handler so we can wipe the screen on exit
    def sigint_handler(signal, frame):
    if disp:
    image = Image.new("RGB", (disp.width, disp.height))
    draw = ImageDraw.Draw(image)
    draw.rectangle((0, 0, disp.width, disp.height), outline=0, fill=0)
    disp.image(image)
    print ('Interrupted')
    sys.exit(0)
    signal.signal(signal.SIGINT, sigint_handler)

    info_commands = [
    "hostname",
    "top -bn1 | grep load | awk '{printf \"CPU: %.2f\", $(NF-2)}'",
    "vcgencmd measure_temp | sed 's/=/: /' | sed 's/t/T/'",
    "free -m | awk 'NR==2{printf \"Mem: %s/%sMB %.2f%%\", $3,$2,$3*100/$2 }'",
    "df -h | awk '$NF==\"/\"{printf \"Disk: %d/%dGB %s\", $3,$2,$5}'"
    ]

    buttonA = digitalio.DigitalInOut(board.D23)
    buttonB = digitalio.DigitalInOut(board.D24)

    while True:
    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, disp.width, disp.height), outline=0, fill=0)

    hostname = platform.node()

    y = 0
    for command in info_commands:
    output = subprocess.check_output(command, shell=True).decode('utf-8')
    draw.text((0, y), output, font=font, fill=(255, 255, 255))
    y += 32

    if not buttonA.value:
    draw.text((0, y), 'button A pressed', font=font, fill=(255, 255, 255))
    y += 32

    if not buttonB.value:
    draw.text((0, y), 'button B pressed', font=font, fill=(255, 255, 255))
    y += 32

    # Display image
    disp.image(image, rotation)
    #disp.show()
    time.sleep(LOOPTIME)