Skip to content

Commit cdeab47

Browse files
committed
Doc task group. Callback interface for ESP32 touchpads.
1 parent 40f08b2 commit cdeab47

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

v3/docs/DRIVERS.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ goes outside defined bounds.
2424
4.1 [Pushbutton class](./DRIVERS.md#41-pushbutton-class)
2525
     4.1.1 [The suppress constructor argument](./DRIVERS.md#411-the-suppress-constructor-argument)
2626
     4.1.2 [The sense constructor argument](./DRIVERS.md#412-the-sense-constructor-argument)
27+
4.2 [ESP32Touch class](./DRIVERS.md#42-esp32touch-class)
2728
5. [ADC monitoring](./DRIVERS.md#5-adc-monitoring) Pause until an ADC goes out of bounds
2829
5.1 [AADC class](./DRIVERS.md#51-aadc-class)
2930
5.2 [Design note](./DRIVERS.md#52-design-note)
@@ -321,6 +322,42 @@ the `closed` state of the button is active `high` or active `low`.
321322
See [Advanced use of callbacks](./DRIVERS.md#8-advanced-use-of-callbacks) for
322323
ways to retrieve a result from a callback and to cancel a task.
323324

325+
## 4.2 ESP32Touch class
326+
327+
This subclass of `Pushbutton` supports ESP32 touchpads providing a callback
328+
based interface. See the
329+
[official docs](http://docs.micropython.org/en/latest/esp32/quickref.html#capacitive-touch).
330+
331+
API and usage are as per `Pushbutton` with the following provisos:
332+
1. The `sense` constructor arg is not supported.
333+
2. The `Pin` instance passed to the constructor must support the touch
334+
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.
339+
340+
Example usage:
341+
```python
342+
from machine import Pin
343+
from primitives import ESP32Touch
344+
import uasyncio as asyncio
345+
346+
async def main():
347+
tb = ESP32Touch(Pin(15), suppress=True)
348+
tb.press_func(lambda : print("press"))
349+
tb.double_func(lambda : print("double"))
350+
tb.long_func(lambda : print("long"))
351+
tb.release_func(lambda : print("release"))
352+
while True:
353+
await asyncio.sleep(1)
354+
355+
asyncio.run(main())
356+
```
357+
If a touchpad is touched on initialisation no callbacks will occur even when
358+
the pad is released. Initial button state is always `False`. Normal behaviour
359+
will commence with subsequent touches.
360+
324361
###### [Contents](./DRIVERS.md#1-contents)
325362

326363
# 5. ADC monitoring

v3/docs/TUTORIAL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ REPL.
3838
3.8 [Delay_ms](./TUTORIAL.md#38-delay_ms-class) Software retriggerable delay.
3939
3.9 [Message](./TUTORIAL.md#39-message)
4040
3.10 [Synchronising to hardware](./TUTORIAL.md#310-synchronising-to-hardware)
41-
Debouncing switches, pushbuttons and encoder knobs. Taming ADC's.
41+
Debouncing switches, pushbuttons, ESP32 touchpads and encoder knobs. Taming ADC's.
4242
4. [Designing classes for asyncio](./TUTORIAL.md#4-designing-classes-for-asyncio)
4343
4.1 [Awaitable classes](./TUTORIAL.md#41-awaitable-classes)
4444
     4.1.1 [Use in context managers](./TUTORIAL.md#411-use-in-context-managers)

v3/primitives/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def _handle_exception(loop, context):
3838
"Encode": "encoder_async",
3939
"Message": "message",
4040
"Pushbutton": "pushbutton",
41+
"ESP32Touch": "pushbutton",
4142
"Queue": "queue",
4243
"Semaphore": "semaphore",
4344
"BoundedSemaphore": "semaphore",

v3/primitives/pushbutton.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
import uasyncio as asyncio
77
import utime as time
8-
from . import launch
9-
from primitives.delay_ms import Delay_ms
8+
from . import launch, Delay_ms
9+
try:
10+
from machine import TouchPad
11+
except ImportError:
12+
pass
1013

11-
12-
# An alternative Pushbutton solution with lower RAM use is available here
13-
# https://github.com/kevinkk525/pysmartnode/blob/dev/pysmartnode/utils/abutton.py
1414
class Pushbutton:
1515
debounce_ms = 50
1616
long_press_ms = 1000
@@ -109,3 +109,25 @@ async def buttoncheck(self):
109109

110110
def deinit(self):
111111
self._run.cancel()
112+
113+
114+
class ESP32Touch(Pushbutton):
115+
sensitivity = 0.9
116+
def __init__(self, pin, suppress=False):
117+
self._thresh = 0 # Detection threshold
118+
self._rawval = 0
119+
try:
120+
self._pad = TouchPad(pin)
121+
except ValueError:
122+
raise ValueError(pin) # Let's have a bit of information :)
123+
super().__init__(pin, suppress, False)
124+
125+
# Current logical button state: True == touched
126+
def rawstate(self):
127+
rv = self._pad.read() # ~220μs
128+
if rv > self._rawval: # Either initialisation or pad was touched
129+
self._rawval = rv # when initialised and has now been released
130+
self._thresh = round(rv * ESP32Touch.sensitivity)
131+
return False # Untouched
132+
return rv < self._thresh
133+

0 commit comments

Comments
 (0)