diff options
-rw-r--r-- | src/cmd.c | 86 | ||||
-rw-r--r-- | src/cmd.h | 28 | ||||
-rw-r--r-- | src/main.c | 13 | ||||
-rw-r--r-- | src/uart.c | 6 | ||||
-rw-r--r-- | src/uart.h | 7 |
5 files changed, 137 insertions, 3 deletions
diff --git a/src/cmd.c b/src/cmd.c new file mode 100644 index 0000000..43b9bfc --- /dev/null +++ b/src/cmd.c @@ -0,0 +1,86 @@ +/* + * src/cmd.c + * (c) 2021 Jonas Gunz <himself@jonasgunz.de> + * License: All rights reserved. + */ + +#include "cmd.h" + +struct { + uint8_t state; + uint8_t cmd; + uint8_t argv[_CMD_MAX_ARGC]; +} cmd_state; + +const struct { + char literal; + void (*fkt)(uint8_t[]); + uint8_t argc; +} cmd[_CMD_CNT] = { + {0,cmd_err,0}, + {'p',cmd_set_pwm,2} +}; + +void cmd_init() { + cmd_state.state = 0; + cmd_state.cmd = 0; +} + +void cmd_tick(char _c) { + uint8_t i; + + if(_c == '\r') { + /* if cmd is not cmd_err and argc do not match */ + if( cmd_state.cmd && cmd[cmd_state.cmd].argc != cmd_state.state - 1 ) { + /* set cmd_err to _ERR_ARGC */ + cmd_state.cmd = 0; + cmd_state.argv[0] = _ERR_ARGC; + } + + (*cmd[cmd_state.cmd].fkt)(cmd_state.argv); + cmd_state.state = 0; + cmd_state.cmd = 0; + return; + } + + if(!cmd_state.state) { + cmd_state.state = 1; + for( i=0; i<_CMD_CNT; i++ ) { + if( _c != cmd[i].literal ) + continue; + + cmd_state.cmd = i; + return; + } + + cmd_state.cmd = 0; + cmd_state.argv[0] = _ERR_CMD; + } + + /* Check, if maximum argc is reached */ + if (cmd_state.state >= _CMD_MAX_ARGC) { + cmd_state.cmd = 0; + cmd_state.argv[0] = _ERR_ARGC; + } + + cmd_state.argv[ (cmd_state.state++) - 1 ] = _c; +} + +void cmd_set_pwm(uint8_t _argv[]) { + uart_putchar(_argv[0]); + uart_putchar(_argv[1]); +} + +void cmd_err( uint8_t _argv[] ) { + switch(_argv[0]) { + case _ERR_ARGC: + uart_putstring("E:ARG\r\n"); + break; + case _ERR_CMD: + uart_putstring("E:CMD\r\n"); + break; + default: + uart_putstring("E\r\n"); + + } +} diff --git a/src/cmd.h b/src/cmd.h new file mode 100644 index 0000000..c547f6e --- /dev/null +++ b/src/cmd.h @@ -0,0 +1,28 @@ +/* + * src/cmd.h + * (c) 2021 Jonas Gunz <himself@jonasgunz.de> + * License: All rights reserved. + */ + +#ifndef _CMD_H_ +#define _CMD_H_ + +#include <stdint.h> + +#include "uart.h" + +#define _CMD_CNT 2 +#define _CMD_MAX_ARGC 4 + +#define _ERR_CMD 1 +#define _ERR_ARGC 2 + +void cmd_init(); + +void cmd_tick(char _c); + +void cmd_set_pwm(uint8_t _argv[]); + +void cmd_err(uint8_t _argv[]); + +#endif @@ -1,8 +1,15 @@ +/* + * src/main.c + * (c) 2021 Jonas Gunz <himself@jonasgunz.de> + * License: All rights reserved. + */ + #include <avr/io.h> #include <avr/interrupt.h> #include <stdint.h> #include "uart.h" +#include "cmd.h" uint8_t t0_ovf_cnt = 0; @@ -48,16 +55,20 @@ int main(void) { /* Uart */ uart_init(); + + cmd_init(); /* Go! */ sei(); + uart_putstring("START\r\n"); + while(1){ if( uart_getchar(&c) ) continue; uart_putchar(c); - pb0_thresh = c; + cmd_tick(c); } return 0; @@ -73,3 +73,9 @@ uint8_t uart_getchar(char *_c) { return 0; } + +void uart_putstring(char *_s) { + do { + uart_putchar(*_s); + } while(*(_s++)); +} @@ -5,7 +5,8 @@ */ /* - * Interrupt controlled UART + * Interrupt controlled UART with + * ringbuffers to TX and RX */ #ifndef _UART_H_ @@ -17,7 +18,7 @@ #include <string.h> #ifndef BAUD -#warning BAUD "BAUD not defined. Dafaulting to 9600" +#warning "BAUD not defined. Dafaulting to 9600" #define BAUD 9600 #endif @@ -33,4 +34,6 @@ uint8_t uart_putchar(char _c); uint8_t uart_getchar(char *_c); +void uart_putstring(char *_s); + #endif |