mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ version 0.1.03
+ refactored for smaller footprint + added support for more datatypes + extended example eketch
This commit is contained in:
parent
093fbf040e
commit
8413c62139
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: XMLWriter.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.02
|
||||
// VERSION: 0.1.03
|
||||
// DATE: 2013-11-06
|
||||
// PURPOSE: Simple XML library
|
||||
//
|
||||
@ -9,6 +9,7 @@
|
||||
// 0.1.00 - 2013-11-06 initial version
|
||||
// 0.1.01 - 2013-11-07 rework interfaces
|
||||
// 0.1.02 - 2013-11-07 +setIndentSize(), corrected history, +escape support
|
||||
// 0.1.03 - 2015-03-07 refactored - footprint + interface
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
@ -36,19 +37,12 @@ void XMLWriter::header()
|
||||
void XMLWriter::comment(char* text, bool multiLine)
|
||||
{
|
||||
_stream->println();
|
||||
if (multiLine)
|
||||
{
|
||||
_stream->println("<!-- ");
|
||||
_stream->println(text);
|
||||
_stream->println(" -->");
|
||||
}
|
||||
else
|
||||
{
|
||||
spaces();
|
||||
_stream->print("<!-- ");
|
||||
_stream->print(text);
|
||||
_stream->println(" -->");
|
||||
}
|
||||
if (!multiLine) spaces();
|
||||
_stream->print("<!-- ");
|
||||
if (multiLine) _stream->println();
|
||||
_stream->print(text);
|
||||
if (multiLine) _stream->println();
|
||||
_stream->println(" -->");
|
||||
}
|
||||
|
||||
void XMLWriter::tagOpen(char* tag, bool newline)
|
||||
@ -58,7 +52,7 @@ void XMLWriter::tagOpen(char* tag, bool newline)
|
||||
|
||||
void XMLWriter::tagOpen(char* tag, char* name, bool newline)
|
||||
{
|
||||
strncpy(tagNames[_idx++], tag, 10);
|
||||
strncpy(tagStack[_idx++], tag, 10);
|
||||
tagStart(tag);
|
||||
if (name[0] != 0) tagField("name", name);
|
||||
tagEnd(newline, NOSLASH);
|
||||
@ -70,44 +64,44 @@ void XMLWriter::tagClose(bool indent)
|
||||
_indent -= _indentStep;
|
||||
if (indent) spaces();
|
||||
_stream->print("</");
|
||||
_stream->print(tagNames[--_idx]);
|
||||
_stream->print(tagStack[--_idx]);
|
||||
_stream->println(">");
|
||||
}
|
||||
|
||||
void XMLWriter::tagStart(char *tag)
|
||||
{
|
||||
spaces();
|
||||
_stream->write('<');
|
||||
_stream->write("<");
|
||||
_stream->print(tag);
|
||||
}
|
||||
|
||||
void XMLWriter::tagField(char *field, char* value)
|
||||
void XMLWriter::tagField(char *field, char* str)
|
||||
{
|
||||
_stream->write(' ');
|
||||
_stream->write(" ");
|
||||
_stream->print(field);
|
||||
_stream->print("=\"");
|
||||
#ifdef XMLWRITER_ESCAPE_SUPPORT
|
||||
escape(value);
|
||||
escape(str);
|
||||
#else
|
||||
_stream->print(value);
|
||||
_stream->print(str);
|
||||
#endif
|
||||
_stream->print("\"");
|
||||
}
|
||||
|
||||
void XMLWriter::tagEnd(bool newline, bool addSlash)
|
||||
{
|
||||
if (addSlash) _stream->write('/');
|
||||
_stream->print('>');
|
||||
if (addSlash) _stream->write("/");
|
||||
_stream->print(">");
|
||||
if (newline) _stream->println();
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, char* value)
|
||||
void XMLWriter::writeNode(char* tag, char* str)
|
||||
{
|
||||
tagOpen(tag, "", NONEWLINE);
|
||||
#ifdef XMLWRITER_ESCAPE_SUPPORT
|
||||
escape(value);
|
||||
escape(str);
|
||||
#else
|
||||
_stream->print(value);
|
||||
_stream->print(str);
|
||||
#endif
|
||||
tagClose(NOINDENT);
|
||||
}
|
||||
@ -118,44 +112,101 @@ void XMLWriter::setIndentSize(uint8_t size)
|
||||
}
|
||||
|
||||
#ifdef XMLWRITER_EXTENDED
|
||||
void XMLWriter::tagField(char *field, int value)
|
||||
void XMLWriter::tagField(char *field, uint8_t value, uint8_t base)
|
||||
{
|
||||
_stream->write(' ');
|
||||
tagField(field, (uint32_t) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::tagField(char *field, uint16_t value, uint8_t base)
|
||||
{
|
||||
tagField(field, (uint32_t) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::tagField(char *field, uint32_t value, uint8_t base)
|
||||
{
|
||||
_stream->write(" ");
|
||||
_stream->print(field);
|
||||
_stream->print("=\"");
|
||||
_stream->print(value);
|
||||
_stream->print(value, base);
|
||||
_stream->print("\"");
|
||||
}
|
||||
|
||||
void XMLWriter::tagField(char *field, long value)
|
||||
void XMLWriter::tagField(char *field, int8_t value, uint8_t base)
|
||||
{
|
||||
_stream->write(' ');
|
||||
tagField(field, (long) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::tagField(char *field, int16_t value, uint8_t base)
|
||||
{
|
||||
tagField(field, (long) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::tagField(char *field, int32_t value, uint8_t base)
|
||||
{
|
||||
_stream->write(" ");
|
||||
_stream->print(field);
|
||||
_stream->print("=\"");
|
||||
_stream->print(value);
|
||||
_stream->print(value, base);
|
||||
_stream->print("\"");
|
||||
}
|
||||
|
||||
|
||||
void XMLWriter::tagField(char *field, bool value)
|
||||
{
|
||||
_stream->write(" ");
|
||||
_stream->print(field);
|
||||
_stream->print("=\"");
|
||||
_stream->print(value?"true":"false");
|
||||
_stream->print("\"");
|
||||
}
|
||||
|
||||
void XMLWriter::tagField(char *field, double value, uint8_t decimals)
|
||||
{
|
||||
_stream->write(' ');
|
||||
_stream->write(" ");
|
||||
_stream->print(field);
|
||||
_stream->print("=\"");
|
||||
_stream->print(value);
|
||||
_stream->print(value, decimals);
|
||||
_stream->print("\"");
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, int value)
|
||||
void XMLWriter::writeNode(char* tag, uint8_t value, uint8_t base)
|
||||
{
|
||||
tagOpen(tag, "", NONEWLINE); // one line
|
||||
_stream->print(value);
|
||||
writeNode(tag, (uint32_t) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, uint16_t value, uint8_t base)
|
||||
{
|
||||
writeNode(tag, (uint32_t) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, uint32_t value, uint8_t base)
|
||||
{
|
||||
tagOpen(tag, "", NONEWLINE);
|
||||
_stream->print(value, base);
|
||||
tagClose(NOINDENT);
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, long value)
|
||||
void XMLWriter::writeNode(char* tag, int8_t value, uint8_t base)
|
||||
{
|
||||
writeNode(tag, (int32_t) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, int16_t value, uint8_t base)
|
||||
{
|
||||
writeNode(tag, (int32_t) value, base);
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, int32_t value, uint8_t base)
|
||||
{
|
||||
tagOpen(tag, "", NONEWLINE);
|
||||
_stream->print(value);
|
||||
_stream->print(value, base);
|
||||
tagClose(NOINDENT);
|
||||
}
|
||||
|
||||
void XMLWriter::writeNode(char* tag, bool value)
|
||||
{
|
||||
tagOpen(tag, "", NONEWLINE);
|
||||
_stream->print(value?"true":"false");
|
||||
tagClose(NOINDENT);
|
||||
}
|
||||
|
||||
@ -171,7 +222,7 @@ void XMLWriter::writeNode(char* tag, double value, uint8_t decimals)
|
||||
|
||||
void XMLWriter::spaces()
|
||||
{
|
||||
for (uint8_t i=_indent; i>0; i--) _stream->write(' ');
|
||||
for (uint8_t i=_indent; i>0; i--) _stream->write(" ");
|
||||
}
|
||||
|
||||
#ifdef XMLWRITER_ESCAPE_SUPPORT
|
||||
|
@ -3,7 +3,7 @@
|
||||
//
|
||||
// FILE: XMLWriter.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.02
|
||||
// VERSION: 0.1.03
|
||||
// DATE: 2013-11-06
|
||||
// PURPOSE: Simple XML writer library
|
||||
//
|
||||
@ -11,24 +11,24 @@
|
||||
//
|
||||
|
||||
#include "Arduino.h"
|
||||
// TODO pre 1.0 support ?
|
||||
// no pre 1.0 support!
|
||||
|
||||
#define XMLWRITER_VERSION "0.1.01"
|
||||
#define XMLWRITER_VERSION "0.1.03"
|
||||
|
||||
// for comment()
|
||||
#define MULTILINE true
|
||||
#define MULTILINE true
|
||||
|
||||
// for tagOpen(), tagEnd()
|
||||
#define NEWLINE true
|
||||
#define NONEWLINE false
|
||||
#define NOINDENT false // for tagClose()
|
||||
#define NEWLINE true
|
||||
#define NONEWLINE false
|
||||
#define NOINDENT false // for tagClose()
|
||||
|
||||
// for tagEnd()
|
||||
#define SLASH true
|
||||
#define NOSLASH false
|
||||
#define SLASH true
|
||||
#define NOSLASH false
|
||||
|
||||
// deepness of XML tree 5..10
|
||||
// needed for stack of tagnames
|
||||
// needed for stack of tagStack
|
||||
#define XMLWRITER_MAXLEVEL 5
|
||||
|
||||
// reduce footprint by not using all
|
||||
@ -71,12 +71,22 @@ public:
|
||||
void setIndentSize(uint8_t size);
|
||||
|
||||
#ifdef XMLWRITER_EXTENDED
|
||||
void tagField(char* field, int value);
|
||||
void tagField(char* field, long value);
|
||||
void tagField(char* field, double value, uint8_t decimals=2);
|
||||
void tagField(char* field, uint8_t value, uint8_t base=DEC);
|
||||
void tagField(char* field, uint16_t value, uint8_t base=DEC);
|
||||
void tagField(char* field, uint32_t value, uint8_t base=DEC);
|
||||
void tagField(char* field, int8_t value, uint8_t base=DEC);
|
||||
void tagField(char* field, int16_t value, uint8_t base=DEC);
|
||||
void tagField(char* field, int32_t value, uint8_t base=DEC);
|
||||
void tagField(char *field, bool value);
|
||||
void tagField(char* field, double value, uint8_t decimals=2);
|
||||
|
||||
void writeNode(char* tag, int value);
|
||||
void writeNode(char* tag, long value);
|
||||
void writeNode(char* tag, uint8_t value, uint8_t base=DEC);
|
||||
void writeNode(char* tag, uint16_t value, uint8_t base=DEC);
|
||||
void writeNode(char* tag, uint32_t value, uint8_t base=DEC);
|
||||
void writeNode(char* tag, int8_t value, uint8_t base=DEC);
|
||||
void writeNode(char* tag, int16_t value, uint8_t base=DEC);
|
||||
void writeNode(char* tag, int32_t value, uint8_t base=DEC);
|
||||
void writeNode(char* tag, bool value);
|
||||
void writeNode(char* tag, double value, uint8_t decimals=2);
|
||||
#endif
|
||||
|
||||
@ -98,7 +108,7 @@ private:
|
||||
// stack - used to remember the current tagname to create
|
||||
// automatic the right close tag.
|
||||
uint8_t _idx;
|
||||
char tagNames[XMLWRITER_MAXLEVEL][11];
|
||||
char tagStack[XMLWRITER_MAXLEVEL][11];
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -20,7 +20,7 @@ void setup()
|
||||
Serial.begin(115200);
|
||||
|
||||
XML.header();
|
||||
XML.comment("XMLWriterTest.ino\nThis is a demo of a simple XML lib for Arduino", MULTILINE); // MULTILINE == true.
|
||||
XML.comment("XMLWriterTest.ino\nThis is a demo of a simple XML lib for Arduino", true);
|
||||
|
||||
XML.tagOpen("Arduino", "42");
|
||||
|
||||
@ -32,6 +32,7 @@ void setup()
|
||||
|
||||
Weather();
|
||||
Weather2();
|
||||
DataTypes();
|
||||
|
||||
XML.tagClose();
|
||||
}
|
||||
@ -85,8 +86,33 @@ void DigitalPorts()
|
||||
XML.tagClose();
|
||||
}
|
||||
|
||||
void DataTypes()
|
||||
{
|
||||
XML.comment("Testing dataTypes I");
|
||||
XML.tagOpen("Datatypes");
|
||||
XML.writeNode("Bool", 1 == 1);
|
||||
XML.writeNode("Bool", 1 == 0);
|
||||
XML.writeNode("BIN", 42, BIN);
|
||||
XML.writeNode("DEC", 42, DEC);
|
||||
XML.writeNode("HEX", 42, HEX);
|
||||
XML.writeNode("OCT", 42, OCT);
|
||||
XML.tagClose();
|
||||
|
||||
XML.comment("Testing dataTypes II");
|
||||
for (int i=0; i<3; i++)
|
||||
{
|
||||
XML.tagStart("dataTypes");
|
||||
XML.tagField("Bool", 1 == 1);
|
||||
XML.tagField("Bool", 1 == 0);
|
||||
int x = analogRead(A0);
|
||||
XML.tagField("BIN", x, BIN);
|
||||
XML.tagField("DEC", x, DEC);
|
||||
XML.tagField("HEX", x, HEX);
|
||||
XML.tagField("OCT", x, OCT);
|
||||
XML.tagEnd();
|
||||
}
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user