Skip to content

Commit 38c24ec

Browse files
committed
pushbutton.py: ESP32Touch uses integer maths.
1 parent 56d8fa0 commit 38c24ec

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

v3/docs/DRIVERS.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ goes outside defined bounds.
4343

4444
# 2. Installation and usage
4545

46-
The drivers require a daily build of firmware or a release build >=1.15. The
47-
drivers are in the primitives package. To install copy the `primitives`
48-
directory and its contents to the target hardware.
46+
The drivers require firmware version >=1.15. The drivers are in the primitives
47+
package. To install copy the `primitives` directory and its contents to the
48+
target hardware.
4949

5050
Drivers are imported with:
5151
```python
@@ -332,17 +332,21 @@ API and usage are as per `Pushbutton` with the following provisos:
332332
1. The `sense` constructor arg is not supported.
333333
2. The `Pin` instance passed to the constructor must support the touch
334334
interface. It is instantiated without args, as per the example below.
335-
3. There is an additional class variable `sensitivity` which should be a float
336-
in range 0.0..1.0. The value `v` returned by the touchpad is read on
337-
initialisation. The touchpad is polled and if the value drops below
338-
`v * sensitivity` the pad is assumed to be pressed. Default `sensitivity` is
339-
0.9 but this is subject to change.
335+
3. There is an additional classmethod `threshold` which takes an integer arg.
336+
The arg represents the detection threshold as a percentage.
337+
338+
The driver determines the untouched state by periodically polling
339+
`machine.TouchPad.read()` and storing its maximum value. If it reads a value
340+
below `maximum * threshold / 100` a touch is deemed to have occurred. Default
341+
threshold is currently 80% but this is subject to change.
340342

341343
Example usage:
342344
```python
343345
from machine import Pin
344-
from primitives import ESP32Touch
345346
import uasyncio as asyncio
347+
from primitives import ESP32Touch
348+
349+
ESP32Touch.threshold(70) # optional
346350

347351
async def main():
348352
tb = ESP32Touch(Pin(15), suppress=True)
@@ -359,6 +363,10 @@ If a touchpad is touched on initialisation no callbacks will occur even when
359363
the pad is released. Initial button state is always `False`. Normal behaviour
360364
will commence with subsequent touches.
361365

366+
The best threshold value depends on physical design. Directly touching a large
367+
pad will result in a low value from `machine.TouchPad.read()`. A small pad
368+
covered with an insulating film will yield a smaller change.
369+
362370
###### [Contents](./DRIVERS.md#1-contents)
363371

364372
# 5. ADC monitoring

v3/primitives/pushbutton.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ def deinit(self):
112112

113113

114114
class ESP32Touch(Pushbutton):
115-
sensitivity = 0.9
115+
thresh = (80 << 8) // 100
116+
@classmethod
117+
def threshold(cls, val):
118+
cls.thresh = (val << 8) // 100
119+
116120
def __init__(self, pin, suppress=False):
117121
self._thresh = 0 # Detection threshold
118122
self._rawval = 0
@@ -127,7 +131,6 @@ def rawstate(self):
127131
rv = self._pad.read() # ~220μs
128132
if rv > self._rawval: # Either initialisation or pad was touched
129133
self._rawval = rv # when initialised and has now been released
130-
self._thresh = round(rv * ESP32Touch.sensitivity)
134+
self._thresh = (rv * ESP32Touch.thresh) >> 8
131135
return False # Untouched
132136
return rv < self._thresh
133-

0 commit comments

Comments
 (0)