Skip to content
Snippets Groups Projects
Commit 2b007357 authored by iggy's avatar iggy
Browse files

py: captouch-demo: autocalib button added (app enter), fixes for misc captouch issues

parent c446dbdf
No related branches found
No related tags found
No related merge requests found
Pipeline #5644 passed
from st3m import logging
log = logging.Log(__name__, level=logging.INFO)
log.info("import")
import cmath import cmath
import math import math
import time import time
import hardware import hardware
from st3m import utils, application from st3m import utils, application, ui, event
class Dot: class Dot:
...@@ -28,6 +33,20 @@ class Dot: ...@@ -28,6 +33,20 @@ class Dot:
class CapTouchDemo(application.Application): class CapTouchDemo(application.Application):
def on_init(self): def on_init(self):
self.dots = [] 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): def main_foreground(self):
self.dots = [] self.dots = []
...@@ -36,7 +55,9 @@ class CapTouchDemo(application.Application): ...@@ -36,7 +55,9 @@ class CapTouchDemo(application.Application):
size += int( size += int(
max( max(
0, 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, / 8000,
) )
) )
...@@ -48,9 +69,18 @@ class CapTouchDemo(application.Application): ...@@ -48,9 +69,18 @@ class CapTouchDemo(application.Application):
self.dots.append(Dot(size, x.imag, x.real)) self.dots.append(Dot(size, x.imag, x.real))
def on_draw(self, ctx): def on_draw(self, ctx):
print(self.last_calib)
ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill() ctx.rgb(0, 0, 0).rectangle(-120, -120, 240, 240).fill()
for i, dot in enumerate(self.dots): for i, dot in enumerate(self.dots):
dot.draw(i, ctx) 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") app = CapTouchDemo("cap touch")
...@@ -35,6 +35,7 @@ class Application: ...@@ -35,6 +35,7 @@ class Application:
name="exit", name="exit",
action=self.exit, action=self.exit,
condition=lambda e: e["type"] == "button" condition=lambda e: e["type"] == "button"
and e["index"] == 1
and e.get("from") == 2 and e.get("from") == 2
and e["change"], and e["change"],
) )
......
...@@ -21,9 +21,9 @@ class Control: ...@@ -21,9 +21,9 @@ class Control:
self.menu = menu.MenuControl(self) self.menu = menu.MenuControl(self)
def draw(self): def draw(self, ctx):
self.ui.value = self.get_value() self.ui.value = self.get_value()
self.ui.draw() self.ui.draw(ctx)
def get_normal_value(self): def get_normal_value(self):
v = self.get_value() v = self.get_value()
...@@ -88,7 +88,6 @@ class ControlKnob(ControlFloat): ...@@ -88,7 +88,6 @@ class ControlKnob(ControlFloat):
def enter(self): def enter(self):
# repeat action with current value # repeat action with current value
self.set_value(self.get_value()) self.set_value(self.get_value())
self.draw()
def scroll(self, delta): def scroll(self, delta):
if self.on_mod: if self.on_mod:
...@@ -99,7 +98,6 @@ class ControlKnob(ControlFloat): ...@@ -99,7 +98,6 @@ class ControlKnob(ControlFloat):
v = self.get_value() v = self.get_value()
v_new = max(self.min, min(self.max, v + delta * self.step)) v_new = max(self.min, min(self.max, v + delta * self.step))
self.set_value(v_new) self.set_value(v_new)
self.draw()
def touch_1d(self, x, z): def touch_1d(self, x, z):
if z > 0: # Inital Contact if z > 0: # Inital Contact
...@@ -138,7 +136,6 @@ class ControlSlide(ControlFloat): ...@@ -138,7 +136,6 @@ class ControlSlide(ControlFloat):
if self.do_reset: if self.do_reset:
self.set_value(self._saved_value) self.set_value(self._saved_value)
self._saved_value = None self._saved_value = None
self.draw()
class ControlString(Control): class ControlString(Control):
......
...@@ -239,6 +239,8 @@ def on_scroll(d): ...@@ -239,6 +239,8 @@ def on_scroll(d):
menu_offset = None menu_offset = None
last = time.ticks_ms() last = time.ticks_ms()
def on_scroll_captouch(d): def on_scroll_captouch(d):
active_menu = get_active_menu() active_menu = get_active_menu()
if active_menu is None: if active_menu is None:
...@@ -269,8 +271,6 @@ def on_scroll_captouch(d): ...@@ -269,8 +271,6 @@ def on_scroll_captouch(d):
if diff < 300: if diff < 300:
active_menu.enter_menu() active_menu.enter_menu()
# active_menu.rotate_to(a) # active_menu.rotate_to(a)
...@@ -298,13 +298,14 @@ def on_touch_1d(d): ...@@ -298,13 +298,14 @@ def on_touch_1d(d):
z = -1 z = -1
log.debug(f"menu: touch_1d ({v},{z})") log.debug(f"menu: touch_1d ({v},{z})")
hovered = active_menu.get_hovered_item()
petal_idx = d["index"] petal_idx = d["index"]
petal_item = active_menu.items_petal[petal_idx] petal_item = active_menu.items_petal[petal_idx]
if petal_item: if petal_item:
petal_item.touch_1d(v, z) petal_item.touch_1d(v, z)
if d["index"] == 8:
hovered = active_menu.get_hovered_item()
if hasattr(hovered, "touch_1d"): if hasattr(hovered, "touch_1d"):
hovered.touch_1d(v, z) hovered.touch_1d(v, z)
...@@ -336,10 +337,14 @@ event.Event( ...@@ -336,10 +337,14 @@ event.Event(
enabled=True, enabled=True,
) )
event.Event(name="menu rotation captouch",group_id="menu", event.Event(
condition=lambda e: e["type"] =="captouch" and (e["value"] == 1 or e["change"]) and e["index"]==2, 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, action=on_scroll_captouch,
enabled=True enabled=True,
) )
...@@ -354,11 +359,9 @@ event.Event( ...@@ -354,11 +359,9 @@ event.Event(
event.Event( event.Event(
name="menu 1d captouch", name="menu 1d captouch",
group_id="menu", group_id="menu",
condition=lambda e: e["type"] == "captouch" condition=lambda e: e["type"] == "captouch" and (e["value"] == 1 or e["change"]),
and (e["value"] == 1 or e["change"])
and e["index"] % 2 == 1,
action=on_touch_1d, action=on_touch_1d,
enabled=False, enabled=True,
) )
event.Event( event.Event(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment