Last active
January 4, 2022 20:04
-
-
Save Corteil/86e760989022b9c22f96de120a642c36 to your computer and use it in GitHub Desktop.
helper python module for SSD1306 OLED displays
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
# Some of the below code is based on an example from Adafruit, please support them. | |
from time import sleep | |
import subprocess | |
from board import SCL, SDA | |
import busio | |
from PIL import Image, ImageDraw, ImageFont | |
import adafruit_ssd1306 | |
import sys | |
import argparse | |
# Create the I2C interface. | |
i2c = busio.I2C(SCL, SDA) | |
# Setup OLED | |
fontSize = 12 | |
x_axis = 0 | |
y_axis = 0 | |
textLine = 0 | |
# Create the SSD1306 OLED class. | |
# The first two parameters are the pixel width and pixel height. Change these | |
# to the right size for your display! | |
disp = adafruit_ssd1306.SSD1306_I2C(128, 32, i2c) | |
# Create blank image for drawing. | |
# Make sure to create image with mode '1' for 1-bit color. | |
width = disp.width | |
height = disp.height | |
image = Image.new("1", (width, 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, width, height), outline=0, fill=0) | |
# Draw some shapes. | |
# First define some constants to allow easy resizing of shapes. | |
padding = -2 | |
top = padding | |
bottom = 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('/home/pi/Code/fonts/MADE TOMMY Regular.otf', fontSize) | |
# define OLED display functions | |
def setFontSize(size): | |
# Set font size | |
global fontSize, font | |
fontSize = size | |
font = ImageFont.truetype('/home/pi/Code/fonts/MADE TOMMY Regular.otf', fontSize) | |
def clearDisplay(): | |
# Clear display. | |
disp.fill(0) | |
disp.show() | |
def clearLine(line): | |
# Draw a black filled box to clear the line. | |
if line == 1: | |
draw.rectangle((0, 0, width, height/2), outline=0, fill=0) | |
elif line == 2: | |
draw.rectangle((0, 16, width, height), outline=0, fill=0) | |
disp.image(image) | |
disp.show() | |
def displayText(text, x = 0, y = 0): | |
# Displays text | |
draw.text((y, x), text, font=font, fill=255) | |
# Display image. | |
disp.image(image) | |
disp.show() | |
#sleep(0.1) | |
def displayLine(text, line): | |
# Draw a black filled box to clear the line. | |
if line == 1: | |
clearLine(1) | |
draw.text((0, 0), text, font=font, fill=255) | |
elif line == 2: | |
clearLine(2) | |
draw.text((0, 16), text, font=font, fill=255) | |
disp.image(image) | |
disp.show() | |
def displayImage(imagePath): | |
# display JPG on display | |
imageFilePath = imagePath | |
with Image.open(imageFilePath).convert("1") as image: | |
disp.image(image) | |
disp.show() | |
def displayIP(): | |
# display IP address | |
draw.rectangle((0, 0, width, height), outline=0, fill=0) | |
cmd = "hostname -I | cut -d' ' -f1" | |
IP = subprocess.check_output(cmd, shell=True).decode("utf-8") | |
message = f"IP: {IP}" | |
displayText(message) | |
def setLine(line): | |
global textLine | |
textLine = line | |
# End of display functions | |
# Clear Display | |
clearDisplay() | |
# Setup Arguments parser | |
# Define program description | |
descriptionText = "Helper program for OLED display" | |
# Initiate the parser with a description | |
parser = argparse.ArgumentParser(description=descriptionText) | |
parser.add_argument("-i", "--image", help="loads JPG images file") | |
parser.add_argument("-c", "--clear", help="clears display", action="store_true") | |
parser.add_argument("-ip", help="displays IP address", action="store_true") | |
parser.add_argument("-t", "--text", help="display text on display") | |
parser.add_argument("-fs", type=int, help="sets font size for display") | |
parser.add_argument("-x", "--x_axis", type=int, help="sets X axis") | |
parser.add_argument("-y", "--y_axis", type=int, help="sets Y axis") | |
parser.add_argument("-l", "--line", type=int, help="select text line, ") | |
parser.add_argument("-l1", "--line1", help="set text for line 1, ") | |
parser.add_argument("-l2", "--line2", help="set text for line 2, ") | |
parser.add_argument("-cl", "--clear_line", type=int, help="clear selected line") | |
args = parser.parse_args() | |
if args.fs: | |
setFontSize(args.fs) | |
if args.y_axis: | |
y_axis = args.y_axis | |
if args.x_axis: | |
x_axis = args.x_axis | |
if args.line: | |
textLine = args.line | |
if args.image: | |
# display JPG on display | |
imageFilePath = args.image | |
displayImage(imageFilePath) | |
if args.clear: | |
# clear display | |
clearDisplay() | |
if args.clear_line: | |
clearLine(args.clear_line) | |
if args.ip: | |
# display IP address | |
displayIP() | |
if args.text: | |
# display text | |
displayText(args.text, y_axis, x_axis) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment