Skip to content

Commit 9021ade

Browse files
pgrawehrkrwq
andauthored
Improvements to the M5 Stack Sample (#2392)
* Improvements to the M5 Stack Sample * Bugfixing * The SentenceCache now forgets about object that are too old. * Stability fixes * Allow selecting the port * Replay is partially broken still * Different problems with real-time decoding addressed * Tests should now finally pass again * Simplify project, to be able to use latest updates * Otherwise, the project can only build against previous package version in release mode. * New PGRME message, and some documentation fixes * More new sentences and tests * Add new messages: DBT and VLW * Update src/devices/Nmea0183/LoggingConfiguration.cs Co-authored-by: Krzysztof Wicher <[email protected]> * SeaSmartFluidLevel tested and fixed * Fix unit tests --------- Co-authored-by: Krzysztof Wicher <[email protected]>
1 parent 08f6f83 commit 9021ade

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1357
-245
lines changed

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<MicrosoftExtensionsLoggingConsolePackageVersion>8.0.0</MicrosoftExtensionsLoggingConsolePackageVersion>
1818
<MicrosoftExtensionsLoggingPackageVersion>8.0.0</MicrosoftExtensionsLoggingPackageVersion>
1919
<MicrosoftExtensionsLoggingAbstractionsPackageVersion>8.0.0</MicrosoftExtensionsLoggingAbstractionsPackageVersion>
20-
<SystemTextJsonPackageVersion>8.0.4</SystemTextJsonPackageVersion>
20+
<SystemTextJsonPackageVersion>8.0.5</SystemTextJsonPackageVersion>
2121
<SystemIOPipelinesVersion>8.0.0</SystemIOPipelinesVersion>
2222
</PropertyGroup>
2323
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 CommandLine;
5+
6+
namespace Iot.Device.Ili934x.Samples
7+
{
8+
internal class Arguments
9+
{
10+
public Arguments()
11+
{
12+
M5Address = string.Empty;
13+
NmeaServer = string.Empty;
14+
}
15+
16+
[Option('d', "debug", Default = false, HelpText = "Wait for debugger to attach")]
17+
public bool Debug { get; set; }
18+
19+
[Option("m5address", HelpText = "Address of the M5 Tough / M5 Core2 device")]
20+
public string M5Address { get; set; }
21+
22+
[Option("ft4222", Default = false, HelpText = "True to use an FT4222 interface")]
23+
public bool IsFt4222 { get; set; }
24+
25+
[Option("nmeaserver", HelpText = "Address of NMEA Server")]
26+
public string NmeaServer { get; set; }
27+
28+
[Option("nmeaport", HelpText = "NMEA TCP Server port", Default = 10110)]
29+
public int NmeaPort { get; set; }
30+
31+
[Option("nosleep", Default = false, HelpText = "Do not send the device to sleep when process terminates")]
32+
public bool NoSleep { get; set; }
33+
34+
[Option("flipscreen", Default = false, HelpText = "Rotates the screen by 180 degrees")]
35+
public bool FlipScreen { get; set; }
36+
}
37+
}

samples/M5StackRemoteDisplay/M5StackRemoteDisplay.csproj

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,31 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<!-- Defaults to false by base Directory.Builds.props. The name is a bit confusing, as it
10+
does _not_ include the project references if it is set to true-->
11+
<PreferPackageReference>true</PreferPackageReference>
12+
</PropertyGroup>
713

814
<ItemGroup>
9-
<PackageReference Include="Iot.Device.Bindings" Version="3.1.0" />
10-
<PackageReference Include="System.Device.Gpio" Version="3.1.0" />
11-
<PackageReference Include="Iot.Device.Bindings.SkiaSharpAdapter" Version="3.1.0" />
15+
<ProjectReference Include="..\..\src\devices\Common\Common.csproj" />
16+
<ProjectReference Include="..\..\src\devices\Board\Board.csproj" />
17+
<ProjectReference Include="..\..\src\devices\Nmea0183\Nmea0183.csproj" />
18+
<ProjectReference Include="..\..\src\devices\Arduino\Arduino.csproj" />
19+
<ProjectReference Include="..\..\src\devices\M5Stack\M5Stack.csproj" />
20+
<ProjectReference Include="..\..\src\devices\Gui\Gui.csproj" />
21+
<ProjectReference Include="..\..\src\devices\Ft4222\Ft4222.csproj" />
22+
<ProjectReference Include="..\..\src\devices\Ili934x\Ili934x.csproj" />
23+
<ProjectReference Include="..\..\src\devices\SkiaSharpAdapter\SkiaSharpAdapter.csproj" />
1224
</ItemGroup>
1325

1426
<ItemGroup>
15-
<None Update="images\Landscape.png">
16-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17-
</None>
18-
<None Update="images\MenuBar.png">
19-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
20-
</None>
21-
<None Update="images\MenuBarLeftMouse.png">
22-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23-
</None>
24-
<None Update="images\MenuBarRightMouse.png">
25-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
26-
</None>
27-
<None Update="images\OpenMenu.png">
28-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
29-
</None>
30-
<None Update="images\test.png">
27+
<PackageReference Include="CommandLineParser" Version="2.9.1" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<None Update="images\*.png">
3132
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3233
</None>
3334
</ItemGroup>

samples/M5StackRemoteDisplay/NmeaDataSet.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,25 @@ public NmeaDataSet(string name)
1818
Name = name;
1919
}
2020

