From 823c00a72712c9e4a98b0103ab7736cab581e2c5 Mon Sep 17 00:00:00 2001
From: Markus <markus@muc.ccc.de>
Date: Fri, 23 Aug 2019 22:44:48 +0000
Subject: [PATCH] Add option to set time

---
 preload/main.py | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)

diff --git a/preload/main.py b/preload/main.py
index 6fc7b6f2d..eb04e2630 100644
--- a/preload/main.py
+++ b/preload/main.py
@@ -174,6 +174,8 @@ class Clock:
                     elif button_pressed and v & buttons.BOTTOM_RIGHT != 0:
                         self.setTheme(self.theme + 1)
                         self.writeConfig()
+                    elif button_pressed and v & buttons.TOP_RIGHT != 0:
+                        self.setTime(disp, localtime)
 
         except KeyboardInterrupt:
             for i in range(11):
@@ -245,6 +247,44 @@ class Clock:
 
         disp.update()
 
+    def setTime(self, disp, localtime):
+        accepted = False
+        blank = False
+        previously_pressed_button = buttons.TOP_RIGHT
+        button_repeat_counter = 0
+        set_seconds = utime.mktime(localtime)
+
+        while not accepted:
+            v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT)
+            button_pressed = v != 0
+
+            if button_pressed:
+                if v & previously_pressed_button != 0:
+                    button_repeat_counter += 1
+                else:
+                    if v & buttons.BOTTOM_LEFT != 0:
+                        previously_pressed_button = buttons.BOTTOM_LEFT
+                    elif v & buttons.BOTTOM_RIGHT != 0:
+                        previously_pressed_button = buttons.BOTTOM_RIGHT
+                    elif v & buttons.TOP_RIGHT != 0 and previously_pressed_button != buttons.TOP_RIGHT:
+                        accepted = True
+                    else:
+                        previously_pressed_button = 0
+            else:
+                previously_pressed_button = 0
+                button_repeat_counter = 0
+
+            seconds_change = int(min(1.1**button_repeat_counter, 60*23))
+            if previously_pressed_button == buttons.BOTTOM_LEFT:
+                set_seconds -= seconds_change
+            elif previously_pressed_button == buttons.BOTTOM_RIGHT:
+                set_seconds += seconds_change
+
+            self.updateClock(disp, utime.localtime(set_seconds))
+            utime.sleep_ms(23)
+
+        utime.set_time(int(set_seconds))
+
     def circlePoint(self, t):
         return (
             int(round(self.radius * math.cos(t))) + self.center[0],
-- 
GitLab