Rich's Wordpress

又一个WordPress站点

Digital Clock

Now that I can use OLED to display text, I have an idea for an electronic clock. I can get real-time time data by calling the time library, and then print the hours, minutes and seconds on the OLED.

The DateTime library is needed, it allows me to get information about dates and times. Here are the 3 most common lines of code.

class datetime.date # year, month and day

class datetime.time # hours, minutes, seconds and microseconds

class datetime.datetime # year, month, day hours, minutes, seconds and microseconds

Code:

import datetime, threading, time
from luma.core.render import canvas
from luma.oled.device import ssd1306
from time import sleep

device = ssd1306(port = 1, address = 0x3C)

def foo():
	next_call = time.time()
	while True:
		x = str(datetime.datetime.now()) #turn the date/time data type into string
		with canvas(device) as draw:
			draw.text((0,0), x[11:13:1], fill = "white") #print the hour
			draw.text((15,0), ':', fill = "white")
			draw.text((20,0), x[14:16:1], fill = "white") #print the minute
			draw.text((35,0), ':', fill = "white")
			draw.text((40,0), x[17:19:1], fill = "white") #print the second
		next_call = next_call+1;
		time.sleep(next_call - time.time())

timerThread = threading.Thread(foo())
timerThread.start()

Result:

Digital Clock
Scroll to top