21+
/// <summary>
22+
/// The name of the data item
23+
/// </summary>
2124
public string Name
2225
{
2326
get;
2427
}
2528

29+
/// <summary>
30+
/// The current value of the data item
31+
/// </summary>
2632
public abstract string Value
2733
{
2834
get;
2935
}
3036

37+
/// <summary>
38+
/// The printable unit string of the data item
39+
/// </summary>
3140
public abstract string Unit
3241
{
3342
get;

samples/M5StackRemoteDisplay/NmeaValueDataSet.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Iot.Device.Nmea0183;
1111
using UnitsNet;
12+
using UnitsNet.Units;
1213

1314
namespace Iot.Device.Ili934x.Samples
1415
{
@@ -35,6 +36,12 @@ public override string Value
3536
return "N/A";
3637
}
3738

39+
if (_lastValue.QuantityInfo.UnitType == typeof(DurationUnit))
40+
{
41+
Duration d = (Duration)_lastValue;
42+
return d.ToTimeSpan().ToString(@"hh\:mm\:ss", CultureInfo.CurrentCulture);
43+
}
44+
3845
return _lastValue.Value.ToString(_format, CultureInfo.CurrentCulture);
3946
}
4047
}
@@ -48,11 +55,22 @@ public override string Unit
4855
return string.Empty;
4956
}
5057

51-
var unitName = _lastValue.Unit;
52-
return unitName.ToString();
58+
return UserUnitName(_lastValue.Unit);
5359
}
5460
}
5561

