From 45a79adc56a7caa11afd58998ec6e322e2fd15e0 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Mon, 6 Apr 2020 17:05:48 +0200 Subject: [PATCH] Fix leaking details into global namespace --- fantemp/terminal.hpp | 67 ++++++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/fantemp/terminal.hpp b/fantemp/terminal.hpp index ee44a67..8a1fa3a 100644 --- a/fantemp/terminal.hpp +++ b/fantemp/terminal.hpp @@ -10,6 +10,8 @@ #include "controller.hpp" +namespace detail { + GF(ENDL, "\r\n"); GF(HELP_CMD, "help"); GF(SHOW_CMD, "show"); @@ -19,8 +21,7 @@ GF(BOOTLOADER_CMD, "bootloader"); GF(VERSION_CMD, "version"); GF(VERSION, "1.1"); -constexpr auto BACKSPACE = uint8_t{0x7f}; -constexpr auto CTRL_C = uint8_t{0x03}; +} // namespace detail template class Terminal { @@ -29,9 +30,9 @@ class Terminal { { m_serial.init(); - m_serial << ENDL; + m_serial << detail::ENDL; printVersion(); - m_serial << ENDL << F("$ "); + m_serial << detail::ENDL << F("$ "); } static void callback() @@ -45,7 +46,7 @@ class Terminal { // Handle Ctrl + C if (inputByte == CTRL_C) { - m_serial << F("^C") << ENDL; + m_serial << F("^C") << detail::ENDL; handleInput = true; break; } @@ -66,13 +67,13 @@ class Terminal { if (m_serial.peek(inputByte) && (inputByte == '\r' || inputByte == '\n')) { m_serial.rxByte(inputByte); } - m_serial << ENDL; + m_serial << detail::ENDL; handleInput = true; break; } if (m_inputSize >= INPUT_BUFFER_SIZE) { - m_serial << ENDL << F("WARNING: Terminal input buffer overflow!") << ENDL; + m_serial << detail::ENDL << F("WARNING: Terminal input buffer overflow!") << detail::ENDL; handleInput = true; break; } @@ -90,6 +91,8 @@ class Terminal { private: static constexpr auto INPUT_BUFFER_SIZE = 128; + static constexpr auto BACKSPACE = uint8_t{0x7f}; + static constexpr auto CTRL_C = uint8_t{0x03}; enum class State { NONE, @@ -106,17 +109,20 @@ class Terminal { if (m_inputSize) { if (m_inputBuffer[m_inputSize - 1] == CTRL_C) { handleCtrlC(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(HELP_CMD), m_inputSize) == 0) { + } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::HELP_CMD), m_inputSize) == 0) { printHelp(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(SHOW_CMD), m_inputSize) == 0) { + } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::SHOW_CMD), m_inputSize) == 0) { showState(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(CURVE_CMD), m_inputSize) == 0) { + } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::CURVE_CMD), m_inputSize) == 0) { printCurve(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(MONITOR_CMD), m_inputSize) == 0) { + } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::MONITOR_CMD), m_inputSize) == + 0) { m_state = State::MONITOR; - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(BOOTLOADER_CMD), m_inputSize) == 0) { + } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::BOOTLOADER_CMD), m_inputSize) == + 0) { handleBootloader(); - } else if (strncmp_P(m_inputBuffer, reinterpret_cast(VERSION_CMD), m_inputSize) == 0) { + } else if (strncmp_P(m_inputBuffer, reinterpret_cast(detail::VERSION_CMD), m_inputSize) == + 0) { printVersion(); } else { printUnknown(); @@ -126,19 +132,20 @@ class Terminal { static void handleCtrlC() { - m_serial << F("Abort!") << ENDL; + m_serial << F("Abort!") << detail::ENDL; m_state = State::NONE; } static void printHelp() { - m_serial << F("FanTemp command overview: ") << ENDL; - m_serial << HELP_CMD << F(" .......: print this help message") << ENDL; - m_serial << SHOW_CMD << F(" .......: shows current temperature and fan speed") << ENDL; - m_serial << CURVE_CMD << F(" ......: shows the curve used to map temperature to fan speed") << ENDL; - m_serial << MONITOR_CMD << F(" ....: loops the show command until Ctrl + C is pressed") << ENDL; - m_serial << BOOTLOADER_CMD << F(" .: enters the bootloader after 10 seconds") << ENDL; - m_serial << VERSION_CMD << F(" ....: displays firmware version") << ENDL; + m_serial << F("FanTemp command overview: ") << detail::ENDL; + m_serial << detail::HELP_CMD << F(" .......: print this help message") << detail::ENDL; + m_serial << detail::SHOW_CMD << F(" .......: shows current temperature and fan speed") << detail::ENDL; + m_serial << detail::CURVE_CMD << F(" ......: shows the curve used to map temperature to fan speed") + << detail::ENDL; + m_serial << detail::MONITOR_CMD << F(" ....: loops the show command until Ctrl + C is pressed") << detail::ENDL; + m_serial << detail::BOOTLOADER_CMD << F(" .: enters the bootloader after 10 seconds") << detail::ENDL; + m_serial << detail::VERSION_CMD << F(" ....: displays firmware version") << detail::ENDL; } static void showState() @@ -146,12 +153,12 @@ class Terminal { char floatBuffer[16]; sprintf(floatBuffer, "%.2f", Controller::m_adcSample); - m_serial << F("ADC value ...: ") << floatBuffer << F(" / 1023") << ENDL; + m_serial << F("ADC value ...: ") << floatBuffer << F(" / 1023") << detail::ENDL; sprintf(floatBuffer, "%.2f", Controller::m_resistance); - m_serial << F("Resistance ..: ") << floatBuffer << F(" Ohm") << ENDL; + m_serial << F("Resistance ..: ") << floatBuffer << F(" Ohm") << detail::ENDL; sprintf(floatBuffer, "%.2f", Controller::m_temperature); - m_serial << F("Temperature .: ") << floatBuffer << F(" C") << ENDL; - m_serial << F("Fan speed ...: ") << Controller::m_fanSpeed << F("%") << ENDL; + m_serial << F("Temperature .: ") << floatBuffer << F(" C") << detail::ENDL; + m_serial << F("Fan speed ...: ") << Controller::m_fanSpeed << F("%") << detail::ENDL; } static void printCurve() @@ -163,18 +170,18 @@ class Terminal { for (uint8_t s = 0; s < Controller::mapTemperature(i); ++s) { m_serial << "#"; } - m_serial << ENDL; + m_serial << detail::ENDL; } } static void handleBootloader() { for (int8_t i = 10; i >= 0; --i) { - m_serial << i << ENDL; + m_serial << i << detail::ENDL; _delay_ms(1000); } - m_serial << F("Entering bootloader...") << ENDL; + m_serial << F("Entering bootloader...") << detail::ENDL; m_serial.flushTx(); _delay_ms(1000); @@ -184,7 +191,7 @@ class Terminal { static void printVersion() { - m_serial << F("FanTemp v") << VERSION << ENDL; + m_serial << F("FanTemp v") << detail::VERSION << detail::ENDL; } static void printUnknown() @@ -192,7 +199,7 @@ class Terminal { m_serial << F("Unknown command \""); for (uint16_t i = 0; i < m_inputSize; ++i) m_serial << static_cast(m_inputBuffer[i]); - m_serial << F("\"") << ENDL; + m_serial << F("\"") << detail::ENDL; } };