diff --git a/synchronised_time.py b/synchronised_time.py new file mode 100644 index 0000000000000000000000000000000000000000..bb326ee9eabf642d704b74fcb0ecd943fad9bb9f --- /dev/null +++ b/synchronised_time.py @@ -0,0 +1,48 @@ +import time + + +def create_synchronised_time(display, button_a, button_b, button_x, button_y): + WHITE = display.create_pen(255, 255, 255) + BLACK = display.create_pen(0, 0, 0) + + datetime = [2021, 1, 1, 1, 1, 0] + selected_idx = 0 + + display_width, display_height = display.get_bounds() + + while True: + if button_a.read(): + selected_idx = (selected_idx + 1) % len(datetime) + if button_x.read(): + datetime[selected_idx] += 1 + if button_y.read(): + datetime[selected_idx] = max(datetime[selected_idx] - 1, 1) + if button_b.read(): + break + + display.set_pen(BLACK) + display.clear() + + display.set_pen(WHITE) + + display.text("Next", 10, 10, 30, 2) + display.text("Inc", display_width - 40, 10, 30, 2) + display.text("Dec", display_width - 40, display_height - 20, 30, 2) + display.text("Confirm", 10, display_height - 20, 30, 2) + + display.text("YYYY MM DD HH MM SS", 30, + display_height // 2 - 10, display_width - 30, 2) + display.text( + " ".join("%s%02d" % (">" if idx == selected_idx else "", sep) + for idx, sep in enumerate(datetime)), + 30, display_height // 2 + 10, display_width - 30, 2) + + display.update() + time.sleep(0.3) + + delta = time.mktime(datetime + [0, 0]) - int(time.time()) + + def synchronised_time(): + return int(time.time()) + delta + + return synchronised_time