From 1dec83870e11657bb9000885aaff6aa3bfca1960 Mon Sep 17 00:00:00 2001
From: markus <markus@muc.ccc.de>
Date: Thu, 5 Sep 2019 18:47:15 +0000
Subject: [PATCH] Squashed commit of the following:

commit a9c3712ca591b56248f9934493e6946c503ec395
Author: Markus <markus@muc.ccc.de>
Date:   Sat Aug 24 01:29:03 2019 +0000

    Code formatting

commit b69c586cd2c535cf480c2b45ebad4900c6d637e1
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 23:15:15 2019 +0000

    Remove unused variable

commit a83ff3c0ea8028957361df2a9949bd66530a1e3c
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 23:06:04 2019 +0000

    Make time setting more appealing

commit 469815237b8e8a8c6588d1c76ed02cff1e4f2a85
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 23:01:25 2019 +0000

    Add wait time to loop

commit 6b58b587c0fb147c5b794285c27b4d0a84d0bd8a
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 22:56:40 2019 +0000

    Give user time to release button

commit 823c00a72712c9e4a98b0103ab7736cab581e2c5
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 22:44:48 2019 +0000

    Add option to set time

commit 927d3057bcb90118a16a0b35f573ae753f8a4cd9
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 22:44:25 2019 +0000

    Remove obsolete lines

commit e2dbfe81e36b9c6f7f214da361ad0be4d518cd77
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 21:37:42 2019 +0000

    Model localtime as parameter to update function

commit ad560d6c5067044a21c5bfaa6d9feb2eaca66228
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 21:29:09 2019 +0000

    Remove obsolete drawImage()

commit 018f911b63e53738cc9e08aa83fb8e21786c5a44
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 21:28:04 2019 +0000

    Fix button_pressed

commit d9b5e26a3b0a50c25caa2c4636b4c33d79b9eb65
Author: Markus <markus@muc.ccc.de>
Date:   Fri Aug 23 21:17:19 2019 +0000

    Remove obsolete Time dummy class
---
 preload/apps/analog_clock/__init__.py | 102 ++++++++++++++------------
 1 file changed, 56 insertions(+), 46 deletions(-)

diff --git a/preload/apps/analog_clock/__init__.py b/preload/apps/analog_clock/__init__.py
index c679a82e..543a0aba 100644
--- a/preload/apps/analog_clock/__init__.py
+++ b/preload/apps/analog_clock/__init__.py
@@ -11,28 +11,6 @@ import os
 CONFIG_NAME = "clock.json"
 
 
-class Time:
-    def __init__(self, start=0):
-        self.time = start
-        self.wait_time = 0.95
-
-    def tick(self):
-        sleep(self.wait_time)
-        self.time += 1
-
-    @property
-    def second(self):
-        return self.time % 60
-
-    @property
-    def minute(self):
-        return (self.time / 60) % 60
-
-    @property
-    def hour(self):
-        return (self.time / 3600) % 24
-
-
 class Clock:
     def __init__(
         self,
@@ -60,7 +38,6 @@ class Clock:
         )
         self.run_once = run_once
         self.offsetx = offsetx
-        self.time = Time()
         self.theme = 0
         self.default_themes = [
             {
@@ -181,46 +158,36 @@ class Clock:
         colored = False
         try:
             with display.open() as disp:
-                button_pressed = False
                 while True:
-                    self.updateClock(disp)
+                    localtime = utime.localtime()
+                    self.updateClock(disp, localtime)
                     if self.run_once:
                         break
 
                     # check for button presses
-                    v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT)
-                    if v == 0:
-                        button_pressed = False
+                    v = buttons.read(
+                        buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT
+                    )
+                    button_pressed = v != 0
 
-                    if not button_pressed and v & buttons.BOTTOM_LEFT != 0:
-                        button_pressed = True
+                    if button_pressed and v & buttons.BOTTOM_LEFT != 0:
                         self.setTheme(self.theme - 1)
                         self.writeConfig()
-                    elif not button_pressed and v & buttons.BOTTOM_RIGHT != 0:
-                        button_pressed = True
+                    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)
+
+                    utime.sleep_ms(23)
 
         except KeyboardInterrupt:
             for i in range(11):
                 leds.set(i, (0, 0, 0))
             return
 
-    def drawImage(self, image):
-        with display.open() as d:
-            d.clear()
-            for x in range(len(image)):
-                for y in range(len(image[x])):
-                    d.pixel(
-                        x + self.offsetx,
-                        y,
-                        col=(255, 255, 255) if image[x][y] else (0, 0, 0),
-                    )
-            d.update()
-
-    def updateClock(self, disp):
+    def updateClock(self, disp, localtime):
         disp.clear(self.background_col)
-        localtime = utime.localtime()
 
         disp.pixel(self.center[0] + self.offsetx, self.center[1], col=self.center_col)
         hour_coords = self.circlePoint(
@@ -284,6 +251,49 @@ class Clock:
 
         disp.update()
 
+    def setTime(self, disp, localtime):
+        accepted = 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 + 1))
+            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))
+        utime.sleep_ms(123)
+
     def circlePoint(self, t):
         return (
             int(round(self.radius * math.cos(t))) + self.center[0],
-- 
GitLab