|
12 | 12 | |:-|---|---|
|
13 | 13 | | nanoFramework.Hardware.Esp32 | [](https://dev.azure.com/nanoframework/nanoFramework.Hardware.Esp32/_build/latest?definitionId=11&repoName=nanoframework%2FnanoFramework.Hardware.Esp32&branchName=main) | [](https://www.nuget.org/packages/nanoFramework.Hardware.Esp32/) |
|
14 | 14 |
|
| 15 | +## Touch Pad essentials |
| 16 | + |
| 17 | +This section will give you essential elements about how to use Touch Pad pins on ESP32 and ESP32-S2. |
| 18 | + |
| 19 | +### Touch Pad vs GPIO pins |
| 20 | + |
| 21 | +Touch Pad pins numbering is different from GPIO pin. You can check which GPIO pins correspond to which GPIO pin using the following: |
| 22 | + |
| 23 | +```csharp |
| 24 | +const int TouchPadNumber = 5; |
| 25 | +var pinNum = TouchPad.GetGpioNumberFromTouchNumber(TouchPadNumber); |
| 26 | +Console.WriteLine($"Pad {TouchPadNumber} is GPIO{pinNum}"); |
| 27 | +``` |
| 28 | + |
| 29 | +The pin numbering is different on ESP32 and S2. There are 10 valid touch pad on ESP32 (0 to 9) and 13 on S2 (1 to 13). |
| 30 | + |
| 31 | +In this example touch pad 5 will be GPIO 12 on ESP32 and GPIO 5 on S2. |
| 32 | + |
| 33 | +### Basic usage ESP32 |
| 34 | + |
| 35 | +On ESP32, if you touch the sensor, the values will be lower, so you have to set a threshold that is lower than the calibration data: |
| 36 | + |
| 37 | +```csharp |
| 38 | +TouchPad touchpad = new(TouchPadNumber); |
| 39 | +Console.WriteLine($"Calibrating touch pad {touchpad.TouchPadNumber}, DO NOT TOUCH it during the process."); |
| 40 | +var calib = touchpad.GetCalibrationData(); |
| 41 | +Console.WriteLine($"calib: {calib} vs Calibration {touchpad.CalibrationData}"); |
| 42 | +// On ESP32: Setup a threshold, usually 2/3 or 80% is a good value. |
| 43 | +touchpad.Threshold = (uint)(touchpad.CalibrationData * 2 / 3); |
| 44 | +touchpad.ValueChanged += TouchpadValueChanged; |
| 45 | + |
| 46 | +Thread.Sleep(Timeout.Infinite); |
| 47 | + |
| 48 | +private static void TouchpadValueChanged(object sender, TouchPadEventArgs e) |
| 49 | +{ |
| 50 | + Console.WriteLine($"Touchpad {e.PadNumber} is {(e.Touched ? "touched" : "not touched")}"); |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### Basic usage S2 |
| 55 | + |
| 56 | +On S2, if you touch the sensor, the values will be higher, so you have to set a threshold that is higher than the calibration data and set trigger mode as above: |
| 57 | + |
| 58 | +```csharp |
| 59 | +TouchPad touchpad = new(TouchPadNumber); |
| 60 | +Console.WriteLine($"Calibrating touch pad {touchpad.TouchPadNumber}, DO NOT TOUCH it during the process."); |
| 61 | +var calib = touchpad.GetCalibrationData(); |
| 62 | +Console.WriteLine($"calib: {calib} vs Calibration {touchpad.CalibrationData}"); |
| 63 | +// On S2/S3, the actual read values will be higher, so let's use 20% more |
| 64 | +TouchPad.TouchTriggerMode = TouchTriggerMode.AboveThreshold; |
| 65 | +touchpad.Threshold = (uint)(touchpad.CalibrationData * 1.2); |
| 66 | + |
| 67 | +touchpad.ValueChanged += TouchpadValueChanged; |
| 68 | + |
| 69 | +Thread.Sleep(Timeout.Infinite); |
| 70 | + |
| 71 | +private static void TouchpadValueChanged(object sender, TouchPadEventArgs e) |
| 72 | +{ |
| 73 | + Console.WriteLine($"Touchpad {e.PadNumber} is {(e.Touched ? "touched" : "not touched")}"); |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Other features |
| 78 | + |
| 79 | +You have quite a lot of other features available, filters, some specific denoising. You can check the sample repository for more details. |
| 80 | + |
| 81 | +## Sleep mode |
| 82 | + |
| 83 | +You can wake up your ESP32 or ESP32-S2 by touch. |
| 84 | + |
| 85 | +### Sleep modes on ESP32 |
| 86 | + |
| 87 | +ESP32 can be woke up by 1 or 2 touch pad. Here is how to do it with 1: |
| 88 | + |
| 89 | +```csharp |
| 90 | +Sleep.EnableWakeupByTouchPad(PadForSleep1, thresholdCoefficient: 80); |
| 91 | +``` |
| 92 | + |
| 93 | +And with 2 pads: |
| 94 | + |
| 95 | +```csharp |
| 96 | +Sleep.EnableWakeupByTouchPad(PadForSleep1, PadForSleep2); |
| 97 | +``` |
| 98 | + |
| 99 | +Note that the coefficient can be adjusted by doing couple of tests, there is default value of 80 that seems to work in all cases. The coefficient represent a percentage value. |
| 100 | + |
| 101 | +### Sleep modes on S2 |
| 102 | + |
| 103 | +S2 can only be woke up with 1 touch pad. It is recommended to do tests to find the best coefficient: |
| 104 | + |
| 105 | +```csharp |
| 106 | +Sleep.EnableWakeupByTouchPad(PadForSleep, thresholdCoefficient: 90); |
| 107 | +``` |
| 108 | + |
15 | 109 | ## Feedback and documentation
|
16 | 110 |
|
17 | 111 | For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).
|
|
0 commit comments