Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Clock/Calendar Using SH1106 128x64 Pixel I2C OLED Display

$
0
0
This Python script provides the following features...

-A large 24-hour clock spaced to fill the entire display width
-A day of the week 3-letter alias followed by zero-suppressed day number and month number
-Auto-justification of the calendar to always display centrally and near full display width
-Settable brightness levels for day, evening and night
-Settable start times for day, evening and night brightness transitions
-The settable brightness feature also provides a degree of burn-in protection for the OLED display extending its useful lifetime

You will need to install luma.oled as described here...
https://luma-oled.readthedocs.io/en/lat ... tware.html

You will also need the free GeistMono-Regular.otf monospaced font from here...
https://fontmeme.com/fonts/geist-mono-font/
Place the font in the same directory as the Python script and it will find it.

Note1: Usage of other fonts will likely require changes to x and y values in the draw.text lines to ensure correct character placement.

Note2: This script will only work entirely correctly with monospaced fonts.

This is tried, tested and working with the current Raspberry Pi OS Bookworm at time of posting.

Code:

# Initialise importsfrom luma.core.interface.serial import i2cfrom luma.oled.device import sh1106from luma.core.render import canvasfrom datetime import datetimefrom PIL import ImageFontfrom time import sleepimport os# Set port type and addressserial = i2c(port=1, address=0x3C)# Set device typedevice = sh1106(serial)# Set brightness levels for day, evening and night (0 to 255 - integer)brightD = 96brightE = 48brightN = 16# Set start times for day, evening and night brightness (0 to 23 - integer)dayS = 7eveningS = 19nightS = 23# Use custom .ttf or .otf font (in same directory as this Python file)custom_font = os.path.abspath(os.path.join(os.path.dirname(__file__), 'GeistMono-Regular.otf'))# Set fontsFontDate = ImageFont.truetype(font=custom_font, size=24)FontTime = ImageFont.truetype(font=custom_font, size=50)# Display loopwhile True:# Set day alias name display stringdayA = datetime.now().strftime("%a")# Set zero-suppressed day number display stringday = datetime.now().strftime("%-d")# Set zero-suppressed month number display stringmonth = datetime.now().strftime("%-m")# Set 2-digit hours display stringtimeH = datetime.now().strftime("%H")# Set 2-digit minutes display stringtimeM = datetime.now().strftime("%M")# Set zero-suppressed hours auto-brightness stringtimeB = datetime.now().strftime("%-H")# Set day/evening/night brightness by timeif dayS <= int(timeB) < eveningS: device.contrast(brightD)elif eveningS <= int(timeB) < nightS: device.contrast(brightE)else:device.contrast(brightN)# Calculate padding length for date right-justificationif (len(day)) == 1 and (len(month)) == 1: pad = "   "elif (len(day)) == 1 or (len(month)) == 1: pad = "  "else:pad = " "# Concatenate to set date display stringdateS = dayA + pad + day + "/" + month# Draw to displaywith canvas(device) as draw:draw.text((-1, -5), (dateS), font=FontDate, fill="white")draw.text((-2, 17), (timeH), font=FontTime, fill="white")draw.text((49, 11), (":"), font=FontTime, fill="white")draw.text((70, 17), (timeM), font=FontTime, fill="white")sleep(1)
And this is how it looks on a blue SH1106 display...
OLED_Clock_Closeup.jpg

It appears far less pixelated when viewed from a sensible distance, and the evening and nighttime dimming feature makes it suitable for use as a bedside clock.

Added bonus: If you'd like to give the time a "flip-clock" appearance, you can edit config.txt to slow down the I2C bus by a factor of 4 by doing the following...

Code:

sudo nano /boot/firmware/config.txt
...then find the line...

Code:

dtparam=i2c_arm=on
...and change it to...

Code:

dtparam=i2c_arm=on,i2c_arm_baudrate=25000
...then save the change with Ctrl-X, Y, Enter and reboot.

I'm new to Python, so if you spot any glaring mistakes or improvements that could be made to this script, please feel free to advise. I'm here to learn.

Statistics: Posted by GTR2Fan — Tue Mar 26, 2024 3:52 pm



Viewing all articles
Browse latest Browse all 1251

Trending Articles