From af3538850539e0b306651ba63f3b941b8d208372 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Wed, 1 Apr 2020 02:27:28 +0200 Subject: [PATCH] Add basic terminal --- fantemp/fantemp.cppproj | 3 ++ fantemp/main.cpp | 12 +++--- fantemp/terminal.hpp | 86 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 fantemp/terminal.hpp diff --git a/fantemp/fantemp.cppproj b/fantemp/fantemp.cppproj index 3c2f23a..35bbaeb 100644 --- a/fantemp/fantemp.cppproj +++ b/fantemp/fantemp.cppproj @@ -218,6 +218,9 @@ compile + + compile + compile diff --git a/fantemp/main.cpp b/fantemp/main.cpp index 961403b..ec0f3f6 100644 --- a/fantemp/main.cpp +++ b/fantemp/main.cpp @@ -13,6 +13,7 @@ #include "uart/hardware0.hpp" #include "pwm.hpp" +#include "terminal.hpp" #include "thermistor.hpp" template @@ -34,9 +35,8 @@ uint8_t temperatureCurve(double temperature) int main() { uart::Uart0> serial; - serial.init(); - - serial << F("Thermistor reader\r\n"); + Terminal terminal; + terminal.init(); using adc_conf = adc::Config; @@ -54,7 +54,7 @@ int main() const auto temperature = temp.getTemperature(fantempResistance); const auto fanSpeed = temperatureCurve(temperature); - char floatBuffer[16]; + /*char floatBuffer[16]; sprintf(floatBuffer, "%.2f", adcSample); serial << F("Read ADC: ") << floatBuffer << F("\r\n"); @@ -62,11 +62,11 @@ int main() serial << F("Resistance: ") << floatBuffer << F("\r\n"); sprintf(floatBuffer, "%.2f", temperature); serial << F("Temperature: ") << floatBuffer << F(" C \r\n"); - serial << F("Fan speed: ") << fanSpeed << F("%\r\n"); + serial << F("Fan speed: ") << fanSpeed << F("%\r\n");*/ pwm::setDuty(fanSpeed); - _delay_ms(1000); + terminal.callback(); } serial.flushTx(); diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp new file mode 100644 index 0000000..8b10923 --- /dev/null +++ b/fantemp/terminal.hpp @@ -0,0 +1,86 @@ +#pragma once + +#include +#include + +#include "flash/flash.hpp" + +#define ENDL F("\r\n") + +template +class Terminal { + public: + static void init() + { + m_serial.init(); + + m_serial << ENDL << F("FanTemp Control Panel") << ENDL << ENDL << F("$ "); + } + + static void callback() + { + uint8_t inputByte; + bool handleInput = false; + + while (m_serial.rxByte(inputByte)) { + if (isprint(inputByte) || inputByte == 0x03) { + m_inputBuffer[m_inputSize++] = inputByte; + + // Handle Ctrl + C + if (inputByte == 0x03) { + m_serial << F("^C"); + handleInput = true; + break; + } + // Echo + else { + m_serial << static_cast(inputByte); + } + } + + // Handle backspace + if (inputByte == 0x7f && m_inputSize > 0) { + m_serial << static_cast(0x7f); + --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); + } + handleInput = true; + break; + } + + if (m_inputSize >= INPUT_BUFFER_SIZE) { + m_serial << ENDL << F("WARNING: Terminal input buffer overflow!") << ENDL; + handleInput = true; + break; + } + } + + if (handleInput) { + parseInput(); + m_inputSize = 0; + m_serial << ENDL << F("$ "); + } + } + + private: + static constexpr auto INPUT_BUFFER_SIZE = 128; + + static Uart m_serial; + static char m_inputBuffer[INPUT_BUFFER_SIZE]; + static uint16_t m_inputSize; + + static void parseInput() {} +}; + +template +char Terminal::m_inputBuffer[INPUT_BUFFER_SIZE]; + +template +uint16_t Terminal::m_inputSize = 0; + +#undef ENDL