From f3cf12db3bee2cde2606f6d7fb9e440ca5e23c2a Mon Sep 17 00:00:00 2001 From: BlackMark Date: Mon, 6 Apr 2020 22:17:32 +0200 Subject: [PATCH] Refactor terminal callback --- fantemp/terminal.hpp | 87 ++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index 0e9198d..f7c1e91 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -54,49 +54,7 @@ class Terminal { static void callback() { - uint8_t inputByte; - 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(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) { + if (receiveInput()) { parseInput(); } @@ -123,6 +81,49 @@ class Terminal { static State m_state; 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(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() { if (m_inputSize) {