Go Down

Topic: _FILL(char c, int len) for Streaming library (Read 2634 times) previous topic - next topic

robtillaart


An addition for the Streaming library from Mikal Hart

include this in the Streaming.h
Code: [Select]
struct _FILL
{
  char ch;
  int len;
  _FILL(char c, int l): ch(c), len(l)
  {}
};

inline Print &operator <<(Print &obj, const _FILL &arg)
{ for (int i=0; i< arg.len; i++) obj.write(arg.ch); return obj; }


some examples
Code: [Select]
Serial << _FILL('=', 20) << endl;
or
Code: [Select]
int table[10] = {5,10,4,6,8,12,6,5,3,2};
for (int i=0; i<10; i++)
{
  Serial << _FILL('*', table[i]) << endl;
}


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

another trick for streaming.h (not optimized but might still be useful)
Code: [Select]

struct _TIME
{
  uint8_t hour;
  uint8_t minu;
  uint8_t sec;
  _TIME(uint8_t h, uint8_t m, uint8_t s): hour(h), minu(m), sec(s)
  {}
};

inline Print &operator <<(Print &obj, const _TIME &arg)
{ obj.print(((arg.hour<10)?"0":""));    obj.print(int(arg.hour));
  obj.print(((arg.minu<10)?":0":":"));  obj.print(int(arg.minu));
  obj.print(((arg.sec<10)?":0":":"));   obj.print(int(arg.sec));
  return obj; }
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

another one, you could make a _TEMPC and a _TEMPF (Celsius Fahrenheit)
Code: [Select]

struct _TEMP
{
  float temp;
  int digits;
  _TEMP(float t, int d=0): temp(t), digits(d)
  {}
};

inline Print &operator <<(Print &obj, const _TEMP &arg)
{ obj.print(arg.temp, arg.digits); obj.print("Celsius"); return obj; }
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

robtillaart

final one for today, just to explore what can be done within the streaming lib.

Code: [Select]
struct _FUNC
{
  double val;
  double (*foo)(double);
  _FUNC(double v, double (*f)(double)): val(v)
  {
  foo = f;
  }
};

inline Print &operator <<(Print &obj, const _FUNC &arg)
{ obj.print(arg.foo(arg.val), 4); return obj; }


you an use this to call a math function on a number as follows:

Code: [Select]
#include <Streaming.h>

void setup()
{
  Serial.begin(115200);
  for (uint8_t a = 0; a < 25; a++)
  {
    Serial << _FUNC(a, sinSqr) << endl;
  }
}

void loop() {}

double sinSqr(double p)
{
  return sin(p) * sin(p);
}



Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

FigZ

Hi, I'm looking into the streaming lib and found your link
would you mind explaining what some of this stuff does?
I'm trying to make some examples for myself for future reference
Thank you

robtillaart

Best way is to give them a try of course.

_FILL(char, len) repeats a char  len times.
so
Serial << _FILL('=', 20) << endl;
outputs
====================


int table[10] = {5,10,4,6,8,12,6,5,3,2};
for (int i=0; i<10; i++)
{
  Serial << _FILL('*', table) << endl;
}

makes a simple histogram from a table
*****
**********
****
******
********
************
*******
*****
***
**


better would be   Serial <<  table << "\t" << _FILL('*', table) << endl;
which would add the value before the stars  => This could be merged into a _HISTO(char, len)

_TIME() prints the time,

The others are more playing.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up