Skip to content

Commit bba1bab

Browse files
committed
Explicitly close previous serial port on scanning. Actual logger utility function for better debugging
1 parent 8c74ef1 commit bba1bab

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

listener/listener.pde

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ void draw()
119119
portIndex = 0;
120120
}
121121

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 );
126124

127125
attemptConnect( portIndex );
128126
}
@@ -204,10 +202,7 @@ void serialEvent( Serial ser )
204202
colorsTemp[j] = COLORMAP.get( arraySub[5 + 3*j] );
205203
if ( colorsTemp[j] == 0 )
206204
{
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 );
211206
colorsTemp[j] = COLORMAP.get( "green" );
212207
}
213208
concatLabels += labelsTemp[j];
@@ -229,14 +224,11 @@ void serialEvent( Serial ser )
229224
{
230225
configCode = arrayMain[0];
231226
lastConfig = millis();
232-
println( "Configured " + graphs.size() + " graphs" );
227+
logMessage( "Configured " + graphs.size() + " graphs", false );
233228
}
234229
lastLabels = concatLabels;
235230

236-
if ( DEBUG )
237-
{
238-
println( "Config code: " + configCode + ", Label config: " + concatLabels );
239-
}
231+
logMessage( "Config code: " + configCode + ", Label config: " + concatLabels, true );
240232
}
241233
else
242234
{
@@ -267,10 +259,7 @@ void serialEvent( Serial ser )
267259
}
268260
catch ( Exception e )
269261
{
270-
if ( DEBUG )
271-
{
272-
println( "Exception... " + e.getMessage() );
273-
}
262+
logMessage( "Exception in serialEvent: " + e.toString(), true );
274263
}
275264
}
276265

@@ -327,37 +316,41 @@ float[][] setupGraphPosition( int numGraphs )
327316
}
328317

329318
void attemptConnect( int index )
330-
{
319+
{
331320
// Attempt connect on specified serial port
321+
if ( index >= Serial.list().length )
322+
{
323+
return;
324+
}
332325
String portName = Serial.list()[portIndex];
333-
println( "Attempting connect on port: " + portName );
326+
logMessage( "Attempting connect on port: " + portName, false );
334327

335328
// Wrap Serial port connect in future to force timeout
336329
ExecutorService exec = Executors.newSingleThreadExecutor();
337330
Future<Serial> future = exec.submit( new ConnectWithTimeout( this, portName, BAUD_RATE ) );
338331

339332
try
340333
{
334+
// Close port if another is open
335+
if ( port != null && port.active() )
336+
{
337+
port.stop();
338+
}
339+
341340
// Do connect with timeout
342341
port = future.get( CONNECT_TIMEOUT, TimeUnit.MILLISECONDS );
343342

344343
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 );
346345
}
347346
catch ( TimeoutException e )
348347
{
349348
future.cancel( true );
350-
if ( DEBUG )
351-
{
352-
println( "Timeout." );
353-
}
349+
logMessage( "Timed out.", true );
354350
}
355351
catch ( Exception e )
356352
{
357-
if ( DEBUG )
358-
{
359-
println( e.getMessage() );
360-
}
353+
logMessage( "Exception on connect: " + e.toString(), true );
361354
}
362355

363356
exec.shutdownNow();
@@ -383,3 +376,16 @@ class ConnectWithTimeout implements Callable<Serial>
383376
return new Serial( this.parent, this.portName, baudRate );
384377
}
385378
}
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

Comments
 (0)