77using System . Device . Spi ;
88using System . Diagnostics ;
99using System . Drawing ;
10+ using System . Globalization ;
1011using System . IO ;
1112using System . Linq ;
1213using System . Net ;
1314using System . Net . Sockets ;
1415using System . Threading ;
1516using System . Threading . Tasks ;
17+ using CommandLine ;
1618using Iot . Device . Arduino ;
1719using Iot . Device . Axp192 ;
1820using 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