Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
neu-rah committed Feb 15, 2023
1 parent bf16ce8 commit 9f77c62
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 107 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@

Simple and light-weight stream operator for Arduino framework.

```c++
Serial<<"Value:"<<value<<endl;
This is not an emulation of C++ ostream, but it mimics those good parts

## Supporting
- operator<<
- endl
- flush
- precision(n) for float only
- dec, hex, oct, bin radix base spec. only for integral numbers

Serial<<"Step 1"<<then;
Serial<<"Step 2"<<endl;

Serial<<dot;
Serial<<dotl;
## Example
```c++
#include <streamFlow.h>
using namespace StreamFlow;

void setup() {
Serial.begin(115200);
while(!Serial);
Serial<<"stream style with arduino Serial"<<endl
<<"version:"<<precision(2)<<2.0<<endl
<<"hex format 0x"<<hex<<1967<<endl
<<"enjoy!"<<endl<<flush;
}
```
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=streamFlow
version=1.1.1
version=2.0.0
author=Rui Azevedo, [email protected]
maintainer=neu-rah, [email protected]
sentence=Arduino light streams
Expand Down
14 changes: 7 additions & 7 deletions src/streamFlow.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

#include "streamFlow.h"

StreamFlush go;
endlObj endl;
tabObj tab;
thenObj then;
dotObj dot;
dotlObj dotl;
namespace StreamFlow {
/// @brief currect output decimal places
static byte m_prec=2;
/// @brief currect output digit base to use on integral print
static byte m_radix=DEC;
};

168 changes: 75 additions & 93 deletions src/streamFlow.h
Original file line number Diff line number Diff line change
@@ -1,104 +1,86 @@
#ifndef RSITE_STREAM_FLOW
#pragma once
#define RSITE_STREAM_FLOW

#include <Stream.h>
//#include <avr/pgmspace.h>
#include <HardwareSerial.h>
#include <Arduino.h>

//#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<typename T> Serial_& _print_integral_num(Serial_& s,T n) {s.print(n,m_radix);m_radix=DEC;return s;}
template<typename T> 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<typename T>
Serial_& operator<<(const T o) {
byte tmp=m_prec;
m_prec=np;
s<<o;
m_prec=tmp;
return s;
}
};
Bound operator<<(Serial_& s) {return Bound(s,np);}
};

class bin:public intFmt {
public:
bin(int v):intFmt(v) {}
Stream& operator<<(Stream& s) {s.print(v,BIN);return s;}
};
/// @brief helps holding fotmat informtation external to stream
/// @param p decimal places
/// @return Precision
Precision precision(byte p) {return Precision(p);}

inline Stream& operator<<(Stream& s,hex v) {return v.operator<<(s);}
inline Stream& operator<<(Stream& s,bin v) {return v.operator<<(s);}
/// @brief operator<< overload for native common types thru Serial_&
/// @param s the output device
/// @param o the data
/// @return the output device
/// @{
Serial_& operator<<(Serial_& s,const __FlashStringHelper *o) {s.print(o);return s;}
Serial_& operator<<(Serial_& s,const unsigned long n) {return _print_integral_num(s,n);}
Serial_& operator<<(Serial_& s,const long n) {return _print_integral_num(s,n);}
Serial_& operator<<(Serial_& s,const unsigned int n) {return _print_integral_num(s,n);}
Serial_& operator<<(Serial_& s,const int n) {return _print_integral_num(s,n);}
Serial_& operator<<(Serial_& s,const float n) {return _print_scientific_num(s,n);}
Serial_& operator<<(Serial_& s,const double n) {return _print_scientific_num(s,n);}
Serial_& operator<<(Serial_& s,const String &str) {s.print(str);return s;}
Serial_& operator<<(Serial_& s,const char str[]) {s.print(str);return s;}
Serial_& operator<<(Serial_& s,const char c) {s.write(c);return s;}
Serial_& operator<<(Serial_& s,const Printable& x) {x.printTo(s);return s;}
Serial_& operator<<(Serial_& s,Serial_&(f)(Serial_&)) {return f(s);}
Precision operator<<(Serial_& s,Precision(p)(Serial_&)) {return p(s);}
Precision::Bound operator<<(Serial_& s,Precision p) {return p.operator<<(s);}
/// @}

inline Stream& operator<<(Stream& s,char v) {s.print(v);return s;}
inline Stream& operator<<(Stream& s,int v) {s.print(v);return s;}
inline Stream& operator<<(Stream& s,long v) {s.print(v);return s;}
inline Stream& operator<<(Stream& s,unsigned int v) {s.print(v);return s;}
inline Stream& operator<<(Stream& s,unsigned long v) {s.print(v);return s;}
inline Stream& operator<<(Stream& s,double v) {s.print(v);return s;}
inline Stream& operator<<(Stream& s,const char* v) {s.print(v);return s;}
//inline Stream& operator<<(Stream& s,const char* const __attribute__((__progmem__)) v) {s.print(v);return s;}
inline Stream& operator<<(Stream& s,const __FlashStringHelper* v) {
const char* const ptr=(const char* const)v;
for(uint16_t n=0;n<0xffff;n++) {
char ch=pgm_read_byte(ptr+n);
if (!ch) break;
s.print(ch);
}
//s.print(v);
return s;
}
//inline Stream& operator<<(Stream& s,fmt& v) {return v.operator<<(s);}
//.. add some more members as needed
/// @brief stream modifyers
/// @param s the output device
/// @return the output device
/// @{
inline Serial_& endl(Serial_ &s) {s.println();s.flush();return s;}
Serial_& flush(Serial_& s) {s.flush();return s;}
Serial_& dec(Serial_& s) {m_radix=DEC;return s;}
Serial_& hex(Serial_& s) {m_radix=HEX;return s;}
Serial_& oct(Serial_& s) {m_radix=OCT;return s;}
Serial_& bin(Serial_& s) {m_radix=BIN;return s;}
/// @}

class endlObj:public fmt {
public:Stream& operator<<(Stream& o) override {return o<<"\r\n";}
};
class tabObj:public fmt {
public:Stream& operator<<(Stream& o) override {return o<<'\t';}
};
class thenObj:public fmt {
public:Stream& operator<<(Stream& o) override {return o<<'\0';}
};
class dotObj:public fmt {
public:Stream& operator<<(Stream& o) override {return o<<'.';}
};
class dotlObj:public fmt {
public:Stream& operator<<(Stream& o) override {return o<<".\r\n";}
};
extern endlObj endl;
extern tabObj tab;
extern thenObj then;
extern dotObj dot;
extern dotlObj dotl;
inline Stream& operator<<(Stream &o,endlObj& v) {return v.operator<<(o);}
inline Stream& operator<<(Stream &o,tabObj& v) {return v.operator<<(o);}
inline Stream& operator<<(Stream &o,thenObj& v) {return v.operator<<(o);}
inline Stream& operator<<(Stream &o,dotObj& v) {return v.operator<<(o);}
inline Stream& operator<<(Stream &o,dotlObj& v) {return v.operator<<(o);}

template <int N>
class tabs {
public:
Stream& operator<<(Stream& o) {for(int n=0;n<N;n++) tab.operator<<(o);return o;}
};

template <int N>
inline Stream& operator<<(Stream& s,tabs<N>& v) {return v.operator<<(s);}
// template<> Stream& operator<<(Stream&,tabs<1> v) {return v.operator<<(s);}

#endif
};/// namespace StreamFlow

0 comments on commit 9f77c62

Please sign in to comment.