From eb40013ddcff35af8d6893f6623674783d723d35 Mon Sep 17 00:00:00 2001 From: geeksville Date: Mon, 27 Apr 2020 09:01:25 -0700 Subject: [PATCH] Create RedirectablePrint and NoopPrint for serial debug redirection --- src/RedirectablePrint.cpp | 13 +++++++++++++ src/RedirectablePrint.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/RedirectablePrint.cpp create mode 100644 src/RedirectablePrint.h diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp new file mode 100644 index 000000000..04205f498 --- /dev/null +++ b/src/RedirectablePrint.cpp @@ -0,0 +1,13 @@ +#include "RedirectablePrint.h" +#include + +/** + * A printer that doesn't go anywhere + */ +NoopPrint noopPrint; + +void RedirectablePrint::setDestination(Print *_dest) +{ + assert(_dest); + dest = _dest; +} \ No newline at end of file diff --git a/src/RedirectablePrint.h b/src/RedirectablePrint.h new file mode 100644 index 000000000..f75aea010 --- /dev/null +++ b/src/RedirectablePrint.h @@ -0,0 +1,34 @@ +#pragma once + +#include + +/** + * A Printable that can be switched to squirt its bytes to a different sink. + * This class is mostly useful to allow debug printing to be redirected away from Serial + * to some other transport if we switch Serial usage (on the fly) to some other purpose. + */ +class RedirectablePrint : public Print +{ + Print *dest; + + public: + RedirectablePrint(Print *_dest) : dest(_dest) {} + + /** + * Set a new destination + */ + void setDestination(Print *dest); + + virtual size_t write(uint8_t c) { return dest->write(c); } +}; + +class NoopPrint : public Print +{ + public: + virtual size_t write(uint8_t c) { return 1; } +}; + +/** + * A printer that doesn't go anywhere + */ +extern NoopPrint noopPrint; \ No newline at end of file