From 94e4923e0ee36ba96cd36ebf67309e0e6a63afb3 Mon Sep 17 00:00:00 2001
From: Serge Bazanski <q3k@q3k.org>
Date: Mon, 12 Jun 2023 14:47:51 +0200
Subject: [PATCH] py/harmonic: port to newer API version

---
 python_payload/apps/harmonic_demo.py | 94 +++++++++++-----------------
 1 file changed, 37 insertions(+), 57 deletions(-)

diff --git a/python_payload/apps/harmonic_demo.py b/python_payload/apps/harmonic_demo.py
index e759439dc3..d80ce22fa1 100644
--- a/python_payload/apps/harmonic_demo.py
+++ b/python_payload/apps/harmonic_demo.py
@@ -1,5 +1,7 @@
 from synth import tinysynth
 from hardware import *
+import leds
+
 
 chords = [
     [-4, 0, 3, 8, 10],
@@ -9,74 +11,52 @@ chords = [
     [3, 7, 10, 14, 15],
 ]
 
-chord_index = 3
-chord = chords[3]
-synths = []
-
-
-def set_chord(i):
-    global chord_index
-    global chord
-    if i != chord_index:
-        chord_index = i
-        for j in range(40):
-            hue = int(72 * (i + 0.5)) % 360
-            set_led_hsv(j, hue, 1, 0.2)
-        chord = chords[i]
-        update_leds()
-
-
-def run():
-    global chord_index
-    global chord
-    global synths
-    for i in range(10):
-        if get_captouch(i):
-            if i % 2:
-                k = int((i - 1) / 2)
-                set_chord(k)
-            else:
-                k = int(i / 2)
-                synths[k].tone(chord[k])
-                synths[k].start()
-
-
-def init():
-    global chord_index
-    global chord
-    global synths
-
-    for i in range(5):
-        synths += [tinysynth(440, 1)]
-
-    for synth in synths:
-        synth.decay(100)
-        synth.waveform(1)
-
-
-def foreground():
-    global chord_index
-    global chord
-    global synths
-    tmp = chord_index
-    chord_index = -1
-    set_chord(tmp)
-
 
 from st3m.application import Application
 
 
 class HarmonicApp(Application):
     def on_init(self):
-        init()
-        # foreground()
+        self.color_intensity = 0
+        self.chord_index = None
+        self.chord = None
+        self.synths = [
+            tinysynth(440, 1) for i in range(5)
+        ]
+        for synth in self.synths:
+            synth.decay(100)
+            synth.waveform(1)
+        self._set_chord(3)
+
+    def _set_chord(self, i):
+        hue = int(72 * (i + 0.5)) % 360
+        if i != self.chord_index:
+            self.chord_index = i
+            for j in range(40):
+                leds.set_hsv(j, hue, 1, 0.2)
+            self.chord = chords[i]
+            leds.update()
 
     def on_foreground(self):
-        # foreground()
         pass
 
+    def on_draw(self, ctx):
+        i = self.color_intensity
+        ctx.rgb(i, i, i).rectangle(-120, -120, 240, 240).fill()
+
     def main_foreground(self):
-        run()
+        if self.color_intensity > 0:
+            self.color_intensity -= self.color_intensity / 20
+        for i in range(10):
+            if get_captouch(i):
+                if i % 2:
+                    k = int((i - 1) / 2)
+                    self._set_chord(k)
+                else:
+                    k = int(i / 2)
+                    self.synths[k].tone(self.chord[k])
+                    self.synths[k].start()
+                    self.color_intensity = 1.0
 
 
 app = HarmonicApp("harmonic")
-- 
GitLab