diff --git a/M5StackCommon/Core2ToughCommon.cs b/M5StackCommon/Core2ToughCommon.cs index 7c2a6959..2819aea1 100644 --- a/M5StackCommon/Core2ToughCommon.cs +++ b/M5StackCommon/Core2ToughCommon.cs @@ -7,7 +7,7 @@ using Iot.Device.Ft6xx6x; using nanoFramework.M5Core2; #elif TOUGH -using Iot.Device.Chs6540; +using Iot.Device.Chsc6540; using nanoFramework.Tough; #endif using Iot.Device.Axp192; @@ -21,6 +21,7 @@ using UnitsNet; using System.Threading; using nanoFramework.Runtime.Events; +using System.Diagnostics; namespace nanoFramework.M5Stack { @@ -37,14 +38,15 @@ public static partial class Tough #if M5CORE2 private static Ft6xx6x _touchController; private static bool _vibrate; -#elif TOUGH - private static Chs6540 _touchController; - private static bool _backLight; -#endif private static Thread _callbackThread; private static CancellationTokenSource _cancelThread; private static CancellationTokenSource _startThread; private static Point _lastPoint; +#elif TOUGH + private static Chsc6540 _touchController; + private static bool _backLight; + private static TouchEventCategory _touchCategory; +#endif /// /// Touch event handler for the touch event. @@ -136,7 +138,7 @@ public static Ft6xx6x TouchController /// /// Gets the touch controller. /// - public static Chs6540 TouchController + public static Chsc6540 TouchController { get { @@ -165,15 +167,16 @@ public static void InitializeScreen(int memoryBitMapAllocation = Screen.DefaultM Console.Font = Resource.GetFont(Resource.FontResources.consolas_regular_16); #if M5CORE2 _touchController = new(I2cDevice.Create(new I2cConnectionSettings(1, Ft6xx6x.DefaultI2cAddress))); -#elif TOUGH - _touchController = new(I2cDevice.Create(new I2cConnectionSettings(1, Chs6540.DefaultI2cAddress))); -#endif - _touchController.SetInterruptMode(false); _lastPoint = new(); _cancelThread = new(); _startThread = new(); _callbackThread = new(ThreadTouchCallback); _callbackThread.Start(); +#elif TOUGH + _touchController = new(I2cDevice.Create(new I2cConnectionSettings(1, Chsc6540.DefaultI2cAddress))); +#endif + _touchController.SetInterruptMode(false); + GpioController.OpenPin(TouchPinInterrupt, PinMode.Input); GpioController.RegisterCallbackForPinValueChangedEvent(TouchPinInterrupt, PinEventTypes.Rising | PinEventTypes.Falling, TouchCallback); } @@ -181,6 +184,8 @@ public static void InitializeScreen(int memoryBitMapAllocation = Screen.DefaultM private static void TouchCallback(object sender, PinValueChangedEventArgs pinValueChangedEventArgs) { +#if M5CORE2 + if (pinValueChangedEventArgs.ChangeType == PinEventTypes.Falling) { _cancelThread = new(); @@ -190,6 +195,7 @@ private static void TouchCallback(object sender, PinValueChangedEventArgs pinVal { _startThread = new(); _cancelThread.Cancel(); + var point = _touchController.GetPoint(true); if ((_lastPoint.X != point.X) && (_lastPoint.Y != point.Y)) { @@ -198,8 +204,26 @@ private static void TouchCallback(object sender, PinValueChangedEventArgs pinVal TouchEvent?.Invoke(_touchController, new TouchEventArgs() { TimeStamp = DateTime.UtcNow, EventCategory = EventCategory.Touch, TouchEventCategory = touchCategory, X = point.X, Y = point.Y, Id = point.TouchId }); } } +#else + if (pinValueChangedEventArgs.ChangeType == PinEventTypes.Falling) + { + var dp = _touchController.GetDoublePoints(); + + if(dp.Point1.Event == Event.PressDown) + { + _touchCategory = TouchEventCategory.ScreenTouch; + } + else if(dp.Point1.Event == Event.PressDown) + { + _touchCategory = TouchEventCategory.TouchGone; + } + + TouchEvent?.Invoke(_touchController, new TouchEventArgs() { TimeStamp = DateTime.UtcNow, EventCategory = EventCategory.Touch, TouchEventCategory = _touchCategory, X = dp.Point1.X, Y = dp.Point1.Y }); + } +#endif } +#if M5CORE2 private static void ThreadTouchCallback() { start: @@ -210,9 +234,11 @@ private static void ThreadTouchCallback() int touchNumber; TouchEventCategory touchCategory; + do { touchNumber = _touchController.GetNumberPoints(); + if (touchNumber == 1) { var point = _touchController.GetPoint(true); @@ -235,6 +261,7 @@ private static void ThreadTouchCallback() // This is necessary to give time to the touch sensor // In theory, the wait should be calculated with the period _cancelThread.Token.WaitHandle.WaitOne(10, true); + } while (!_cancelThread.IsCancellationRequested); // 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 return touchCategory; } +#endif #if M5CORE2 static M5Core2() diff --git a/M5StackCommon/Screen.cs b/M5StackCommon/Screen.cs index e4a31a4a..d31dc314 100644 --- a/M5StackCommon/Screen.cs +++ b/M5StackCommon/Screen.cs @@ -61,6 +61,14 @@ public Screen(int memoryBitMapAllocation = DefaultMemoryAllocationBitmap) _power.Gpio4Value = PinValue.High; Thread.Sleep(100); +#if TOUGH + // Reset touch controller + _power.Gpio1Value = PinValue.Low; + Thread.Sleep(100); + _power.Gpio1Value = PinValue.High; + Thread.Sleep(100); +#endif + // Create the screen DisplayControl.Initialize(new SpiConfiguration(2, ChipSelect, DataCommand, Reset, BackLightPin), new ScreenConfiguration(0, 0, 320, 240), (uint)MemoryAllocationBitmap); diff --git a/M5StackCommon/TouchEventArgs.cs b/M5StackCommon/TouchEventArgs.cs index 89806fe4..66e540a9 100644 --- a/M5StackCommon/TouchEventArgs.cs +++ b/M5StackCommon/TouchEventArgs.cs @@ -38,11 +38,12 @@ public class TouchEventArgs : EventArgs /// public int Y { get; set; } +#if M5CORE2 /// /// Gets or sets the contacty point Id. This is useful in a multi point context. /// public byte Id { get; set; } - +#endif /// /// Gets or sets the time stamp. /// diff --git a/M5StackCommon/TouchEventCategory.cs b/M5StackCommon/TouchEventCategory.cs index 3520a8b4..1a10327a 100644 --- a/M5StackCommon/TouchEventCategory.cs +++ b/M5StackCommon/TouchEventCategory.cs @@ -13,14 +13,17 @@ namespace nanoFramework.M5Core2 #if TOUGH || M5CORE2 { /// - /// Sub event touch catgory + /// Sub event touch category. /// +#if M5CORE2 [Flags] +#endif public enum TouchEventCategory { - /// Unknown + /// Unknown. Unknown = 0b0000_0000, +#if M5CORE2 /// Left Button LeftButton = 0b0000_0001, @@ -38,6 +41,17 @@ public enum TouchEventCategory /// Lift Up LiftUp = 0b0010_0000, +#else + /// + /// Screen touched. + /// + ScreenTouch, + + /// + /// Touch gone. + /// + TouchGone, +#endif } } #endif \ No newline at end of file diff --git a/Tests/ToughTestApp/Program.cs b/Tests/ToughTestApp/Program.cs index 68fcfd8a..a9967eca 100644 --- a/Tests/ToughTestApp/Program.cs +++ b/Tests/ToughTestApp/Program.cs @@ -1,11 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using nanoFramework.Tough; using nanoFramework.M5Stack; using nanoFramework.Networking; -using nanoFramework.Runtime.Native; -using System; +using nanoFramework.Tough; using System.Diagnostics; using System.Threading; using Console = nanoFramework.M5Stack.Console; @@ -37,58 +35,14 @@ void TouchEventCallback(object sender, TouchEventArgs e) { - const string StrLB = "LEFT BUTTON PRESSED "; - const string StrMB = "MIDDLE BUTTON PRESSED "; - const string StrRB = "RIGHT BUTTON PRESSED "; const string StrXY1 = "TOUCHED at X= "; const string StrXY2 = ",Y= "; - const string StrID = ",Id= "; - const string StrDoubleTouch = "Double touch. "; - const string StrMove = "Moving... "; - const string StrLiftUp = "Lift up. "; - Debug.WriteLine($"Touch Panel Event Received Category= {e.EventCategory} Subcategory= {e.TouchEventCategory}"); Console.CursorLeft = 0; Console.CursorTop = 0; - Debug.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id); - Console.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id + " "); - - if ((e.TouchEventCategory & TouchEventCategory.LeftButton) == TouchEventCategory.LeftButton) - { - Debug.WriteLine(StrLB); - Console.WriteLine(StrLB); - } - else if ((e.TouchEventCategory & TouchEventCategory.MiddleButton) == TouchEventCategory.MiddleButton) - { - Debug.WriteLine(StrMB); - Console.WriteLine(StrMB); - } - else if ((e.TouchEventCategory & TouchEventCategory.RightButton) == TouchEventCategory.RightButton) - { - Debug.WriteLine(StrRB); - Console.WriteLine(StrRB); - } - - if ((e.TouchEventCategory & TouchEventCategory.Moving) == TouchEventCategory.Moving) - { - Debug.WriteLine(StrMove); - Console.Write(StrMove); - } - - if ((e.TouchEventCategory & TouchEventCategory.LiftUp) == TouchEventCategory.LiftUp) - { - Debug.WriteLine(StrLiftUp); - Console.Write(StrLiftUp); - } - - if ((e.TouchEventCategory & TouchEventCategory.DoubleTouch) == TouchEventCategory.DoubleTouch) - { - Debug.WriteLine(StrDoubleTouch); - Console.Write(StrDoubleTouch); - } + Debug.WriteLine(StrXY1 + e.X + StrXY2 + e.Y); + Console.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + " "); - Console.WriteLine(" "); - Console.WriteLine(" "); - Console.WriteLine(" "); + Console.WriteLine(" "); } diff --git a/Tests/ToughTestApp/ToughTestApp.nfproj b/Tests/ToughTestApp/ToughTestApp.nfproj index b3703fb1..783266ea 100644 --- a/Tests/ToughTestApp/ToughTestApp.nfproj +++ b/Tests/ToughTestApp/ToughTestApp.nfproj @@ -25,8 +25,8 @@ ..\..\packages\nanoFramework.Iot.Device.Axp192.1.1.74.7591\lib\Iot.Device.Axp192.dll - - ..\..\packages\nanoFramework.Iot.Device.Chs6540.1.0.16\lib\Iot.Device.Chs6540.dll + + ..\..\packages\nanoFramework.Iot.Device.Chsc6540.1.0.1.61117\lib\Iot.Device.Chsc6540.dll ..\..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll diff --git a/Tests/ToughTestApp/packages.config b/Tests/ToughTestApp/packages.config index 753fab35..ccb578c9 100644 --- a/Tests/ToughTestApp/packages.config +++ b/Tests/ToughTestApp/packages.config @@ -3,7 +3,7 @@ - + diff --git a/nanoFramework.Tough.nuspec b/nanoFramework.Tough.nuspec index 7a18275e..7f974268 100644 --- a/nanoFramework.Tough.nuspec +++ b/nanoFramework.Tough.nuspec @@ -22,7 +22,7 @@ - + diff --git a/nanoFramework.Tough/nanoFramework.Tough.nfproj b/nanoFramework.Tough/nanoFramework.Tough.nfproj index ec721949..4fd15fc5 100644 --- a/nanoFramework.Tough/nanoFramework.Tough.nfproj +++ b/nanoFramework.Tough/nanoFramework.Tough.nfproj @@ -32,8 +32,8 @@ ..\packages\nanoFramework.Iot.Device.Axp192.1.1.74.7591\lib\Iot.Device.Axp192.dll - - ..\packages\nanoFramework.Iot.Device.Chs6540.1.0.16\lib\Iot.Device.Chs6540.dll + + ..\packages\nanoFramework.Iot.Device.Chsc6540.1.0.1.61117\lib\Iot.Device.Chsc6540.dll ..\packages\nanoFramework.Iot.Device.Common.NumberHelper.1.1.1\lib\Iot.Device.Common.NumberHelper.dll diff --git a/nanoFramework.Tough/packages.config b/nanoFramework.Tough/packages.config index 9fd9102a..be0bb8e1 100644 --- a/nanoFramework.Tough/packages.config +++ b/nanoFramework.Tough/packages.config @@ -4,7 +4,7 @@ - +