Last active
May 12, 2022 06:50
-
-
Save evindunn/ea56a687b7950e7fbc1bf0b9269cd6c5 to your computer and use it in GitHub Desktop.
RPLCD World Clock
This file contains hidden or 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
#!/usr/bin/env python3 | |
from RPLCD.i2c import CharLCD | |
from pytz import timezone, utc | |
from datetime import datetime | |
from time import sleep, time as get_ts | |
from concurrent.futures import ThreadPoolExecutor | |
I2C_EXPANDER = "PCF8574" | |
I2C_ADDRS = [0x26, 0x27] | |
TIMEZONES = [ | |
timezone("America/Los_Angeles"), | |
timezone("America/Denver"), | |
timezone("America/Chicago"), | |
timezone("America/New_York"), | |
utc, | |
timezone("Europe/Berlin"), | |
timezone("Asia/Tokyo"), | |
timezone("Australia/Sydney"), | |
] | |
with open("/etc/timezone") as f: | |
local_tz = timezone(f.read().strip()) | |
lcd0 = CharLCD( | |
i2c_expander=I2C_EXPANDER, | |
address=I2C_ADDRS[0], | |
port=1, | |
rows=4, | |
cols=20, | |
backlight_enabled=True | |
) | |
lcd1 = CharLCD( | |
i2c_expander=I2C_EXPANDER, | |
address=I2C_ADDRS[1], | |
port=1, | |
rows=4, | |
cols=20, | |
backlight_enabled=True | |
) | |
lcd0.clear() | |
lcd1.clear() | |
quit = False | |
with ThreadPoolExecutor(2) as pool: | |
while not quit: | |
try: | |
lcd0.cursor = (0, 0) | |
lcd1.cursor = (0, 0) | |
now_ts = get_ts() | |
now = local_tz.localize(datetime.fromtimestamp(now_ts)) | |
lcd_message0 = "" | |
lcd_message1 = "" | |
for idx, tz in enumerate(TIMEZONES): | |
tz_str = tz.zone.split("/")[-1].replace("_", " ") | |
time = now.astimezone(tz).strftime("%T") | |
new_line = "{} {}".format(tz_str[:(20 - len(time))], time).rjust(20) | |
if idx < 4: | |
lcd_message0 += new_line | |
else: | |
lcd_message1 += new_line | |
pool.submit(lcd0.write_string, lcd_message0) | |
pool.submit(lcd1.write_string, lcd_message1) | |
sleep(1 - now_ts % 1) | |
except KeyboardInterrupt: | |
quit = True | |
lcd0.close(clear=True) | |
lcd1.close(clear=True) |
This file contains hidden or 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
RPLCD~=1.3.0 | |
smbus2~=0.4.1 | |
pytz~=2022.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment