From 8bd91bcdfcbebb43a5b585aa77b45cc7b4182d25 Mon Sep 17 00:00:00 2001 From: Peter Hinch Date: Thu, 21 Jul 2022 13:09:38 +0100 Subject: [PATCH] Enable touch threshold control. --- README.md | 14 ++++++++++---- gui/core/ugui.py | 1 + gui/primitives/pushbutton.py | 9 ++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6af56c6..6350f28 100644 --- a/README.md +++ b/README.md @@ -810,8 +810,8 @@ The constructor takes the following positional args: 5. `incr=None` A `Pin` instance for the `increase` button (if used). 6. `decr=None` A `Pin` instance for the `decrease` button (if used). 7. `encoder=False` If an encoder is used, an integer must be passed. - 8. `touch=False` Set `True` to use ESP32 `TouchPad` instances in place of all - physical pushbuttons. See [ESP32 touch pads](./README.md#8-esp32-touch-pads). + 8. `touch=False` Supply an integer to use ESP32 `TouchPad` instances in place + of all physical pushbuttons. See [ESP32 touch pads](./README.md#8-esp32-touch-pads). Class variables: * `verbose=True` Causes a message to be printed indicating whether an encoder @@ -2893,11 +2893,17 @@ prev = Pin(15) # Move to previous control increase = Pin(33) # Increase control's value decrease = Pin(32) # Decrease control's value # Create a Display instance and assign to display. -display = Display(ssd, nxt, sel, prev, increase, decrease, False, True) +display = Display(ssd, nxt, sel, prev, increase, decrease, False, 80) ``` The final two constructor args are: * `encoder=False` Not being used in this example. - * `touch=True` Use touch interface. + * `touch=80` Use touch interface with a threshold of 80%. + +The `touch` value determines the level from `machine.TouchPad.read()` at which +a touch is determined to have occurred. Assume a value of 50 is passed, and the +untouched value read is 1020. If a value below 50% of 1020 = 510 is read, a +touch is deemed to have occurred. +[further docs](https://github.com/peterhinch/micropython-async/blob/master/v3/docs/DRIVERS.md#42-esp32touch-class) # 9. Realtime applications diff --git a/gui/core/ugui.py b/gui/core/ugui.py index 683e491..9effe7f 100644 --- a/gui/core/ugui.py +++ b/gui/core/ugui.py @@ -257,6 +257,7 @@ class Display(DisplayIP): if touch: from gui.primitives import ESP32Touch + ESP32Touch.threshold(touch) ipdev = Input(nxt, sel, prev, incr, decr, encoder, ESP32Touch) else: ipdev = Input(nxt, sel, prev, incr, decr, encoder, Pushbutton) diff --git a/gui/primitives/pushbutton.py b/gui/primitives/pushbutton.py index dd1a386..4859f33 100644 --- a/gui/primitives/pushbutton.py +++ b/gui/primitives/pushbutton.py @@ -112,7 +112,11 @@ class Pushbutton: class ESP32Touch(Pushbutton): - sensitivity = 0.9 + thresh = (80 << 8) // 100 + @classmethod + def threshold(cls, val): + cls.thresh = (val << 8) // 100 + def __init__(self, pin, suppress=False): self._thresh = 0 # Detection threshold self._rawval = 0 @@ -127,7 +131,6 @@ class ESP32Touch(Pushbutton): rv = self._pad.read() # ~220μs if rv > self._rawval: # Either initialisation or pad was touched self._rawval = rv # when initialised and has now been released - self._thresh = round(rv * ESP32Touch.sensitivity) + self._thresh = (rv * ESP32Touch.thresh) >> 8 return False # Untouched return rv < self._thresh -