Refactor terminal callback

This commit is contained in:
BlackMark 2020-04-06 22:17:32 +02:00
parent 34428b76dd
commit f3cf12db3b

View File

@ -54,49 +54,7 @@ class Terminal {
static void callback() static void callback()
{ {
uint8_t inputByte; if (receiveInput()) {
bool handleInput = false;
while (m_serial.rxByte(inputByte)) {
if (isprint(inputByte) || inputByte == CTRL_C) {
m_inputBuffer[m_inputSize++] = inputByte;
// Handle Ctrl + C
if (inputByte == CTRL_C) {
m_serial << F("^C") << detail::ENDL;
handleInput = true;
break;
}
// Echo
else {
m_serial << static_cast<char>(inputByte);
}
}
// Handle backspace
if (inputByte == BACKSPACE && m_inputSize > 0) {
m_serial << F("\b \b");
--m_inputSize;
}
// Handle line terminator
else if (inputByte == '\r' || inputByte == '\n') {
// Consume possible second line terminator
if (m_serial.peek(inputByte) && (inputByte == '\r' || inputByte == '\n')) {
m_serial.rxByte(inputByte);
}
m_serial << detail::ENDL;
handleInput = true;
break;
}
if (m_inputSize >= INPUT_BUFFER_SIZE) {
m_serial << detail::ENDL << F("WARNING: Terminal input buffer overflow!") << detail::ENDL;
handleInput = true;
break;
}
}
if (handleInput) {
parseInput(); parseInput();
} }
@ -123,6 +81,49 @@ class Terminal {
static State m_state; static State m_state;
static uint64_t m_monitorDelayLastUpdate; static uint64_t m_monitorDelayLastUpdate;
static bool receiveInput()
{
uint8_t inputByte;
while (m_serial.rxByte(inputByte)) {
if (isprint(inputByte) || inputByte == CTRL_C) {
m_inputBuffer[m_inputSize++] = inputByte;
// Handle Ctrl + C
if (inputByte == CTRL_C) {
m_serial << F("^C") << detail::ENDL;
return true;
}
// Echo
else {
m_serial << static_cast<char>(inputByte);
}
}
// Handle backspace
if (inputByte == BACKSPACE && m_inputSize > 0) {
m_serial << F("\b \b");
--m_inputSize;
}
// Handle line terminator
else if (inputByte == '\r' || inputByte == '\n') {
// Consume possible second line terminator
if (m_serial.peek(inputByte) && (inputByte == '\r' || inputByte == '\n')) {
m_serial.rxByte(inputByte);
}
m_serial << detail::ENDL;
return true;
}
if (m_inputSize >= INPUT_BUFFER_SIZE) {
m_serial << detail::ENDL << F("WARNING: Terminal input buffer overflow!") << detail::ENDL;
return true;
}
}
return false;
}
static void parseInput() static void parseInput()
{ {
if (m_inputSize) { if (m_inputSize) {