From 7e3ece64cd5138081263413461dc6856a668bfae Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 24 May 2022 21:04:12 +0200 Subject: sim reading serial in from stdin working --- robot/hypervisor.c | 1 + robot/main.c | 9 ++++++++- robot/sercomm.c | 1 + robot/sercomm.h | 2 ++ robot/sim.c | 30 +++++++++++++++++++++++++++++- robot/sim.h | 10 ++++++++-- robot/tests/info.bin | 1 + robot/tests/ping.bin | Bin 0 -> 3 bytes robot/tests/readme.md | 11 +++++++++++ 9 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 robot/tests/info.bin create mode 100644 robot/tests/ping.bin create mode 100644 robot/tests/readme.md diff --git a/robot/hypervisor.c b/robot/hypervisor.c index c08a24c..4d46a12 100644 --- a/robot/hypervisor.c +++ b/robot/hypervisor.c @@ -7,6 +7,7 @@ void w2_hypervisor_main() { #ifdef W2_SIM + w2_sim_cycle_begin(); if (DBG_ENABLE_CYCLEINFO) siminfo("cycle start\n"); #endif diff --git a/robot/main.c b/robot/main.c index fbfd38b..d76dbaf 100644 --- a/robot/main.c +++ b/robot/main.c @@ -1,8 +1,15 @@ #include "main.h" #include "hypervisor.h" #include "setup.h" +#ifdef W2_SIM +#include "sim.h" +#endif + +int main(int argc, char **argv) { +#ifdef W2_SIM + w2_sim_setup(argc, argv); +#endif -int main() { w2_setup_main(); for (;;) w2_hypervisor_main(); diff --git a/robot/sercomm.c b/robot/sercomm.c index d7dc7f1..2faaa27 100644 --- a/robot/sercomm.c +++ b/robot/sercomm.c @@ -12,6 +12,7 @@ uint8_t g_w2_sercomm_buffer_full = 0; char g_w2_serial_buffer[W2_SERIAL_READ_BUFFER_SIZE] = {0}; uint8_t g_w2_serial_buffer_index = 0; +uint8_t g_w2_serial_buffer_head = 0; void w2_sercomm_main() { #ifdef W2_SIM diff --git a/robot/sercomm.h b/robot/sercomm.h index f411374..bc9fc1e 100644 --- a/robot/sercomm.h +++ b/robot/sercomm.h @@ -48,6 +48,8 @@ extern uint8_t g_w2_sercomm_offset; extern char g_w2_serial_buffer[W2_SERIAL_READ_BUFFER_SIZE]; /** serial input (receive) buffer current position */ extern uint8_t g_w2_serial_buffer_index; +/** serial input (receive) buffer head (sim only) */ +extern uint8_t g_w2_serial_buffer_head; /** * serial pc-robot communication module diff --git a/robot/sim.c b/robot/sim.c index 554b174..47c0c78 100644 --- a/robot/sim.c +++ b/robot/sim.c @@ -2,10 +2,15 @@ #include #include #include +#include +#include #include "sim.h" +#include "consts.h" +#include "sercomm.h" struct timespec reference_time; // NOLINT +bool g_w2_sim_headless = false; void time_reset() { simprintfunc("time_reset", ""); @@ -70,6 +75,29 @@ void serial_receive_ring(char* buffer, unsigned char size) { unsigned char serial_get_received_bytes() { simprintfunc("serial_get_received_bytes", ""); - return 0; + return g_w2_serial_buffer_head; +} + +void w2_sim_setup(int argc, char **argv) { + if (argc > 1 && strcmp(argv[1], "headless") == 0) + g_w2_sim_headless = true; + + // disable echo and enable raw mode + struct termios term; + tcgetattr(STDIN_FILENO, &term); + term.c_lflag &= ~(ECHO | ICANON); + term.c_cc[VTIME] = 0; + term.c_cc[VMIN] = 0; + tcsetattr(STDIN_FILENO, 0, &term); + + return; +} + +void w2_sim_cycle_begin() { + // read bytes from stdin + while(read(STDIN_FILENO, (g_w2_serial_buffer + sizeof(char) * g_w2_serial_buffer_head), 1) > 0) + g_w2_serial_buffer_head = (g_w2_serial_buffer_head + 1) % W2_SERIAL_READ_BUFFER_SIZE; + + return; } diff --git a/robot/sim.h b/robot/sim.h index 75f6a5c..501552e 100644 --- a/robot/sim.h +++ b/robot/sim.h @@ -3,9 +3,12 @@ #include #include #include +#include + +extern bool g_w2_sim_headless; // debug fine-tuning -#define DBG_ENABLE_PRINTFUNC (1) +#define DBG_ENABLE_PRINTFUNC (0) #define DBG_ENABLE_SIMWARN (1) #define DBG_ENABLE_SIMINFO (1) #define DBG_ENABLE_CYCLEINFO (0) @@ -25,7 +28,7 @@ #define COL_RST "\e[0m" // debug stdout print macros -#define simprintf(message, ...) printf(COL_RED "[SIM] " COL_RST message, ##__VA_ARGS__) +#define simprintf(message, ...) if (!g_w2_sim_headless) printf(COL_RED "[SIM] " COL_RST message, ##__VA_ARGS__) #define simprint(message) simprintf(message "\n") #define simprintfunc(name, fmt, ...) if (DBG_ENABLE_PRINTFUNC) { simprintf(COL_BLU "[FUNC] " \ COL_CYN name COL_RST "(" COL_YEL fmt COL_RST ")\n", ##__VA_ARGS__); } @@ -46,3 +49,6 @@ void serial_set_baud_rate(unsigned int rate); // NOLINT void serial_send(char* message, unsigned int length); // NOLINT void serial_receive_ring(char* buffer, unsigned char size); // NOLINT unsigned char serial_get_received_bytes(); // NOLINT +void w2_sim_setup(int argc, char **argv); +void w2_sim_cycle_begin(); + diff --git a/robot/tests/info.bin b/robot/tests/info.bin new file mode 100644 index 0000000..e1ce36c --- /dev/null +++ b/robot/tests/info.bin @@ -0,0 +1 @@ +ÿ \ No newline at end of file diff --git a/robot/tests/ping.bin b/robot/tests/ping.bin new file mode 100644 index 0000000..456804e Binary files /dev/null and b/robot/tests/ping.bin differ diff --git a/robot/tests/readme.md b/robot/tests/readme.md new file mode 100644 index 0000000..7f4cc9a --- /dev/null +++ b/robot/tests/readme.md @@ -0,0 +1,11 @@ +this is a subdirectory that contains binary example commands to send to the +simultation robot code + +to use: + +``` +./a.out < file +``` + +where `file` is one of the .bin files in this folder + -- cgit v1.2.3