From c99d120e264b5dbf1f6b519bb230e02d7210707a Mon Sep 17 00:00:00 2001
From: Serge Bazanski <q3k@q3k.org>
Date: Sun, 11 Jun 2023 19:36:31 +0200
Subject: [PATCH] cap_touch_demo: do not use global ctx

---
 python_payload/apps/cap_touch_demo.py | 65 +++++++++++++++------------
 1 file changed, 37 insertions(+), 28 deletions(-)

diff --git a/python_payload/apps/cap_touch_demo.py b/python_payload/apps/cap_touch_demo.py
index d22d3c9957..acfe5cf613 100644
--- a/python_payload/apps/cap_touch_demo.py
+++ b/python_payload/apps/cap_touch_demo.py
@@ -3,45 +3,54 @@ import math
 import time
 
 import hardware
-from st3m import utils
+from st3m import utils, application
 
-ctx = hardware.get_ctx()
 
+class Dot:
+    def __init__(self, size, imag, real):
+        self.size = size
+        self.imag = imag
+        self.real = real
 
-def init():
-    pass
+    def draw(self, i, ctx):
+        imag = self.imag
+        real = self.real
+        size = self.szie
 
-
-def run():
-    ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
-    time.sleep_ms(30)
-    for i in range(10):
-        size = (hardware.get_captouch(i) * 4) + 4
-        size += int(
-            max(
-                0,
-                sum([hardware.captouch_get_petal_pad(i, x) for x in range(0, 3 + 1)])
-                / 8000,
-            )
-        )
-        x = 70 + (hardware.captouch_get_petal_rad(i) / 1000)
-        x += (hardware.captouch_get_petal_phi(i) / 600) * 1j
-        rot = cmath.exp(2j * math.pi * i / 10)
-        x = x * rot
         col = (1.0, 0.0, 1.0)
         if i % 2:
             col = (0.0, 1.0, 1.0)
         ctx.rgb(*col).rectangle(
-            -int(x.imag - (size / 2)), -int(x.real - (size / 2)), size, size
+            -int(imag - (size / 2)), -int(real - (size / 2)), size, size
         ).fill()
-    hardware.display_update()
 
 
-def foreground():
-    pass
+class CapTouchDemo(application.Application):
+    def on_init(self):
+        self.dots = []
+
+    def main_foreground(self):
+        self.dots = []
+        for i in range(10):
+            size = (hardware.get_captouch(i) * 4) + 4
+            size += int(
+                max(
+                    0,
+                    sum([hardware.captouch_get_petal_pad(i, x) for x in range(0, 3 + 1)])
+                    / 8000,
+                )
+            )
+            x = 70 + (hardware.captouch_get_petal_rad(i) / 1000)
+            x += (hardware.captouch_get_petal_phi(i) / 600) * 1j
+            rot = cmath.exp(2j * math.pi * i / 10)
+            x = x * rot
+
+            self.dots.append(Dot(size, x.imag, x.real))
 
+    def on_draw(self, ctx):
+        ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
+        for i, dot in enumerate(self.dots):
+            dot.draw(i, ctx)
 
-from st3m.application import Application
 
-app = Application("cap touch")
-app.main_foreground = run
+app = CapTouchDemo("cap touch")
\ No newline at end of file
-- 
GitLab