Skip to content

Commit d2e6de1

Browse files
authored
Rework touch for TOUGH (#166)
1 parent 9378892 commit d2e6de1

File tree

10 files changed

+75
-70
lines changed

10 files changed

+75
-70
lines changed

M5StackCommon/Core2ToughCommon.cs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Iot.Device.Ft6xx6x;
88
using nanoFramework.M5Core2;
99
#elif TOUGH
10-
using Iot.Device.Chs6540;
10+
using Iot.Device.Chsc6540;
1111
using nanoFramework.Tough;
1212
#endif
1313
using Iot.Device.Axp192;
@@ -21,6 +21,7 @@
2121
using UnitsNet;
2222
using System.Threading;
2323
using nanoFramework.Runtime.Events;
24+
using System.Diagnostics;
2425

2526
namespace nanoFramework.M5Stack
2627
{
@@ -37,14 +38,15 @@ public static partial class Tough
3738
#if M5CORE2
3839
private static Ft6xx6x _touchController;
3940
private static bool _vibrate;
40-
#elif TOUGH
41-
private static Chs6540 _touchController;
42-
private static bool _backLight;
43-
#endif
4441
private static Thread _callbackThread;
4542
private static CancellationTokenSource _cancelThread;
4643
private static CancellationTokenSource _startThread;
4744
private static Point _lastPoint;
45+
#elif TOUGH
46+
private static Chsc6540 _touchController;
47+
private static bool _backLight;
48+
private static TouchEventCategory _touchCategory;
49+
#endif
4850

4951
/// <summary>
5052
/// Touch event handler for the touch event.
@@ -136,7 +138,7 @@ public static Ft6xx6x TouchController
136138
/// <summary>
137139
/// Gets the touch controller.
138140
/// </summary>
139-
public static Chs6540 TouchController
141+
public static Chsc6540 TouchController
140142
{
141143
get
142144
{
@@ -165,22 +167,25 @@ public static void InitializeScreen(int memoryBitMapAllocation = Screen.DefaultM
165167
Console.Font = Resource.GetFont(Resource.FontResources.consolas_regular_16);
166168
#if M5CORE2
167169
_touchController = new(I2cDevice.Create(new I2cConnectionSettings(1, Ft6xx6x.DefaultI2cAddress)));
168-
#elif TOUGH
169-
_touchController = new(I2cDevice.Create(new I2cConnectionSettings(1, Chs6540.DefaultI2cAddress)));
170-
#endif
171-
_touchController.SetInterruptMode(false);
172170
_lastPoint = new();
173171
_cancelThread = new();
174172
_startThread = new();
175173
_callbackThread = new(ThreadTouchCallback);
176174
_callbackThread.Start();
175+
#elif TOUGH
176+
_touchController = new(I2cDevice.Create(new I2cConnectionSettings(1, Chsc6540.DefaultI2cAddress)));
177+
#endif
178+
_touchController.SetInterruptMode(false);
179+
177180
GpioController.OpenPin(TouchPinInterrupt, PinMode.Input);
178181
GpioController.RegisterCallbackForPinValueChangedEvent(TouchPinInterrupt, PinEventTypes.Rising | PinEventTypes.Falling, TouchCallback);
179182
}
180183
}
181184

182185
private static void TouchCallback(object sender, PinValueChangedEventArgs pinValueChangedEventArgs)
183186
{
187+
#if M5CORE2
188+
184189
if (pinValueChangedEventArgs.ChangeType == PinEventTypes.Falling)
185190
{
186191
_cancelThread = new();
@@ -190,6 +195,7 @@ private static void TouchCallback(object sender, PinValueChangedEventArgs pinVal
190195
{
191196
_startThread = new();
192197
_cancelThread.Cancel();
198+
193199
var point = _touchController.GetPoint(true);
194200
if ((_lastPoint.X != point.X) && (_lastPoint.Y != point.Y))
195201
{
@@ -198,8 +204,26 @@ private static void TouchCallback(object sender, PinValueChangedEventArgs pinVal
198204
TouchEvent?.Invoke(_touchController, new TouchEventArgs() { TimeStamp = DateTime.UtcNow, EventCategory = EventCategory.Touch, TouchEventCategory = touchCategory, X = point.X, Y = point.Y, Id = point.TouchId });
199205
}
200206
}
207+
#else
208+
if (pinValueChangedEventArgs.ChangeType == PinEventTypes.Falling)
209+
{
210+
var dp = _touchController.GetDoublePoints();
211+
212+
if(dp.Point1.Event == Event.PressDown)
213+
{
214+
_touchCategory = TouchEventCategory.ScreenTouch;
215+
}
216+
else if(dp.Point1.Event == Event.PressDown)
217+
{
218+
_touchCategory = TouchEventCategory.TouchGone;
219+
}
220+
221+
TouchEvent?.Invoke(_touchController, new TouchEventArgs() { TimeStamp = DateTime.UtcNow, EventCategory = EventCategory.Touch, TouchEventCategory = _touchCategory, X = dp.Point1.X, Y = dp.Point1.Y });
222+
}
223+
#endif
201224
}
202225

226+
#if M5CORE2
203227
private static void ThreadTouchCallback()
204228
{
205229
start:
@@ -210,9 +234,11 @@ private static void ThreadTouchCallback()
210234

211235
int touchNumber;
212236
TouchEventCategory touchCategory;
237+
213238
do
214239
{
215240
touchNumber = _touchController.GetNumberPoints();
241+
216242
if (touchNumber == 1)
217243
{
218244
var point = _touchController.GetPoint(true);
@@ -235,6 +261,7 @@ private static void ThreadTouchCallback()
235261
// This is necessary to give time to the touch sensor
236262
// In theory, the wait should be calculated with the period
237263
_cancelThread.Token.WaitHandle.WaitOne(10, true);
264+
238265
} while (!_cancelThread.IsCancellationRequested);
239266

240267
// If both token are cancelled, we exit. This is in case this won't become static and will have a dispose.
@@ -274,6 +301,7 @@ private static TouchEventCategory CheckIfInButtons(int x, int y, TouchEventCateg
274301

275302
return touchCategory;
276303
}
304+
#endif
277305

278306
#if M5CORE2
279307
static M5Core2()

M5StackCommon/Screen.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public Screen(int memoryBitMapAllocation = DefaultMemoryAllocationBitmap)
6161
_power.Gpio4Value = PinValue.High;
6262
Thread.Sleep(100);
6363

64+
#if TOUGH
65+
// Reset touch controller
66+
_power.Gpio1Value = PinValue.Low;
67+
Thread.Sleep(100);
68+
_power.Gpio1Value = PinValue.High;
69+
Thread.Sleep(100);
70+
#endif
71+
6472
// Create the screen
6573
DisplayControl.Initialize(new SpiConfiguration(2, ChipSelect, DataCommand, Reset, BackLightPin), new ScreenConfiguration(0, 0, 320, 240), (uint)MemoryAllocationBitmap);
6674

M5StackCommon/TouchEventArgs.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ public class TouchEventArgs : EventArgs
3838
/// </summary>
3939
public int Y { get; set; }
4040

41+
#if M5CORE2
4142
/// <summary>
4243
/// Gets or sets the contacty point Id. This is useful in a multi point context.
4344
/// </summary>
4445
public byte Id { get; set; }
45-
46+
#endif
4647
/// <summary>
4748
/// Gets or sets the time stamp.
4849
/// </summary>

M5StackCommon/TouchEventCategory.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@ namespace nanoFramework.M5Core2
1313
#if TOUGH || M5CORE2
1414
{
1515
/// <summary>
16-
/// Sub event touch catgory
16+
/// Sub event touch category.
1717
/// </summary>
18+
#if M5CORE2
1819
[Flags]
20+
#endif
1921
public enum TouchEventCategory
2022
{
21-
/// <summary>Unknown</summary>
23+
/// <summary>Unknown.</summary>
2224
Unknown = 0b0000_0000,
2325

26+
#if M5CORE2
2427
/// <summary>Left Button</summary>
2528
LeftButton = 0b0000_0001,
2629

@@ -38,6 +41,17 @@ public enum TouchEventCategory
3841

3942
/// <summary>Lift Up</summary>
4043
LiftUp = 0b0010_0000,
44+
#else
45+
/// <summary>
46+
/// Screen touched.
47+
/// </summary>
48+
ScreenTouch,
49+
50+
/// <summary>
51+
/// Touch gone.
52+
/// </summary>
53+
TouchGone,
54+
#endif
4155
}
4256
}
4357
#endif

Tests/ToughTestApp/Program.cs

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using nanoFramework.Tough;
54
using nanoFramework.M5Stack;
65
using nanoFramework.Networking;
7-
using nanoFramework.Runtime.Native;
8-
using System;
6+
using nanoFramework.Tough;
97
using System.Diagnostics;
108
using System.Threading;
119
using Console = nanoFramework.M5Stack.Console;
@@ -37,58 +35,14 @@
3735

3836
void TouchEventCallback(object sender, TouchEventArgs e)
3937
{
40-
const string StrLB = "LEFT BUTTON PRESSED ";
41-
const string StrMB = "MIDDLE BUTTON PRESSED ";
42-
const string StrRB = "RIGHT BUTTON PRESSED ";
4338
const string StrXY1 = "TOUCHED at X= ";
4439
const string StrXY2 = ",Y= ";
45-
const string StrID = ",Id= ";
46-
const string StrDoubleTouch = "Double touch. ";
47-
const string StrMove = "Moving... ";
48-
const string StrLiftUp = "Lift up. ";
4940

50-
Debug.WriteLine($"Touch Panel Event Received Category= {e.EventCategory} Subcategory= {e.TouchEventCategory}");
5141
Console.CursorLeft = 0;
5242
Console.CursorTop = 0;
5343

54-
Debug.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id);
55-
Console.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id + " ");
56-
57-
if ((e.TouchEventCategory & TouchEventCategory.LeftButton) == TouchEventCategory.LeftButton)
58-
{
59-
Debug.WriteLine(StrLB);
60-
Console.WriteLine(StrLB);
61-
}
62-
else if ((e.TouchEventCategory & TouchEventCategory.MiddleButton) == TouchEventCategory.MiddleButton)
63-
{
64-
Debug.WriteLine(StrMB);
65-
Console.WriteLine(StrMB);
66-
}
67-
else if ((e.TouchEventCategory & TouchEventCategory.RightButton) == TouchEventCategory.RightButton)
68-
{
69-
Debug.WriteLine(StrRB);
70-
Console.WriteLine(StrRB);
71-
}
72-
73-
if ((e.TouchEventCategory & TouchEventCategory.Moving) == TouchEventCategory.Moving)
74-
{
75-
Debug.WriteLine(StrMove);
76-
Console.Write(StrMove);
77-
}
78-
79-
if ((e.TouchEventCategory & TouchEventCategory.LiftUp) == TouchEventCategory.LiftUp)
80-
{
81-
Debug.WriteLine(StrLiftUp);
82-
Console.Write(StrLiftUp);
83-
}
84-
85-
if ((e.TouchEventCategory & TouchEventCategory.DoubleTouch) == TouchEventCategory.DoubleTouch)
86-
{
87-
Debug.WriteLine(StrDoubleTouch);
88-
Console.Write(StrDoubleTouch);
89-
}
44+
Debug.WriteLine(StrXY1 + e.X + StrXY2 + e.Y);
45+
Console.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + " ");
9046

91-
Console.WriteLine(" ");
92-
Console.WriteLine(" ");
93-
Console.WriteLine(" ");
47+
Console.WriteLine(" ");
9448
}

Tests/ToughTestApp/ToughTestApp.nfproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
<Reference Include="Iot.Device.Axp192">
2626
<HintPath>..\..\packages\nanoFramework.Iot.Device.Axp192.1.1.74.7591\lib\Iot.Device.Axp192.dll</HintPath>
2727
</Reference>
28-
<Reference Include="Iot.Device.Chs6540">
29-
<HintPath>..\..\packages\nanoFramework.Iot.Device.Chs6540.1.0.16\lib\Iot.Device.Chs6540.dll</HintPath>
28+
<Reference Include="Iot.Device.Chsc6540">
29+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Chsc6540.1.0.1.61117\lib\Iot.Device.Chsc6540.dll</HintPath>
3030
</Reference>
3131
<Reference Include="mscorlib">
3232
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>

Tests/ToughTestApp/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnanoframework10" />
44
<package id="nanoFramework.Graphics" version="1.0.3.2" targetFramework="netnanoframework10" />
55
<package id="nanoFramework.Iot.Device.Axp192" version="1.1.74.7591" targetFramework="netnano1.0" />
6-
<package id="nanoFramework.Iot.Device.Chs6540" version="1.0.16" targetFramework="netnanoframework10" />
6+
<package id="nanoFramework.Iot.Device.Chsc6540" version="1.0.1.61117" targetFramework="netnano1.0" />
77
<package id="nanoFramework.ResourceManager" version="1.1.4" targetFramework="netnanoframework10" />
88
<package id="nanoFramework.Runtime.Events" version="1.10.0" targetFramework="netnanoframework10" />
99
<package id="nanoFramework.Runtime.Native" version="1.5.4" targetFramework="netnanoframework10" />

nanoFramework.Tough.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<dependency id="nanoFramework.Graphics" version="1.0.3.2" />
2323
<dependency id="nanoFramework.Hardware.Esp32" version="1.3.6" />
2424
<dependency id="nanoFramework.Iot.Device.Axp192" version="1.1.74.7591" />
25-
<dependency id="nanoFramework.Iot.Device.Chs6540" version="1.0.16" />
25+
<dependency id="nanoFramework.Iot.Device.Chsc6540" version="1.0.1.61117" />
2626
<dependency id="nanoFramework.Iot.Device.Rtc" version="1.1.72.29765" />
2727
<dependency id="nanoFramework.System.Device.Adc" version="1.0.2" />
2828
<dependency id="nanoFramework.System.Device.Dac" version="1.4.3" />

nanoFramework.Tough/nanoFramework.Tough.nfproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
<Reference Include="Iot.Device.Axp192">
3333
<HintPath>..\packages\nanoFramework.Iot.Device.Axp192.1.1.74.7591\lib\Iot.Device.Axp192.dll</HintPath>
3434
</Reference>
35-
<Reference Include="Iot.Device.Chs6540">
36-
<HintPath>..\packages\nanoFramework.Iot.Device.Chs6540.1.0.16\lib\Iot.Device.Chs6540.dll</HintPath>
35+
<Reference Include="Iot.Device.Chsc6540">
36+
<HintPath>..\packages\nanoFramework.Iot.Device.Chsc6540.1.0.1.61117\lib\Iot.Device.Chsc6540.dll</HintPath>
3737
</Reference>
3838
<Reference Include="Iot.Device.Common.NumberHelper">
3939
<HintPath>..\packages\nanoFramework.Iot.Device.Common.NumberHelper.1.1.1\lib\Iot.Device.Common.NumberHelper.dll</HintPath>

nanoFramework.Tough/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<package id="nanoFramework.Graphics" version="1.0.3.2" targetFramework="netnanoframework10" />
55
<package id="nanoFramework.Hardware.Esp32" version="1.3.6" targetFramework="netnanoframework10" />
66
<package id="nanoFramework.Iot.Device.Axp192" version="1.1.74.7591" targetFramework="netnano1.0" />
7-
<package id="nanoFramework.Iot.Device.Chs6540" version="1.0.16" targetFramework="netnanoframework10" />
7+
<package id="nanoFramework.Iot.Device.Chsc6540" version="1.0.1.61117" targetFramework="netnano1.0" />
88
<package id="nanoFramework.Iot.Device.Common.NumberHelper" version="1.1.1" targetFramework="netnanoframework10" />
99
<package id="nanoFramework.Iot.Device.Rtc" version="1.1.72.29765" targetFramework="netnano1.0" />
1010
<package id="nanoFramework.ResourceManager" version="1.1.4" targetFramework="netnanoframework10" />

0 commit comments

Comments
 (0)