From 66002a0e1d642da85c0777c596316e0dadcfc3da Mon Sep 17 00:00:00 2001 From: smayzy Date: Sun, 3 May 2026 14:40:06 +0200 Subject: [PATCH] add write_uart --- src/main.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main.c b/src/main.c index 4d97d4c..a910e66 100644 --- a/src/main.c +++ b/src/main.c @@ -12,6 +12,7 @@ #include #include #include +#include const int digit_bitmaps[10][5][3] = { {{1,1,1},{1,0,1},{1,0,1},{1,0,1},{1,1,1}}, // 0 @@ -172,6 +173,35 @@ void read_uart(Tel *t, int fd, SerialParser *uart_str) { } } +void write_uart(Tel *t, int fd) { + // speed,rpm,power,eff,bat,tens,amp,warn + char msg[256]; + int len = snprintf(msg, sizeof(msg), "%d,%d,%d,%.1f,%d,%.1f,%.1f,%d\n", + t->speed.data, + t->rpm.data, + t->power.data, + t->eff.data, + t->bat.data, + t->tens.data, + t->amp.data, + t->message.error_level ); + + if (len < 0 || len >= (int)sizeof(msg)) { + return; + } + + int total = 0; + while (total < len) { + int n = write(fd, msg + total, len - total); + if (n < 0) { + if (errno == EINTR) continue; + if (errno == EAGAIN) continue; + return; + } + total += n; + } +} + void calc_data(Tel *t, Bat_state *b, long now) { t->power.data = (int)(t->tens.data * t->amp.data); t->speed.data = (int)(t->rpm.data * 0.017f); @@ -655,6 +685,9 @@ int main(int argc, char **argv) { win_float(&tel.amp, use_color); win_int(&tel.bat, use_color); win_warn(&tel, use_color, now); + + if (use_uart) write_uart(&tel, uart_fd); + t100 = now; } napms(10);