Skip to content

Commit ed4841e

Browse files
committed
updated format to be arduino IDE library compliant. Added buffer key to beginning of transmission to flag handle bad transmissions
1 parent c3c57db commit ed4841e

File tree

7 files changed

+65
-44
lines changed

7 files changed

+65
-44
lines changed

Plotter.cpp renamed to Plotter/Plotter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ void Plotter::init() {
5959

6060
void Plotter::plot() {
6161
// Print configuration code
62+
Serial.print(OUTER_KEY);
6263
Serial.print(num_graphs); Serial.print('@'); Serial.print(total_size); Serial.print('@');
6364
Serial.print(last_updated); Serial.print('@'); Serial.println();
6465
GraphNode* temp = head;
6566
while (temp != NULL) {
6667
temp->plot();
6768
temp = temp->next;
6869
}
69-
Serial.print('#');
70+
Serial.print(OUTER_KEY);
7071
}
7172

7273
// Functions for helper class

Plotter.h renamed to Plotter/Plotter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class Plotter {
3838
unsigned long last_updated;
3939
GraphNode* head;
4040

41+
static const char OUTER_KEY = '#';
42+
static const char INNER_KEY = '@';
4143
};
4244

4345
#endif

examples/test/test.ino renamed to Plotter/examples/test/test.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void loop() {
2828
test.addTimeGraph("Anothah one", "x", &x, "y", &y, "z", &z);
2929
anotherOne = false;
3030
}
31+
Serial.print("hello");
3132
x = 10*sin(2.0*PI*(millis()/1000.0));
3233
y = 10*cos(2.0*PI*(millis()/1000.0));
3334
z = 5*sin(2.0*PI*(millis()/1000.0));

Plotter/keywords.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Plotter KEYWORD1
2+
addTimeGraph KEYWORD2
3+
addXYGraph KEYWORD2
4+
plot KEYWORD2

Plotter/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Plotter
2+
version=0.0
3+
author=Devin Conley <[email protected]>
4+
maintainer=Devin Conley <[email protected]>
5+
sentence=an Arduino library for easy plotting using Processing via serial communication.
6+
paragraph=Supports multi-variable plots against time as well as 2D plotting of an X vs Y variable. Multiple graphs can be displayed at once, will all formatting and scaling handled automatically. Various listener options are provided, including stand-alone options so that the Processing software does NOT need to be downloaded. The listener requires no modification; just start the program.
7+
category=Graphing Data
8+
url=https://github.com/devinconley/ArduinoPlotter
9+
architectures=*

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
arduinoPlotter
1+
ArduinoPlotter
22
===============
33
an Arduino library for easy plotting using Processing via serial communication
44

listeners/arduinoPlotter_processingListener/arduinoPlotter_processingListener.pde renamed to listeners/ArduinoPlotter_processingListener/ArduinoPlotter_processingListener.pde

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import processing.serial.*;
22
Serial port;
33

4-
54
//CONSTANTS
65
final int[] COLORS = {#00FF00,#FF0000,#0000FF}; //int color codes
6+
final char OUTER_KEY = '#';
7+
final String INNER_KEY = "@";
78

89
// Setup and config Globals
910
int h;
@@ -20,7 +21,7 @@ int[][] pos_graphs; // stored as {x, width, y, height}
2021
String[] titles;
2122
String[] labels;
2223
boolean[] xvy;
23-
int[] min_var_index_graphs;
24+
int[] first_index_graphs;
2425
int[] sz_graphs;
2526

2627
// Data handling Globals
@@ -61,7 +62,7 @@ void plot_time(int graph_index) {
6162
rect(pos_graphs[g][0],pos_graphs[g][1],sub_width,sub_height);
6263

6364
int textPos = pos_graphs[g][1] + 30;
64-
int k = min_var_index_graphs[g];
65+
int k = first_index_graphs[g];
6566
for (int i = 0; i < sz_graphs[g]; i++) {
6667
fill(COLORS[i]);
6768
text(labels[k + i],pos_graphs[g][0] + 10,textPos);
@@ -79,7 +80,7 @@ void plot_time(int graph_index) {
7980
void serialEvent(Serial ser) {
8081
// Listen for serial data until #, the end of transmission key
8182
try {
82-
String temp = ser.readStringUntil('#');
83+
String temp = ser.readStringUntil(OUTER_KEY);
8384
String[] array_main = temp.split("\n");
8485

8586
// ********************************************************* //
@@ -88,10 +89,13 @@ void serialEvent(Serial ser) {
8889

8990
// If config code has changed, need to go through setup again
9091
if (!config_code.equals(array_main[0])) {
91-
configured = false;
92-
String[] array_sub = array_main[0].split("@");
93-
// Get number of graphs and determine orientation of each
92+
String[] array_sub = array_main[0].split(INNER_KEY);
93+
// Check for size of full transmission against expected to flag bad transmission
9494
num_graphs = Integer.parseInt(array_sub[0]);
95+
if (array_main.length != num_graphs+2) {
96+
throw new Exception();
97+
}
98+
// Determine orientation of each graph
9599
int num_high = 1;
96100
int num_wide = 1;
97101
// Increase num subsections in each direction until all graphs can fit
@@ -103,7 +107,7 @@ void serialEvent(Serial ser) {
103107
num_wide++;
104108
}
105109
}
106-
110+
107111
// Set bounding box for each subsection
108112
pos_graphs = new int[num_graphs][2];
109113
int k = 0; // k tracks overall graph index
@@ -125,7 +129,7 @@ void serialEvent(Serial ser) {
125129
titles = new String[num_graphs];
126130
xvy = new boolean[num_graphs];
127131
sz_graphs = new int[num_graphs];
128-
min_var_index_graphs = new int[num_graphs];
132+
first_index_graphs = new int[num_graphs];
129133
data = new double[sub_width][total_vars];
130134
pos_x = new int[sub_width];
131135
val_avg = new double[total_vars];
@@ -135,12 +139,12 @@ void serialEvent(Serial ser) {
135139
// Iterate through the individual graph data blocks to get graph specific info
136140
k = 0; // k tracks overall variable index
137141
for (int i = 0; i < num_graphs; i++) {
138-
array_sub = array_main[i+1].split("@");
142+
array_sub = array_main[i+1].split(INNER_KEY);
139143
titles[i] = array_sub[0];
140144
xvy[i] = Boolean.parseBoolean(array_sub[1]);
141145
num2avg[i] = round(Integer.parseInt(array_sub[2]) / sub_width);
142146
sz_graphs[i] = Integer.parseInt(array_sub[3]);
143-
min_var_index_graphs[i] = k;
147+
first_index_graphs[i] = k;
144148
int p = 4; // first label of this graph falls at index 4
145149
for (int j = 0; j < sz_graphs[i]; j++) {
146150
labels[k] = array_sub[p];
@@ -155,44 +159,44 @@ void serialEvent(Serial ser) {
155159
} else {
156160
// Matching a code means we have configured correctly
157161
configured = true;
158-
}
159-
160-
// *********************************************************** //
161-
// ************ NORMAL PLOTTING FUNCTIONALITY **************** //
162-
// *********************************************************** //
163162

164-
for (int i = 0; i < num_graphs; i++) {
165-
String[] array_sub = array_main[i+1].split("@");
166163

167-
if (xvy[i]) {
168-
// Plot x vs y graph
169-
170-
} else {
171-
// TIME GRAPH HANDLER
172-
int p = 5; // first index of double in split array
173-
// j is only a counter for var in graph context
174-
for (int j = min_var_index_graphs[i]; j < min_var_index_graphs[i] + sz_graphs[i]; j++) {
175-
// Update rolling average for all values associated with that graph
176-
val_avg[j] = (weight_avg[i]*val_avg[j] + Double.parseDouble(array_sub[p]))
177-
/ (weight_avg[i] + 1);
178-
p += 2; // value will appear every 2 positions
179-
}
180-
weight_avg[i]++;
164+
// *********************************************************** //
165+
// ************ NORMAL PLOTTING FUNCTIONALITY **************** //
166+
// *********************************************************** //
167+
168+
for (int i = 0; i < num_graphs; i++) {
169+
String[] array_sub = array_main[i+1].split(INNER_KEY);
170+
171+
if (xvy[i]) {
172+
// Plot x vs y graph
181173

182-
// If enough values have been averaged for this graph, set vals appropriately
183-
if (weight_avg[i] >= num2avg[i]) {
184-
for (int j = min_var_index_graphs[i]; j < min_var_index_graphs[i]+sz_graphs[i]; j++) {
185-
data[pos_x[i]][j] = val_avg[j];
174+
} else {
175+
// TIME GRAPH HANDLER
176+
int p = 5; // first index of double in split array
177+
// j is only a counter for var in graph context
178+
for (int j = first_index_graphs[i]; j < first_index_graphs[i] + sz_graphs[i]; j++) {
179+
// Update rolling average for all values associated with that graph
180+
val_avg[j] = (weight_avg[i]*val_avg[j] + Double.parseDouble(array_sub[p]))
181+
/ (weight_avg[i] + 1);
182+
p += 2; // value will appear every 2 positions
186183
}
187-
pos_x[i]++; // advance position of next placed value
188-
if (pos_x[i] >= sub_width) {
189-
pos_x[i] = 0;
184+
weight_avg[i]++;
185+
186+
// If enough values have been averaged for this graph, set vals appropriately
187+
if (weight_avg[i] >= num2avg[i]) {
188+
for (int j = first_index_graphs[i]; j < first_index_graphs[i]+sz_graphs[i]; j++) {
189+
data[pos_x[i]][j] = val_avg[j];
190+
}
191+
pos_x[i]++; // advance position of next placed value
192+
if (pos_x[i] >= sub_width) {
193+
pos_x[i] = 0;
194+
}
195+
weight_avg[i] = 0;
190196
}
191-
weight_avg[i] = 0;
192197
}
193-
}
198+
}
194199
}
195-
196200
}
197201
catch (Exception e) {
198202
println("exception....");

0 commit comments

Comments
 (0)