Last active
December 13, 2022 08:20
-
-
Save ReinforceZwei/55cc0d4eaca2323a9de1ee5657b87b6c to your computer and use it in GitHub Desktop.
OLED display clock for SSD1306 + Raspberry Pi
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
import time | |
from datetime import datetime | |
import Adafruit_GPIO.SPI as SPI | |
import Adafruit_SSD1306 | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
import subprocess | |
# Raspberry Pi pin configuration: | |
RST = None # on the PiOLED this pin isnt used | |
# Note the following are only used with SPI: | |
DC = 23 | |
SPI_PORT = 0 | |
SPI_DEVICE = 0 | |
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST) | |
disp.begin() | |
# Clear display. | |
disp.clear() | |
disp.display() | |
# 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() | |
font_16 = ImageFont.truetype('Montserrat-Regular.ttf', 16) | |
font_20 = ImageFont.truetype('Montserrat-Regular.ttf', 20) | |
while True: | |
start_time = time.time() | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0,0,width,height), outline=0, fill=0) | |
now = datetime.now() | |
_time = now.strftime("%I:%M:%S") | |
_am_pm = now.strftime("%p") | |
_date = now.strftime("%Y/%m/%d") | |
# Write two lines of text. | |
_size_am_pm = font_20.getlength(_am_pm) | |
_size_date = font_16.getlength(_date) | |
_pad_date = (width - _size_date) / 2 | |
draw.text((x, top), _time, font=font_20, fill=255) | |
draw.text((width-_size_am_pm, top-1), _am_pm, font=font_20, fill=255) | |
draw.text((x+_pad_date, height-16), _date, font=font_16, fill=255) | |
# Display image. | |
disp.image(image.rotate(180)) | |
disp.display() | |
time.sleep(1 - (time.time() - start_time)) |
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
import time | |
from datetime import datetime | |
import Adafruit_GPIO.SPI as SPI | |
import Adafruit_SSD1306 | |
from PIL import Image | |
from PIL import ImageDraw | |
from PIL import ImageFont | |
LNG_JP = 0 | |
LNG_ENG = 1 | |
LNG_CHI = 2 | |
def week_str(day, lng): | |
"""0 is Sunday, 1 is Monday... 6 is Sat""" | |
day = int(day) | |
return [ | |
['日', 'Sun', '日'], | |
['月', 'Mon', '一'], | |
['火', 'Tue', '二'], | |
['水', 'Wed', '三'], | |
['木', 'Thu', '四'], | |
['金', 'Fri', '五'], | |
['土', 'Sat', '六'], | |
][day][lng] | |
def ampm_to_chi(am_pm:str): | |
am_pm = am_pm.lower() | |
if am_pm == 'am': | |
return '上午' | |
elif am_pm == 'pm': | |
return '下午' | |
else: | |
raise Exception("Invalid string. Excepted AM or PM") | |
# Raspberry Pi pin configuration: | |
RST = None # on the PiOLED this pin isnt used | |
# Note the following are only used with SPI: | |
DC = 23 | |
SPI_PORT = 0 | |
SPI_DEVICE = 0 | |
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST, i2c_bus=1) | |
disp.begin() | |
# Clear display. | |
disp.clear() | |
disp.display() | |
# 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() | |
font_path = '/home/pi/Montserrat-Regular.ttf' | |
font_path_ckj = '/home/pi/setofont.ttf' | |
font_16 = ImageFont.truetype(font_path, 16) | |
font_20 = ImageFont.truetype(font_path_ckj, 19) | |
font_16_ckj = ImageFont.truetype(font_path_ckj, 15) | |
while True: | |
start_time = time.time() | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0,0,width,height), outline=0, fill=0) | |
now = datetime.now() | |
_time = now.strftime("%I:%M:%S") | |
_am_pm = ampm_to_chi(now.strftime("%p")) | |
#_date = now.strftime("%Y/%m/%d") | |
_week = week_str(now.strftime("%w"), LNG_JP) | |
_time = f"{_am_pm} {_time}" | |
_date = f"{now.month}月{now.day}日 {_week}曜日" | |
draw.text((int(width/2), top+20), _time, font=font_20, fill=255, anchor="md") | |
#draw.text((width-_size_am_pm, top-1), _am_pm, font=font_20, fill=255) | |
draw.text((int(width/2), height-14), _date, font=font_16_ckj, fill=255, anchor="mt") | |
# Display image. | |
disp.image(image.rotate(180)) | |
disp.display() | |
time.sleep(1 - (time.time() - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment