refactor: make cel_serial_read non-blocking

Drop timeout_ms; read now returns immediately with whatever
data is available (0 if none), so callers don't block the rest
of their loop waiting on serial I/O.

telemetry's poll loop now sleeps interval_ms itself between
empty reads via a small sleep_ms helper.
This commit is contained in:
2026-06-14 20:29:47 +02:00
parent 5324f6e36e
commit a1ea02771c
4 changed files with 26 additions and 8 deletions
+20 -2
View File
@@ -5,6 +5,12 @@
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <time.h>
#endif
/* TX power index to dBm mapping (ELRS standard) */
static int const s_tx_power_dbm[] = {
0, 20, 26, 30, 32, 34, 36, 38,
@@ -31,6 +37,15 @@ static int telemetry_parse_link(int16_t* rssi, uint8_t* link_quality,
return 0;
}
static void sleep_ms(int ms) {
#ifdef _WIN32
Sleep((DWORD)ms);
#else
struct timespec ts = {.tv_sec = ms / 1000, .tv_nsec = (ms % 1000) * 1000000L};
nanosleep(&ts, NULL);
#endif
}
static void print_usage(char const* prog) {
printf("Usage: %s --port <serial_port> [--baudrate <rate>] [interval_ms]\n", prog);
printf(" %s --list\n", prog);
@@ -106,8 +121,11 @@ int main(int argc, char const* argv[]) {
int frames = 0, errors = 0;
for (int i = 0; i < 20; i++) { /* read up to 20 telemetry frames */
size_t n = cel_serial_read(port, buf, sizeof(buf), interval_ms);
if (n == 0) continue;
size_t n = cel_serial_read(port, buf, sizeof(buf));
if (n == 0) {
sleep_ms(interval_ms);
continue;
}
cel_crsf_frame frame;
if (cel_crsf_frame_parse(&frame, buf, n) != 0) {