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:
+2
-3
@@ -37,12 +37,11 @@ void cel_serial_close(cel_serial_port* port) {
|
|||||||
free(port);
|
free(port);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len, int timeout_ms) {
|
size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len) {
|
||||||
(void)port;
|
(void)port;
|
||||||
(void)buf;
|
(void)buf;
|
||||||
(void)len;
|
(void)len;
|
||||||
(void)timeout_ms;
|
/* TODO: platform-specific non-blocking read */
|
||||||
/* TODO: platform-specific read */
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -15,8 +15,9 @@ cel_serial_port* cel_serial_open(char const* path, int baud_rate);
|
|||||||
/* Close and free the serial port */
|
/* Close and free the serial port */
|
||||||
void cel_serial_close(cel_serial_port* port);
|
void cel_serial_close(cel_serial_port* port);
|
||||||
|
|
||||||
/* Read up to len bytes. Returns bytes read, 0 on timeout/error. */
|
/* Read up to len bytes without blocking.
|
||||||
size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len, int timeout_ms);
|
* Returns bytes read immediately available, or 0 if none/error. */
|
||||||
|
size_t cel_serial_read(cel_serial_port* port, uint8_t* buf, size_t len);
|
||||||
|
|
||||||
/* Write data. Returns bytes written, 0 on error. */
|
/* Write data. Returns bytes written, 0 on error. */
|
||||||
size_t cel_serial_write(cel_serial_port* port, uint8_t const* buf, size_t len);
|
size_t cel_serial_write(cel_serial_port* port, uint8_t const* buf, size_t len);
|
||||||
|
|||||||
+1
-1
@@ -34,7 +34,7 @@ void test_read_returns_zero_stub(void) {
|
|||||||
TEST_ASSERT_NOT_NULL(port);
|
TEST_ASSERT_NOT_NULL(port);
|
||||||
uint8_t buf[16];
|
uint8_t buf[16];
|
||||||
/* Stub implementation returns 0 */
|
/* Stub implementation returns 0 */
|
||||||
size_t n = cel_serial_read(port, buf, sizeof(buf), 100);
|
size_t n = cel_serial_read(port, buf, sizeof(buf));
|
||||||
TEST_ASSERT_EQUAL_UINT(0, n);
|
TEST_ASSERT_EQUAL_UINT(0, n);
|
||||||
cel_serial_close(port);
|
cel_serial_close(port);
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-2
@@ -5,6 +5,12 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#else
|
||||||
|
#include <time.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
/* TX power index to dBm mapping (ELRS standard) */
|
/* TX power index to dBm mapping (ELRS standard) */
|
||||||
static int const s_tx_power_dbm[] = {
|
static int const s_tx_power_dbm[] = {
|
||||||
0, 20, 26, 30, 32, 34, 36, 38,
|
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;
|
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) {
|
static void print_usage(char const* prog) {
|
||||||
printf("Usage: %s --port <serial_port> [--baudrate <rate>] [interval_ms]\n", prog);
|
printf("Usage: %s --port <serial_port> [--baudrate <rate>] [interval_ms]\n", prog);
|
||||||
printf(" %s --list\n", prog);
|
printf(" %s --list\n", prog);
|
||||||
@@ -106,8 +121,11 @@ int main(int argc, char const* argv[]) {
|
|||||||
int frames = 0, errors = 0;
|
int frames = 0, errors = 0;
|
||||||
|
|
||||||
for (int i = 0; i < 20; i++) { /* read up to 20 telemetry frames */
|
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);
|
size_t n = cel_serial_read(port, buf, sizeof(buf));
|
||||||
if (n == 0) continue;
|
if (n == 0) {
|
||||||
|
sleep_ms(interval_ms);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
cel_crsf_frame frame;
|
cel_crsf_frame frame;
|
||||||
if (cel_crsf_frame_parse(&frame, buf, n) != 0) {
|
if (cel_crsf_frame_parse(&frame, buf, n) != 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user