-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonSerialStream.cpp
More file actions
63 lines (54 loc) · 2.17 KB
/
JsonSerialStream.cpp
File metadata and controls
63 lines (54 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Format to JSON as data is printed to Serial
// This class was written to transfer information off the Arduino using the
// JavaScript Object Notation (JSON) standard. This causes us to send
// extra characters, take the time to format the data, and this class
// takes executable space, but this standardization will improve usability
// on reciever's side as many libraries exist for many langauges to
// "rehydrate" JSON strings into variables again.
// This class makes extensive use of template functions. These are
// functions in which a datatype is variable. This class makes use of this
// in order to make functions independent of input type, essentially
// acting as an overload for any datatype, and instead the work of sending
// the data by Serial is put upon .print() and not me.
// Please note:
// - template functions are required to be implemented at declaration
// - the use of F() is a macro to save RAM
// - the use of & is Pass by Reference, saves RAM and time by giving the
// actual String rather than a copy of the String
// - this class does not have proper format error checking
#include "JsonSerialStream.h"
// Attach JsonSerialStream to a HardwareSerial line
// serialStream call initializes object before constructor
JsonSerialStream::JsonSerialStream(HardwareSerial& serial) :
serialStream(serial)
{
empty = true;
}
// This class has many template classes that are implemented in the header
// void addName(S name)
// void addStringValue(S value)
// void addProperty(S name, String &value)
// void addProperty(S name, const char value[])
// void addProperty(S name, bool value)
// void addProperty(S name, T value)
// void addPropertyAsString(S name, T value)
// void addNestedObject(S objectName)
// Open JSON String
void JsonSerialStream::openMessage()
{
// Opening brace indicates new message object
serialStream.print(F("{"));
empty = true;
}
// Close JSON String
void JsonSerialStream::closeMessage()
{
// Newline \n indicates end of message
serialStream.print(F("}\n"));
}
// Close nested object, but not end of message
void JsonSerialStream::closeNestedObject()
{
serialStream.print(F("}"));
empty = false;
}