/* * terminal.c * * Created on: Feb 26, 2023 * Author: Daniel Peter Chokola */ /* Includes */ #include #include #include "config.h" #include "terminal.h" /* Private Variables */ static char wbuf[TERM_RXBUF_LEN]; /* working buffer */ static char rxbuf[TERM_RXBUF_LEN]; /* receive buffer */ static uint32_t rxlen; static int8_t rxline; /* Function Prototypes */ __attribute__((always_inline)) inline int8_t term_is_whitespace(char c) { if(c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == '\f' || c == ' ') { return !0; } return 0; } static int32_t term_stripn(char *buf, int32_t n); /* Function Definitions */ void term_prompt() { term_printf("\r%s-g%s $ ", APP_NAME, GIT_REV); } void term_rx(char *buf, uint32_t len) { uint32_t i; char c; for(i = 0; i < len; i++) { c = buf[i]; /* ignore null bytes */ if(c == '\0') { continue; } /* handle backspace */ if(c == '\b') { if(rxlen) { rxlen--; } rxbuf[rxlen] = '\0'; if(rxlen) { /* term_printf("\b\x1b[0K"); */ } continue; } /* handle escape sequences */ if(c == '\x1b') { c = '^'; } rxbuf[rxlen++] = (char) c; /* local echo */ /* term_printf("%c", c); */ if(c == '\n' || c == '\r') { /* EOL */ rxbuf[rxlen - 1] = '\0'; rxline = 1; } } } int32_t term_printf(const char *fmt, ...) { va_list args; int32_t len; uint8_t res; va_start(args, fmt); len = vsprintf(wbuf, fmt, args); va_end(args); res = CDC_Transmit_FS((uint8_t *) wbuf, (uint16_t) len); return (res != USBD_OK); } /* strip trailing whitespace from a string of length n */ static int32_t term_stripn(char *buf, int32_t n) { int32_t i = n - 1; while(i >= 0) { if(term_is_whitespace(buf[i])) { buf[i--] = '\0'; } else if(buf[i] == '\0') { i--; continue; } else { break; } } return i + 1; }