62+
private string UserUnitName(Enum unit)
63+
{
64+
return unit switch
65+
{
66+
RotationalSpeedUnit.RevolutionPerMinute => "RPM",
67+
AngleUnit.Degree => "Degrees",
68+
SpeedUnit.Knot => "Knots",
69+
LengthUnit.NauticalMile => "NM",
70+
_ => unit.ToString()
71+
};
72+
}
73+
5674
public override bool Update(SentenceCache cache, double tolerance)
5775
{
5876
var newValue = _valueFunc.Invoke(cache);

samples/M5StackRemoteDisplay/Program.cs

Lines changed: 59 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
using System.Device.Spi;
88
using System.Diagnostics;
99
using System.Drawing;
10+
using System.Globalization;
1011
using System.IO;
1112
using System.Linq;
1213
using System.Net;
1314
using System.Net.Sockets;
1415
using System.Threading;
1516
using System.Threading.Tasks;
17+
using CommandLine;
1618
using Iot.Device.Arduino;
1719
using Iot.Device.Axp192;
1820
using Iot.Device.Common;
@@ -30,75 +32,43 @@ internal class Program
3032
{
3133
public static int Main(string[] args)
3234
{
33-
bool isFt4222 = false;
34-
bool isArduino = false;
35-
IPAddress address = IPAddress.None;
3635
SkiaSharpAdapter.Register();
37-
string nmeaSourceAddress = "localhost";
3836

39-
if (args.Length < 2)
37+
var parser = new Parser(x =>
4038
{
41-
Console.WriteLine("Are you using Ft4222? Type 'yes' and press ENTER if so, anything else will be treated as no.");
42-
isFt4222 = Console.ReadLine() == "yes";
43-
isArduino = true;
39+
x.AutoHelp = true;
40+
x.AutoVersion = true;
41+
x.CaseInsensitiveEnumValues = true;
42+
x.ParsingCulture = CultureInfo.InvariantCulture;
43+
x.CaseSensitive = false;
44+
x.HelpWriter = Console.Out;
45+
});
4446

45-
if (!isFt4222)
46-
{
47-
Console.WriteLine("Are you using an Arduino/Firmata? Type 'yes' and press ENTER if so.");
48-
isArduino = Console.ReadLine() == "yes";
49-
}
50-
}
51-
else
52-
{
53-
if (args[0] == "Ft4222")
54-
{
55-
isFt4222 = true;
56-
}
57-
else if (args[0] == "INET" && args.Length >= 2)
58-
{
59-
isArduino = true;
60-
IPAddress[] addr = Array.Empty<IPAddress>();
61-
try
62-
{
63-
addr = Dns.GetHostAddresses(args[1]);
64-
}
65-
catch (SocketException)
66-
{
67-
// Ignore, will be handled below
68-
}
69-
70-
if (addr.Any())
71-
{
72-
address = addr.First();
73-
}
74-
else
75-
{
76-
Console.WriteLine($"Could not resolve host: {args[1]}");
77-
return 1;
78-
}
79-
}
47+
var parsed = parser.ParseArguments<Arguments>(args);
8048

81-
if (args.Any(x => x.Equals("--debug", StringComparison.OrdinalIgnoreCase)))
82-
{
83-
Console.WriteLine("Waiting for debugger...");
84-
while (!Debugger.IsAttached)
85-
{
86-
Thread.Sleep(100);
87-
}
88-
}
49+
if (parsed.Errors.Any())
50+
{
51+
Console.WriteLine("Error in command line");
52+
return 1;
8953
}
9054

91-
var idx = Array.IndexOf(args, "--nmeaserver");
92-
if (idx >= 0 && args.Length > idx)
55+
Arguments parsedArguments = parsed.Value;
56+
57+
if (parsedArguments.Debug)
9358
{
94-
nmeaSourceAddress = args[idx + 1];
59+
Console.WriteLine("Waiting for debugger...");
60+
while (!Debugger.IsAttached)
61+
{
62+
Thread.Sleep(100);
63+
}
9564
}
65+
9666

97-
int pinDC = isFt4222 ? 1 : 23;
98-
int pinReset = isFt4222 ? 0 : 24;
99-
int pinLed = isFt4222 ? 2 : -1;
67+
int pinDC = parsedArguments.IsFt4222 ? 1 : 23;
68+
int pinReset = parsedArguments.IsFt4222 ? 0 : 24;
69+
int pinLed = parsedArguments.IsFt4222 ? 2 : -1;
10070

101-
if (isArduino)
71+
if (!parsedArguments.IsFt4222)
10272
{
10373
// Pin mappings for the display in an M5Core2/M5Though
10474
pinDC = 15;
@@ -114,13 +84,34 @@ public static int Main(string[] args)
11484
M5ToughPowerControl? powerControl = null;
11585
Chsc6440? touch = null;
11686

117-
if (isFt4222)
87+
if (parsedArguments.IsFt4222)
11888
{
11989
gpio = GetGpioControllerFromFt4222();
12090
displaySPI = GetSpiFromFt4222();
12191
}
122-
else if (isArduino)
92+
else
12393
{
94+
IPAddress[] addr = Array.Empty<IPAddress>();
95+
try
96+
{
97+
addr = Dns.GetHostAddresses(parsedArguments.M5Address);
98+
}
99+
catch (SocketException)
100+
{
101+
// Ignore, will be handled below
102+
}
103+
104+
IPAddress address;
105+
if (addr.Any())
106+
{
107+
address = addr.First();
108+
}
109+
else
110+
{
111+
Console.WriteLine($"Could not resolve host: {parsedArguments.M5Address}");
112+
return 1;
113+
}
114+
124115
if (!ArduinoBoard.TryConnectToNetworkedBoard(address, 27016, out board))
125116
{
126117
throw new IOException("Couldn't connect to board");
@@ -142,17 +133,13 @@ public static int Main(string[] args)
142133
powerControl.EnableSpeaker = false; // With my current firmware, it's used instead of the status led. Noisy!
143134
powerControl.Sleep(false);
144135
}
145-
else
146-
{
147-
gpio = new GpioController();
148-
displaySPI = GetSpiFromDefault();
149-
}
150136

151137
Ili9342 display = new Ili9342(displaySPI, pinDC, pinReset, backlightPin: pinLed, gpioController: gpio, spiBufferSize: spiBufferSize, shouldDispose: false);
152138

153139
if (board != null)
154140
{
155-
touch = new Chsc6440(board.CreateI2cDevice(new I2cConnectionSettings(0, Chsc6440.DefaultI2cAddress)), new Size(display.ScreenWidth, display.ScreenHeight), 39, board.CreateGpioController(), false);
141+
touch = new Chsc6440(board.CreateI2cDevice(new I2cConnectionSettings(0, Chsc6440.DefaultI2cAddress)),
142+
new Size(display.ScreenWidth, display.ScreenHeight), parsedArguments.FlipScreen, 39, board.CreateGpioController(), false);
156143
touch.UpdateInterval = TimeSpan.FromMilliseconds(100);
157144
touch.EnableEvents();
158145
}
@@ -163,14 +150,17 @@ public static int Main(string[] args)
163150
var size = screenCapture.ScreenSize();
164151
touchSimulator = VirtualPointingDevice.CreateAbsolute(size.Width, size.Height);
165152

166-
using RemoteControl ctrol = new RemoteControl(touch, display, powerControl, touchSimulator, screenCapture, nmeaSourceAddress);
167-
ctrol.DisplayFeatures();
153+
using RemoteControl ctrol = new RemoteControl(touch, display, powerControl, touchSimulator, screenCapture, parsedArguments);
154+
ctrol.Run();
168155

169156
display.ClearScreen(true);
170157
if (powerControl != null)
171158
{
172159
powerControl.SetLcdVoltage(ElectricPotential.Zero);
173-
powerControl.Sleep(true);
160+
if (!parsedArguments.NoSleep)
161+
{
162+
powerControl.Sleep(true);
163+
}
174164
}
175165

176166
touch?.Dispose();

0 commit comments

Comments
 (0)