summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-14 14:14:47 +0100
committerHampusM <hampus@hampusmat.com>2022-03-14 14:14:47 +0100
commitd5b6bc7bb8250dd252b8569e0896febf94c60b12 (patch)
tree62f595979e0e46eeede04b48abfa199eaba886c6 /src
parent5b6427dde0bb8e3b466793243bbfc185f4739ac6 (diff)
refactor: add serial stream enabled check
Diffstat (limited to 'src')
-rw-r--r--src/serial.cpp35
-rw-r--r--src/serial.hpp26
2 files changed, 38 insertions, 23 deletions
diff --git a/src/serial.cpp b/src/serial.cpp
index e60de6c..874d6d7 100644
--- a/src/serial.cpp
+++ b/src/serial.cpp
@@ -2,12 +2,30 @@
#include "common/conversion.hpp"
-SerialStream::SerialStream(Serial_ serial, const unsigned long &baud_rate)
+SerialStream::SerialStream(Serial_ serial, const unsigned long &baud_rate) noexcept
: _serial(serial)
{
_serial.begin(baud_rate);
}
+void SerialStream::waitReady()
+{
+ while (!_serial) {}
+}
+
+void SerialStream::write(const char *str)
+{
+ if (SerialStream::is_enabled())
+ {
+ _serial.write(str);
+ }
+}
+
+void SerialStream::flush()
+{
+ _serial.flush();
+}
+
SerialStream &SerialStream::operator<<(const char *str)
{
write(str);
@@ -50,21 +68,6 @@ SerialStream &SerialStream::operator<<(void (*manipulator)(SerialStream *))
return *this;
}
-void SerialStream::waitReady()
-{
- while (!_serial) {}
-}
-
-void SerialStream::write(const char *str)
-{
- _serial.write(str);
-}
-
-void SerialStream::flush()
-{
- _serial.flush();
-}
-
void endl(SerialStream *serial_stream)
{
serial_stream->write("\n");
diff --git a/src/serial.hpp b/src/serial.hpp
index 36aa6ed..ae51c3f 100644
--- a/src/serial.hpp
+++ b/src/serial.hpp
@@ -4,10 +4,28 @@
#include <USBAPI.h>
+#define LEONARDO_XINPUT_VID 0x045E // NOLINT(cppcoreguidelines-macro-usage)
+#define LEONARDO_XINPUT_PID 0x028E // NOLINT(cppcoreguidelines-macro-usage)
+
class SerialStream
{
public:
- SerialStream(Serial_ serial, const unsigned long &baud_rate);
+ SerialStream(Serial_ serial, const unsigned long &baud_rate) noexcept;
+
+ void waitReady();
+
+ void write(const char *str);
+
+ void flush();
+
+ constexpr static bool is_enabled() noexcept
+ {
+#if USB_VID != LEONARDO_XINPUT_VID && USB_PID != LEONARDO_XINPUT_PID
+ return true;
+#else
+ return false;
+#endif
+ }
SerialStream &operator<<(const char *str);
SerialStream &operator<<(const common::String &str);
@@ -18,12 +36,6 @@ public:
SerialStream &operator<<(void (*manipulator)(SerialStream *));
- void waitReady();
-
- void write(const char *str);
-
- void flush();
-
private:
Serial_ _serial;
};