-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathProgram.cs
207 lines (181 loc) · 6.88 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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(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
});
}
}
}