diff --git a/python_payload/apps/cap_touch_demo.py b/python_payload/apps/cap_touch_demo.py index b991d4cf07dc9f3b98e3cd4baae3f0d764d95043..5c35465804318c511e094a6e5421be2c00dcfe51 100644 --- a/python_payload/apps/cap_touch_demo.py +++ b/python_payload/apps/cap_touch_demo.py @@ -1,9 +1,14 @@ +from st3m import logging + +log = logging.Log(__name__, level=logging.INFO) +log.info("import") + import cmath import math import time import hardware -from st3m import utils, application +from st3m import utils, application, ui, event class Dot: @@ -28,6 +33,20 @@ class Dot: class CapTouchDemo(application.Application): def on_init(self): self.dots = [] + self.last_calib = None + self.ui_autocalib = ui.IconLabel("Autocalib done", size=30) + + self.add_event( + event.Event( + name="captouch_autocalib", + action=self.do_autocalib, + condition=lambda data: data["type"] == "button" + and data["index"] == 0 + and data["change"] + and data["from"] == 2, + enabled=True, + ) + ) def main_foreground(self): self.dots = [] @@ -36,7 +55,9 @@ class CapTouchDemo(application.Application): size += int( max( 0, - sum([hardware.captouch_get_petal_pad(i, x) for x in range(0, 3 + 1)]) + sum( + [hardware.captouch_get_petal_pad(i, x) for x in range(0, 3 + 1)] + ) / 8000, ) ) @@ -48,9 +69,18 @@ class CapTouchDemo(application.Application): self.dots.append(Dot(size, x.imag, x.real)) def on_draw(self, ctx): + print(self.last_calib) ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill() for i, dot in enumerate(self.dots): dot.draw(i, ctx) + if not self.last_calib is None and self.last_calib > 0: + self.last_calib -= 1 + self.ui_autocalib.draw(ctx) + + def do_autocalib(self, data): + log.info("Performing captouch autocalibration") + hardware.captouch_autocalib() + self.last_calib = 50 -app = CapTouchDemo("cap touch") \ No newline at end of file +app = CapTouchDemo("cap touch") diff --git a/python_payload/st3m/application.py b/python_payload/st3m/application.py index 9a6a6ec49d89e4676c18a800610d10f4699c1ab9..1cf755c804e4480a82452e9898bf3554e0e08355 100644 --- a/python_payload/st3m/application.py +++ b/python_payload/st3m/application.py @@ -35,6 +35,7 @@ class Application: name="exit", action=self.exit, condition=lambda e: e["type"] == "button" + and e["index"] == 1 and e.get("from") == 2 and e["change"], ) diff --git a/python_payload/st3m/control.py b/python_payload/st3m/control.py index 3ae6a59928290d5cfd3ac7b4abec7d6d499691d3..1fcc2b1273f9ea562c9ad9bcb1976c45eab2c67e 100644 --- a/python_payload/st3m/control.py +++ b/python_payload/st3m/control.py @@ -21,9 +21,9 @@ class Control: self.menu = menu.MenuControl(self) - def draw(self): + def draw(self, ctx): self.ui.value = self.get_value() - self.ui.draw() + self.ui.draw(ctx) def get_normal_value(self): v = self.get_value() @@ -88,7 +88,6 @@ class ControlKnob(ControlFloat): def enter(self): # repeat action with current value self.set_value(self.get_value()) - self.draw() def scroll(self, delta): if self.on_mod: @@ -99,7 +98,6 @@ class ControlKnob(ControlFloat): v = self.get_value() v_new = max(self.min, min(self.max, v + delta * self.step)) self.set_value(v_new) - self.draw() def touch_1d(self, x, z): if z > 0: # Inital Contact @@ -138,7 +136,6 @@ class ControlSlide(ControlFloat): if self.do_reset: self.set_value(self._saved_value) self._saved_value = None - self.draw() class ControlString(Control): diff --git a/python_payload/st3m/menu.py b/python_payload/st3m/menu.py index ee4e0d1dde0e207e32860960c2de92f42dbc8f64..3a0b63caae13a72f5e4101fca025d84664dbff6c 100644 --- a/python_payload/st3m/menu.py +++ b/python_payload/st3m/menu.py @@ -239,16 +239,18 @@ def on_scroll(d): menu_offset = None last = time.ticks_ms() + + def on_scroll_captouch(d): active_menu = get_active_menu() if active_menu is None: return global menu_offset global last - #if abs(d["radius"]) < 10000: + # if abs(d["radius"]) < 10000: # return - a = math.atan2(-d["radius"]/600,d["angle"]/600) + a = math.atan2(-d["radius"] / 600, d["angle"] / 600) z = 0 if d["change"]: @@ -257,21 +259,19 @@ def on_scroll_captouch(d): else: z = -1 - if z==1: - menu_offset = active_menu.angle-a + if z == 1: + menu_offset = active_menu.angle - a last = time.ticks_ms() - if z==0: - active_menu.rotate_to(menu_offset+a) + if z == 0: + active_menu.rotate_to(menu_offset + a) - if z==-1: - diff = time.ticks_diff(time.ticks_ms(),last) + if z == -1: + diff = time.ticks_diff(time.ticks_ms(), last) print(diff) - if diff<300: + if diff < 300: active_menu.enter_menu() - - - #active_menu.rotate_to(a) + # active_menu.rotate_to(a) def on_release(d): @@ -298,15 +298,16 @@ def on_touch_1d(d): z = -1 log.debug(f"menu: touch_1d ({v},{z})") - hovered = active_menu.get_hovered_item() petal_idx = d["index"] petal_item = active_menu.items_petal[petal_idx] if petal_item: petal_item.touch_1d(v, z) - if hasattr(hovered, "touch_1d"): - hovered.touch_1d(v, z) + if d["index"] == 8: + hovered = active_menu.get_hovered_item() + if hasattr(hovered, "touch_1d"): + hovered.touch_1d(v, z) def on_enter(d): @@ -336,10 +337,14 @@ event.Event( enabled=True, ) -event.Event(name="menu rotation captouch",group_id="menu", - condition=lambda e: e["type"] =="captouch" and (e["value"] == 1 or e["change"]) and e["index"]==2, +event.Event( + name="menu rotation captouch", + group_id="menu", + condition=lambda e: e["type"] == "captouch" + and (e["value"] == 1 or e["change"]) + and e["index"] == 2, action=on_scroll_captouch, - enabled=True + enabled=True, ) @@ -354,11 +359,9 @@ event.Event( event.Event( name="menu 1d captouch", group_id="menu", - condition=lambda e: e["type"] == "captouch" - and (e["value"] == 1 or e["change"]) - and e["index"] % 2 == 1, + condition=lambda e: e["type"] == "captouch" and (e["value"] == 1 or e["change"]), action=on_touch_1d, - enabled=False, + enabled=True, ) event.Event(