fix: drop slow startup ping, log connect time
verify_connection retried DEVICE_INFO pings up to 3x2s, blocking startup for ~6s even when the module just needs more time to come up. The main loop already pings every 5s and shows DEVICE_INFO in the type breakdown, so the upfront check added latency without useful signal. Log how long opening the port took instead.
This commit is contained in:
+13
-20
@@ -5,7 +5,6 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#include "celrs/crsf.h"
|
#include "celrs/crsf.h"
|
||||||
#include "celrs/crsf_param.h"
|
|
||||||
#include "celrs/crsf_telemetry.h"
|
#include "celrs/crsf_telemetry.h"
|
||||||
#include "celrs/logger.h"
|
#include "celrs/logger.h"
|
||||||
#include "celrs/serial.h"
|
#include "celrs/serial.h"
|
||||||
@@ -56,6 +55,11 @@ static void sleep_ms(int ms) {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Milliseconds elapsed since a clock() reading - used to time startup. */
|
||||||
|
static double elapsed_ms(clock_t start) {
|
||||||
|
return (double)(clock() - start) * 1000.0 / CLOCKS_PER_SEC;
|
||||||
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------- */
|
||||||
/* ANSI helpers - all go to stdout so dashboard owns it */
|
/* ANSI helpers - all go to stdout so dashboard owns it */
|
||||||
/* --------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------- */
|
||||||
@@ -422,20 +426,11 @@ static int list_ports(void) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Send a ping and wait for DEVICE_INFO to verify the module responds. */
|
|
||||||
static int verify_connection(cel_serial_port* port) {
|
|
||||||
/* The module may take a few seconds to respond to the first ping,
|
|
||||||
* so retry a few times before giving up. */
|
|
||||||
for (int attempt = 0; attempt < 3; attempt++) {
|
|
||||||
if (cel_crsf_param_ping(port, 2.0f) == 0) return 0;
|
|
||||||
}
|
|
||||||
cel_log_warn("No DEVICE_INFO response - module may not be connected");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char const* argv[]) {
|
int main(int argc, char const* argv[]) {
|
||||||
char const* port_path = NULL;
|
char const* port_path = NULL;
|
||||||
int baud_rate = 0; /* 0 = auto-probe */
|
int baud_rate = 0; /* 0 = auto-probe */
|
||||||
|
char msg[256];
|
||||||
|
clock_t t_startup = clock();
|
||||||
|
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = 1; i < argc; i++) {
|
||||||
if (strcmp(argv[i], "--list") == 0) {
|
if (strcmp(argv[i], "--list") == 0) {
|
||||||
@@ -450,10 +445,13 @@ int main(int argc, char const* argv[]) {
|
|||||||
|
|
||||||
/* Auto-detect port if not specified */
|
/* Auto-detect port if not specified */
|
||||||
if (port_path == NULL) {
|
if (port_path == NULL) {
|
||||||
|
clock_t t = clock();
|
||||||
char detected[256];
|
char detected[256];
|
||||||
if (cel_serial_find_elrs_port(detected, sizeof(detected)) == 0) {
|
if (cel_serial_find_elrs_port(detected, sizeof(detected)) == 0) {
|
||||||
port_path = detected;
|
port_path = detected;
|
||||||
cel_log_info("Auto-detected ELRS port");
|
snprintf(msg, sizeof(msg), "Auto-detected ELRS port (%.0f ms)",
|
||||||
|
elapsed_ms(t));
|
||||||
|
cel_log_info(msg);
|
||||||
} else {
|
} else {
|
||||||
cel_log_err("No ELRS-like port found. Use --list to see ports.");
|
cel_log_err("No ELRS-like port found. Use --list to see ports.");
|
||||||
return 1;
|
return 1;
|
||||||
@@ -477,15 +475,10 @@ int main(int argc, char const* argv[]) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char msg[256];
|
snprintf(msg, sizeof(msg), "Connected to %s (%d baud) in %.0f ms",
|
||||||
snprintf(msg, sizeof(msg), "Connected to %s (%d baud)", port_path, actual_baud);
|
port_path, actual_baud, elapsed_ms(t_startup));
|
||||||
cel_log_info(msg);
|
cel_log_info(msg);
|
||||||
|
|
||||||
/* Verify module responds to CRSF ping */
|
|
||||||
if (verify_connection(port) != 0) {
|
|
||||||
cel_log_warn("Continuing anyway - telemetry may not arrive");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create CRSF stream for incremental parsing */
|
/* Create CRSF stream for incremental parsing */
|
||||||
cel_crsf_stream* stream = cel_crsf_stream_create();
|
cel_crsf_stream* stream = cel_crsf_stream_create();
|
||||||
if (stream == NULL) {
|
if (stream == NULL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user