Monitor the growing conditions in a hothouse with simple sensors.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

136 lines
2.0 KiB

/*
* terminal.c
*
* Created on: Aug 2, 2022
* Author: daniel.chokola
*/
/* Includes */
#include <string.h>
#include <stdarg.h>
/*
#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);
*/
term_printf("\r%s $ ", APP_NAME);
}
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;
}
}
}
void 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);
/* FIXME: this will lock up if there's too much Tx/Rx traffic */
do
{
res = CDC_Transmit_FS((uint8_t *) wbuf, (uint16_t) len);
}
while(res == USBD_BUSY);
}
/* 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;
}