@@ -119,10 +119,8 @@ void draw()
119
119
portIndex = 0 ;
120
120
}
121
121
122
- if ( DEBUG )
123
- {
124
- println ( " Trying next port... index: " + portIndex + " , name: " + Serial . list()[portIndex] );
125
- }
122
+ logMessage( " Trying next port... index: " + portIndex + " , name: " + Serial . list()[portIndex],
123
+ true );
126
124
127
125
attemptConnect( portIndex );
128
126
}
@@ -204,10 +202,7 @@ void serialEvent( Serial ser )
204
202
colorsTemp[j] = COLORMAP . get( arraySub[5 + 3 * j] );
205
203
if ( colorsTemp[j] == 0 )
206
204
{
207
- if ( DEBUG )
208
- {
209
- println ( " Invalid color: " + arraySub[5 + 3 * j] + " , defaulting to green." );
210
- }
205
+ logMessage( " Invalid color: " + arraySub[5 + 3 * j] + " , defaulting to green." , true );
211
206
colorsTemp[j] = COLORMAP . get( " green" );
212
207
}
213
208
concatLabels += labelsTemp[j];
@@ -229,14 +224,11 @@ void serialEvent( Serial ser )
229
224
{
230
225
configCode = arrayMain[0 ];
231
226
lastConfig = millis ();
232
- println ( " Configured " + graphs. size() + " graphs" );
227
+ logMessage ( " Configured " + graphs. size() + " graphs" , false );
233
228
}
234
229
lastLabels = concatLabels;
235
230
236
- if ( DEBUG )
237
- {
238
- println ( " Config code: " + configCode + " , Label config: " + concatLabels );
239
- }
231
+ logMessage( " Config code: " + configCode + " , Label config: " + concatLabels, true );
240
232
}
241
233
else
242
234
{
@@ -267,10 +259,7 @@ void serialEvent( Serial ser )
267
259
}
268
260
catch ( Exception e )
269
261
{
270
- if ( DEBUG )
271
- {
272
- println ( " Exception... " + e. getMessage() );
273
- }
262
+ logMessage( " Exception in serialEvent: " + e. toString(), true );
274
263
}
275
264
}
276
265
@@ -327,37 +316,41 @@ float[][] setupGraphPosition( int numGraphs )
327
316
}
328
317
329
318
void attemptConnect( int index )
330
- {
319
+ {
331
320
// Attempt connect on specified serial port
321
+ if ( index >= Serial . list(). length )
322
+ {
323
+ return ;
324
+ }
332
325
String portName = Serial . list()[portIndex];
333
- println ( " Attempting connect on port: " + portName );
326
+ logMessage ( " Attempting connect on port: " + portName, false );
334
327
335
328
// Wrap Serial port connect in future to force timeout
336
329
ExecutorService exec = Executors . newSingleThreadExecutor();
337
330
Future<Serial > future = exec. submit( new ConnectWithTimeout ( this , portName, BAUD_RATE ) );
338
331
339
332
try
340
333
{
334
+ // Close port if another is open
335
+ if ( port != null && port. active() )
336
+ {
337
+ port. stop();
338
+ }
339
+
341
340
// Do connect with timeout
342
341
port = future. get( CONNECT_TIMEOUT , TimeUnit . MILLISECONDS );
343
342
344
343
lastPortSwitch = millis (); // at end so that we try again immediately on invalid port
345
- println ( " Connected on " + portName + " . Listening for configuration..." );
344
+ logMessage ( " Connected on " + portName + " . Listening for configuration..." , false );
346
345
}
347
346
catch ( TimeoutException e )
348
347
{
349
348
future. cancel( true );
350
- if ( DEBUG )
351
- {
352
- println ( " Timeout." );
353
- }
349
+ logMessage( " Timed out." , true );
354
350
}
355
351
catch ( Exception e )
356
352
{
357
- if ( DEBUG )
358
- {
359
- println ( e. getMessage() );
360
- }
353
+ logMessage( " Exception on connect: " + e. toString(), true );
361
354
}
362
355
363
356
exec. shutdownNow();
@@ -383,3 +376,16 @@ class ConnectWithTimeout implements Callable<Serial>
383
376
return new Serial ( this . parent, this . portName, baudRate );
384
377
}
385
378
}
379
+
380
+ // Logger helper
381
+ void logMessage( String message, boolean debugOnly )
382
+ {
383
+ if ( DEBUG || ! debugOnly )
384
+ {
385
+ String level = debugOnly ? " DEBUG" : " STATUS" ;
386
+ println ( " [Time: " + millis () + " ms]" + " [" + level + " ] " + message );
387
+ }
388
+ }
389
+
390
+
391
+
0 commit comments