Skip to content

Commit

Permalink
Implementing reading (subset of) interface for emulated Serial class.
Browse files Browse the repository at this point in the history
  • Loading branch information
krulis-martin committed Mar 1, 2024
1 parent f1ea711 commit 4f27af7
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
46 changes: 46 additions & 0 deletions shared/emulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ friend class ArduinoSimulationController;
logtime_t mPinWriteDelay;
logtime_t mPinSetModeDelay;

std::deque<char> mSerialData;

void reset()
{
mCurrentTime = 0;
Expand Down Expand Up @@ -616,6 +618,50 @@ friend class ArduinoSimulationController;
{
return mEnableSerial;
}

/**
* Enqueue additional serial data to be read by the emulated code.
*/
void addSerialData(const std::string& str)
{
for (char c : str) {
mSerialData.push_back(c);
}
}

/**
* Return number of bytes enqueued in serial buffer.
*/
std::size_t serialDataAvailable() const
{
return mSerialData.size();
}

/**
* Return the next byte (char) to be read from the serial buffer.
*/
char peekSerial() const
{
if (mSerialData.empty()) {
return '\0';
}

return mSerialData.front();
}

/**
* Return and pop one char from the serial data buffer.
*/
char readSerial()
{
if (mSerialData.empty()) {
return '\0';
}

char res = mSerialData.front();
mSerialData.pop_front();
return res;
}
};

#endif
44 changes: 44 additions & 0 deletions shared/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,48 @@ void SerialMock::println(const char* val)
}
}

std::size_t SerialMock::available() const
{
if (!emulator.isSerialEnabled()) {
throw ArduinoEmulatorException("The Serial interface is disabled in the emulator.");
}
return emulator.serialDataAvailable();
}

int SerialMock::peek() const
{
if (!emulator.isSerialEnabled()) {
throw ArduinoEmulatorException("The Serial interface is disabled in the emulator.");
}
if (emulator.serialDataAvailable() == 0) {
return -1;
}
return emulator.peekSerial();
}

int SerialMock::read()
{
if (!emulator.isSerialEnabled()) {
throw ArduinoEmulatorException("The Serial interface is disabled in the emulator.");
}
if (emulator.serialDataAvailable() == 0) {
return -1;
}
return emulator.readSerial();

}

std::size_t SerialMock::readBytes(char* buffer, std::size_t length)
{
if (!emulator.isSerialEnabled()) {
throw ArduinoEmulatorException("The Serial interface is disabled in the emulator.");
}
length = std::min(length, emulator.serialDataAvailable());
for (std::size_t i = 0; i < length; ++i) {
buffer[i] = emulator.readSerial();
}
return length;
}


SerialMock Serial;
6 changes: 6 additions & 0 deletions shared/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ enum SerialPrintFormat {
HEX,
};

// A subset of Serial class
class SerialMock
{
public:
Expand Down Expand Up @@ -221,6 +222,11 @@ class SerialMock
void println(unsigned long long val, SerialPrintFormat format = DEC);
void println(double val);
void println(const char* val);

std::size_t available() const;
int peek() const;
int read();
std::size_t readBytes(char* buffer, std::size_t length);
};

extern SerialMock Serial;
Expand Down

0 comments on commit 4f27af7

Please sign in to comment.