From 480ab2affce2a1c3599e0dafe13680f4cde48b57 Mon Sep 17 00:00:00 2001
From: dx <dequis@dequis.org>
Date: Sun, 13 Aug 2023 12:00:25 +0000
Subject: [PATCH] Make captouch petal numbering and angle clockwise (api
 breaking)

---
 components/flow3r_bsp/flow3r_bsp_captouch.c  | 80 ++++++++++----------
 components/flow3r_bsp/flow3r_bsp_captouch.h  |  6 +-
 components/st3m/st3m_captouch.c              |  2 +-
 components/st3m/st3m_captouch.h              |  4 +-
 python_payload/apps/demo_cap_touch/main.py   |  4 +-
 python_payload/apps/demo_melodic/__init__.py |  6 +-
 python_payload/apps/demo_scroll/__init__.py  |  4 +-
 python_payload/apps/demo_worms/__init__.py   |  2 +-
 python_payload/apps/gay_drums/__init__.py    |  6 +-
 python_payload/apps/led_painter/__init__.py  | 12 +--
 python_payload/apps/otamatone/__init__.py    |  2 +-
 python_payload/apps/shoegaze/__init__.py     |  4 +-
 python_payload/mypystubs/captouch.pyi        |  4 +-
 python_payload/st3m/input.py                 |  2 +-
 python_payload/st3m/ui/elements/overlays.py  |  4 +-
 15 files changed, 71 insertions(+), 71 deletions(-)

diff --git a/components/flow3r_bsp/flow3r_bsp_captouch.c b/components/flow3r_bsp/flow3r_bsp_captouch.c
index 8c5a3e711f..2b84cbeb73 100644
--- a/components/flow3r_bsp/flow3r_bsp_captouch.c
+++ b/components/flow3r_bsp/flow3r_bsp_captouch.c
@@ -22,35 +22,35 @@ static const pad_mapping_t _map_top[12] = {
     { 0, petal_pad_tip },  // 0
     { 0, petal_pad_ccw },  // 1
     { 0, petal_pad_cw },   // 2
-    { 2, petal_pad_cw },   // 3
-    { 2, petal_pad_ccw },  // 4
-    { 2, petal_pad_tip },  // 5
-    { 6, petal_pad_tip },  // 6
-    { 6, petal_pad_ccw },  // 7
-    { 6, petal_pad_cw },   // 8
-    { 4, petal_pad_cw },   // 9
-    { 4, petal_pad_ccw },  // 10
-    { 4, petal_pad_tip },  // 11
+    { 8, petal_pad_cw },   // 3
+    { 8, petal_pad_ccw },  // 4
+    { 8, petal_pad_tip },  // 5
+    { 4, petal_pad_tip },  // 6
+    { 4, petal_pad_ccw },  // 7
+    { 4, petal_pad_cw },   // 8
+    { 6, petal_pad_cw },   // 9
+    { 6, petal_pad_ccw },  // 10
+    { 6, petal_pad_tip },  // 11
 };
 static const pad_mapping_t _map_bot[13] = {
-    { 1, petal_pad_base },  // 0
-    { 1, petal_pad_tip },   // 1
+    { 9, petal_pad_base },  // 0
+    { 9, petal_pad_tip },   // 1
 
-    { 3, petal_pad_base },  // 2
-    { 3, petal_pad_tip },   // 3
+    { 7, petal_pad_base },  // 2
+    { 7, petal_pad_tip },   // 3
 
     { 5, petal_pad_base },  // 4
     { 5, petal_pad_tip },   // 5
 
-    { 7, petal_pad_tip },   // 6
-    { 7, petal_pad_base },  // 7
+    { 3, petal_pad_tip },   // 6
+    { 3, petal_pad_base },  // 7
 
-    { 9, petal_pad_tip },   // 8
-    { 9, petal_pad_base },  // 9
+    { 1, petal_pad_tip },   // 8
+    { 1, petal_pad_base },  // 9
 
-    { 8, petal_pad_tip },  // 10
-    { 8, petal_pad_cw },   // 11
-    { 8, petal_pad_ccw },  // 12
+    { 2, petal_pad_tip },  // 10
+    { 2, petal_pad_cw },   // 11
+    { 2, petal_pad_ccw },  // 12
 };
 static gpio_num_t _interrupt_gpio_top = GPIO_NUM_15;
 static gpio_num_t _interrupt_gpio_bot = GPIO_NUM_15;
