Skip to content

Instantly share code, notes, and snippets.

@rsbohn
Created February 25, 2024 18:03
Show Gist options
  • Save rsbohn/9002ab69ea63cea77ab71f4a8f9f805b to your computer and use it in GitHub Desktop.
Save rsbohn/9002ab69ea63cea77ab71f4a8f9f805b to your computer and use it in GitHub Desktop.
Time and Temperature
# SPDX-FileCopyrightText: Copyright (c) 2024 Randall Bohn (dexter)
#
# SPDX-License-Identifier: MIT
#
# EQUIPMENT
## Adafruit Feather ESP32-S2 TFT
## CircuitPython 9.0.0 or 8.2.9
## FeatherWing Doubler https://www.adafruit.com/product/2890
import board
import displayio
import time
from adafruit_bitmap_font.bitmap_font import load_font
from adafruit_display_text.label import Label
from adafruit_display_shapes.circle import Circle
import adafruit_adt7410
import wifi
from socketpool import SocketPool
from adafruit_ntp import NTP
import rtc
display = board.DISPLAY
i2c = board.I2C()
therm = adafruit_adt7410.ADT7410(i2c)
METRIC = False
pool = SocketPool(wifi.radio)
ntp = NTP(pool, tz_offset=-7.0) # US/Denver
rtc.RTC().datetime = ntp.datetime
def get_time():
now = time.localtime()
a = f"{now.tm_year}-{now.tm_mon:02d}-{now.tm_mday:02d} "
hour = now.tm_hour % 12
if hour == 0: hour = 12
b= f"{hour:02d}:{now.tm_min:02d}:{now.tm_sec:02d}"
return a,b
def slowly(func, period=2.0):
"rate limit decorator"
check = time.monotonic() - period
def _slowly(*args, **kwargs):
nonlocal check
now = time.monotonic()
if now > check:
check = now + period
return func(*args, **kwargs)
return None
return _slowly
@slowly
def get_temp_C():
"Temperature Celsius"
T = therm.temperature
return f"{T:0.1f} C"
@slowly
def get_temp_F():
"Temperature Fahrenheit"
T = therm.temperature * 1.8 + 32
return f"{T:0.1f} F"
radius = 24
splash = displayio.Group()
cc = Circle(display.width*7//8, display.height//2, radius, outline=0xFFFFFF)
splash.append(cc)
font = load_font("/fonts/Arial-18.pcf")
clock = Label(font, text="----",
color=0xFFFF66,
x=display.width//8,
y=display.height*7//8)
splash.append(clock)
temperature = Label(font, text="10.0 K",
color=0x3333FF,
x=display.width//8,
y=display.height*3//8)
splash.append(temperature)
display.root_group=splash
def loop():
clock.text = get_time()[1]
if METRIC:
temperature.text = get_temp_C() or temperature.text
else:
temperature.text = get_temp_F() or temperature.text
time.sleep(0.1)
if __name__=="__main__":
while True:
loop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment