Skip to content

Commit a93d94a

Browse files
authored
Add M5Stack Tough (#157)
1 parent afed5f1 commit a93d94a

36 files changed

+1340
-396
lines changed

M5StackCommon/Core2ToughCommon.cs

Lines changed: 476 additions & 0 deletions
Large diffs are not rendered by default.

M5StackCommon/M5StackCommon.projitems

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
</PropertyGroup>
1111
<ItemGroup>
1212
<Compile Include="$(MSBuildThisFileDirectory)Console.cs" />
13+
<Compile Include="$(MSBuildThisFileDirectory)Core2ToughCommon.cs" />
14+
<Compile Include="$(MSBuildThisFileDirectory)Screen.cs" />
1315
<Compile Include="$(MSBuildThisFileDirectory)ScreenBase.cs" />
16+
<Compile Include="$(MSBuildThisFileDirectory)TouchEventArgs.cs" />
17+
<Compile Include="$(MSBuildThisFileDirectory)TouchEventCategory.cs" />
1418
</ItemGroup>
1519
</Project>

M5StackCommon/Screen.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
#if M5CORE2 || TOUGH
5+
using Iot.Device.Axp192;
6+
using nanoFramework.M5Stack;
7+
using nanoFramework.UI;
8+
using System.Device.Gpio;
9+
using System.Threading;
10+
using UnitsNet;
11+
12+
namespace nanoFramework.M5Stack
13+
{
14+
/// <summary>
15+
#if M5CORE2
16+
/// M5Core2 screen class.
17+
#elif TOUGH
18+
/// M5Tough screen class.
19+
#endif
20+
/// </summary>
21+
public class Screen : ScreenBase
22+
{
23+
/// <summary>
24+
/// Default memory allocation
25+
/// </summary>
26+
public const int DefaultMemoryAllocationBitmap = 320 * 240 * 4;
27+
28+
private const byte DefaultScreenBrightness = 75;
29+
private const int ChipSelect = 5;
30+
private const int DataCommand = 15;
31+
private const int Reset = -1;
32+
private static Axp192 _power;
33+
private static byte _brightness;
34+
private static bool _isInitialized = false;
35+
36+
/// <summary>
37+
/// Initializes the screen
38+
/// </summary>
39+
/// <param name="memoryBitMapAllocation">The memory allocation.</param>
40+
public Screen(int memoryBitMapAllocation = DefaultMemoryAllocationBitmap)
41+
{
42+
if (_isInitialized)
43+
{
44+
return;
45+
}
46+
47+
// We're allocating enough memory for the full screen because these targets have PSRAM
48+
MemoryAllocationBitmap = memoryBitMapAllocation;
49+
// backligth is not controlled by the screen driver
50+
BackLightPin = -1;
51+
52+
#if M5CORE2
53+
_power = M5Stack.M5Core2.Power;
54+
#elif TOUGH
55+
_power = M5Stack.Tough.Power;
56+
#endif
57+
58+
// Reset screen
59+
_power.Gpio4Value = PinValue.Low;
60+
Thread.Sleep(100);
61+
_power.Gpio4Value = PinValue.High;
62+
Thread.Sleep(100);
63+
64+
// Create the screen
65+
DisplayControl.Initialize(new SpiConfiguration(2, ChipSelect, DataCommand, Reset, BackLightPin), new ScreenConfiguration(0, 0, 320, 240), (uint)MemoryAllocationBitmap);
66+
67+
// set initial value for brightness
68+
BrightnessPercentage = DefaultScreenBrightness;
69+
70+
// enable back-light
71+
Enabled = true;
72+
73+
_isInitialized = true;
74+
}
75+
76+
/// <summary>
77+
/// Enables or disables the screen.
78+
/// </summary>
79+
/// <value><see langword="true"/> to enable the screen, <see langword="false"/> to disable it.</value>
80+
public static new bool Enabled
81+
{
82+
get => IsEnabled;
83+
84+
set
85+
{
86+
IsEnabled = value;
87+
88+
#if M5CORE2
89+
_power.EnableDCDC3(IsEnabled);
90+
#elif TOUGH
91+
_power.EnableLDO3(IsEnabled);
92+
#endif
93+
}
94+
}
95+
96+
/// <summary>
97+
/// Gets or sets the screen brightness.
98+
/// </summary>
99+
/// <value>Brightness as percentage.</value>
100+
public static new byte BrightnessPercentage
101+
{
102+
get => _brightness;
103+
104+
set
105+
{
106+
// For M5Core2 and M5Tough, values from 2.5 to 3V are working fine
107+
// 2.5 V = dark, 3.0 V full luminosity
108+
_brightness = (byte)(value > 100 ? 100 : value);
109+
var backLightVoltage = ElectricPotential.FromVolts(_brightness * 0.5 / 100.0 + 2.5);
110+
#if M5CORE2
111+
_power.LDO3OutputVoltage = backLightVoltage;
112+
#elif TOUGH
113+
_power.LDO3OutputVoltage = backLightVoltage;
114+
#endif
115+
}
116+
}
117+
}
118+
}
119+
120+
#endif

nanoFramework.M5Core2/TouchEventArgs.cs renamed to M5StackCommon/TouchEventArgs.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
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+
#if TOUGH || M5CORE2
45
using nanoFramework.Runtime.Events;
56
using System;
7+
#endif
68

9+
#if TOUGH
10+
namespace nanoFramework.Tough
11+
#elif M5CORE2
712
namespace nanoFramework.M5Core2
13+
#endif
14+
#if TOUGH || M5CORE2
815
{
916
/// <summary>
1017
/// Touch event arguments
@@ -42,3 +49,4 @@ public class TouchEventArgs : EventArgs
4249
public DateTime TimeStamp { get; set; }
4350
}
4451
}
52+
#endif

