Skip to content

Instantly share code, notes, and snippets.

@extrasleepy
Created August 2, 2021 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save extrasleepy/b5598361ac0f9d071a6d4f837d4297f0 to your computer and use it in GitHub Desktop.
Save extrasleepy/b5598361ac0f9d071a6d4f837d4297f0 to your computer and use it in GitHub Desktop.
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: Unlicense
#Based on code by Author: Mark Roberts (mdroberts1243) from Adafruit code
#Adapted in July 2021 by Andrew Kleindolph
import board
import time
import displayio
import terminalio
import adafruit_ahtx0
import busio
import adafruit_pcf8523
# can try import bitmap_label below for alternative
from adafruit_display_text import label
import adafruit_displayio_sh1107
# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
rtc = adafruit_pcf8523.PCF8523(i2c)
displayio.release_displays()
# oled_reset = board.D9
# Use for I2C
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)
# SH1107 is vertically oriented 64x128
WIDTH = 128
HEIGHT = 64
BORDER = 2
days = ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
if False: # change to True if you want to write the time!
# year, mon, date, hour, min, sec, wday, yday, isdst
t = time.struct_time((2021, 08, 02, 03, 09, 00, 1, -1, -1))
# you must set year, mon, date, hour, min, sec and weekday
# yearday is not supported, isdst can be set but we don't do anything with it at this time
print("Setting time to:", t) # uncomment for debugging
rtc.datetime = t
print()
display = adafruit_displayio_sh1107.SH1107(display_bus, width=WIDTH, height=HEIGHT)
# Make the display context
splash = displayio.Group(max_size=10)
display.show(splash)
color_bitmap = displayio.Bitmap(WIDTH, HEIGHT, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0xFFFFFF # White
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
splash.append(bg_sprite)
# Draw a smaller inner rectangle in black
inner_bitmap = displayio.Bitmap(WIDTH - BORDER * 2, HEIGHT - BORDER * 2, 1)
inner_palette = displayio.Palette(1)
inner_palette[0] = 0x000000 # Black
inner_sprite = displayio.TileGrid(
inner_bitmap, pixel_shader=inner_palette, x=BORDER, y=BORDER
)
splash.append(inner_sprite)
while True:
t = rtc.datetime
#print(t) # uncomment for debugging
print("The date is %s %d/%d/%d" % (days[t.tm_wday], t.tm_mday, t.tm_mon, t.tm_year))
print("The time is %d:%02d:%02d" % (t.tm_hour, t.tm_min, t.tm_sec))
text2 = str("%s %d/%d/%d" % (days[t.tm_wday], t.tm_mon, t.tm_mday, t.tm_year))
text_area2 = label.Label(
terminalio.FONT, text=text2, scale=1, color=0xFFFFFF, x=9, y=15
)
splash.append(text_area2)
text2 = str("%d:%02d" % (t.tm_hour, t.tm_min))
text_area2 = label.Label(
terminalio.FONT, text=text2, scale=3, color=0xFFFFFF, x=15, y=37
)
splash.append(text_area2)
display.show(splash)
time.sleep(20) # wait a second
splash.pop(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment