Pages: [1]   Go Down
Author Topic: _FILL(char c, int len) for Streaming library  (Read 2506 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


An addition for the Streaming library from Mikal Hart

include this in the Streaming.h
Code:
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:
Serial << _FILL('=', 20) << endl;
or
Code:
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;
}

Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

another trick for streaming.h (not optimized but might still be useful)
Code:
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; }
Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

another one, you could make a _TEMPC and a _TEMPF (Celsius Fahrenheit)
Code:
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; }
Logged

Rob Tillaart

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
#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);
}


Logged

Rob Tillaart

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 35
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 220
Posts: 13836
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Rob Tillaart

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

Pages: [1]   Go Up
Jump to: