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
Binary file added samples/M5StackRemoteDisplay/M5Example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions samples/M5StackRemoteDisplay/M5StackRemoteDisplay.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Iot.Device.Bindings" Version="3.0.0" />
<PackageReference Include="System.Device.Gpio" Version="3.0.0" />
<PackageReference Include="Iot.Device.Bindings.SkiaSharpAdapter" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="images\Landscape.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="images\MenuBar.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="images\MenuBarLeftMouse.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="images\MenuBarRightMouse.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="images\OpenMenu.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="images\test.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>


</Project>
55 changes: 55 additions & 0 deletions samples/M5StackRemoteDisplay/NmeaDataSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iot.Device.Nmea0183;
using UnitsNet;

namespace Iot.Device.Ili934x.Samples
{
internal abstract class NmeaDataSet
{
public NmeaDataSet(string name)
{
Name = name;
}

public string Name
{
get;
}

public abstract string Value
{
get;
}

public abstract string Unit
{
get;
}

/// <summary>
/// Updates the value from the cache.
/// </summary>
/// <param name="cache">The data cache</param>
/// <param name="tolerance">Allowed data tolerance (for values that will be truncated before display, it's not meaningful
/// to refresh them if only the 9th digit has changed)</param>
/// <returns>True if the value has changed</returns>
public abstract bool Update(SentenceCache cache, double tolerance);

/// <summary>
/// Updates the value from the cache.
/// </summary>
/// <param name="cache">The data cache</param>
/// <returns>True if the value has changed</returns>
public bool Update(SentenceCache cache)
{
return Update(cache, 0);
}
}
}
77 changes: 77 additions & 0 deletions samples/M5StackRemoteDisplay/NmeaValueDataSet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Iot.Device.Nmea0183;
using UnitsNet;

namespace Iot.Device.Ili934x.Samples
{
internal class NmeaValueDataSet : NmeaDataSet
{
private readonly Func<SentenceCache, IQuantity?> _valueFunc;
private readonly string _format;
private IQuantity? _lastValue;

public NmeaValueDataSet(String name, Func<SentenceCache, IQuantity?> valueFunc, string format = "F2")
: base(name)
{
_valueFunc = valueFunc;
_format = format;
_lastValue = null;
}

public override string Value
{
get
{
if (_lastValue == null)
{
return "N/A";
}

return _lastValue.Value.ToString(_format, CultureInfo.CurrentCulture);
}
}

public override string Unit
{
get
{
if (_lastValue == null)
{
return string.Empty;
}

var unitName = _lastValue.Unit;
return unitName.ToString();
}
}

public override bool Update(SentenceCache cache, double tolerance)
{
var newValue = _valueFunc.Invoke(cache);
if (_lastValue != null)
{
if (newValue == null)
{
_lastValue = null;
return true;
}

bool ret = Math.Abs((double)newValue.Value - (double)_lastValue.Value) > tolerance;
_lastValue = newValue;

return ret;
}

_lastValue = newValue;
return newValue != null;
}
}
}
207 changes: 207 additions & 0 deletions samples/M5StackRemoteDisplay/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Device.Gpio;
using System.Device.I2c;
using System.Device.Spi;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Iot.Device.Arduino;
using Iot.Device.Axp192;
using Iot.Device.Common;
using Iot.Device.Ft4222;
using Iot.Device.Graphics;
using Iot.Device.Graphics.SkiaSharpAdapter;
using Iot.Device.Gui;
using Iot.Device.Ili934x;
using Iot.Device.M5Stack;
using UnitsNet;

