Skip to content

Commit e9c0049

Browse files
authored
Adding touch events on M5Core2 (#48)
1 parent 9be7714 commit e9c0049

19 files changed

+622
-20
lines changed

README.md

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ These NuGet packages provide a support for M5Stack products:
2828
- [M5StickCPlus](https://docs.m5stack.com/en/core/m5stickc_plus)
2929
- [M5Core2](https://docs.m5stack.com/en/core/core2)
3030

31-
NOTE1: Before trying to add NuGet packages to your projects and/or before flashing the devices (see next section) using MS Visual Studio (VS), open VS > Tools > Options > NuGet Package Manager > Package Sources and make sure that it contains an entry pointing to https://api.nuget.org/v3/index.json , otherwise add it.
32-
NOTE2: When invoking VS > Project > Manage NuGet Packages make sure that in the Package source drop-down menu (right upper corner) "nuget.org" is selected. Also if you're using preview version the "include prerelease" checkbox should be clicked/selected as well.
31+
> Note 1: Before trying to add NuGet packages to your projects and/or before flashing the devices (see next section) using MS Visual Studio (VS), open VS > Tools > Options > NuGet Package Manager > Package Sources and make sure that it contains an entry pointing to <https://api.nuget.org/v3/index.json> , otherwise add it.
32+
> Note 2: When invoking VS > Project > Manage NuGet Packages make sure that in the Package source drop-down menu (right upper corner) "nuget.org" is selected. Also if you're using preview version the "include prerelease" checkbox should be clicked/selected as well.
3333
3434
The NuGets bring support for the screens as well and require to be flashed with the proper image (using [`nanoff`](https://github.com/nanoframework/nanoFirmwareFlasher) dotnet CLI).
3535
On the examples below replace `COM3` with the appropriate number of the COM port to which your device is connected. (on Windows you can check this in the Device Manager).
@@ -58,11 +58,11 @@ For the M5Core2:
5858
nanoff --target M5Core2 --update --preview --serialport COM3
5959
```
6060

61-
NOTE3: If the `nanoff` commands fails, make sure you have followed instruction from NOTE1 above.
61+
> Note 3: If the `nanoff` commands fails, make sure you have followed instruction from Note 1 above.
6262
6363
Once you have the NuGets, you can then enjoy accessing the screen, the accelerometer, get a Grove I2C connecter, add events on the buttons. And you don't even need to think about anything, all is done for you in the most transparent way!
6464

65-
> Note: All the classes that you'll have access are all using the Lazy pattern to be instantiated including the screen. This have the advantage to use as little memory and setup time as possible.
65+
> Note 4: All the classes that you'll have access are all using the Lazy pattern to be instantiated including the screen. This have the advantage to use as little memory and setup time as possible.
6666
6767
In the samples below, we'll use either M5Core or M5Stick as examples, they are all working in a very similar way.
6868

@@ -158,7 +158,82 @@ M5StickC.M5Button.Holding += (sender, e) =>
158158
};
159159
```
160160

161-
> Note: The M5Core2 has touch screen and the buttons are "virtual"". At the time of this writing the .NET nanoFramework team is implementing and testing the touch-screen (touch-panel) functionality (will be released soon).
161+
> Note: The M5Core2 has touch screen and the buttons are "virtual"". See next section to see how to use them.
162+
163+
### M5Core2 touch panel and buttons
164+
165+
The touch panel comes with the screen. Both are initialized and activated at the same time. To get the touch events, you'll have to register to the `TouchEvent` event:
166+
167+
```csharp
168+
M5Core2.InitializeScreen();
169+
M5Core2.TouchEvent += TouchEventCallback;
170+
```
171+
172+
Here is an example on how to check if you are on a button or not and get the various elements:
173+
174+
```csharp
175+
void TouchEventCallback(object sender, TouchEventArgs e)
176+
{
177+
const string StrLB = "LEFT BUTTON PRESSED ";
178+
const string StrMB = "MIDDLE BUTTON PRESSED ";
179+
const string StrRB = "RIGHT BUTTON PRESSED ";
180+
const string StrXY1 = "TOUCHED at X= ";
181+
const string StrXY2 = ",Y= ";
182+
const string StrID = ",Id= ";
183+
const string StrDoubleTouch = "Double touch. ";
184+
const string StrMove = "Moving... ";
185+
const string StrLiftUp = "Lift up. ";
186+
187+
Debug.WriteLine($"Touch Panel Event Received Category= {e.EventCategory} Subcategory= {e.TouchEventCategory}");
188+
Console.CursorLeft = 0;
189+
Console.CursorTop = 0;
190+
191+
Debug.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id);
192+
Console.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id + " ");
193+
194+
if ((e.TouchEventCategory & TouchEventCategory.LeftButton) == TouchEventCategory.LeftButton)
195+
{
196+
Debug.WriteLine(StrLB);
197+
Console.WriteLine(StrLB);
198+
}
199+
else if ((e.TouchEventCategory & TouchEventCategory.MiddleButton) == TouchEventCategory.MiddleButton)
200+
{
201+
Debug.WriteLine(StrMB);
202+
Console.WriteLine(StrMB);
203+
}
204+
else if ((e.TouchEventCategory & TouchEventCategory.RightButton) == TouchEventCategory.RightButton)
205+
{
206+
Debug.WriteLine(StrRB);
207+
Console.WriteLine(StrRB);
208+
}
209+
210+
if ((e.TouchEventCategory & TouchEventCategory.Moving) == TouchEventCategory.Moving)
211+
{
212+
Debug.WriteLine(StrMove);
213+
Console.Write(StrMove);
214+
}
215+
216+
if ((e.TouchEventCategory & TouchEventCategory.LiftUp) == TouchEventCategory.LiftUp)
217+
{
218+
Debug.WriteLine(StrLiftUp);
219+
Console.Write(StrLiftUp);
220+
}
221+
222+
if ((e.TouchEventCategory & TouchEventCategory.DoubleTouch) == TouchEventCategory.DoubleTouch)
223+
{
224+
Debug.WriteLine(StrDoubleTouch);
225+
Console.Write(StrDoubleTouch);
226+
}
227+
228+
Console.WriteLine(" ");
229+
Console.WriteLine(" ");
230+
Console.WriteLine(" ");
231+
}
232+
```
233+
234+
The `TouchEventCategory` enum is a flag and can combine buttons and states. The buttons are mutually exclusive, so you can only have the Left, Middle or Right button, the states are `Moving` and `LiftUp`. `Moving` is happening when a contact has already been made and the touch point is moving. `LiftUp` will appear when the contact is released.
235+
236+
`DoubleTouch` is a specific that let you know there is another contact point happening. Each contact point will receive this flag. The event will be raised 2 times, one for each point. In a double touch context, you may not get the second point `LiftUp` event but you'll get the change with the disappearance of the DoubleTouch flag and the final `LiftUp` on the first point.
162237

163238
### Power management
164239

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>20266750-53f3-46d5-8626-1438ac985033</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>M5Core2TestApp</RootNamespace>
16+
<AssemblyName>M5Core2TestApp</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
<DefineConstants>$(DefineConstants);M5CORE2</DefineConstants>
19+
</PropertyGroup>
20+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
21+
<ItemGroup>
22+
<Compile Include="Program.cs" />
23+
<Compile Include="Properties\AssemblyInfo.cs" />
24+
</ItemGroup>
25+
<ItemGroup>
26+
<Reference Include="Iot.Device.Axp192, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
27+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Axp192.1.0.272\lib\Iot.Device.Axp192.dll</HintPath>
28+
<Private>True</Private>
29+
<SpecificVersion>True</SpecificVersion>
30+
</Reference>
31+
<Reference Include="Iot.Device.Bmm150, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
32+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Bmm150.1.0.272\lib\Iot.Device.Bmm150.dll</HintPath>
33+
<Private>True</Private>
34+
<SpecificVersion>True</SpecificVersion>
35+
</Reference>
36+
<Reference Include="Iot.Device.Button, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
37+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Button.1.0.272\lib\Iot.Device.Button.dll</HintPath>
38+
<Private>True</Private>
39+
<SpecificVersion>True</SpecificVersion>
40+
</Reference>
41+
<Reference Include="Iot.Device.Common.NumberHelper">
42+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Common.NumberHelper.1.0.259\lib\Iot.Device.Common.NumberHelper.dll</HintPath>
43+
</Reference>
44+
<Reference Include="Iot.Device.Mpu6886, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
45+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Mpu6886.1.0.272\lib\Iot.Device.Mpu6886.dll</HintPath>
46+
<Private>True</Private>
47+
<SpecificVersion>True</SpecificVersion>
48+
</Reference>
49+
<Reference Include="Iot.Device.Rtc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
50+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Rtc.1.0.272\lib\Iot.Device.Rtc.dll</HintPath>
51+
<Private>True</Private>
52+
<SpecificVersion>True</SpecificVersion>
53+
</Reference>
54+
<Reference Include="mscorlib">
55+
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.11.7\lib\mscorlib.dll</HintPath>
56+
</Reference>
57+
<Reference Include="nanoFramework.Graphics">
58+
<HintPath>..\..\packages\nanoFramework.Graphics.1.0.2-preview.3\lib\nanoFramework.Graphics.dll</HintPath>
59+
</Reference>
60+
<Reference Include="nanoFramework.Hardware.Esp32">
61+
<HintPath>..\..\packages\nanoFramework.Hardware.Esp32.1.3.5-preview.3\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
62+
</Reference>
63+
<Reference Include="nanoFramework.ResourceManager">
64+
<HintPath>..\..\packages\nanoFramework.ResourceManager.1.1.3\lib\nanoFramework.ResourceManager.dll</HintPath>
65+
</Reference>
66+
<Reference Include="nanoFramework.Runtime.Events">
67+
<HintPath>..\..\packages\nanoFramework.Runtime.Events.1.10.0-preview.1\lib\nanoFramework.Runtime.Events.dll</HintPath>
68+
</Reference>
69+
<Reference Include="nanoFramework.Runtime.Native">
70+
<HintPath>..\..\packages\nanoFramework.Runtime.Native.1.5.2\lib\nanoFramework.Runtime.Native.dll</HintPath>
71+
</Reference>
72+
<Reference Include="nanoFramework.System.Collections">
73+
<HintPath>..\..\packages\nanoFramework.System.Collections.1.3.0\lib\nanoFramework.System.Collections.dll</HintPath>
74+
</Reference>
75+
<Reference Include="System.Buffers.Binary.BinaryPrimitives">
76+
<HintPath>..\..\packages\nanoFramework.System.Buffers.Binary.BinaryPrimitives.1.0.259\lib\System.Buffers.Binary.BinaryPrimitives.dll</HintPath>
77+
</Reference>
78+
<Reference Include="System.Device.Adc">
79+
<HintPath>..\..\packages\nanoFramework.System.Device.Adc.1.0.1\lib\System.Device.Adc.dll</HintPath>
80+
</Reference>
81+
<Reference Include="System.Device.Dac">
82+
<HintPath>..\..\packages\nanoFramework.System.Device.Dac.1.4.2\lib\System.Device.Dac.dll</HintPath>
83+
</Reference>
84+
<Reference Include="System.Device.Gpio">
85+
<HintPath>..\..\packages\nanoFramework.System.Device.Gpio.1.0.3-preview.3\lib\System.Device.Gpio.dll</HintPath>
86+
</Reference>
87+
<Reference Include="System.Device.I2c">
88+
<HintPath>..\..\packages\nanoFramework.System.Device.I2c.1.0.2\lib\System.Device.I2c.dll</HintPath>
89+
</Reference>
90+
<Reference Include="System.Device.Model">
91+
<HintPath>..\..\packages\nanoFramework.System.Device.Model.1.0.259\lib\System.Device.Model.dll</HintPath>
92+
</Reference>
93+
<Reference Include="System.Device.Pwm">
94+
<HintPath>..\..\packages\nanoFramework.System.Device.Pwm.1.0.0\lib\System.Device.Pwm.dll</HintPath>
95+
</Reference>
96+
<Reference Include="System.Device.Spi">
97+
<HintPath>..\..\packages\nanoFramework.System.Device.Spi.1.0.3-preview.3\lib\System.Device.Spi.dll</HintPath>
98+
</Reference>
99+
<Reference Include="System.Diagnostics.Stopwatch">
100+
<HintPath>..\..\packages\nanoFramework.System.Diagnostics.Stopwatch.1.0.259\lib\System.Diagnostics.Stopwatch.dll</HintPath>
101+
</Reference>
102+
<Reference Include="System.IO.Ports, Version=1.0.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
103+
<HintPath>..\..\packages\nanoFramework.System.IO.Ports.1.0.3-preview.6\lib\System.IO.Ports.dll</HintPath>
104+
<Private>True</Private>
105+
<SpecificVersion>True</SpecificVersion>
106+
</Reference>
107+
<Reference Include="System.Math">
108+
<HintPath>..\..\packages\nanoFramework.System.Math.1.4.3\lib\System.Math.dll</HintPath>
109+
</Reference>
110+
<Reference Include="System.Numerics">
111+
<HintPath>..\..\packages\nanoFramework.System.Numerics.1.0.259\lib\System.Numerics.dll</HintPath>
112+
</Reference>
113+
<Reference Include="UnitsNet.ElectricCurrent, Version=4.112.0.0, Culture=neutral, PublicKeyToken=null">
114+
<HintPath>..\..\packages\UnitsNet.nanoFramework.ElectricCurrent.4.112.0\lib\UnitsNet.ElectricCurrent.dll</HintPath>
115+
<Private>True</Private>
116+
<SpecificVersion>True</SpecificVersion>
117+
</Reference>
118+
<Reference Include="UnitsNet.ElectricPotential, Version=4.112.0.0, Culture=neutral, PublicKeyToken=null">
119+
<HintPath>..\..\packages\UnitsNet.nanoFramework.ElectricPotential.4.112.0\lib\UnitsNet.ElectricPotential.dll</HintPath>
120+
<Private>True</Private>
121+
<SpecificVersion>True</SpecificVersion>
122+
</Reference>
123+
<Reference Include="UnitsNet.Power, Version=4.112.0.0, Culture=neutral, PublicKeyToken=null">
124+
<HintPath>..\..\packages\UnitsNet.nanoFramework.Power.4.112.0\lib\UnitsNet.Power.dll</HintPath>
125+
<Private>True</Private>
126+
<SpecificVersion>True</SpecificVersion>
127+
</Reference>
128+
<Reference Include="UnitsNet.Temperature, Version=4.112.0.0, Culture=neutral, PublicKeyToken=null">
129+
<HintPath>..\..\packages\UnitsNet.nanoFramework.Temperature.4.112.0\lib\UnitsNet.Temperature.dll</HintPath>
130+
<Private>True</Private>
131+
<SpecificVersion>True</SpecificVersion>
132+
</Reference>
133+
</ItemGroup>
134+
<ItemGroup>
135+
<None Include="packages.config" />
136+
</ItemGroup>
137+
<ItemGroup>
138+
<ProjectReference Include="..\..\nanoFramework.M5Core2\nanoFramework.M5Core2.nfproj" />
139+
</ItemGroup>
140+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
141+
<ProjectExtensions>
142+
<ProjectCapabilities>
143+
<ProjectConfigurationsDeclaredAsItems />
144+
</ProjectCapabilities>
145+
</ProjectExtensions>
146+
</Project>

Tests/M5Core2TestApp/Program.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using nanoFramework.M5Core2;
5+
using nanoFramework.M5Stack;
6+
using nanoFramework.Runtime.Native;
7+
using System;
8+
using System.Diagnostics;
9+
using System.Threading;
10+
using Console = nanoFramework.M5Stack.Console;
11+
12+
M5Core2.InitializeScreen();
13+
14+
Debug.WriteLine("Hello from M5Core2!");
15+
16+
M5Core2.TouchEvent += TouchEventCallback;
17+
18+
Thread.Sleep(Timeout.Infinite);
19+
20+
void TouchEventCallback(object sender, TouchEventArgs e)
21+
{
22+
const string StrLB = "LEFT BUTTON PRESSED ";
23+
const string StrMB = "MIDDLE BUTTON PRESSED ";
24+
const string StrRB = "RIGHT BUTTON PRESSED ";
25+
const string StrXY1 = "TOUCHED at X= ";
26+
const string StrXY2 = ",Y= ";
27+
const string StrID = ",Id= ";
28+
const string StrDoubleTouch = "Double touch. ";
29+
const string StrMove = "Moving... ";
30+
const string StrLiftUp = "Lift up. ";
31+
32+
Debug.WriteLine($"Touch Panel Event Received Category= {e.EventCategory} Subcategory= {e.TouchEventCategory}");
33+
Console.CursorLeft = 0;
34+
Console.CursorTop = 0;
35+
36+
Debug.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id);
37+
Console.WriteLine(StrXY1 + e.X + StrXY2 + e.Y + StrID + e.Id + " ");
38+
39+
if ((e.TouchEventCategory & TouchEventCategory.LeftButton) == TouchEventCategory.LeftButton)
40+
{
41+
Debug.WriteLine(StrLB);
42+
Console.WriteLine(StrLB);
43+
}
44+
else if ((e.TouchEventCategory & TouchEventCategory.MiddleButton) == TouchEventCategory.MiddleButton)
45+
{
46+
Debug.WriteLine(StrMB);
47+
Console.WriteLine(StrMB);
48+
}
49+
else if ((e.TouchEventCategory & TouchEventCategory.RightButton) == TouchEventCategory.RightButton)
50+
{
51+
Debug.WriteLine(StrRB);
52+
Console.WriteLine(StrRB);
53+
}
54+
55+
if ((e.TouchEventCategory & TouchEventCategory.Moving) == TouchEventCategory.Moving)
56+
{
57+
Debug.WriteLine(StrMove);
58+
Console.Write(StrMove);
59+
}
60+
61+
if ((e.TouchEventCategory & TouchEventCategory.LiftUp) == TouchEventCategory.LiftUp)
62+
{
63+
Debug.WriteLine(StrLiftUp);
64+
Console.Write(StrLiftUp);
65+
}
66+
67+
if ((e.TouchEventCategory & TouchEventCategory.DoubleTouch) == TouchEventCategory.DoubleTouch)
68+
{
69+
Debug.WriteLine(StrDoubleTouch);
70+
Console.Write(StrDoubleTouch);
71+
}
72+
73+
Console.WriteLine(" ");
74+
Console.WriteLine(" ");
75+
Console.WriteLine(" ");
76+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
13+
[assembly: AssemblyCopyright("Copyright © ")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)