1
1
import processing.serial.* ;
2
2
Serial port;
3
3
4
-
5
4
// CONSTANTS
6
5
final int [] COLORS = {#00FF00,#FF0000 ,#0000FF}; // int color codes
6
+ final char OUTER_KEY = ' #' ;
7
+ final String INNER_KEY = " @" ;
7
8
8
9
// Setup and config Globals
9
10
int h;
@@ -20,7 +21,7 @@ int[][] pos_graphs; // stored as {x, width, y, height}
20
21
String [] titles;
21
22
String [] labels;
22
23
boolean [] xvy;
23
- int [] min_var_index_graphs ;
24
+ int [] first_index_graphs ;
24
25
int [] sz_graphs;
25
26
26
27
// Data handling Globals
@@ -61,7 +62,7 @@ void plot_time(int graph_index) {
61
62
rect (pos_graphs[g][0 ],pos_graphs[g][1 ],sub_width,sub_height);
62
63
63
64
int textPos = pos_graphs[g][1 ] + 30 ;
64
- int k = min_var_index_graphs [g];
65
+ int k = first_index_graphs [g];
65
66
for (int i = 0 ; i < sz_graphs[g]; i++ ) {
66
67
fill (COLORS [i]);
67
68
text (labels[k + i],pos_graphs[g][0 ] + 10 ,textPos);
@@ -79,7 +80,7 @@ void plot_time(int graph_index) {
79
80
void serialEvent (Serial ser ) {
80
81
// Listen for serial data until #, the end of transmission key
81
82
try {
82
- String temp = ser. readStringUntil(' # ' );
83
+ String temp = ser. readStringUntil(OUTER_KEY );
83
84
String [] array_main = temp. split(" \n " );
84
85
85
86
// ********************************************************* //
@@ -88,10 +89,13 @@ void serialEvent(Serial ser) {
88
89
89
90
// If config code has changed, need to go through setup again
90
91
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
94
94
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
95
99
int num_high = 1 ;
96
100
int num_wide = 1 ;
97
101
// Increase num subsections in each direction until all graphs can fit
@@ -103,7 +107,7 @@ void serialEvent(Serial ser) {
103
107
num_wide++ ;
104
108
}
105
109
}
106
-
110
+
107
111
// Set bounding box for each subsection
108
112
pos_graphs = new int [num_graphs][2 ];
109
113
int k = 0 ; // k tracks overall graph index
@@ -125,7 +129,7 @@ void serialEvent(Serial ser) {
125
129
titles = new String [num_graphs];
126
130
xvy = new boolean [num_graphs];
127
131
sz_graphs = new int [num_graphs];
128
- min_var_index_graphs = new int [num_graphs];
132
+ first_index_graphs = new int [num_graphs];
129
133
data = new double [sub_width][total_vars];
130
134
pos_x = new int [sub_width];
131
135
val_avg = new double [total_vars];
@@ -135,12 +139,12 @@ void serialEvent(Serial ser) {
135
139
// Iterate through the individual graph data blocks to get graph specific info
136
140
k = 0 ; // k tracks overall variable index
137
141
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 );
139
143
titles[i] = array_sub[0 ];
140
144
xvy[i] = Boolean . parseBoolean(array_sub[1 ]);
141
145
num2avg[i] = round (Integer . parseInt(array_sub[2 ]) / sub_width);
142
146
sz_graphs[i] = Integer . parseInt(array_sub[3 ]);
143
- min_var_index_graphs [i] = k;
147
+ first_index_graphs [i] = k;
144
148
int p = 4 ; // first label of this graph falls at index 4
145
149
for (int j = 0 ; j < sz_graphs[i]; j++ ) {
146
150
labels[k] = array_sub[p];
@@ -155,44 +159,44 @@ void serialEvent(Serial ser) {
155
159
} else {
156
160
// Matching a code means we have configured correctly
157
161
configured = true ;
158
- }
159
-
160
- // *********************************************************** //
161
- // ************ NORMAL PLOTTING FUNCTIONALITY **************** //
162
- // *********************************************************** //
163
162
164
- for (int i = 0 ; i < num_graphs; i++ ) {
165
- String [] array_sub = array_main[i+ 1 ]. split(" @" );
166
163
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
181
173
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
186
183
}
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 ;
190
196
}
191
- weight_avg[i] = 0 ;
192
197
}
193
- }
198
+ }
194
199
}
195
-
196
200
}
197
201
catch (Exception e) {
198
202
println (" exception...." );
0 commit comments