aboutsummaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-05-29 13:15:58 +0200
committerlonkaars <loek@pipeframe.xyz>2022-05-29 13:15:58 +0200
commit529f067e65b0146c5afa150103809ba5e09869b7 (patch)
tree80fa7a44ce8f5d43b2cc04dc1d0fd1fb79eb254e /client
parentdbd005573295293d35647dc0e9feb2beec48a31c (diff)
client/robot sim connected
Diffstat (limited to 'client')
-rw-r--r--client/main.c30
-rw-r--r--client/makefile3
-rw-r--r--client/serial.h18
-rw-r--r--client/serial_linux.c99
-rw-r--r--client/serial_win32.c10
5 files changed, 159 insertions, 1 deletions
diff --git a/client/main.c b/client/main.c
index 76e8197..d51f30b 100644
--- a/client/main.c
+++ b/client/main.c
@@ -1 +1,29 @@
-int main() { return 0; }
+#include "serial.h"
+
+#include <stdio.h>
+#include <unistd.h>
+
+int main(int argc, char **argv) {
+ if (argc < 2) {
+ printf("usage: %s <serial port>\n", argv[0]);
+ return 1;
+ }
+
+ if (w2_serial_open(argv[1]) == 0) {
+ printf("serial port open fout");
+ return 1;
+ }
+
+ printf("writing...\n");
+ bool success = w2_serial_write("\xff\x14", 2);
+ printf("writing %s\n", success ? "succeeded" : "failed");
+
+ printf("reading...\n");
+ while (1) {
+ int res = w2_serial_read();
+ if (res == -1) continue;
+
+ printf("%02x ", (uint8_t)res);
+ fflush(stdout);
+ }
+}
diff --git a/client/makefile b/client/makefile
index f79dd11..87d06a9 100644
--- a/client/makefile
+++ b/client/makefile
@@ -4,11 +4,14 @@ RM = rm -f
CFLAGS =
EXECNAME = main
+include ../shared/os.mk
+
all: $(EXECNAME)
SOURCES := $(wildcard *.c)
HEADERS := $(wildcard *.h)
# include ../shared/makefile
+
OBJECTS := $(patsubst %.c,%.o, $(SOURCES))
.o:
diff --git a/client/serial.h b/client/serial.h
new file mode 100644
index 0000000..caa3cda
--- /dev/null
+++ b/client/serial.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "../shared/bool.h"
+
+/** @file serial.h */
+
+/**
+ * read byte from serial port
+ *
+ * @return -1 if read fails, else char read
+ */
+int w2_serial_read();
+/** write `data` with length `length` to serial port */
+bool w2_serial_write(char *data, uint8_t length);
+/** open serial port */
+bool w2_serial_open(const char *port_name);
+/** close serial port */
+void w2_serial_close();
diff --git a/client/serial_linux.c b/client/serial_linux.c
new file mode 100644
index 0000000..ae8e646
--- /dev/null
+++ b/client/serial_linux.c
@@ -0,0 +1,99 @@
+#ifdef W2_HOST_LINUX
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <termios.h>
+#include <unistd.h>
+
+#include "../shared/consts.h"
+#include "serial.h"
+
+struct termios g_w2_tty;
+struct termios g_w2_tty_old;
+int g_w2_serial_handle;
+
+char g_w2_serial_buffer[W2_SERIAL_READ_BUFFER_SIZE];
+uint8_t g_w2_serial_buffer_index;
+uint8_t g_w2_serial_buffer_head;
+
+speed_t w2_baud_map(int baud) {
+ switch (baud) {
+ case 9600:
+ return B9600;
+ case 19200:
+ return B19200;
+ case 38400:
+ return B38400;
+ case 57600:
+ return B57600;
+ case 115200:
+ return B115200;
+ case 230400:
+ return B230400;
+ case 460800:
+ return B460800;
+ case 500000:
+ return B500000;
+ case 576000:
+ return B576000;
+ case 921600:
+ return B921600;
+ case 1000000:
+ return B1000000;
+ case 1152000:
+ return B1152000;
+ case 1500000:
+ return B1500000;
+ case 2000000:
+ return B2000000;
+ case 2500000:
+ return B2500000;
+ case 3000000:
+ return B3000000;
+ case 3500000:
+ return B3500000;
+ case 4000000:
+ return B4000000;
+ default:
+ return B0;
+ }
+}
+
+int w2_serial_read() {
+ int return_val;
+ int bytes = read(g_w2_serial_handle, &return_val, 1);
+ return return_val == -1 || bytes != 1 ? -1 : (uint8_t)return_val;
+}
+
+bool w2_serial_write(char *data, uint8_t length) {
+ return write(g_w2_serial_handle, data, length) == length;
+}
+
+bool w2_serial_open(const char *port_name) {
+ g_w2_serial_handle = open(port_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
+ if (g_w2_serial_handle < 0 || tcgetattr(g_w2_serial_handle, &g_w2_tty) != 0) return false;
+
+ g_w2_tty_old = g_w2_tty;
+
+ speed_t baud = w2_baud_map(W2_SERIAL_BAUD);
+ cfsetospeed(&g_w2_tty, baud);
+ cfsetispeed(&g_w2_tty, baud);
+
+ g_w2_tty.c_cflag &= ~(PARENB | CSTOPB | CSIZE | CRTSCTS);
+ g_w2_tty.c_cflag |= CS8 | CREAD | CLOCAL;
+ g_w2_tty.c_cc[VMIN] = 0;
+ g_w2_tty.c_cc[VTIME] = 0;
+
+ cfmakeraw(&g_w2_tty);
+
+ tcflush(g_w2_serial_handle, TCIFLUSH);
+
+ if (tcsetattr(g_w2_serial_handle, TCSANOW, &g_w2_tty) != 0) return false;
+
+ return true;
+}
+
+void w2_serial_close() { close(g_w2_serial_handle); }
+
+#endif
diff --git a/client/serial_win32.c b/client/serial_win32.c
new file mode 100644
index 0000000..9406a34
--- /dev/null
+++ b/client/serial_win32.c
@@ -0,0 +1,10 @@
+#ifdef W2_HOST_WIN32
+
+#include "serial.h"
+
+bool w2_serial_read(uint8_t *target, uint8_t bytes);
+bool w2_serial_write(uint8_t *target, uint8_t bytes);
+void w2_serial_open();
+void w2_serial_close();
+
+#endif