Last active
October 17, 2022 18:49
-
-
Save caseyanderson/ce3ff24593d9b48ae61405433b868a08 to your computer and use it in GitHub Desktop.
Display RPi IP address on Adafruit's i2c Pi OLED (128x32) screen via python 3 subprocess (hostname -I). If MAC address is present in hostname output (no idea why this happens BTW) split by space and only pass IP to screen. Note: this file is written by Adafruit unless denoted otherwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries | |
# SPDX-License-Identifier: MIT | |
""" | |
This demo will fill the screen with white, draw a black box on top | |
and then print Hello World! in the center of the display | |
This example 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 | |
# added by CTA | |
import subprocess | |
# 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 | |
BORDER = 5 | |
# Use for I2C. | |
i2c = board.I2C() | |
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset) | |
# 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 white background | |
draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255) | |
# Draw a smaller inner rectangle | |
draw.rectangle( | |
(BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1), | |
outline=0, | |
fill=0, | |
) | |
# Load default font. | |
font = ImageFont.load_default() | |
# Draw Some Text | |
# 60 - 70 added by CTA | |
IP = subprocess.Popen(['hostname', '-I'],stdout=subprocess.PIPE) | |
ip_output = IP.communicate()[0].decode('utf-8') | |
if ip_output.find(" ") != -1: | |
# print("there is a space") | |
ip_output = ip_output.split() | |
text = ip_output[0] | |
else: | |
# print("no space") | |
text = ip_output | |
(font_width, font_height) = font.getsize(text) | |
draw.text( | |
(oled.width // 2 - font_width // 2, oled.height // 2 - font_height // 2), | |
text, | |
font=font, | |
fill=255, | |
) | |
# Display image | |
oled.image(image) | |
oled.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment