From cf82ec5071838b6d603a1bbb1adf26cc0e5fa41b Mon Sep 17 00:00:00 2001 From: smayzy Date: Sun, 26 Apr 2026 20:31:43 +0200 Subject: [PATCH] add uart support --- src/main.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 5befb16..917d92e 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,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 @@ -60,6 +61,11 @@ typedef struct{ char title[30]; } StStr; +typedef struct { + char linebuf[128]; + int linepos; +} SerialParser; + typedef struct { StInt speed; StInt rpm; @@ -124,6 +130,31 @@ void read_can(Tel *t, int soc) { } } +void read_uart(Tel *t, int fd, SerialParser *uart_str) { + char buf[128]; + int n = read(fd, buf, sizeof(buf)); + if (n > 0) { + for (int i = 0; i < n; i++) { + char c = buf[i]; + if (c == '\n') { + uart_str->linebuf[uart_str->linepos] = '\0'; + float tmp_v, tmp_a; + if (sscanf(uart_str->linebuf, "%f,%f", &tmp_v, &tmp_a) == 2) { + //t->v = tmp_v; + //t->a = tmp_a; + } + + uart_str->linepos = 0; + } + if (uart_str->linepos < (int)sizeof(uart_str->linebuf) - 1) { + uart_str->linebuf[uart_str->linepos++] = c; + } else { + uart_str->linepos = 0; // drop line to prevent overflow + } + } + } +} + long now_ms(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); @@ -361,11 +392,16 @@ int main(int argc, char **argv) { int fake_data = 0; int use_can = 0; int soc = 0; + int use_uart = 0; + int uart_fd = 0; + struct sockaddr_can addr; struct ifreq ifr; + struct termios uart_str; + SerialParser uart_parser = {0}; - while ((option = getopt(argc, argv, "d:hfc")) !=-1) { + while ((option = getopt(argc, argv, "d:hfcu")) !=-1) { switch (option) { case 'd' : delay = atoi(optarg); @@ -379,8 +415,11 @@ int main(int argc, char **argv) { case 'c' : use_can = 1; break; + case 'u' : + use_uart = 1; + break; case 'h' : - printf("Usage: ./cocomobile-tui [-d int ] [-h ] [-f ] [-c ]\n"); + printf("Usage: ./cocomobile-tui [-d int ] [-h ] [-f ] [-c ] [-u ]\n"); return(0); } } @@ -407,6 +446,25 @@ int main(int argc, char **argv) { } } + if (use_uart) { + uart_fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NONBLOCK); + if (uart_fd < 0) { + perror("uart open error"); + return 1; + } + + tcgetattr(uart_fd, &uart_str); + + uart_str.c_cflag &= ~CSIZE; // clear bit size + uart_str.c_cflag |= (CLOCAL | CREAD | CS8 | B115200); + + uart_str.c_lflag = 0; // raw mode + uart_str.c_iflag = 0; + uart_str.c_oflag = 0; + + tcsetattr(uart_fd, TCSAFLUSH, &uart_str); // aplly settings + } + initscr(); noecho(); cbreak(); @@ -481,6 +539,7 @@ int main(int argc, char **argv) { get_fake_data(&tel); } else { if (use_can) read_can(&tel, soc); + if (use_uart) read_uart(&tel, uart_fd, &uart_parser); } break; }