Skip to content

Commit

Permalink
Fixed build and Serial console in emulator
Browse files Browse the repository at this point in the history
Signed-off-by: simonmicro <[email protected]>
  • Loading branch information
simonmicro committed Apr 6, 2024
1 parent 3567b69 commit 5c42a96
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ target_compile_definitions(emulator.run PUBLIC
# Comment these as you wish...
OSW_FEATURE_STATS_STEPS
OSW_FEATURE_WEATHER
OSW_SERVICE_CONSOLE
OSW_APPS_EXAMPLES
GAME_SNAKE=1
GAME_BRICK_BREAKER=1
Expand Down
6 changes: 5 additions & 1 deletion emulator/include/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Serial_t {
public:
Serial_t() {};
Serial_t();
~Serial_t() {};

std::list<std::stringstream> buffer;
Expand Down Expand Up @@ -40,8 +40,12 @@ class Serial_t {
this->println();
}

int available();
int read();

void println();
private:
std::list<char> inputBuffer;
int bauds = 0;
bool buffered = false;
bool addBufferNewline = true;
Expand Down
27 changes: 27 additions & 0 deletions emulator/src/Serial.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
#include "../include/Serial.h"
#include <OswLogger.h>

#include <iostream>
#include <unistd.h>
#include <fcntl.h>

Serial_t Serial;

Serial_t::Serial_t() {
// put the stdin-stream into non-blocking mode to avoid hanging in case available() is called
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
}

void Serial_t::setBuffered(bool buffered) {
this->buffered = buffered;
this->addBufferNewline = true;
Expand All @@ -26,4 +36,21 @@ void Serial_t::println() {
this->addBufferNewline = true;
else
std::cout << std::endl;
}

int Serial_t::available() {
char c;
if(::read(STDIN_FILENO, &c, 1) > 0) {
this->inputBuffer.push_back(c);
return true;
}
return false;
}

int Serial_t::read() {
if(this->inputBuffer.empty())
return -1;
char c = this->inputBuffer.front();
this->inputBuffer.pop_front();
return c;
}
9 changes: 9 additions & 0 deletions src/services/OswServiceTaskConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,17 @@ void OswServiceTaskConsole::runPrompt() {
this->m_configuring = true;
} else if (this->m_inputBuffer == "help") {
this->showHelp();
#ifdef OSW_FEATURE_WIFI
} else if (this->m_inputBuffer == "hostname") {
Serial.println(OswConfigAllKeys::hostname.get());
#endif
} else if (this->m_inputBuffer == "lock") {
this->m_locked = true;
#ifndef OSW_EMULATOR
} else if (this->m_inputBuffer == "reboot") {
// this does not work in the emulator as it is running under an own thread, of which the shutdown-exception is not captured - populating here and crashing
ESP.restart();
#endif
} else if (this->m_inputBuffer == "time") {
Serial.println(OswHal::getInstance()->getUTCTime());
} else if (this->m_inputBuffer == "wipe") {
Expand Down Expand Up @@ -155,9 +160,13 @@ void OswServiceTaskConsole::showHelp() {
} else {
Serial.println(" configure - enter configuration mode");
Serial.println(" help - show this help");
#ifdef OSW_FEATURE_WIFI
Serial.println(" hostname - show the device hostname");
#endif
Serial.println(" lock - lock the console");
#ifndef OSW_EMULATOR
Serial.println(" reboot - warm-start the device forcefully");
#endif
Serial.println(" time - show current UTC time");
Serial.println(" wipe - format NVS partition and reset configuration");
}
Expand Down

0 comments on commit 5c42a96

Please sign in to comment.