Skip to content

Commit 62bd3b3

Browse files
authored
Add Touch Pad support (#178)
1 parent ce63c9e commit 62bd3b3

28 files changed

+1336
-70
lines changed

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,100 @@
1212
|:-|---|---|
1313
| nanoFramework.Hardware.Esp32 | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.Hardware.Esp32/_apis/build/status/nanoFramework.Hardware.Esp32?repoName=nanoframework%2FnanoFramework.Hardware.Esp32&branchName=main)](https://dev.azure.com/nanoframework/nanoFramework.Hardware.Esp32/_build/latest?definitionId=11&repoName=nanoframework%2FnanoFramework.Hardware.Esp32&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.Hardware.Esp32.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.Hardware.Esp32/) |
1414

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+
15109
## Feedback and documentation
16110

17111
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).

nanoFramework.Hardware.Esp32/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
////////////////////////////////////////////////////////////////
1414
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.0.7.3")]
15+
[assembly: AssemblyNativeVersion("100.0.8.0")]
1616
////////////////////////////////////////////////////////////////
1717

1818
// Setting ComVisible to false makes the types in this assembly not visible

nanoFramework.Hardware.Esp32/Sleep.cs

Lines changed: 10 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -175,68 +175,7 @@ public enum WakeupGpioPin : UInt64
175175
/// Gpio Pin 39 used for wakeup.
176176
/// </summary>
177177
Pin39 = (UInt64)1 << 39
178-
}
179-
180-
/// <summary>
181-
/// Enumeration of Touchpad numbers.
182-
/// </summary>
183-
public enum TouchPad
184-
{
185-
/// <summary>
186-
/// Touchpad channel 0 is GPIO4.
187-
/// </summary>
188-
Num0 = 0,
189-
190-
/// <summary>
191-
/// Touchpad channel 1 is GPIO0.
192-
/// </summary>
193-
Num1,
194-
195-
/// <summary>
196-
/// Touchpad channel 2 is GPIO2.
197-
/// </summary>
198-
Num2,
199-
200-
/// <summary>
201-
/// Touchpad channel 3 is GPIO15.
202-
/// </summary>
203-
Num3,
204-
205-
/// <summary>
206-
/// Touchpad channel 4 is GPIO13.
207-
/// </summary>
208-
Num4,
209-
210-
/// <summary>
211-
/// Touchpad channel 5 is GPIO12.
212-
/// </summary>
213-
Num5,
214-
215-
/// <summary>
216-
/// Touchpad channel 6 is GPIO14.
217-
/// </summary>
218-
Num6,
219-
220-
/// <summary>
221-
/// Touchpad channel 7 is GPIO27.
222-
/// </summary>
223-
Num7,
224-
225-
/// <summary>
226-
/// Touchpad channel 8 is GPIO33.
227-
/// </summary>
228-
Num8,
229-
230-
/// <summary>
231-
/// Touchpad channel 9 is GPIO32.
232-
/// </summary>
233-
Num9,
234-
235-
/// <summary>
236-
/// Number returned when no Touchpad used on wakeup.
237-
/// </summary>
238-
None
239-
}
178+
}
240179

