diff --git a/src/main.c b/src/main.c index e69de29..2293a6d 100644 --- a/src/main.c +++ b/src/main.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include + +int main() { + int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NONBLOCK); + if (fd < 0) { + perror("open error"); + return 1; + } + + struct termios tty; + tcgetattr(fd, &tty); + + tty.c_cflag &= ~CSIZE; // clear bit size + tty.c_cflag |= (CLOCAL | CREAD | CS8 | B115200); + + tty.c_lflag = 0; // raw mode + tty.c_iflag = 0; + tty.c_oflag = 0; + + tcsetattr(fd, TCSAFLUSH, &tty); // aplly settings + + char linebuf[512]; + int linepos = 0; + char buf[128]; + float v = 0.0f, a = 0.0f; + while (1) { + int n = read(fd, buf, sizeof(buf)); + if (n > 0) { + for (int i = 0; i < n; i++) { + char c = buf[i]; + + if (linepos < (int)sizeof(linebuf) - 1) { + linebuf[linepos++] = c; + } + + if (c == '\n') { + linebuf[linepos] = '\0'; + + float tmp_v, tmp_a; + + // expected format: "12.34,56.78\n" + if (sscanf(linebuf, "%f,%f", &tmp_v, &tmp_a) == 2) { + v = tmp_v; + a = tmp_a; + printf("latest: %f %f\n", v, a); + } + linepos = 0; // clear buffer + } + } + } + + usleep(1000); + } + close(fd); +}