From 9f77c621289890e217e250837f017dc0275c34e0 Mon Sep 17 00:00:00 2001 From: Rui Azevedo Date: Wed, 15 Feb 2023 19:18:03 -0100 Subject: [PATCH] v2.0 --- .vscode/settings.json | 1 + README.md | 27 +++++-- library.properties | 2 +- src/streamFlow.cpp | 14 ++-- src/streamFlow.h | 168 +++++++++++++++++++----------------------- 5 files changed, 105 insertions(+), 107 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/README.md b/README.md index e610865..7562493 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,27 @@ Simple and light-weight stream operator for Arduino framework. -```c++ -Serial<<"Value:"< +using namespace StreamFlow; + +void setup() { + Serial.begin(115200); + while(!Serial); + Serial<<"stream style with arduino Serial"< -//#include -#include +#include -//#define endl "\n\r" -//#define tab "\t" -//#define then "\0" -//#define dot "." -//#define dotl ".\n\r" -class fmt { - public: - virtual Stream& operator<<(Stream& o)=0; -}; +namespace StreamFlow { -class StreamFlush:public fmt { - public: - StreamFlush() {} - virtual Stream& operator<<(Stream& o) {o.flush();return o;} -}; + using Serial_=decltype(Serial); -extern StreamFlush go; + /// @brief currect output decimal places + static extern byte m_prec; + /// @brief currect output digit base to use on integral print + static extern byte m_radix; -class intFmt:public fmt { - public: - intFmt(int v):v(v) {} - int v; -}; + /// @brief use _radix stream external info to request Arduino radix format + /// @tparam T numeric integral type + /// @param s output object + /// @param n numeric integral data + /// @return the output object + template Serial_& _print_integral_num(Serial_& s,T n) {s.print(n,m_radix);m_radix=DEC;return s;} + template Serial_& _print_scientific_num(Serial_& s,T n) {s.print(n,m_prec);return s;} -class hex:public intFmt { - public: - hex(int v):intFmt(v) {} - Stream& operator<<(Stream& s) {s.print(v,HEX);return s;} -}; + /// @brief hold desired precision till next data in printedm then restores the previous precision + struct Precision { + byte np; + Precision(int p):np(p) {} + /// @brief hold precision and stream while waitibg for next data + struct Bound { + Serial_& s; + byte np; + Bound(Serial_& s,int p):s(s),np(p) {} + /// @brief print the next data and return to old precision format + /// @tparam T data type + /// @param o data + /// @return the stream to resume printing + template + Serial_& operator<<(const T o) { + byte tmp=m_prec; + m_prec=np; + s< -class tabs { - public: - Stream& operator<<(Stream& o) {for(int n=0;n -inline Stream& operator<<(Stream& s,tabs& v) {return v.operator<<(s);} -// template<> Stream& operator<<(Stream&,tabs<1> v) {return v.operator<<(s);} - -#endif +};/// namespace StreamFlow \ No newline at end of file