Change architecture of command handling. Now we do it outside of the interrupt context. Changes in inverter.c are done for testing/playing around.

This commit is contained in:
Marco 2022-05-30 06:21:25 +02:00
parent 754fc2bc92
commit 424e2e710f
3 changed files with 30 additions and 17 deletions

View File

@ -5,41 +5,46 @@
#include "commands.h"
#include "pins.h"
#include "uart.h"
cmd_t command = {
.buf = {0},
.index = 0
};
void cmd_mirror_char(char c)
int cmd_terminated = 0;
cmd_t *cmd_getcommand(void)
{
printf("%c", c);
return &command;
}
void cmd_collect_char(char c)
{
command.buf[command.index] = c;
cmd_mirror_char(c);
command.index++;
if (c == 0x3) {
/* React on Ctl-C */
command.index = 0;
printf("\r\n");
}
if (c == 0xD) {
/* Null-terminate the string */
command.buf[command.index-1] = '\0';
cmd_handle(&command);
command.index = 0;
cmd_terminated = 1;
}
}
void cmd_handle(cmd_t *command)
{
if (!cmd_terminated)
return;
printf("\n\r");
char *tok1 = strtok(command->buf, " ");
@ -51,6 +56,8 @@ void cmd_handle(cmd_t *command)
if (0 == strcmp(tok1, "switch") || 0 == strcmp(tok1, "sw"))
cmd_handle_switch(tok2, tok3);
cmd_terminated = 0;
}
void cmd_handle_set(char *op1, char *op2)
@ -67,6 +74,15 @@ void cmd_handle_set(char *op1, char *op2)
else
printf("No valid value for t0_top given\n\r");
}
else if (0 == strcmp(op1, "t1_top")) {
val = strtol(op2, &end, 10);
if (op2 != end) {
OCR0A = val;
printf("Setting Timer1 TOP to %li\r\n", val);
}
else
printf("No valid value for t0_top given\n\r");
}
else if (0 == strcmp(op1, "t0_ps")) {
val = strtol(op2, &end, 10);
TCCR0B &= ~((1 << CS00) | (1 << CS01) | (1 << CS02));

View File

@ -3,7 +3,8 @@ typedef struct {
int index;
} cmd_t;
void cmd_mirror_char(char c);
cmd_t *cmd_getcommand(void);
void cmd_collect_char(char c);
void cmd_handle(cmd_t *command);
void cmd_handle_set(char *op1, char *op2);

View File

@ -9,21 +9,17 @@
int main()
{
/* With this MCU frequency, it is 38,4k baud */
sei();
/* With this MCU frequency (8MHz), 12 is a baudrate of 38.4k baud */
uart_init(12);
printf("Starting up!\r\n");
/*
* We are using Timer 1 Compare Match Unit B.
* OC1B is on Pin PB6. Let's make it an output.
*/
DDRB |= (1 << DDB6);
initPwmTimer1();
while (1) {
/* We handle terminated commands here.
Outside of the context of the UART receive interrupt. */
cmd_handle(cmd_getcommand());
}
return 0;