Skip to content

Commit e5f0ea3

Browse files
committed
auto-scaling added. support for X-Y graphing added.
1 parent ed4841e commit e5f0ea3

File tree

4 files changed

+200
-86
lines changed

4 files changed

+200
-86
lines changed

Plotter/Plotter.cpp

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,75 +5,99 @@ Plotter::Plotter() {
55
head = NULL;
66
total_size = 0;
77
num_graphs = 0;
8+
max_points_displayed = 0;
89
last_updated = millis();
910
}
1011

1112
Plotter::~Plotter() { }
1213

1314

14-
void Plotter::addTimeGraph(String title, String labelA, double* refA) {
15+
void Plotter::addTimeGraph(String title, int points_displayed, String labelA, double* refA) {
1516
GraphNode* temp = head;
1617
String labels[] = {labelA};
1718
double* refs[] = {refA};
18-
head = new GraphNode(title, labels, refs, 1, false);
19+
head = new GraphNode(title, labels, refs, 1, false, points_displayed);
1920
head->next = temp;
2021
total_size++;
2122
num_graphs++;
23+
if (points_displayed > max_points_displayed) {
24+
max_points_displayed = points_displayed;
25+
}
2226
last_updated = millis();
2327
}
2428

25-
void Plotter::addTimeGraph(String title, String labelA, double* refA,
29+
void Plotter::addTimeGraph(String title, int points_displayed, String labelA, double* refA,
2630
String labelB, double* refB) {
2731
GraphNode* temp = head;
2832
String labels[] = {labelA, labelB};
2933
double* refs[] = {refA, refB};
30-
head = new GraphNode(title, labels, refs, 2, false);
34+
head = new GraphNode(title, labels, refs, 2, false, points_displayed);
3135
head->next = temp;
3236
total_size+=2;
3337
num_graphs++;
38+
if (points_displayed > max_points_displayed) {
39+
max_points_displayed = points_displayed;
40+
}
3441
last_updated = millis();
3542
}
3643

37-
void Plotter::addTimeGraph(String title, String labelA, double* refA,
44+
void Plotter::addTimeGraph(String title, int points_displayed, String labelA, double* refA,
3845
String labelB, double* refB, String labelC, double* refC) {
3946
GraphNode* temp = head;
4047
String labels[] = {labelA, labelB, labelC};
4148
double* refs[] = {refA, refB, refC};
42-
head = new GraphNode(title, labels, refs, 3, false);
49+
head = new GraphNode(title, labels, refs, 3, false, points_displayed);
4350
head->next = temp;
4451
total_size+=3;
4552
num_graphs++;
53+
if (points_displayed > max_points_displayed) {
54+
max_points_displayed = points_displayed;
55+
}
4656
last_updated = millis();
4757
}
4858

4959

50-
void Plotter::addXYGraph(String title, String labelX, double* refX, String labelY, double* refY) {
60+
void Plotter::addXYGraph(String title, int points_displayed, String labelX, double* refX, String labelY, double* refY) {
61+
// Code for creating XYgraph...
62+
GraphNode* temp = head;
63+
String labels[] = {labelX, labelY};
64+
double* refs[] = {refX, refY};
65+
head = new GraphNode(title, labels, refs, 2, true, points_displayed);
66+
head->next = temp;
5167
total_size+=2;
5268
num_graphs++;
69+
if (points_displayed > max_points_displayed) {
70+
max_points_displayed = points_displayed;
71+
}
5372
last_updated = millis();
5473
}
5574

56-
void Plotter::init() {
57-
58-
}
59-
6075
void Plotter::plot() {
6176
// Print configuration code
62-
Serial.print(OUTER_KEY);
63-
Serial.print(num_graphs); Serial.print('@'); Serial.print(total_size); Serial.print('@');
77+
String code = OUTER_KEY;
78+
code += (num_graphs + INNER_KEY + total_size + INNER_KEY
79+
+ max_points_displayed + INNER_KEY + last_updated + INNER_KEY);
80+
Serial.print(code);
81+
/*
82+
Serial.print(OUTER_KEY);
83+
Serial.print(num_graphs); Serial.print('@'); Serial.print(num_time); Serial.print(INNER_KEY);
84+
Serial.print(total_size); Serial.print('@');
6485
Serial.print(last_updated); Serial.print('@'); Serial.println();
86+
*/
6587
GraphNode* temp = head;
6688
while (temp != NULL) {
89+
Serial.println();
6790
temp->plot();
6891
temp = temp->next;
6992
}
70-
Serial.print(OUTER_KEY);
93+
Serial.println(OUTER_KEY);
7194
}
7295

7396
// Functions for helper class
74-
Plotter::GraphNode::GraphNode(String title, String* _labels, double** _refs, int size, bool xvy) : title(title), size(size), xvy(xvy) {
97+
Plotter::GraphNode::GraphNode(String title, String* _labels, double** _refs, int size, bool xvy,
98+
int points_displayed) : title(title), size(size), xvy(xvy),
99+
points_displayed(points_displayed) {
75100
next = NULL;
76-
points_displayed = 300;
77101
labels = new String[size];
78102
refs = new double*[size];
79103
for (int i = 0; i < size; i++) {
@@ -84,12 +108,11 @@ Plotter::GraphNode::GraphNode(String title, String* _labels, double** _refs, int
84108

85109
void Plotter::GraphNode::plot() {
86110
Serial.print(title); Serial.print('@');
87-
Serial.print(true); Serial.print('@');
111+
Serial.print(xvy); Serial.print('@');
88112
Serial.print(points_displayed); Serial.print('@');
89113
Serial.print(size); Serial.print('@');
90114
for (int i = 0; i < size; i++) {
91115
Serial.print(labels[i]); Serial.print('@');
92116
Serial.print(*refs[i]); Serial.print('@');
93117
}
94-
Serial.println();
95118
}

Plotter/Plotter.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ class Plotter {
77
public:
88
Plotter();
99
~Plotter();
10-
void addTimeGraph(String title, String labelA, double* refA);
11-
void addTimeGraph(String title, String labelA, double* refA, String labelB, double* refB);
12-
void addTimeGraph(String title, String labelA, double* refA, String labelB, double* refB,
13-
String labelC, double* refC);
10+
void addTimeGraph(String title, int points_displayed, String labelA, double* refA);
11+
void addTimeGraph(String title, int points_displayed, String labelA, double* refA,
12+
String labelB, double* refB);
13+
void addTimeGraph(String title, int points_displayed, String labelA, double* refA,
14+
String labelB, double* refB, String labelC, double* refC);
1415

15-
void addXYGraph(String title, String labelX, double* refX, String labelY, double* refY);
16+
void addXYGraph(String title, int points_displayed,
17+
String labelX, double* refX, String labelY, double* refY);
1618
void init();
1719
void plot();
1820

@@ -22,7 +24,8 @@ class Plotter {
2224
class GraphNode {
2325
public:
2426
GraphNode* next;
25-
GraphNode(String title, String* labels, double** refs, int size, bool xvy);
27+
GraphNode(String title, String* labels, double** refs, int size, bool xvy,
28+
int points_displayed);
2629
void plot();
2730
private:
2831
bool xvy;
@@ -35,11 +38,14 @@ class Plotter {
3538
// Private members
3639
int num_graphs;
3740
int total_size;
41+
int max_points_displayed;
3842
unsigned long last_updated;
3943
GraphNode* head;
4044

41-
static const char OUTER_KEY = '#';
42-
static const char INNER_KEY = '@';
4345
};
4446

47+
static const String OUTER_KEY = "#";
48+
static const String INNER_KEY = "@";
49+
50+
4551
#endif

Plotter/examples/test/test.ino

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,21 @@ boolean anotherOne = true;
1313
void setup() {
1414
Serial.begin(9600);
1515
test = Plotter();
16-
test.addTimeGraph("Title of graph", "x label", &x);
16+
17+
test.addXYGraph("X-Y graph", 3000, "x", &x, "y", &y);
1718
x = 10;
1819
test.plot();
1920
}
2021

2122
void loop() {
2223

23-
if (millis() > 5000 && oneTime){
24-
test.addTimeGraph("Graph 2", "y label", &y, "z label", &z);
25-
oneTime = false;
26-
}
2724
if (millis() > 10100 && anotherOne) {
28-
test.addTimeGraph("Anothah one", "x", &x, "y", &y, "z", &z);
25+
test.addTimeGraph("Anothah one", 3000, "x", &x, "y", &y, "z", &z);
2926
anotherOne = false;
3027
}
31-
Serial.print("hello");
32-
x = 10*sin(2.0*PI*(millis()/1000.0));
33-
y = 10*cos(2.0*PI*(millis()/1000.0));
34-
z = 5*sin(2.0*PI*(millis()/1000.0));
28+
29+
x = 10*sin(2.0*PI*(millis()/10000.0));
30+
y = 10*cos(2.0*PI*(millis()/10000.0));
31+
z = 5*sin(2.0*PI*(millis()/10000.0));
3532
test.plot();
3633
}

0 commit comments

Comments
 (0)