namespace Iot.Device.Ili934x.Samples
{
internal class Program
{
public static int Main(string[] args)
{
bool isFt4222 = false;
bool isArduino = false;
IPAddress address = IPAddress.None;
SkiaSharpAdapter.Register();
string nmeaSourceAddress = "localhost";

if (args.Length < 2)
{
Console.WriteLine("Are you using Ft4222? Type 'yes' and press ENTER if so, anything else will be treated as no.");
isFt4222 = Console.ReadLine() == "yes";
isArduino = true;

if (!isFt4222)
{
Console.WriteLine("Are you using an Arduino/Firmata? Type 'yes' and press ENTER if so.");
isArduino = Console.ReadLine() == "yes";
}
}
else
{
if (args[0] == "Ft4222")
{
isFt4222 = true;
}
else if (args[0] == "INET" && args.Length >= 2)
{
isArduino = true;
IPAddress[] addr = Array.Empty<IPAddress>();
try
{
addr = Dns.GetHostAddresses(args[1]);
}
catch (SocketException)
{
// Ignore, will be handled below
}

if (addr.Any())
{
address = addr.First();
}
else
{
Console.WriteLine($"Could not resolve host: {args[1]}");
return 1;
}
}

if (args.Any(x => x.Equals("--debug", StringComparison.OrdinalIgnoreCase)))
{
Console.WriteLine("Waiting for debugger...");
while (!Debugger.IsAttached)
{
Thread.Sleep(100);
}
}
}

var idx = Array.IndexOf(args, "--nmeaserver");
if (idx >= 0 && args.Length > idx)
{
nmeaSourceAddress = args[idx + 1];
}

int pinDC = isFt4222 ? 1 : 23;
int pinReset = isFt4222 ? 0 : 24;
int pinLed = isFt4222 ? 2 : -1;

if (isArduino)
{
// Pin mappings for the display in an M5Core2/M5Though
pinDC = 15;
pinReset = -1;
pinLed = -1;
}

LogDispatcher.LoggerFactory = new SimpleConsoleLoggerFactory();
SpiDevice displaySPI;
ArduinoBoard? board = null;
GpioController gpio;
int spiBufferSize = 4096;
M5ToughPowerControl? powerControl = null;
Chsc6440? touch = null;

if (isFt4222)
{
gpio = GetGpioControllerFromFt4222();
displaySPI = GetSpiFromFt4222();
}
else if (isArduino)
{
if (!ArduinoBoard.TryConnectToNetworkedBoard(address, 27016, out board))
{
throw new IOException("Couldn't connect to board");
}

gpio = board.CreateGpioController();
displaySPI = board.CreateSpiDevice(new SpiConnectionSettings(0, 5)
{
ClockFrequency = 50_000_000
});
spiBufferSize = 25;
if (board.GetSystemVariable(SystemVariable.MaxSysexSize, out int maxSize))
{
int maxPayloadSizePerMsg = Encoder7Bit.Num8BitOutBytes(maxSize - 6);
spiBufferSize = maxPayloadSizePerMsg;
}

powerControl = new M5ToughPowerControl(board);
powerControl.EnableSpeaker = false; // With my current firmware, it's used instead of the status led. Noisy!
powerControl.Sleep(false);
}
else
{
gpio = new GpioController();
displaySPI = GetSpiFromDefault();
}

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

if (board != null)
{
touch = new Chsc6440(board.CreateI2cDevice(new I2cConnectionSettings(0, Chsc6440.DefaultI2cAddress)), new Size(display.ScreenWidth, display.ScreenHeight), 39, board.CreateGpioController(), false);
touch.UpdateInterval = TimeSpan.FromMilliseconds(100);
touch.EnableEvents();
}

IPointingDevice touchSimulator;

using ScreenCapture screenCapture = new ScreenCapture();
var size = screenCapture.ScreenSize();
touchSimulator = VirtualPointingDevice.CreateAbsolute(size.Width, size.Height);

using RemoteControl ctrol = new RemoteControl(touch, display, powerControl, touchSimulator, screenCapture, nmeaSourceAddress);
ctrol.DisplayFeatures();

display.ClearScreen(true);
if (powerControl != null)
{
powerControl.SetLcdVoltage(ElectricPotential.Zero);
powerControl.Sleep(true);
}

touch?.Dispose();

display.Dispose();

powerControl?.Dispose();
board?.Dispose();

return 0;
}

private static GpioController GetGpioControllerFromFt4222()
{
return new GpioController(PinNumberingScheme.Logical, new Ft4222Gpio());
}

private static SpiDevice GetSpiFromFt4222()
{
return new Ft4222Spi(new SpiConnectionSettings(0, 1)
{
ClockFrequency = Ili9341.DefaultSpiClockFrequency, Mode = Ili9341.DefaultSpiMode
});
}

private static SpiDevice GetSpiFromDefault()
{
return SpiDevice.Create(new SpiConnectionSettings(0, 0)
{
ClockFrequency = Ili9341.DefaultSpiClockFrequency, Mode = Ili9341.DefaultSpiMode
});
}
}
}
15 changes: 15 additions & 0 deletions samples/M5StackRemoteDisplay/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Mirror the screen to an external display

This larger sample mirrors the screen (or parts of it) from Windows or Linux onto an external, small display. The sample is configured to talk to an M5Though IoT module, a readily available integrated piece of hardware from M5Stack. The device features a QVGA display with touchscreen, a power distribution unit connected to an ESP32 microcontroller.

To run the sample, load the [ConfigurableFirmata](https://github.com/firmata/ConfigurableFirmata) Firmware (V3.0 or later) onto the ESP32 first. The tool has some command line options:

```text
Ft4222 Connect to the screen using an FT4222 instead of an Arduino/ESP32.
INET Connect using a network connection instead of serial (recommended, as it is faster). Provide the IP address as additional argument
--nmeaserver If an NMEA server is also available, the display can show some information from an NMEA stream (e.g. speed)
```

And this then looks as follows:

![M5Though Mirror Display](M5Example.png)
Loading