241180
/// <summary>
242181
/// Enable Wakeup by Timer.
@@ -279,10 +218,14 @@ public static EspNativeError EnableWakeupByMultiPins(WakeupGpioPin pins, WakeupM
279218
/// <summary>
280219
/// Enable wakeup by Touchpad.
281220
/// </summary>
221+
/// <param name="padNumber1">A valid pad number to wake up the device.</param>
222+
/// <param name="padNumber2">If a valid pad number, will be used in comibation of the first pad number.</param>
223+
/// <param name="thresholdCoefficient">Threshold coefficient for automatic calibration. Percentage from 0 to 100. Default value is 80% seems to work in most cases.</param>
224+
/// <remarks>See <see cref="Touch.TouchPad.GetGpioNumberFromTouchNumber(int)"/> to understand which GPIO maps with which pad.</remarks>
282225
/// <returns>Returns ESP32 native error enumeration.</returns>
283-
public static EspNativeError EnableWakeupByTouchPad()
226+
public static EspNativeError EnableWakeupByTouchPad(int padNumber1, int padNumber2 = -1, byte thresholdCoefficient = 80)
284227
{
285-
return NativeEnableWakeupByTouchPad();
228+
return NativeEnableWakeupByTouchPad(padNumber1, padNumber2, thresholdCoefficient);
286229
}
287230

288231
/// <summary>
@@ -338,7 +281,7 @@ public static WakeupGpioPin GetWakeupGpioPin()
338281
/// Get the Touchpad which caused the wakeup.
339282
/// </summary>
340283
/// <returns>Returns TouchPad number which caused the wakeup, else <see cref="WakeupGpioPin.None"/>.</returns>
341-
public static TouchPad GetWakeupTouchpad()
284+
public static int GetWakeupTouchpad()
342285
{
343286
return NativeGetWakeupTouchpad();
344287
}
@@ -355,7 +298,7 @@ public static TouchPad GetWakeupTouchpad()
355298
private static extern EspNativeError NativeEnableWakeupByMultiPins(WakeupGpioPin pins, WakeupMode mode);
356299

357300
[MethodImpl(MethodImplOptions.InternalCall)]
358-
private static extern EspNativeError NativeEnableWakeupByTouchPad();
301+
private static extern EspNativeError NativeEnableWakeupByTouchPad(int padNumber1, int padNumber2, byte thresholdCoefficient);
359302

360303
[MethodImpl(MethodImplOptions.InternalCall)]
361304
private static extern EspNativeError NativeStartLightSleep();
@@ -370,7 +313,7 @@ public static TouchPad GetWakeupTouchpad()
370313
private static extern WakeupGpioPin NativeGetWakeupGpioPin();
371314

372315
[MethodImpl(MethodImplOptions.InternalCall)]
373-
private static extern TouchPad NativeGetWakeupTouchpad();
316+
private static extern int NativeGetWakeupTouchpad();
374317

375318
#endregion
376319
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright(c).NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
namespace nanoFramework.Hardware.Esp32.Touch
5+
{
6+
/// <summary>
7+
/// Specific to S2/S3, the denoise capacitance used to denoise the touch pad.
8+
/// </summary>
9+
public enum DenoiseCapacitance
10+
{
11+
/// <summary>Denoise channel internal reference capacitance is 5pf.</summary>
12+
Cap5pf = 0,
13+
14+
/// <summary>Denoise channel internal reference capacitance is 6.4pf.</summary>
15+
Cap6pf4 = 1,
16+
17+
/// <summary>Denoise channel internal reference capacitance is 7.8pf.</summary>
18+
Cap7pf8 = 2,
19+
20+
/// <summary>Denoise channel internal reference capacitance is 9.2pf.</summary>
21+
Cap9pf2 = 3,
22+
23+
/// <summary>Denoise channel internal reference capacitance is 10.6pf.</summary>
24+
Cap10pf6 = 4,
25+
26+
/// <summary>Denoise channel internal reference capacitance is 12.0pf.</summary>
27+
Cap12pf0 = 5,
28+
29+
/// <summary>Denoise channel internal reference capacitance is 13.4pf.</summary>
30+
Cap13pf4 = 6,
31+
32+
/// <summary>Denoise channel internal reference capacitance is 14.8pf.</summary>
33+
Cap14pf8 = 7,
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright(c).NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
namespace nanoFramework.Hardware.Esp32.Touch
5+
{
6+
/// <summary>
7+
/// Specific to S2/S3, the denoise range used to denoise the touch pad.
8+
/// </summary>
9+
public enum DenoiseRange
10+
{
11+
/// <summary>Denoise range is 12bit.</summary>
12+
Bit12 = 0,
13+
14+
/// <summary>Denoise range is 10bit.</summary>
15+
Bit10 = 1,
16+
17+
/// <summary>Denoise range is 8bit.</summary>
18+
Bit8 = 2,
19+
20+
/// <summary>Denoise range is 4bit.</summary>
21+
Bit4 = 3,
22+
23+
}
24+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
namespace nanoFramework.Hardware.Esp32.Touch
5+
{
6+
/// <summary>
7+
/// Specific to S2/S3, the denoise capacitance used to denoise the touch pad.
8+
/// </summary>
9+
public class DenoiseSetting
10+
{
11+
private DenoiseCapacitance _denoiseCapacitance;
12+
private DenoiseRange _denoiseRange;
13+
14+
/// <summary>
15+
/// Gets or sets the denoise capacitance.
16+
/// </summary>
17+
public DenoiseCapacitance DenoiseCapacitance
18+
{
19+
get => _denoiseCapacitance;
20+
set
21+
{
22+
_denoiseCapacitance = value;
23+
}
24+
}
25+
26+
/// <summary>
27+
/// Gets or sets the denoise range.
28+
/// </summary>
29+
public DenoiseRange DenoiseRange
30+
{
31+
get => _denoiseRange;
32+
set
33+
{
34+
_denoiseRange = value;
35+
}
36+
}
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
namespace nanoFramework.Hardware.Esp32.Touch
5+
{
6+
/// <summary>
7+
/// ESP32 filter setting class.
8+
/// </summary>
9+
public class Esp32FilterSetting : IFilterSetting
10+
{
11+
private uint _period;
12+
13+
/// <summary>
14+
/// The period in milliseconds for the filtering.
15+
/// </summary>
16+
public uint Period
17+
{
18+
get => _period;
19+
set
20+
{
21+
_period = value;
22+
}
23+
}
24+
}
25+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) .NET Foundation and Contributors
2+
// See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
6+
namespace nanoFramework.Hardware.Esp32.Touch
7+
{
8+
/// <summary>
9+
/// Set debounce count, such as n. If the measured values continue to exceed the threshold for n+1 times, the touch sensor state changes. Range: 0 ~ 7
10+
/// </summary>
11+
public enum FilterSettingDebounce
12+
{
13+
/// <summary>No debounce</summary>
14+
NoDebounce = 0,
15+
16+
/// <summary>1 period.</summary>
17+
One,
18+
19+
/// <summary>2 periods.</summary>
20+
Two,
21+
22+
/// <summary>3 periods.</summary>
23+
Three,
24+
25+
/// <summary>4 periods.</summary>
26+
Four,
27+
28+
/// <summary>5 periods.</summary>
29+
Five,
30+
31+
/// <summary>6 periods.</summary>
32+
Six,
33+
34+
/// <summary>7 periods.</summary>
35+
Seven,
36+
}
37+
}

0 commit comments

Comments
 (0)