Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions M5StackCommon/Core2ToughCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +21,7 @@
using UnitsNet;
using System.Threading;
using nanoFramework.Runtime.Events;
using System.Diagnostics;

namespace nanoFramework.M5Stack
{
Expand All @@ -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

/// <summary>
/// Touch event handler for the touch event.
Expand Down Expand Up @@ -136,7 +138,7 @@ public static Ft6xx6x TouchController
/// <summary>
/// Gets the touch controller.
/// </summary>
public static Chs6540 TouchController
public static Chsc6540 TouchController
{
get
{
Expand Down Expand Up @@ -165,22 +167,25 @@ 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);
}
}

private static void TouchCallback(object sender, PinValueChangedEventArgs pinValueChangedEventArgs)
{
#if M5CORE2

if (pinValueChangedEventArgs.ChangeType == PinEventTypes.Falling)
{
_cancelThread = new();
Expand All @@ -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))
{
Expand All @@ -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:
Expand All @@ -210,9 +234,11 @@ private static void ThreadTouchCallback()

int touchNumber;
TouchEventCategory touchCategory;

do
{
touchNumber = _touchController.GetNumberPoints();

if (touchNumber == 1)
{
var point = _touchController.GetPoint(true);
Expand All @@ -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.
Expand Down Expand Up @@ -274,6 +301,7 @@ private static TouchEventCategory CheckIfInButtons(int x, int y, TouchEventCateg

return touchCategory;
}
#endif

#if M5CORE2
static M5Core2()
Expand Down
8 changes: 8 additions & 0 deletions M5StackCommon/Screen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion M5StackCommon/TouchEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ public class TouchEventArgs : EventArgs
/// </summary>
public int Y { get; set; }

#if M5CORE2
/// <summary>
/// Gets or sets the contacty point Id. This is useful in a multi point context.
/// </summary>
public byte Id { get; set; }

#endif
/// <summary>
/// Gets or sets the time stamp.
/// </summary>
Expand Down
18 changes: 16 additions & 2 deletions M5StackCommon/TouchEventCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ namespace nanoFramework.M5Core2
#if TOUGH || M5CORE2
{
/// <summary>
/// Sub event touch catgory
/// Sub event touch category.
/// </summary>
#if M5CORE2
[Flags]
#endif
public enum TouchEventCategory
{
/// <summary>Unknown</summary>
/// <summary>Unknown.</summary>
Unknown = 0b0000_0000,

#if M5CORE2
/// <summary>Left Button</summary>
LeftButton = 0b0000_0001,

Expand All @@ -38,6 +41,17 @@ public enum TouchEventCategory

/// <summary>Lift Up</summary>
LiftUp = 0b0010_0000,
#else
/// <summary>
/// Screen touched.
/// </summary>
ScreenTouch,

/// <summary>
/// Touch gone.
/// </summary>
TouchGone,
#endif
}
}
#endif
54 changes: 4 additions & 50 deletions Tests/ToughTestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(" ");
}
4 changes: 2 additions & 2 deletions Tests/ToughTestApp/ToughTestApp.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
<Reference Include="Iot.Device.Axp192">
<HintPath>..\..\packages\nanoFramework.Iot.Device.Axp192.1.1.74.7591\lib\Iot.Device.Axp192.dll</HintPath>
</Reference>
<Reference Include="Iot.Device.Chs6540">
<HintPath>..\..\packages\nanoFramework.Iot.Device.Chs6540.1.0.16\lib\Iot.Device.Chs6540.dll</HintPath>
<Reference Include="Iot.Device.Chsc6540">
<HintPath>..\..\packages\nanoFramework.Iot.Device.Chsc6540.1.0.1.61117\lib\Iot.Device.Chsc6540.dll</HintPath>
</Reference>
<Reference Include="mscorlib">
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion Tests/ToughTestApp/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnanoframework10" />
<package id="nanoFramework.Graphics" version="1.0.3.2" targetFramework="netnanoframework10" />
<package id="nanoFramework.Iot.Device.Axp192" version="1.1.74.7591" targetFramework="netnano1.0" />
<package id="nanoFramework.Iot.Device.Chs6540" version="1.0.16" targetFramework="netnanoframework10" />
<package id="nanoFramework.Iot.Device.Chsc6540" version="1.0.1.61117" targetFramework="netnano1.0" />
<package id="nanoFramework.ResourceManager" version="1.1.4" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Events" version="1.10.0" targetFramework="netnanoframework10" />
<package id="nanoFramework.Runtime.Native" version="1.5.4" targetFramework="netnanoframework10" />
Expand Down
2 changes: 1 addition & 1 deletion nanoFramework.Tough.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<dependency id="nanoFramework.Graphics" version="1.0.3.2" />
<dependency id="nanoFramework.Hardware.Esp32" version="1.3.6" />
<dependency id="nanoFramework.Iot.Device.Axp192" version="1.1.74.7591" />
<dependency id="nanoFramework.Iot.Device.Chs6540" version="1.0.16" />
<dependency id="nanoFramework.Iot.Device.Chsc6540" version="1.0.1.61117" />
<dependency id="nanoFramework.Iot.Device.Rtc" version="1.1.72.29765" />
<dependency id="nanoFramework.System.Device.Adc" version="1.0.2" />
<dependency id="nanoFramework.System.Device.Dac" version="1.4.3" />
Expand Down
4 changes: 2 additions & 2 deletions nanoFramework.Tough/nanoFramework.Tough.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<Reference Include="Iot.Device.Axp192">
<HintPath>..\packages\nanoFramework.Iot.Device.Axp192.1.1.74.7591\lib\Iot.Device.Axp192.dll</HintPath>
</Reference>
<Reference Include="Iot.Device.Chs6540">
<HintPath>..\packages\nanoFramework.Iot.Device.Chs6540.1.0.16\lib\Iot.Device.Chs6540.dll</HintPath>
<Reference Include="Iot.Device.Chsc6540">
<HintPath>..\packages\nanoFramework.Iot.Device.Chsc6540.1.0.1.61117\lib\Iot.Device.Chsc6540.dll</HintPath>
</Reference>
<Reference Include="Iot.Device.Common.NumberHelper">
<HintPath>..\packages\nanoFramework.Iot.Device.Common.NumberHelper.1.1.1\lib\Iot.Device.Common.NumberHelper.dll</HintPath>
Expand Down
2 changes: 1 addition & 1 deletion nanoFramework.Tough/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<package id="nanoFramework.Graphics" version="1.0.3.2" targetFramework="netnanoframework10" />
<package id="nanoFramework.Hardware.Esp32" version="1.3.6" targetFramework="netnanoframework10" />
<package id="nanoFramework.Iot.Device.Axp192" version="1.1.74.7591" targetFramework="netnano1.0" />
<package id="nanoFramework.Iot.Device.Chs6540" version="1.0.16" targetFramework="netnanoframework10" />
<package id="nanoFramework.Iot.Device.Chsc6540" version="1.0.1.61117" targetFramework="netnano1.0" />
<package id="nanoFramework.Iot.Device.Common.NumberHelper" version="1.1.1" targetFramework="netnanoframework10" />
<package id="nanoFramework.Iot.Device.Rtc" version="1.1.72.29765" targetFramework="netnano1.0" />
<package id="nanoFramework.ResourceManager" version="1.1.4" targetFramework="netnanoframework10" />
Expand Down