Enable touch threshold control.

touch
Peter Hinch 2022-07-21 13:09:38 +01:00
rodzic 3b5f2f2628
commit 8bd91bcdfc
3 zmienionych plików z 17 dodań i 7 usunięć

Wyświetl plik

@ -810,8 +810,8 @@ The constructor takes the following positional args:
5. `incr=None` A `Pin` instance for the `increase` button (if used). 5. `incr=None` A `Pin` instance for the `increase` button (if used).
6. `decr=None` A `Pin` instance for the `decrease` 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. 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 8. `touch=False` Supply an integer to use ESP32 `TouchPad` instances in place
physical pushbuttons. See [ESP32 touch pads](./README.md#8-esp32-touch-pads). of all physical pushbuttons. See [ESP32 touch pads](./README.md#8-esp32-touch-pads).
Class variables: Class variables:
* `verbose=True` Causes a message to be printed indicating whether an encoder * `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 increase = Pin(33) # Increase control's value
decrease = Pin(32) # Decrease control's value decrease = Pin(32) # Decrease control's value
# Create a Display instance and assign to display. # 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: The final two constructor args are:
* `encoder=False` Not being used in this example. * `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 # 9. Realtime applications

Wyświetl plik

@ -257,6 +257,7 @@ class Display(DisplayIP):
if touch: if touch:
from gui.primitives import ESP32Touch from gui.primitives import ESP32Touch
ESP32Touch.threshold(touch)
ipdev = Input(nxt, sel, prev, incr, decr, encoder, ESP32Touch) ipdev = Input(nxt, sel, prev, incr, decr, encoder, ESP32Touch)
else: else:
ipdev = Input(nxt, sel, prev, incr, decr, encoder, Pushbutton) ipdev = Input(nxt, sel, prev, incr, decr, encoder, Pushbutton)

Wyświetl plik

@ -112,7 +112,11 @@ class Pushbutton:
class ESP32Touch(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): def __init__(self, pin, suppress=False):
self._thresh = 0 # Detection threshold self._thresh = 0 # Detection threshold
self._rawval = 0 self._rawval = 0
@ -127,7 +131,6 @@ class ESP32Touch(Pushbutton):
rv = self._pad.read() # ~220μs rv = self._pad.read() # ~220μs
if rv > self._rawval: # Either initialisation or pad was touched if rv > self._rawval: # Either initialisation or pad was touched
self._rawval = rv # when initialised and has now been released 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 False # Untouched
return rv < self._thresh return rv < self._thresh