From ab91d5f50ec61fc54270f2dbd34ab8f63fdabb5d Mon Sep 17 00:00:00 2001 From: Marco Date: Thu, 12 May 2022 04:44:56 +0200 Subject: [PATCH] Add files for handling commands. --- src/commands.c | 62 ++++++++++++++++++++++++++++++++++++++++++ src/include/commands.h | 8 ++++++ 2 files changed, 70 insertions(+) create mode 100644 src/commands.c create mode 100644 src/include/commands.h diff --git a/src/commands.c b/src/commands.c new file mode 100644 index 0000000..78448df --- /dev/null +++ b/src/commands.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include + +#include "include/commands.h" + +cmd_t command = { + .buf = {0}, + .index = 0 +}; + +void cmd_collect(char c) +{ + command.buf[command.index] = c; + printf("%c", command.buf[command.index]); + command.index++; + + + if (c == 0x3) { + /* React on Ctl-C */ + command.index = 0; + printf("\r\n"); + } + + + if (c == 0xD) { + printf("\n\r"); + + /* Null-terminate the string */ + command.buf[command.index-1] = '\0'; + cmd_handle(&command); + command.index = 0; + } +} + +void cmd_handle(cmd_t *command) +{ + printf("\n\r"); + + char *tok1 = strtok(command->buf, " "); + char *tok2 = strtok(NULL, " "); + char *tok3 = strtok(NULL, " "); + + if (0 == strcmp(tok1, "set")) + cmd_handle_set(tok2, tok3); +} + +void cmd_handle_set(char *op1, char *op2) +{ + int val; + + if (0 == strcmp(op1, "t0_top")) { + val = atoi(op2); + if (val) { + OCR0A = val; + printf("Setting Timer0 TOP to %i\r\n", val); + } + else + printf("No valid value for t0_top given\n\r"); + } +} \ No newline at end of file diff --git a/src/include/commands.h b/src/include/commands.h new file mode 100644 index 0000000..086f040 --- /dev/null +++ b/src/include/commands.h @@ -0,0 +1,8 @@ +typedef struct { + char buf[100]; + int index; +} cmd_t; + +void cmd_collect(char c); +void cmd_handle(cmd_t *command); +void cmd_handle_set(char *op1, char *op2);