1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 13:21:23 +01:00

Add optional loop time monitor.

By defining ENABLE_LOOP_MEASUREMENT as true in config.h, the loop measurement will be enabled.  This measures the time between successive executions of the main CS loop to help identify if something is taking too long and holding up the other loop functions.
This commit is contained in:
Neil McKechnie 2021-03-08 15:32:40 +00:00
parent f3d7851467
commit 7a2beda2a9

View File

@ -134,4 +134,31 @@ void loop()
LCD(2,F("Free RAM=%5db"), ramLowWatermark);
}
#endif
// Optionally report average and maximum loop time
#if ENABLE_LOOP_MEASUREMENT
static unsigned long startTime = micros();
static unsigned int maxElapsed = 0;
static unsigned long totalElapsed = 0;
static unsigned long count = 0;
static unsigned long lastOutput = millis();
unsigned long endTime = micros();
unsigned int elapsed = endTime - startTime;
if (elapsed > maxElapsed) maxElapsed = elapsed;
count++;
totalElapsed += elapsed;
unsigned long currentMillis = millis();
if (currentMillis - lastOutput >= 5000) {
DIAG(F("\nLoop: max=%dus ave=%dus\n"), maxElapsed, totalElapsed/count);
maxElapsed = 0;
totalElapsed = 0;
count = 0;
lastOutput = currentMillis;
}
startTime = micros();
#endif
}