@@ -60,35 +60,35 @@ static const pad_mapping_t _map_top[12] = {
     { 0, petal_pad_ccw },   // 0
     { 0, petal_pad_base },  // 1
     { 0, petal_pad_cw },    // 2
-    { 2, petal_pad_cw },    // 3
-    { 2, petal_pad_base },  // 4
-    { 2, petal_pad_ccw },   // 5
-    { 6, petal_pad_ccw },   // 6
-    { 6, petal_pad_base },  // 7
-    { 6, petal_pad_cw },    // 8
-    { 4, petal_pad_ccw },   // 9
-    { 4, petal_pad_base },  // 10
-    { 4, petal_pad_cw },    // 11
+    { 8, petal_pad_cw },    // 3
+    { 8, petal_pad_base },  // 4
+    { 8, petal_pad_ccw },   // 5
+    { 4, petal_pad_ccw },   // 6
+    { 4, petal_pad_base },  // 7
+    { 4, petal_pad_cw },    // 8
+    { 6, petal_pad_ccw },   // 9
+    { 6, petal_pad_base },  // 10
+    { 6, petal_pad_cw },    // 11
 };
 static const pad_mapping_t _map_bot[13] = {
-    { 1, petal_pad_base },  // 0
-    { 1, petal_pad_tip },   // 1
+    { 9, petal_pad_base },  // 0
+    { 9, petal_pad_tip },   // 1
 
-    { 3, petal_pad_base },  // 2
-    { 3, petal_pad_tip },   // 3
+    { 7, petal_pad_base },  // 2
+    { 7, petal_pad_tip },   // 3
 
     { 5, petal_pad_base },  // 4
     { 5, petal_pad_tip },   // 5
 
-    { 7, petal_pad_tip },   // 6
-    { 7, petal_pad_base },  // 7
+    { 3, petal_pad_tip },   // 6
+    { 3, petal_pad_base },  // 7
 
-    { 9, petal_pad_tip },   // 8
-    { 9, petal_pad_base },  // 9
+    { 1, petal_pad_tip },   // 8
+    { 1, petal_pad_base },  // 9
 
-    { 8, petal_pad_ccw },   // 10
-    { 8, petal_pad_cw },    // 11
-    { 8, petal_pad_base },  // 12
+    { 2, petal_pad_ccw },   // 10
+    { 2, petal_pad_cw },    // 11
+    { 2, petal_pad_base },  // 12
 };
 #if defined(CONFIG_FLOW3R_HW_GEN_P4)
 static gpio_num_t _interrupt_gpio_top = GPIO_NUM_15;
diff --git a/components/flow3r_bsp/flow3r_bsp_captouch.h b/components/flow3r_bsp/flow3r_bsp_captouch.h
index 65a752c30c..a8fd1721a0 100644
--- a/components/flow3r_bsp/flow3r_bsp_captouch.h
+++ b/components/flow3r_bsp/flow3r_bsp_captouch.h
@@ -9,8 +9,8 @@
 // pads.
 //
 // The petals are numbered from 0 to 9 (inclusive). Petal 0 is next to the USB
-// port, and is a top petal. Petal 1 is a bottom petal to its left. Petal 2 is a
-// top petal to its left, and the rest continue counter-clockwise accordingly.
+// port, and is a top petal. Petal 1 is a bottom petal to its right. Petal 2 is
+// a top petal to its right, and the rest continue clockwise accordingly.
 
 #include "esp_err.h"
 