nanoFramework.M5Core2/TouchEventCategory.cs renamed to M5StackCommon/TouchEventCategory.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
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+
#if TOUGH || M5CORE2
45
using System;
6+
#endif
57

8+
#if TOUGH
9+
namespace nanoFramework.Tough
10+
#elif M5CORE2
611
namespace nanoFramework.M5Core2
12+
#endif
13+
#if TOUGH || M5CORE2
714
{
815
/// <summary>
916
/// Sub event touch catgory
@@ -33,3 +40,4 @@ public enum TouchEventCategory
3340
LiftUp = 0b0010_0000,
3441
}
3542
}
43+
#endif

Tests/M5Core2TestApp/M5Core2TestApp.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,4 @@
172172
<ProjectConfigurationsDeclaredAsItems />
173173
</ProjectCapabilities>
174174
</ProjectExtensions>
175-
</Project>
175+
</Project>

Tests/M5Core2TestApp/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@
3434
<package id="UnitsNet.nanoFramework.ElectricPotential" version="4.132.0" targetFramework="netnanoframework10" />
3535
<package id="UnitsNet.nanoFramework.Power" version="4.132.0" targetFramework="netnanoframework10" />
3636
<package id="UnitsNet.nanoFramework.Temperature" version="4.132.0" targetFramework="netnanoframework10" />
37-
</packages>
37+
</packages>

Tests/M5StickTestApp/M5StickTestApp.nfproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@
132132
<ProjectConfigurationsDeclaredAsItems />
133133
</ProjectCapabilities>
134134
</ProjectExtensions>
135-
</Project>
135+
</Project>

Tests/M5StickTestApp/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
<package id="UnitsNet.nanoFramework.ElectricPotential" version="4.132.0" targetFramework="netnanoframework10" />
2525
<package id="UnitsNet.nanoFramework.Power" version="4.132.0" targetFramework="netnanoframework10" />
2626
<package id="UnitsNet.nanoFramework.Temperature" version="4.132.0" targetFramework="netnanoframework10" />
27-
</packages>
27+
</packages>

Tests/ToughTestApp/Program.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.Tough;
5+
using nanoFramework.M5Stack;
6+
using nanoFramework.Networking;
7+
using nanoFramework.Runtime.Native;
8+
using System;
9+
using System.Diagnostics;
10+
using System.Threading;
11+
using Console = nanoFramework.M5Stack.Console;
12+
13+
Tough.InitializeScreen();
14+
15+
Debug.WriteLine("Hello from Tough!");
16+
17+
Console.WriteLine("Hello from Tough!");
18+
19+
const string Ssid = "SSID";
20+
const string Password = "YourWifiPasswordHere";
21+
// Give 60 seconds to the wifi join to happen
22+
CancellationTokenSource cs = new(60000);
23+
var success = WifiNetworkHelper.ConnectDhcp(Ssid, Password, requiresDateTime: true, token: cs.Token);
24+
if (!success)
25+
{
26+
// Something went wrong, you can get details with the ConnectionError property:
27+
Debug.WriteLine($"Can't connect to the network, error: {WifiNetworkHelper.Status}");
28+
if (WifiNetworkHelper.HelperException != null)
29+
{
30+
Debug.WriteLine($"ex: {WifiNetworkHelper.HelperException}");
31+
}
32+
}
33+
34+
Tough.TouchEvent += TouchEventCallback;
35+
36+
Thread.Sleep(Timeout.Infinite);
37+
38+
void TouchEventCallback(object sender, TouchEventArgs e)
39+
{
40+
const string StrLB = "LEFT BUTTON PRESSED ";
41+
const string StrMB = "MIDDLE BUTTON PRESSED ";
42+
const string StrRB = "RIGHT BUTTON PRESSED ";
43+
const string StrXY1 = "TOUCHED at X= ";
44+
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. ";
49+
50+
Debug.WriteLine($"Touch Panel Event Received Category= {e.EventCategory} Subcategory= {e.TouchEventCategory}");
51+
Console.CursorLeft = 0;
52+
Console.CursorTop = 0;
53+
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+
}
90+
91+
Console.WriteLine(" ");
92+
Console.WriteLine(" ");
93+
Console.WriteLine(" ");
94+
}

0 commit comments

Comments
 (0)