-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathProgram.cs
310 lines (270 loc) · 12.5 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using nanoFramework.Hardware.Esp32;
using nanoFramework.Hardware.Esp32.Touch;
using System;
using System.Diagnostics;
using System.Threading;
namespace TestTouchApp
{
public class Program
{
public static void Main()
{
// This is a test application for the touch pad on ESP32 and S2/S3
// Uncomment the one you want to use. This is about sleep
// using the touch pad as a wake up source
// DeepSleepExampleS2S3();
// DeepSleepExampleEsp32With2Pins();
// DeepSleepExampleEsp32();
// Adjust the touch pad you want to use
// S2/S3 goes from 1 to 13.
const int TouchPadNumber0 = 5;
const int TouchPadNumber1 = 12;
// ESP32 from 0 to 9
// const int TouchPadNumber0 = 0;
// const int TouchPadNumber1 = 9;
Debug.WriteLine("Hello touch pad on ESP32 and S2/S3!");
var pinNum = TouchPad.GetGpioNumberFromTouchNumber(TouchPadNumber0);
Console.WriteLine($"Pad {TouchPadNumber0} is GPIO{pinNum}");
pinNum = TouchPad.GetGpioNumberFromTouchNumber(TouchPadNumber1);
Console.WriteLine($"Pad {TouchPadNumber1} is GPIO{pinNum}");
//TestVoltageChange();
//TestTriggerMode();
//TestWakeupMode();
//TestDenoise();
TouchPad touchpad0 = new(TouchPadNumber0);
Debug.WriteLine("Initialized!");
//TestChargeSpeed(touchpad0);
//TestThreshold(touchpad0);
//TestMeasurementTime();
// Always sets the voltage before calibrating
TouchPad.SetVoltage(TouchHighVoltage.Volt2V7, TouchLowVoltage.Volt0V5, TouchHighVoltageAttenuation.Volt1V0);
// This function calibrate
Calibrate(touchpad0);
// On S2/S3, the actual read vallues will be higher, so let's use 30% more
TouchPad.TouchTriggerMode = TouchTriggerMode.AboveThreshold;
touchpad0.Threshold = (uint)(touchpad0.CalibrationData * 1.3);
// On ESP32: Setup a threshold, usually 2/3 or 80% is a good value.
// touchpad0.Threshold = (uint)(touchpad0.CalibrationData * 2 / 3);
// Optional, you can setup a filter for ESP32
// TestFilterEsp();
// TestFilterS2();
Console.WriteLine("Testing timer mode for 20 seconds, touch the pad to generate events");
TouchPad.MeasurementMode = MeasurementMode.Timer;
touchpad0.ValueChanged += TouchpadValueChanged;
Thread.Sleep(20_000);
Console.WriteLine("Testing manual mode for 20 seconds");
TouchPad.MeasurementMode = MeasurementMode.Software;
int cnt = 0;
uint val;
while (cnt++ < 100)
{
val = touchpad0.Read();
Console.WriteLine($"Val: {val}");
Thread.Sleep(200);
}
// Open a second one
Console.WriteLine($"Opening a second touchpad {TouchPadNumber1}");
TouchPad touchpad1 = new(TouchPadNumber1);
touchpad1.ValueChanged += TouchpadValueChanged;
touchpad1.GetCalibrationData();
Console.WriteLine($"Calibration touchpad {touchpad1.TouchPadNumber}: {touchpad1.CalibrationData}");
Console.WriteLine("Testing timer mode for 20 seconds, touch the pads to generate events");
// On S2/S3, the actual read vallues will be higher, so let's use 30% more
touchpad1.Threshold = (uint)(touchpad1.CalibrationData * 1.3);
// On ESP32: Setup a threshold, usually 2/3 or 80% is a good value.
// touchpad1.Threshold = (uint)(touchpad1.CalibrationData * 2 / 3);
TouchPad.MeasurementMode = MeasurementMode.Timer;
Thread.Sleep(20_000);
Console.WriteLine("Manually reading both pads for 20 seconds");
uint val1;
cnt = 0;
while (cnt++ < 100)
{
val = touchpad0.Read();
val1 = touchpad1.Read();
Console.WriteLine($"Val: {val}, val1: {val1}");
Thread.Sleep(200);
}
Console.WriteLine("End of tests!");
Thread.Sleep(Timeout.Infinite);
}
private static void TouchpadValueChanged(object sender, TouchPadEventArgs e)
{
Console.WriteLine($"Touchpad {e.PadNumber} is {(e.Touched ? "touched" : "not touched")}");
}
private static void TestVoltageChange()
{
//// Changing voltage
Console.WriteLine($"Initial Voltage: {TouchPad.TouchHighVoltage} {TouchPad.TouchLowVoltage} {TouchPad.TouchHighVoltageAttenuation}");
Console.WriteLine($"Setting Voltage: {TouchHighVoltage.Volt2V7} {TouchLowVoltage.Volt0V6} {TouchHighVoltageAttenuation.Volt1V0}");
TouchPad.SetVoltage(TouchHighVoltage.Volt2V7, TouchLowVoltage.Volt0V6, TouchHighVoltageAttenuation.Volt1V0);
Console.WriteLine($"New set Voltage: {TouchPad.TouchHighVoltage} {TouchPad.TouchLowVoltage} {TouchPad.TouchHighVoltageAttenuation}");
}
private static void TestTriggerMode()
{
try
{
// static trigger mode, this is ESP32 only
Console.WriteLine($"Initial trigger mode: {TouchPad.TouchTriggerMode}");
TouchPad.TouchTriggerMode = TouchTriggerMode.AboveThreshold;
Console.WriteLine($"New trigger mode: {TouchPad.TouchTriggerMode}");
Console.WriteLine($"Setting new triggermode: {TouchTriggerMode.BellowThreshold}");
TouchPad.TouchTriggerMode = TouchTriggerMode.BellowThreshold;
Console.WriteLine($"New trigger mode: {TouchPad.TouchTriggerMode}");
}
catch (Exception ex)
{
Console.WriteLine($"Only supported in ESP32, not on S2/S3. Exception rasied while changing trigger mode.");
Console.WriteLine(ex.ToString());
}
}
private static void TestWakeupMode()
{
try
{
// Static wakeup source
Console.WriteLine($"Initial wakeup source: {TouchPad.WakeUpSource}");
TouchPad.WakeUpSource = WakeUpSource.OnlySet1;
Console.WriteLine($"New wakeup source: {TouchPad.WakeUpSource}");
Console.WriteLine($"Setting new wakeup source: {WakeUpSource.BothSet1AndSet2}");
TouchPad.WakeUpSource = WakeUpSource.BothSet1AndSet2;
Console.WriteLine($"New wakeup source: {TouchPad.WakeUpSource}");
}
catch (Exception ex)
{
Console.WriteLine($"Only supported in ESP32, not on S2/S3. Exception rasied while changing wakeup mode.");
Console.WriteLine(ex.ToString());
}
}
private static void TestChargeSpeed(TouchPad touchpad)
{
try
{
// Charge speed
var chargeSpeed = touchpad.GetChargeSpeed();
TouchChargeSpeed touchChargeSpeed = chargeSpeed;
Console.WriteLine($"Initial speed: {chargeSpeed.Speed} charge {chargeSpeed.Charge}");
chargeSpeed.Speed = TouchChargeSpeed.ChargeSpeed.Speed3;
chargeSpeed.Charge = TouchChargeSpeed.InitialCharge.High;
Console.WriteLine($"Set speed: {chargeSpeed.Speed} charge {chargeSpeed.Charge}");
touchpad.SetChargeSpeed(chargeSpeed);
chargeSpeed = touchpad.GetChargeSpeed();
Console.WriteLine($"New speed: {chargeSpeed.Speed} charge {chargeSpeed.Charge}");
chargeSpeed.Speed = touchChargeSpeed.Speed;
chargeSpeed.Charge = touchChargeSpeed.Charge;
touchpad.SetChargeSpeed(chargeSpeed);
}
catch (Exception ex)
{
Console.WriteLine("Exception in changing charge speed");
Console.WriteLine(ex.ToString());
}
}
private static void TestThreshold(TouchPad touchpad)
{
try
{
// Changing threashold
Console.WriteLine($"Initial Threshold: {touchpad.Threshold}");
uint oldThreshold = touchpad.Threshold;
Console.WriteLine("Setting new Threshold: 42");
touchpad.Threshold = 42;
Console.WriteLine($"New Threshold is: {touchpad.Threshold}");
Console.WriteLine($"Setting new Threshold: {oldThreshold}");
touchpad.Threshold = oldThreshold;
Console.WriteLine($"New Threshold is: {touchpad.Threshold}");
}
catch (Exception ex)
{
Console.WriteLine("Exception in changing charge speed");
Console.WriteLine(ex.ToString());
}
}
private static void Calibrate(TouchPad touchpad)
{
try
{
Console.WriteLine($"Calibrating touch pad {touchpad.TouchPadNumber}, DO NOT TOUCH it during the process.");
var calib = touchpad.GetCalibrationData();
Console.WriteLine($"calib: {calib} vs Calibration {touchpad.CalibrationData}");
}
catch (Exception ex)
{
Console.WriteLine("Exception raised while calibrating.");
Console.WriteLine(ex.ToString());
}
}
private static void DeepSleepExampleS2S3()
{
const int PadForSleep = 6;
var wakeup = Sleep.GetWakeupCause();
var padNum = Sleep.GetWakeupTouchpad();
Console.WriteLine($"Woke up, cause: {wakeup}, PadNum: {padNum}");
Console.WriteLine("Waiting 10 seconds");
WriteDot(10);
Console.WriteLine($"Setting up sleep mode and calibrating, DO NOT TOUCH the pad, using pad {PadForSleep}");
Sleep.EnableWakeupByTouchPad(PadForSleep, thresholdCoefficient: 90);
Console.WriteLine("Sleeping");
Sleep.StartDeepSleep();
}
private static void DeepSleepExampleEsp32()
{
var wakeup = Sleep.GetWakeupCause();
var padNum = Sleep.GetWakeupTouchpad();
Console.WriteLine($"Woke up, cause: {wakeup}, PadNum: {padNum}");
WriteDot(10);
Console.WriteLine("Setting up sleep mode and calibrating, DO NOT TOUCH the pad, using pad 0.");
Sleep.EnableWakeupByTouchPad(0);
Sleep.StartDeepSleep();
}
private static void DeepSleepExampleEsp32With2Pins()
{
var wakeup = Sleep.GetWakeupCause();
var padNum = Sleep.GetWakeupTouchpad();
Console.WriteLine($"Woke up, cause: {wakeup}, PadNum: {padNum}");
WriteDot(10);
Console.WriteLine("Setting up sleep mode and calibrating, DO NOT TOUCH the pads, using pad 0 and 9.");
Sleep.EnableWakeupByTouchPad(0, 9);
Sleep.StartDeepSleep();
}
private static void WriteDot(int iterations)
{
int cnt = 0;
while (cnt++ < iterations)
{
Console.WriteLine($"{iterations - cnt}");
Thread.Sleep(1000);
}
}
private static void TestMeasurementTime()
{
var meas = TouchPad.GetMeasurementTime();
Console.WriteLine($"Cycles speed: {meas.SleepCycles} meas {meas.MeasurementCycles.TotalMilliseconds} ms");
}
private static void TestDenoise()
{
{
var desnoise = TouchPad.GetDenoise();
Console.WriteLine($"Desnoise: {desnoise.DenoiseRange} {desnoise.DenoiseCapacitance}");
desnoise.DenoiseRange = DenoiseRange.Bit4;
desnoise.DenoiseCapacitance = DenoiseCapacitance.Cap10pf6;
TouchPad.SetDenoise(desnoise);
}
}
private static void TestFilterS2()
{
TouchPad.StartFilter(new S2S3FilterSetting()
{
PeriodeSettingMode = FilterSettingMode.Iir16,
FilterSettingDebounce = FilterSettingDebounce.One,
FilterSettingNoiseThreshold = FilterSettingNoiseThreshold.Low,
JitterSize = 4,
FilterSettingSmoothMode = FilterSettingSmoothMode.Iir2
});
}
private static void TestFilterEsp()
{
TouchPad.StartFilter(new Esp32FilterSetting() { Period = 10 });
}
}
}