From e17e31cd42b747bd6c2c8f0be6d9aab91a22b80e Mon Sep 17 00:00:00 2001
From: schneider <schneider@blinkenlichts.net>
Date: Thu, 22 Aug 2019 00:10:19 +0200
Subject: [PATCH] feat(preload): update nick name preloads

---
 preload/apps/card10_nickname/__init__.py   | 99 +++++++++++++++-------
 preload/apps/card10_nickname/metadata.json |  2 +-
 preload/apps/lsd_nickname/__init__.py      | 37 ++++++++
 preload/apps/lsd_nickname/metadata.json    |  1 +
 4 files changed, 109 insertions(+), 30 deletions(-)
 create mode 100644 preload/apps/lsd_nickname/__init__.py
 create mode 100644 preload/apps/lsd_nickname/metadata.json

diff --git a/preload/apps/card10_nickname/__init__.py b/preload/apps/card10_nickname/__init__.py
index 301b0906d..d38523fbf 100644
--- a/preload/apps/card10_nickname/__init__.py
+++ b/preload/apps/card10_nickname/__init__.py
@@ -7,7 +7,6 @@ Improvement ideas
     - fade effekt
 - led nick writing
 """
-
 import utime
 import display
 import leds
@@ -19,7 +18,7 @@ import os
 
 FILENAME = 'nickname.txt'
 FILENAME_ADV = 'nickname.json'
-ANIM_TYPES = ['none', 'led', 'fade']
+ANIM_TYPES = ['none', 'led', 'fade', 'gay']
 
 
 def render_error(err1, err2):
@@ -31,24 +30,70 @@ def render_error(err1, err2):
         disp.close()
 
 
+def get_time():
+    timestamp = ''
+    if utime.localtime()[3] < 10:
+        timestamp = timestamp + '0'
+    timestamp = timestamp + str(utime.localtime()[3]) + ':'
+    if utime.localtime()[4] < 10:
+        timestamp = timestamp + '0'
+    timestamp = timestamp + str(utime.localtime()[4]) + ':'
+    if utime.localtime()[5] < 10:
+        timestamp = timestamp + '0'
+    timestamp = timestamp + str(utime.localtime()[5])
+    return timestamp
+
+
+def toggle_rockets(state):
+    brightness = 15
+    if not state:
+        brightness = 0
+    leds.set_rocket(0, brightness)
+    leds.set_rocket(1, brightness)
+    leds.set_rocket(2, brightness)
+
+
 def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg):
-    anim = 'led'
+    anim = 0
     posy = 30
     if sub != '':
         posy = 18
     r = 255
     g = 0
     b = 0
+    r_sub = sub
+    last_btn_poll = utime.time() - 2
     while True:
+        sleep = 0.5
+        if sub == '#time':
+            r_sub = get_time()
         dark = 0
-        if light_sensor.get_reading() < 30:
+        if light_sensor.get_reading() < 40:
             dark = 1
         r_fg_color = fg[dark]
         r_bg_color = bg[dark]
         r_fg_sub_color = fg_sub[dark]
         r_bg_sub_color = bg_sub[dark]
         r_bg = main_bg[dark]
-        if anim == 'fade':
+        # Button handling
+        pressed = buttons.read(
+            buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT
+        )
+        if utime.time() - last_btn_poll >= 1:
+            last_btn_poll = utime.time()
+            if pressed & buttons.BOTTOM_RIGHT != 0:
+                anim = anim + 1
+                if anim >= len(ANIM_TYPES):
+                    anim = 0
+            if pressed & buttons.BOTTOM_LEFT != 0:
+                anim = anim - 1
+                if anim < 0:
+                    anim = len(ANIM_TYPES) - 1
+        # Animations
+        if ANIM_TYPES[anim] == 'fade':
+            sleep = 0.1
+            leds.clear()
+            toggle_rockets(False)
             if r > 0 and b == 0:
                 r = r - 1
                 g = g + 1
@@ -59,34 +104,32 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg):
                 r = r + 1
                 b = b - 1
             r_bg = [r, g, b]
-        if anim == 'led':
-            for i in range(0, 11):
-                leds.prep(i, r_bg)
-            leds.update()
-            leds.dim_top(3)
-            leds.set_rocket(0, 15)
-            leds.set_rocket(1, 15)
-            leds.set_rocket(2, 15)
-        if anim == 'none':
+            r_bg_color = r_bg
+            r_bg_sub_color = r_bg
+        if ANIM_TYPES[anim] == 'led':
+            if dark == 1:
+                for i in range(0, 11):
+                    leds.prep(i, r_bg)
+                leds.update()
+                leds.dim_top(4)
+                toggle_rockets(True)
+            else:
+                leds.clear()
+                toggle_rockets(False)
+        if ANIM_TYPES[anim] == 'gay':
+            toggle_rockets(False)
+            leds.gay(0.4)
+        if ANIM_TYPES[anim] == 'none':
             leds.clear()
-            leds.set_rocket(0, 0)
-            leds.set_rocket(1, 0)
-            leds.set_rocket(2, 0)
+            toggle_rockets(False)
         with display.open() as disp:
             disp.rect(0, 0, 160, 80, col=r_bg, filled=True)
             disp.print(title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy)
-            if sub != '':
-                disp.print(sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(sub) / 2 * 14), posy=42)
+            if r_sub != '':
+                disp.print(r_sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(r_sub) / 2 * 14), posy=42)
             disp.update()
             disp.close()
-        pressed = buttons.read(
-            buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT
-        )
-        if pressed & buttons.BOTTOM_LEFT != 0:
-            anim = ANIM_TYPES[1]
-        if pressed & buttons.BOTTOM_RIGHT != 0:
-            anim = ANIM_TYPES[0]
-        utime.sleep(0.3)
+        utime.sleep(sleep)
 
 
 def get_key(json, key, default):
@@ -95,12 +138,10 @@ def get_key(json, key, default):
     except KeyError:
         return default
 
-
 leds.clear()
 with display.open() as disp:
     disp.clear().update()
     disp.close()
-
 if FILENAME_ADV in os.listdir("."):
     f = open(FILENAME_ADV, 'r')
     try:
diff --git a/preload/apps/card10_nickname/metadata.json b/preload/apps/card10_nickname/metadata.json
index db70873d6..2323731ba 100644
--- a/preload/apps/card10_nickname/metadata.json
+++ b/preload/apps/card10_nickname/metadata.json
@@ -1 +1 @@
-{"name":"Card10 Nickname","description":"Nickname app for the card10 badge\r\n\r\nEverything you need can be found here: https:\/\/github.com\/vabene1111\/card10-nickname","category":"graphics","author":"vabene1111","revision":3}
\ No newline at end of file
+{"name":"Card10 Nickname","description":"Nickname app for the card10 badge\r\n\r\nEverything you need can be found here: https:\/\/github.com\/vabene1111\/card10-nickname","category":"graphics","author":"vabene1111","revision":4}
\ No newline at end of file
diff --git a/preload/apps/lsd_nickname/__init__.py b/preload/apps/lsd_nickname/__init__.py
new file mode 100644
index 000000000..b4d20c38b
--- /dev/null
+++ b/preload/apps/lsd_nickname/__init__.py
@@ -0,0 +1,37 @@
+import display
+import leds
+import utime
+
+_rand = 123456789
+def rand():
+    global _rand
+    _rand = (1103515245 * _rand + 12345) & 0xffffff
+    return _rand
+
+gs = 160
+colors = [ ((i>>2)*gs, (i>>1&1)*gs, (i&1)*gs) for i in range(1, 8) ]
+
+nick = 'sample text'
+try:
+    with open('/nickname.txt') as f:
+        nick = f.read()
+except:
+    pass
+
+while True:
+    with display.open() as d:
+        for k in range(4):
+            (x1, y1) = (rand()%159, rand()%79)
+            (x2, y2) = (min(x1+rand()%40, 159), min(y1+rand()%40, 79))
+            try:
+                d.rect(x1, y1, x2, y2, col=colors[rand() % len(colors)], filled=True)
+            except:
+                pass
+        fg = colors[rand()%len(colors)]
+        nx = 80-round(len(nick)/2 * 14)
+        d.print(nick, fg=fg, bg=[0xff-c for c in fg], posx=(nx-8)+rand()%16, posy=22+rand()%16)
+        d.update()
+        d.close()
+    leds.set(rand() % 11, colors[rand() % len(colors)])
+    leds.set_rocket(rand() % 3, rand() % 32)
+    utime.sleep_us(1) # Feed watch doge
\ No newline at end of file
diff --git a/preload/apps/lsd_nickname/metadata.json b/preload/apps/lsd_nickname/metadata.json
new file mode 100644
index 000000000..5aa83b008
--- /dev/null
+++ b/preload/apps/lsd_nickname/metadata.json
@@ -0,0 +1 @@
+{"name":"LSD-Nickname","description":"Renderer for nickname.txt, but oh god what the fuck","category":"graphics","author":"polyfloyd","revision":1}
\ No newline at end of file
-- 
GitLab