Optimized oled script startup and shutdown (#573)

* Optimize the oled script for quicker startup. Blank the screen instead of showing a splash.

* Remove shutdown splash bitmap images

* Cleanup

* Resolve pylint warnings
This commit is contained in:
Daniel Markstedt
2021-12-28 12:10:42 -08:00
committed by GitHub
parent fecdc44629
commit 5f39afc5d1
3 changed files with 17 additions and 39 deletions

View File

@@ -31,8 +31,8 @@
THE SOFTWARE. THE SOFTWARE.
""" """
import argparse import argparse
import sys
from time import sleep from time import sleep
from sys import argv
from collections import deque from collections import deque
from board import I2C from board import I2C
from adafruit_ssd1306 import SSD1306_I2C from adafruit_ssd1306 import SSD1306_I2C
@@ -95,20 +95,22 @@ I2C = I2C()
# 128x32 display with hardware I2C: # 128x32 display with hardware I2C:
OLED = SSD1306_I2C(WIDTH, HEIGHT, I2C, addr=0x3C, reset=OLED_RESET) OLED = SSD1306_I2C(WIDTH, HEIGHT, I2C, addr=0x3C, reset=OLED_RESET)
OLED.rotation = ROTATION
print("Running with the following display:") print("Running with the following display:")
print(OLED) print(OLED)
print() print()
print("Will update the OLED display every " + str(DELAY_TIME_MS) + "ms (approximately)") print("Will update the OLED display every " + str(DELAY_TIME_MS) + "ms (approximately)")
# Clear display. # Show a startup splash bitmap image before starting the main loop
OLED.rotation = ROTATION # Convert the image to mode '1' for 1-bit color (monochrome)
OLED.fill(0) # Make sure the splash bitmap image is in the same dir as this script
IMAGE = Image.open(f"splash_start_{HEIGHT}.bmp").convert("1")
OLED.image(IMAGE)
OLED.show() OLED.show()
# Create blank image for drawing. # Keep the pretty splash on screen for a number of seconds
# Make sure to create image with mode '1' for 1-bit color. sleep(4)
IMAGE = Image.new("1", (OLED.width, OLED.height))
# Get drawing object to draw on image. # Get drawing object to draw on image.
DRAW = ImageDraw.Draw(IMAGE) DRAW = ImageDraw.Draw(IMAGE)
@@ -138,11 +140,6 @@ LINE_SPACING = 8
# Some other nice fonts to try: http://www.dafont.com/bitmap.php # Some other nice fonts to try: http://www.dafont.com/bitmap.php
FONT = ImageFont.truetype('type_writer.ttf', FONT_SIZE) FONT = ImageFont.truetype('type_writer.ttf', FONT_SIZE)
# Load a bitmap image for start and stop splash screens and convert to monocrome
# Make sure the splash bitmap image is in the same dir as this script
SPLASH_START = Image.open(f"splash_start_{HEIGHT}.bmp").convert("1")
SPLASH_STOP = Image.open(f"splash_stop_{HEIGHT}.bmp").convert("1")
IP_ADDR, HOSTNAME = get_ip_and_host() IP_ADDR, HOSTNAME = get_ip_and_host()
@@ -190,28 +187,7 @@ def formatted_output():
return output return output
def start_splash():
"""
Displays a splash screen for the startup sequence
"""
OLED.image(SPLASH_START)
OLED.show()
sleep(4)
def stop_splash():
"""
Displays a splash screen for the shutdown sequence
"""
OLED.image(SPLASH_STOP)
OLED.show()
# Show a startup splash bitmap image before starting the main loop
start_splash()
with GracefulInterruptHandler() as handler: with GracefulInterruptHandler() as handler:
"""
The main loop of displaying attached device info, and other info
"""
while True: while True:
# The reference snapshot of attached devices that will be compared against each cycle # The reference snapshot of attached devices that will be compared against each cycle
@@ -225,10 +201,10 @@ with GracefulInterruptHandler() as handler:
while snapshot == ref_snapshot: while snapshot == ref_snapshot:
# Draw a black filled box to clear the image. # Draw a black filled box to clear the image.
DRAW.rectangle((0, 0, WIDTH, HEIGHT), outline=0, fill=0) DRAW.rectangle((0, 0, WIDTH, HEIGHT), outline=0, fill=0)
y_pos = TOP Y_POS = TOP
for output_line in active_output: for output_line in active_output:
DRAW.text((X_POS, y_pos), output_line, font=FONT, fill=255) DRAW.text((X_POS, Y_POS), output_line, font=FONT, fill=255)
y_pos += LINE_SPACING Y_POS += LINE_SPACING
# Shift the index of the array by one to get a scrolling effect # Shift the index of the array by one to get a scrolling effect
if len(active_output) > LINES: if len(active_output) > LINES:
@@ -242,6 +218,8 @@ with GracefulInterruptHandler() as handler:
snapshot = formatted_output() snapshot = formatted_output()
if handler.interrupted: if handler.interrupted:
# Catch interrupt signals and show a shutdown splash bitmap image # Catch interrupt signals and blank out the screen
stop_splash() DRAW.rectangle((0, 0, WIDTH, HEIGHT), outline=0, fill=0)
exit("Shutting down the OLED display...") OLED.image(IMAGE)
OLED.show()
sys.exit("Shutting down the OLED display...")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 574 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.1 KiB