Class Touch

extends Object
Touch sensor.

Class summary


constructor pin/any --threshold/int=
Creates a touch pad.

Constructors

constructor pin/any --threshold/int=null
Creates a touch pad.
The pin is a GPIO number. It must refer to a touch-capable GPIO on the current chip. See the class-level documentation for the set of supported pins. The touch pad reserves the pin and releases it again when the touch pad is closed.
The threshold is used by get and for deep-sleep wakeup. A value of 0 disables get and makes the pin unavailable as a wakeup source. If left null, call calibrate to pick one automatically.
Passing a Pin is deprecated; provide the integer GPIO number instead. The Pin form will be removed in a future release.

Methods

The idle baseline recorded by calibrate, or null if the pad has not been calibrated and no baseline has been set via baseline=.
Only used on the ESP32-S2/S3, where get compares the raw reading against baseline + threshold.

baseline= value/int -> none
Sets the idle baseline used by get on the ESP32-S2/S3.
Prefer calibrate unless you have a specific reason to override it.

calibrate --iterations/int=16 -> int
Calibrates the touch pad.
Reads the raw value iterations times while the pad must be untouched, records the average as the baseline, and sets threshold to trigger on a roughly one-third change from that baseline. The threshold sign and meaning is handled per chip; see the library-level documentation.
Returns the computed baseline.

close -> none
Closes the touch pad and releases the associated resources.

Returns whether the touch pad is currently pressed according to the configured threshold.
On the ESP32 this is true while the raw reading is below the threshold. On the ESP32-S2/S3 it is true while the raw reading exceeds baseline by at least the threshold; calibrate (or baseline=) must have been called first. A threshold of 0 always returns false.

Whether this touch pad is closed.

operator == other/any -> bool
Whether this object is equal to the other.
By default, identical is used for equality.
Inheritance
Classes overwrite this operator to get an equality specific to their needs. Equality operators often compare the type and field contents. For example:

class Pin:
  number/int

  constructor .number:

  operator == other:
    if other is not Pin: return false
    return number == other.number
A class doesn't have to follow the above format, but it must keep the operator in sync with any hash-code method. That is, if a class has a hash-code member, then the equality and hash-code must agree. If two instances are equal (a == b), then their hash codes must also be equal (a.hash-code == b.hash-code).

read --raw/bool -> int
Reads the raw value of the touch pad.

Stringifies this object.
Inheritance
Objects that need a human-friendly string representation should overwrite this method. The default string is based on the internal class-ID.

The threshold at which a pin is considered "touched".
This value is used for get and for the deep-sleep wakeup.

threshold= new-value/int -> none
Sets a new threshold value.
The new-value must be between 0 and 0xFFFF.

Fields

pin / any