-
Notifications
You must be signed in to change notification settings - Fork 609
New sample using an M5Stack display as a remote controller #2152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
596951c
New sample using an M5Stack display as a remote controller
pgrawehr 31662e0
Update TFM for sample
pgrawehr 47bc958
Fix, combine and test.
pgrawehr 04d63ec
Add documentation
pgrawehr 33843a0
Update README.md
pgrawehr 1c45830
Copy data files to output
pgrawehr 790e1f4
Add nice picture
pgrawehr c3fe7ac
Merge branch 'M5StackSample' of https://github.com/pgrawehr/iot into …
pgrawehr 7978756
Use Debug output instead of Console
pgrawehr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: | ||
|
|
||
|  | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.