Add basic terminal

This commit is contained in:
BlackMark 2020-04-01 02:27:28 +02:00
parent ed4972c87e
commit af35388505
3 changed files with 95 additions and 6 deletions

View File

@ -218,6 +218,9 @@
<Compile Include="pwm.hpp"> <Compile Include="pwm.hpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>
<Compile Include="terminal.hpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="thermistor.cpp"> <Compile Include="thermistor.cpp">
<SubType>compile</SubType> <SubType>compile</SubType>
</Compile> </Compile>

View File

@ -13,6 +13,7 @@
#include "uart/hardware0.hpp" #include "uart/hardware0.hpp"
#include "pwm.hpp" #include "pwm.hpp"
#include "terminal.hpp"
#include "thermistor.hpp" #include "thermistor.hpp"
template <typename T> template <typename T>
@ -34,9 +35,8 @@ uint8_t temperatureCurve(double temperature)
int main() int main()
{ {
uart::Uart0<uart::Config<115200>> serial; uart::Uart0<uart::Config<115200>> serial;
serial.init(); Terminal<decltype(serial)> terminal;
terminal.init();
serial << F("Thermistor reader\r\n");
using adc_conf = adc::Config<adc::SingleMode>; using adc_conf = adc::Config<adc::SingleMode>;
@ -54,7 +54,7 @@ int main()
const auto temperature = temp.getTemperature(fantempResistance); const auto temperature = temp.getTemperature(fantempResistance);
const auto fanSpeed = temperatureCurve(temperature); const auto fanSpeed = temperatureCurve(temperature);
char floatBuffer[16]; /*char floatBuffer[16];
sprintf(floatBuffer, "%.2f", adcSample); sprintf(floatBuffer, "%.2f", adcSample);
serial << F("Read ADC: ") << floatBuffer << F("\r\n"); serial << F("Read ADC: ") << floatBuffer << F("\r\n");
@ -62,11 +62,11 @@ int main()
serial << F("Resistance: ") << floatBuffer << F("\r\n"); serial << F("Resistance: ") << floatBuffer << F("\r\n");
sprintf(floatBuffer, "%.2f", temperature); sprintf(floatBuffer, "%.2f", temperature);
serial << F("Temperature: ") << floatBuffer << F(" C \r\n"); 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); pwm::setDuty(fanSpeed);
_delay_ms(1000); terminal.callback();
} }
serial.flushTx(); serial.flushTx();

86
fantemp/terminal.hpp Normal file
View File

@ -0,0 +1,86 @@
#pragma once
#include <ctype.h>
#include <stdint.h>
#include "flash/flash.hpp"
#define ENDL F("\r\n")
template <class Uart>
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<char>(inputByte);
}
}
// Handle backspace
if (inputByte == 0x7f && m_inputSize > 0) {
m_serial << static_cast<char>(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 <class Uart>
char Terminal<Uart>::m_inputBuffer[INPUT_BUFFER_SIZE];
template <class Uart>
uint16_t Terminal<Uart>::m_inputSize = 0;
#undef ENDL