@@ -85,4 +85,4 @@ void flow3r_bsp_captouch_calibrate();
 // Returns true if captouch is currently calibrating.
 //
 // TODO(q3k): this seems glitchy, investigate.
-bool flow3r_bsp_captouch_calibrating();
\ No newline at end of file
+bool flow3r_bsp_captouch_calibrating();
diff --git a/components/st3m/st3m_captouch.c b/components/st3m/st3m_captouch.c
index 7ac6e235c4..44cda82af7 100644
--- a/components/st3m/st3m_captouch.c
+++ b/components/st3m/st3m_captouch.c
@@ -42,7 +42,7 @@ static inline void _petal_process(st3m_petal_state_t *petal, bool top) {
         int32_t right = ringbuffer_avg(&petal->cw.rb);
         int32_t base = ringbuffer_avg(&petal->base.rb);
         petal->pos_distance = (left + right) / 2 - base;
-        petal->pos_angle = left - right;
+        petal->pos_angle = right - left;
 #if defined(CONFIG_FLOW3R_HW_GEN_P3)
         petal->pos_distance = -petal->pos_distance;
 #endif
diff --git a/components/st3m/st3m_captouch.h b/components/st3m/st3m_captouch.h
index 77ef3175b6..9c8771e528 100644
--- a/components/st3m/st3m_captouch.h
+++ b/components/st3m/st3m_captouch.h
@@ -22,7 +22,7 @@
 // layout (tip, cw, ccw). This API pretends base == tip in this case.
 //
 // Petals are numbered. 0 is the top petal above the USB-C jack, increases
-// counter-clockwise so that bottom petals are uneven and top petals even.
+// clockwise so that bottom petals are uneven and top petals even.
 //
 // Processing:
 //
@@ -106,7 +106,7 @@ typedef struct {
 
 typedef struct {
     // Petal 0 is a top petal next to the USB socket. Then, all other petals
-    // follow counter-clockwise.
+    // follow clockwise.
     st3m_petal_state_t petals[10];
 } st3m_captouch_state_t;
 
diff --git a/python_payload/apps/demo_cap_touch/main.py b/python_payload/apps/demo_cap_touch/main.py
index c5057ceb33..de204e1422 100644
--- a/python_payload/apps/demo_cap_touch/main.py
+++ b/python_payload/apps/demo_cap_touch/main.py
@@ -60,8 +60,8 @@ class CapTouchDemo(Application):
             if petal.pressed:
                 size += 4
             x = 70 + (rad / 1000) + 0j
-            x += (phi / 600) * 1j
-            rot = cmath.exp(2j * math.pi * i / 10)
+            x += ((-phi) / 600) * 1j
+            rot = cmath.exp(-2j * math.pi * i / 10)
             x = x * rot
 
             self.dots.append(Dot(size, x.imag, x.real))
diff --git a/python_payload/apps/demo_melodic/__init__.py b/python_payload/apps/demo_melodic/__init__.py
index 97e4cc1951..64a3a7f5f8 100644
--- a/python_payload/apps/demo_melodic/__init__.py
+++ b/python_payload/apps/demo_melodic/__init__.py
@@ -51,17 +51,17 @@ def run(ins: InputState) -> None:
     for i in range(10):
         petal = ins.captouch.petals[i]
         if petal.pressed:
-            if i == 4:
+            if i == 6:
                 octave = -1
                 adjust_playing_field_to_octave()
             elif i == 5:
                 octave = 0
                 adjust_playing_field_to_octave()
-            elif i == 6:
+            elif i == 4:
                 octave = 1
                 adjust_playing_field_to_octave()
             else:
-                k = i
+                k = 10 - i
                 if k > 3:
                     k -= 10
                 k = 3 - k
diff --git a/python_payload/apps/demo_scroll/__init__.py b/python_payload/apps/demo_scroll/__init__.py
index 953ff669af..d64ace914e 100644
--- a/python_payload/apps/demo_scroll/__init__.py
+++ b/python_payload/apps/demo_scroll/__init__.py
@@ -14,7 +14,7 @@ from ctx import Context
 
 
 class ScrollDemo(Application):
-    PETAL_NO = 8
+    PETAL_NO = 2
 
     def __init__(self, app_ctx: ApplicationContext) -> None:
         super().__init__(app_ctx)
@@ -32,7 +32,7 @@ class ScrollDemo(Application):
         ctx.text_baseline = ctx.MIDDLE
         ctx.text_align = ctx.CENTER
 
-        ctx.rotate((self.PETAL_NO * -tau / 10))
+        ctx.rotate((self.PETAL_NO * tau / 10))
         ctx.translate(0, -90)
         ctx.gray(1)
         ctx.text("\ue5c4\ue5c8")
diff --git a/python_payload/apps/demo_worms/__init__.py b/python_payload/apps/demo_worms/__init__.py
index 3d6262ee0d..6656569a62 100644
--- a/python_payload/apps/demo_worms/__init__.py
+++ b/python_payload/apps/demo_worms/__init__.py
@@ -72,7 +72,7 @@ class AppWorms(Application):
             self.bufn = 6
         for index, petal in enumerate(self.input.captouch.petals):
             if petal.whole.pressed or petal.whole.repeated:
-                self.worms.append(Worm(tau * index / 10 + math.pi))
+                self.worms.append(Worm(-tau * index / 10 + math.pi))
         while len(self.worms) > 10:
             self.worms.pop(0)
 
diff --git a/python_payload/apps/gay_drums/__init__.py b/python_payload/apps/gay_drums/__init__.py
index fe44241aca..d29ba965c0 100644
--- a/python_payload/apps/gay_drums/__init__.py
+++ b/python_payload/apps/gay_drums/__init__.py
@@ -391,13 +391,13 @@ class GayDrums(Application):
 
         ct = captouch.read()
         for i in range(4):
-            if ct.petals[4 - i].pressed:
+            if ct.petals[6 + i].pressed:
                 if not self._group_highlight_on[i]:
                     self._group_highlight_redraw[i] = True
                 self._group_highlight_on[i] = True
                 for j in range(4):
-                    if ct.petals[6 + j].pressed and not (
-                        self.ct_prev.petals[6 + j].pressed
+                    if ct.petals[4 - j].pressed and not (
+                        self.ct_prev.petals[4 - j].pressed
                     ):
                         self.track_incr_state(self.track, self.bar * 16 + i * 4 + j)
 
diff --git a/python_payload/apps/led_painter/__init__.py b/python_payload/apps/led_painter/__init__.py
index d831df44db..38f140a31a 100644
--- a/python_payload/apps/led_painter/__init__.py
+++ b/python_payload/apps/led_painter/__init__.py
@@ -37,13 +37,13 @@ class LEDPainter(Application):
         self.g = 40
         self.b = 155
         self.PETAL_R_PLUS = 0
-        self.PETAL_R_MINUS = 9
-        self.PETAL_G_PLUS = 8
-        self.PETAL_G_MINUS = 7
-        self.PETAL_B_PLUS = 6
+        self.PETAL_R_MINUS = 1
+        self.PETAL_G_PLUS = 2
+        self.PETAL_G_MINUS = 3
+        self.PETAL_B_PLUS = 4
         self.PETAL_B_MINUS = 5
-        self.PETAL_BLACK = 4
-        self.PETAL_WHITE = 3
+        self.PETAL_BLACK = 6
+        self.PETAL_WHITE = 7
         self.LEDS = [[0, 0, 0] for i in range(40)]
         #  https://www.sr-research.com/circular-coordinate-calculator/
         self.PETAL_POS = [
diff --git a/python_payload/apps/otamatone/__init__.py b/python_payload/apps/otamatone/__init__.py
index 72ecaf3ebb..4a1f8a0197 100644
--- a/python_payload/apps/otamatone/__init__.py
+++ b/python_payload/apps/otamatone/__init__.py
@@ -75,7 +75,7 @@ class Otamatone(Application):
     A friendly lil' guy that is not annoying at all.
     """
 
-    PETAL_NO = 3
+    PETAL_NO = 7
 
     def __init__(self, app_ctx: ApplicationContext) -> None:
         super().__init__(app_ctx)
diff --git a/python_payload/apps/shoegaze/__init__.py b/python_payload/apps/shoegaze/__init__.py
index e45d1b1f98..1974eb20d9 100644
--- a/python_payload/apps/shoegaze/__init__.py
+++ b/python_payload/apps/shoegaze/__init__.py
@@ -212,13 +212,13 @@ class ShoegazeApp(Application):
 
         for i in range(1, 10, 2):
             if petals[i].whole.pressed:
-                k = int((i - 1) / 2)
+                k = int(((10 - i) - 1) / 2)
                 self._set_chord(k)
 
         if self.blm is None:
             return
         for i in range(2, 10, 2):
-            k = int(i / 2) - 1
+            k = int((10 - i) / 2) - 1
             if petals[i].whole.pressed:
                 self._git_string_tuning[k] = self.chord[k] - 12
                 self.git_strings[k].signals.pitch.tone = self._git_string_tuning[k]
diff --git a/python_payload/mypystubs/captouch.pyi b/python_payload/mypystubs/captouch.pyi
index e3e276010f..5233f7c2b8 100644
--- a/python_payload/mypystubs/captouch.pyi
+++ b/python_payload/mypystubs/captouch.pyi
@@ -75,7 +75,7 @@ class CaptouchPetalState(Protocol):
         An increase in distance means the touch is further away from the centre
         of the badge.
 
-        An increase in angle means the touch is more counter-clockwise.
+        An increase in angle means the touch is more clockwise.
         """
         ...
 
@@ -90,7 +90,7 @@ class CaptouchState(Protocol):
         State of individual petals.
 
         Contains 10 elements, with the zeroth element being the pad closest to
-        the USB port. Then, every other pad in a counter-clockwise direction.
+        the USB port. Then, every other pad in a clockwise direction.
 
         Pads 0, 2, 4, 6, 8 are Top pads.
 
diff --git a/python_payload/st3m/input.py b/python_payload/st3m/input.py
index 99dafd6b7f..27db554c36 100644
--- a/python_payload/st3m/input.py
+++ b/python_payload/st3m/input.py
@@ -483,7 +483,7 @@ class CaptouchState:
     State of capacitive touch petals.
 
     The petals are indexed from 0 to 9 (inclusive). Petal 0 is above the USB-C
-    socket, then the numbering continues counter-clockwise.
+    socket, then the numbering continues clockwise.
     """
 
     __slots__ = "petals"
diff --git a/python_payload/st3m/ui/elements/overlays.py b/python_payload/st3m/ui/elements/overlays.py
index 390dc82526..a5db8189e8 100644
--- a/python_payload/st3m/ui/elements/overlays.py
+++ b/python_payload/st3m/ui/elements/overlays.py
@@ -172,10 +172,10 @@ class OverlayCaptouch(Overlay):
                 return
 
             a = (tau / 10) * self.ix
-            ctx.rotate(-a)
+            ctx.rotate(a)
             ctx.translate(0, -80)
 
-            offs_x = -self.phi / 1000
+            offs_x = self.phi / 1000
             offs_y = -self.rad / 1000
             ctx.rectangle(-5 + offs_x, -5 + offs_y, 10, 10)
             ctx.rgb(1, 0, 1)
-- 
GitLab