Integrate terminal and implement read sensor command
This commit is contained in:
parent
5a26169539
commit
608f29e81b
@ -5,6 +5,8 @@
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "light_sensors.hpp"
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr auto ENDL = "\r\n";
|
||||
@ -47,6 +49,7 @@ class Terminal {
|
||||
if(receiveInput()) {
|
||||
parseInput();
|
||||
}
|
||||
m_serial.flushTx();
|
||||
}
|
||||
|
||||
private:
|
||||
@ -57,6 +60,7 @@ class Terminal {
|
||||
static Uart m_serial;
|
||||
static char m_inputBuffer[INPUT_BUFFER_SIZE];
|
||||
static uint16_t m_inputSize;
|
||||
static LightSensors m_lightSensors;
|
||||
|
||||
static bool receiveInput()
|
||||
{
|
||||
@ -112,7 +116,7 @@ class Terminal {
|
||||
printHelp();
|
||||
}
|
||||
else if(detail::substringEquals(m_inputBuffer, detail::READ_CMD, m_inputSize)) {
|
||||
readSensor();
|
||||
readSensors();
|
||||
}
|
||||
else if(detail::substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) {
|
||||
printVersion();
|
||||
@ -137,7 +141,16 @@ class Terminal {
|
||||
m_serial << detail::VERSION_CMD << " ....: displays firmware version" << detail::ENDL;
|
||||
}
|
||||
|
||||
static void readSensor() { m_serial << "Sensor values: 1 2 3" << detail::ENDL; }
|
||||
static void readSensors()
|
||||
{
|
||||
const auto sensorValues = m_lightSensors.getValues();
|
||||
m_serial << "Sensor values: ";
|
||||
for(const auto& ldrValue: sensorValues) {
|
||||
m_serial.txNumber(ldrValue);
|
||||
m_serial << ", ";
|
||||
}
|
||||
m_serial << detail::ENDL;
|
||||
}
|
||||
|
||||
static void printVersion() { m_serial << "AdaptiveBrightness v" << detail::VERSION << detail::ENDL; }
|
||||
|
||||
|
@ -1,14 +1,9 @@
|
||||
#include <array>
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
#include "init.h"
|
||||
#include "light_sensors.hpp"
|
||||
#include "terminal.hpp"
|
||||
#include "uart.hpp"
|
||||
|
||||
using serial_t = uart::Vcp<>;
|
||||
using terminal_t = Terminal<serial_t>;
|
||||
|
||||
static volatile bool g_error = false;
|
||||
|
||||
@ -19,16 +14,13 @@ extern "C" void Error_Handler(void)
|
||||
|
||||
static inline void initializeHardware()
|
||||
{
|
||||
serial_t serial;
|
||||
terminal_t terminal;
|
||||
|
||||
do {
|
||||
g_error = false;
|
||||
init();
|
||||
serial.init();
|
||||
if(g_error)
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
||||
else
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
|
||||
terminal.init();
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, g_error ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
} while(g_error);
|
||||
}
|
||||
|
||||
@ -44,27 +36,10 @@ int main()
|
||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
|
||||
serial_t serial;
|
||||
LightSensors lightSensors;
|
||||
terminal_t terminal;
|
||||
|
||||
while(true) {
|
||||
const auto ldrValues = lightSensors.getValues();
|
||||
|
||||
std::array<char, 32> printBuffer;
|
||||
|
||||
for(uint8_t i = 0; i < ldrValues.size(); ++i) {
|
||||
const auto ldrID = i + 1;
|
||||
const auto percentage = ldrValues[i] * 100 / 0xFFF;
|
||||
const auto bufLen = std::sprintf(printBuffer.data(), "LDR%d: %04hu - %03d%%\r\n%s", ldrID, ldrValues[i], percentage, (i == 2) ? "\r\n" : "");
|
||||
if(bufLen > 0) {
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
|
||||
serial.txString(printBuffer.data());
|
||||
serial.flushTx();
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
}
|
||||
|
||||
HAL_Delay(1000);
|
||||
terminal.callback();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user