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
476 changes: 476 additions & 0 deletions M5StackCommon/Core2ToughCommon.cs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions M5StackCommon/M5StackCommon.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Console.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Core2ToughCommon.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Screen.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ScreenBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TouchEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)TouchEventCategory.cs" />
</ItemGroup>
</Project>
120 changes: 120 additions & 0 deletions M5StackCommon/Screen.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if M5CORE2 || TOUGH
using Iot.Device.Axp192;
using nanoFramework.M5Stack;
using nanoFramework.UI;
using System.Device.Gpio;
using System.Threading;
using UnitsNet;

namespace nanoFramework.M5Stack
{
/// <summary>
#if M5CORE2
/// M5Core2 screen class.
#elif TOUGH
/// M5Tough screen class.
#endif
/// </summary>
public class Screen : ScreenBase
{
/// <summary>
/// Default memory allocation
/// </summary>
public const int DefaultMemoryAllocationBitmap = 320 * 240 * 4;

private const byte DefaultScreenBrightness = 75;
private const int ChipSelect = 5;
private const int DataCommand = 15;
private const int Reset = -1;
private static Axp192 _power;
private static byte _brightness;
private static bool _isInitialized = false;

/// <summary>
/// Initializes the screen
/// </summary>
/// <param name="memoryBitMapAllocation">The memory allocation.</param>
public Screen(int memoryBitMapAllocation = DefaultMemoryAllocationBitmap)
{
if (_isInitialized)
{
return;
}

// We're allocating enough memory for the full screen because these targets have PSRAM
MemoryAllocationBitmap = memoryBitMapAllocation;
// backligth is not controlled by the screen driver
BackLightPin = -1;

#if M5CORE2
_power = M5Stack.M5Core2.Power;
#elif TOUGH
_power = M5Stack.Tough.Power;
#endif

// Reset screen
_power.Gpio4Value = PinValue.Low;
Thread.Sleep(100);
_power.Gpio4Value = PinValue.High;
Thread.Sleep(100);

// Create the screen
DisplayControl.Initialize(new SpiConfiguration(2, ChipSelect, DataCommand, Reset, BackLightPin), new ScreenConfiguration(0, 0, 320, 240), (uint)MemoryAllocationBitmap);

// set initial value for brightness
BrightnessPercentage = DefaultScreenBrightness;

// enable back-light
Enabled = true;

_isInitialized = true;
}

/// <summary>
/// Enables or disables the screen.
/// </summary>
/// <value><see langword="true"/> to enable the screen, <see langword="false"/> to disable it.</value>
public static new bool Enabled
{
get => IsEnabled;

set
{
IsEnabled = value;

#if M5CORE2
_power.EnableDCDC3(IsEnabled);
#elif TOUGH
_power.EnableLDO3(IsEnabled);
#endif
}
}

/// <summary>
/// Gets or sets the screen brightness.
/// </summary>
/// <value>Brightness as percentage.</value>
public static new byte BrightnessPercentage
{
get => _brightness;

set
{
// For M5Core2 and M5Tough, values from 2.5 to 3V are working fine
// 2.5 V = dark, 3.0 V full luminosity
_brightness = (byte)(value > 100 ? 100 : value);
var backLightVoltage = ElectricPotential.FromVolts(_brightness * 0.5 / 100.0 + 2.5);
#if M5CORE2
_power.LDO3OutputVoltage = backLightVoltage;
#elif TOUGH
_power.LDO3OutputVoltage = backLightVoltage;
#endif
}
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if TOUGH || M5CORE2
using nanoFramework.Runtime.Events;
using System;
#endif

#if TOUGH
namespace nanoFramework.Tough
#elif M5CORE2
namespace nanoFramework.M5Core2
#endif
#if TOUGH || M5CORE2
{
/// <summary>
/// Touch event arguments
Expand Down Expand Up @@ -42,3 +49,4 @@ public class TouchEventArgs : EventArgs
public DateTime TimeStamp { get; set; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if TOUGH || M5CORE2
using System;
#endif

#if TOUGH
namespace nanoFramework.Tough
#elif M5CORE2
namespace nanoFramework.M5Core2
#endif
#if TOUGH || M5CORE2
{
/// <summary>
/// Sub event touch catgory
Expand Down Expand Up @@ -33,3 +40,4 @@ public enum TouchEventCategory
LiftUp = 0b0010_0000,
}
}
#endif
2 changes: 1 addition & 1 deletion Tests/M5Core2TestApp/M5Core2TestApp.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,4 @@
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
</Project>
</Project>
2 changes: 1 addition & 1 deletion Tests/M5Core2TestApp/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
<package id="UnitsNet.nanoFramework.ElectricPotential" version="4.132.0" targetFramework="netnanoframework10" />
<package id="UnitsNet.nanoFramework.Power" version="4.132.0" targetFramework="netnanoframework10" />
<package id="UnitsNet.nanoFramework.Temperature" version="4.132.0" targetFramework="netnanoframework10" />
</packages>
</packages>
2 changes: 1 addition & 1 deletion Tests/M5StickTestApp/M5StickTestApp.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
</Project>
</Project>
2 changes: 1 addition & 1 deletion Tests/M5StickTestApp/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
<package id="UnitsNet.nanoFramework.ElectricPotential" version="4.132.0" targetFramework="netnanoframework10" />
<package id="UnitsNet.nanoFramework.Power" version="4.132.0" targetFramework="netnanoframework10" />
<package id="UnitsNet.nanoFramework.Temperature" version="4.132.0" targetFramework="netnanoframework10" />
</packages>
</packages>
94 changes: 94 additions & 0 deletions Tests/ToughTestApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// 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 System.Diagnostics;
using System.Threading;
using Console = nanoFramework.M5Stack.Console;

Tough.InitializeScreen();

Debug.WriteLine("Hello from Tough!");

Console.WriteLine("Hello from Tough!");

const string Ssid = "SSID";
const string Password = "YourWifiPasswordHere";
// Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = WifiNetworkHelper.ConnectDhcp(Ssid, Password, requiresDateTime: true, token: cs.Token);
if (!success)
{
// Something went wrong, you can get details with the ConnectionError property:
Debug.WriteLine($"Can't connect to the network, error: {WifiNetworkHelper.Status}");
if (WifiNetworkHelper.HelperException != null)
{
Debug.WriteLine($"ex: {WifiNetworkHelper.HelperException}");
}
}

Tough.TouchEvent += TouchEventCallback;

Thread.Sleep(Timeout.Infinite);

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);
}

Console.WriteLine(" ");
Console.WriteLine(" ");
Console.WriteLine(" ");
}
33 changes: 33 additions & 0 deletions Tests/ToughTestApp/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CSharp.BlankApplication")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CSharp.BlankApplication")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading