2014-12-22 12:35:05 +01:00
|
|
|
#include "ets_sys.h"
|
|
|
|
#include "os_type.h"
|
|
|
|
#include "osapi.h"
|
|
|
|
#include "driver/uart.h"
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
LOCAL os_timer_t readline_timer;
|
|
|
|
|
|
|
|
// UartDev is defined and initialized in rom code.
|
|
|
|
extern UartDevice UartDev;
|
|
|
|
|
|
|
|
#define uart_putc uart0_putc
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
bool uart_getc(char *c){
|
2014-12-22 12:35:05 +01:00
|
|
|
RcvMsgBuff *pRxBuff = &(UartDev.rcv_buff);
|
|
|
|
if(pRxBuff->pWritePos == pRxBuff->pReadPos){ // empty
|
2014-12-30 08:42:24 +01:00
|
|
|
return false;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
// ETS_UART_INTR_DISABLE();
|
|
|
|
ETS_INTR_LOCK();
|
2014-12-30 08:42:24 +01:00
|
|
|
*c = (char)*(pRxBuff->pReadPos);
|
2014-12-22 12:35:05 +01:00
|
|
|
if (pRxBuff->pReadPos == (pRxBuff->pRcvMsgBuff + RX_BUFF_SIZE)) {
|
2019-02-17 19:26:29 +01:00
|
|
|
pRxBuff->pReadPos = pRxBuff->pRcvMsgBuff ;
|
2014-12-22 12:35:05 +01:00
|
|
|
} else {
|
|
|
|
pRxBuff->pReadPos++;
|
|
|
|
}
|
|
|
|
// ETS_UART_INTR_ENABLE();
|
|
|
|
ETS_INTR_UNLOCK();
|
2014-12-30 08:42:24 +01:00
|
|
|
return true;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2015-01-05 03:09:51 +01:00
|
|
|
int readline4lua(const char *prompt, char *buffer, int length){
|
2014-12-22 12:35:05 +01:00
|
|
|
char ch;
|
|
|
|
int line_position;
|
|
|
|
|
|
|
|
start:
|
|
|
|
/* show prompt */
|
|
|
|
uart0_sendStr(prompt);
|
|
|
|
|
|
|
|
line_position = 0;
|
|
|
|
os_memset(buffer, 0, length);
|
|
|
|
while (1)
|
|
|
|
{
|
2014-12-30 08:42:24 +01:00
|
|
|
while (uart_getc(&ch))
|
2014-12-22 12:35:05 +01:00
|
|
|
{
|
|
|
|
/* handle CR key */
|
|
|
|
if (ch == '\r')
|
|
|
|
{
|
|
|
|
char next;
|
2014-12-30 08:42:24 +01:00
|
|
|
if (uart_getc(&next))
|
2014-12-22 12:35:05 +01:00
|
|
|
ch = next;
|
|
|
|
}
|
|
|
|
/* backspace key */
|
|
|
|
else if (ch == 0x7f || ch == 0x08)
|
|
|
|
{
|
|
|
|
if (line_position > 0)
|
|
|
|
{
|
|
|
|
uart_putc(0x08);
|
|
|
|
uart_putc(' ');
|
|
|
|
uart_putc(0x08);
|
|
|
|
line_position--;
|
|
|
|
}
|
|
|
|
buffer[line_position] = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* EOF(ctrl+d) */
|
|
|
|
else if (ch == 0x04)
|
|
|
|
{
|
|
|
|
if (line_position == 0)
|
|
|
|
/* No input which makes lua interpreter close */
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2014-12-22 12:35:05 +01:00
|
|
|
/* end of line */
|
|
|
|
if (ch == '\r' || ch == '\n')
|
|
|
|
{
|
|
|
|
buffer[line_position] = 0;
|
|
|
|
uart_putc('\n');
|
|
|
|
if (line_position == 0)
|
|
|
|
{
|
|
|
|
/* Get a empty line, then go to get a new line */
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return line_position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* other control character or not an acsii character */
|
|
|
|
if (ch < 0x20 || ch >= 0x80)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* echo */
|
|
|
|
uart_putc(ch);
|
|
|
|
buffer[line_position] = ch;
|
|
|
|
ch = 0;
|
|
|
|
line_position++;
|
|
|
|
|
|
|
|
/* it's a large line, discard it */
|
|
|
|
if (line_position >= length)
|
|
|
|
line_position = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|