I bought PaperS3 to build a dashboard displaying current time, weather forecast and bus departures. So far, I am unable to build a skeleton even after three weeks. My intention is to display the current time and a battery level and take a sleep for one minute. When running on a battery, I use deep sleep, because I target charging once per month. If connected to USB, I use a standard sleep to keep the USB connection (deep sleep kills it). Can you review my latest code please?
import M5
from M5 import *
import time, machine
SLEEP_MS = 60_000
WIDTH = 540
def get_battery():
try:
return Power.getBatteryLevel()
except:
return None
def is_charging():
try:
return Power.isCharging()
except:
return False
def draw_screen():
Widgets.setRotation(2)
Widgets.fillScreen(0xFFFFFF)
t = time.localtime()
day = t[2]
month = t[1]
year = t[0]
h = t[3]
m = t[4]
batt = get_battery()
batt_str = f"{batt}%" if batt is not None else "--%"
date_str = f"{day}.{month}.{year}"
time_str = f"{h:02d}:{m:02d}"
battery_str = f"Battery: {batt_str}"
FONT = Widgets.FONTS.DejaVu56
y_start = 250
spacing = 120
Widgets.Label(date_str, WIDTH//2 - 100, y_start, 1.0,
0x000000, 0xFFFFFF, FONT)
Widgets.Label(time_str, WIDTH//2 - 75, y_start + spacing, 1.0,
0x000000, 0xFFFFFF, FONT)
Widgets.Label(battery_str, WIDTH//2 - 175, y_start + spacing * 2, 1.0,
0x000000, 0xFFFFFF, FONT)
M5.update()
def main():
M5.begin()
if is_charging():
while True:
draw_screen()
time.sleep(2)
try:
machine.lightsleep(SLEEP_MS)
except:
time.sleep_ms(SLEEP_MS)
else:
draw_screen()
time.sleep(2)
try:
machine.deepsleep(SLEEP_MS)
except:
time.sleep_ms(SLEEP_MS)
if __name__ == "__main__":
main()
So far I used AI tools to build the code. Thank you