mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
initial version
This commit is contained in:
parent
e78a45f39c
commit
2d0265c07b
55
libraries/PrintCharArray/PrintCharArray.h
Normal file
55
libraries/PrintCharArray/PrintCharArray.h
Normal file
@ -0,0 +1,55 @@
|
||||
//
|
||||
// FILE: PrintCharArray.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: Class that captures prints into a char array
|
||||
// DATE: 2017-12-07
|
||||
// URL:
|
||||
// HISTORY: 0.1.0 2017-12-07 initial version
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
#ifndef PrintCharArray_h
|
||||
#define PrintCharArray_h
|
||||
|
||||
#include "Print.h"
|
||||
|
||||
#define PRINTCHARARRAY_VERSION "0.1.0"
|
||||
#define BUFFERSIZE 256
|
||||
|
||||
class PrintCharArray: public Print
|
||||
{
|
||||
public:
|
||||
PrintCharArray() {};
|
||||
|
||||
size_t write(uint8_t c)
|
||||
{
|
||||
if (index < BUFFERSIZE-1)
|
||||
{
|
||||
buffer[index] = c;
|
||||
index++;
|
||||
buffer[index] = '\0';
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
index = 0;
|
||||
buffer[index] = '\0';
|
||||
}
|
||||
|
||||
int free() { return (BUFFERSIZE - index); }
|
||||
|
||||
int size() { return index; }
|
||||
|
||||
char * getBuffer() { return buffer; }
|
||||
|
||||
private:
|
||||
char buffer[BUFFERSIZE];
|
||||
int index = 0;
|
||||
};
|
||||
#endif
|
||||
// -- END OF FILE --
|
@ -0,0 +1,37 @@
|
||||
//
|
||||
// FILE: printCharArray1.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2017-12-07 initial version
|
||||
//
|
||||
|
||||
#include "PrintCharArray.h"
|
||||
|
||||
PrintCharArray ps;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
Serial.println(ps.free());
|
||||
ps.println("Hello World");
|
||||
Serial.println(ps.free());
|
||||
ps.println(3.14159265, 4);
|
||||
Serial.println(ps.free());
|
||||
Serial.println(ps.getBuffer());
|
||||
|
||||
ps.clear();
|
||||
ps.println(3.14159265, 4);
|
||||
ps.println("Hello World");
|
||||
Serial.println(ps.getBuffer());
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
//
|
||||
// FILE: printCharArray2.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2017-12-09 initial version
|
||||
//
|
||||
|
||||
#include "PrintCharArray.h"
|
||||
|
||||
PrintCharArray ps;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
Serial.println("Determine length of 10 random numbers and right ");
|
||||
Serial.println("align the numbers in a table with their sum.");
|
||||
Serial.println();
|
||||
|
||||
uint32_t sum = 0;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
uint32_t rn = random(100000000);
|
||||
ps.clear();
|
||||
ps.println(rn);
|
||||
printSpaces(15 - ps.size());
|
||||
sum += rn;
|
||||
Serial.print(ps.getBuffer());
|
||||
}
|
||||
Serial.print("================ +\n");
|
||||
ps.clear();
|
||||
ps.println(sum);
|
||||
printSpaces(15 - ps.size());
|
||||
Serial.println(sum);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void printSpaces(int n)
|
||||
{
|
||||
if (n <= 0) return;
|
||||
while (n)
|
||||
{
|
||||
Serial.print(' ');
|
||||
n--;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
//
|
||||
// FILE: printCharArray3.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: demo
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 2017-12-09 initial version
|
||||
//
|
||||
|
||||
#include "PrintCharArray.h"
|
||||
|
||||
#include "XMLWriter.h"
|
||||
|
||||
PrintCharArray ps;
|
||||
XMLWriter XML(&ps);
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
ps.clear();
|
||||
|
||||
XML.header();
|
||||
XML.comment("Weather in Nebraska");
|
||||
XML.tagOpen("Data");
|
||||
XML.writeNode("Date", "20131106");
|
||||
XML.writeNode("Time", "11:42");
|
||||
XML.writeNode("Temp", "23.4");
|
||||
XML.writeNode("Humi", "50%");
|
||||
XML.writeNode("Rain", "10mm");
|
||||
XML.writeNode("Sun", "40");
|
||||
XML.tagClose();
|
||||
|
||||
// write the XML generated in one call
|
||||
Serial.println(ps.getBuffer());
|
||||
Serial.println(ps.free());
|
||||
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
23
libraries/PrintCharArray/library.json
Normal file
23
libraries/PrintCharArray/library.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "PrintCharArray",
|
||||
"keywords": "Size print buffer stream char array",
|
||||
"description": "Library to capture prints into a char array.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/Arduino.git"
|
||||
},
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
"export": {
|
||||
"include": "libraries/PrintCharArray"
|
||||
}
|
||||
}
|
10
libraries/PrintCharArray/library.properties
Normal file
10
libraries/PrintCharArray/library.properties
Normal file
@ -0,0 +1,10 @@
|
||||
name=PrintCharArray
|
||||
version=0.1.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library to capture prints into a char array.
|
||||
paragraph=
|
||||
category=Data Processing
|
||||
url=https://github.com/RobTillaart/Arduino/tree/master/libraries
|
||||
architectures=*
|
||||
|
12
libraries/PrintCharArray/readme.md
Normal file
12
libraries/PrintCharArray/readme.md
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
PrintCharArray is a class that buffers a number of print statements in a char array.
|
||||
This char array can be processed later.
|
||||
|
||||
Typical usecase:
|
||||
- buffer slow generated of a packet of data
|
||||
and send it with minimum time between bytes
|
||||
|
||||
- print to buffer an see how long output is;
|
||||
use to prevent "display line overflow"
|
||||
(e.g. floats)
|
||||
|
Loading…
Reference in New Issue
Block a user