From 4e25e98fefd834848c0e483eab9e60f35843cbec Mon Sep 17 00:00:00 2001
From: Stefan `Sec` Zehl <sec@42.org>
Date: Thu, 17 Aug 2023 00:33:53 +0200
Subject: [PATCH] py/nick: added imu-based rotation mode

---
 docs/badge/usage.rst                 |  2 ++
 python_payload/apps/nick/__init__.py | 24 ++++++++++++++++++++++--
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/docs/badge/usage.rst b/docs/badge/usage.rst
index 4fbae55dcd..d0d40b8742 100644
--- a/docs/badge/usage.rst
+++ b/docs/badge/usage.rst
@@ -61,6 +61,8 @@ least once.
 
 Use ``"color": "0xffffff",`` to color your name and pronouns.
 
+Use ``"mode": "1",`` to use a different animation mode rotating your nick based on badge orientation.
+
 When you're done editing, unmount/eject the badge from your computer
 (``umount`` on Linux is enough) and press the left shoulder button to exit Disk
 Mode. Then, go to Badge |rarr| Nick to see your changes!
diff --git a/python_payload/apps/nick/__init__.py b/python_payload/apps/nick/__init__.py
index c7f157fea7..c5a9194280 100644
--- a/python_payload/apps/nick/__init__.py
+++ b/python_payload/apps/nick/__init__.py
@@ -16,6 +16,7 @@ class Configuration:
         self.pronouns: list[str] = []
         self.pronouns_size: int = 25
         self.color = "0x40ff22"
+        self.mode = 0
 
     @classmethod
     def load(cls, path: str) -> "Configuration":
@@ -54,6 +55,11 @@ class Configuration:
             and len(data["color"]) == 8
         ):
             res.color = data["color"]
+        if "mode" in data:
+            if type(data["mode"]) == float:
+                res.mode = int(data["mode"])
+            if type(data["mode"]) == int:
+                res.mode = data["mode"]
         return res
 
     def save(self, path: str) -> None:
@@ -88,6 +94,7 @@ class NickApp(Application):
         self._filename = "/flash/nick.json"
         self._config = Configuration.load(self._filename)
         self._pronouns_serialized = " ".join(self._config.pronouns)
+        self._angle = 0.0
 
     def draw(self, ctx: Context) -> None:
         ctx.text_align = ctx.CENTER
@@ -100,7 +107,10 @@ class NickApp(Application):
 
         ctx.move_to(0, 0)
         ctx.save()
-        ctx.scale(self._scale_name, 1)
+        if self._config.mode == 0:
+            ctx.scale(self._scale_name, 1)
+        elif self._config.mode == 1:
+            ctx.rotate(self._angle)
         ctx.text(self._config.name)
         ctx.restore()
 
@@ -108,7 +118,10 @@ class NickApp(Application):
             ctx.move_to(0, -60)
             ctx.font_size = self._config.pronouns_size
             ctx.save()
-            ctx.scale(1, self._scale_pronouns)
+            if self._config.mode == 0:
+                ctx.scale(self._scale_pronouns, 1)
+            elif self._config.mode == 1:
+                ctx.rotate(self._angle)
             ctx.text(self._pronouns_serialized)
             ctx.restore()
 
@@ -126,6 +139,13 @@ class NickApp(Application):
         self._phase += delta_ms / 1000
         self._scale_name = math.sin(self._phase)
         self._scale_pronouns = math.cos(self._phase)
+
+        iy = ins.imu.acc[0] * delta_ms / 10.0
+        ix = ins.imu.acc[1] * delta_ms / 10.0
+        ang = math.atan2(ix, iy)
+        d_ang = self._angle + (ang + math.pi / 8 * math.sin(self._phase))
+        self._angle -= d_ang / 20
+
         self._led += delta_ms / 45
         if self._led >= 40:
             self._led = 0
-- 
GitLab