Add files for handling commands.

This commit is contained in:
Marco 2022-05-12 04:44:56 +02:00
parent bc0acd3b1a
commit ab91d5f50e
2 changed files with 70 additions and 0 deletions

62
src/commands.c Normal file
View File

@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#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");
}
}

8
src/include/commands.h Normal file
View File

@ -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);