Skip to content

Commit 4b8ca40

Browse files
committed
Converted to flattened-JSON serialization. This enabled limiting full config info to an sparse interval, ultimately improving data transfer speeds over 2x.
1 parent bba1bab commit 4b8ca40

File tree

3 files changed

+187
-88
lines changed

3 files changed

+187
-88
lines changed

listener/listener.pde

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import java.util.concurrent.TimeUnit;
3535
import java.util.concurrent.TimeoutException;
3636

3737
// FLAG FOR DEBUG MODE
38-
final boolean DEBUG = false;
38+
final boolean DEBUG = true;
3939

4040
//CONSTANTS
4141
final char OUTER_KEY = '#';
@@ -94,6 +94,10 @@ void setup()
9494

9595
void draw()
9696
{
97+
// REMOVE ***
98+
int start = millis();
99+
// *** REMOVE
100+
97101
//PLOT ALL
98102
try
99103
{
@@ -139,38 +143,67 @@ void draw()
139143
}
140144
catch ( Exception e )
141145
{}
146+
147+
// REMOVE ***
148+
totalGraphing += ( millis() - start );
149+
// *** REMOVE
142150
}
143151

152+
// REMOVE **** timing vars
153+
int countTo = 1000;
154+
int count = 0;
155+
int startTrial = 0;
156+
int totalSerialEvent = 0;
157+
int totalGraphing = 0;
158+
// *** REMOVE
159+
144160
void serialEvent( Serial ser )
145161
{
162+
// REMOVE**** timing logic: start
163+
int start = millis();
164+
if ( count == 0 )
165+
{
166+
startTrial = start;
167+
}
168+
// ***** REMOVE
169+
146170
// Listen for serial data until #, the end of transmission key
147171
try
148172
{
149173
String message = ser.readStringUntil( OUTER_KEY );
150-
if ( message == null || message.isEmpty() || message.equals( "\n\n" + OUTER_KEY ) )
174+
if ( message == null || message.isEmpty() || message.equals( OUTER_KEY ) )
151175
{
152176
return;
153177
}
154-
String[] arrayMain = message.split( "\n" );
155-
178+
179+
JSONObject json = parseJSONObject( message );
180+
181+
if ( json == null )
182+
{
183+
return;
184+
}
185+
156186
// ********************************************************* //
157187
// ************* PLOT SETUP FROM CONFIG CODE *************** //
158188
// ********************************************************* //
159-
189+
190+
String tempCode = "";
191+
boolean config = false;
192+
if ( json.hasKey( "ng" ) && json.hasKey( "lu" ) )
193+
{
194+
tempCode = Integer.toString( json.getInt( "ng" ) ) + Integer.toString( json.getInt( "lu" ) );
195+
config = true;
196+
}
197+
160198
// If config code has changed, need to go through setup again
161-
if ( !configCode.equals( arrayMain[0] ) )
199+
if ( config && !configCode.equals( tempCode ) )
162200
{
163-
String[] arraySub = arrayMain[0].split( INNER_KEY );
164201
// Check for size of full transmission against expected to flag bad transmission
165-
try
166-
{
167-
numGraphs = Integer.parseInt( arraySub[0] );
168-
}
169-
catch ( Exception e )
170-
{
171-
return;
172-
}
173-
if ( arrayMain.length != numGraphs + 1 )
202+
numGraphs = json.getInt( "ng" );
203+
204+
JSONArray jsonGraphs = json.getJSONArray( "g" );
205+
206+
if ( jsonGraphs.size() != numGraphs )
174207
{
175208
return;
176209
}
@@ -186,23 +219,26 @@ void serialEvent( Serial ser )
186219
// Iterate through the individual graph data blocks to get graph specific info
187220
for ( int i = 0; i < numGraphs; i++ )
188221
{
189-
arraySub = arrayMain[i+1].split( INNER_KEY );
190-
String title = arraySub[0];
191-
boolean xvyTemp = Integer.parseInt( arraySub[1] ) == 1;
192-
int maxPoints = Integer.parseInt( arraySub[2] );
193-
int numVars = Integer.parseInt( arraySub[3] );
222+
JSONObject g = jsonGraphs.getJSONObject( i );
223+
224+
String title = g.getString( "t" );
225+
boolean xvyTemp = g.getInt( "xvy" ) == 1;
226+
int maxPoints = g.getInt( "pd" );
227+
int numVars = g.getInt( "sz" );
194228
String[] labelsTemp = new String[numVars];
195229
int[] colorsTemp = new int[numVars];
196230

197231
concatLabels += title;
198-
232+
233+
JSONArray l = g.getJSONArray( "l" );
234+
JSONArray c = g.getJSONArray( "c" );
199235
for ( int j = 0; j < numVars; j++ )
200236
{
201-
labelsTemp[j] = arraySub[4 + 3*j];
202-
colorsTemp[j] = COLORMAP.get( arraySub[5 + 3*j] );
237+
labelsTemp[j] = l.getString( j );
238+
colorsTemp[j] = COLORMAP.get( c.getString( j ) );
203239
if ( colorsTemp[j] == 0 )
204240
{
205-
logMessage( "Invalid color: " + arraySub[5 + 3*j] + ", defaulting to green.", true );
241+
logMessage( "Invalid color: " + c.getString( j ) + ", defaulting to green.", true );
206242
colorsTemp[j] = COLORMAP.get( "green" );
207243
}
208244
concatLabels += labelsTemp[j];
@@ -222,7 +258,7 @@ void serialEvent( Serial ser )
222258
// Set new config code
223259
if ( concatLabels.equals( lastLabels ) ) // Only when we're sure on labels
224260
{
225-
configCode = arrayMain[0];
261+
configCode = tempCode;
226262
lastConfig = millis();
227263
logMessage( "Configured " + graphs.size() + " graphs", false );
228264
}
@@ -238,20 +274,20 @@ void serialEvent( Serial ser )
238274
// *********************************************************** //
239275
// ************ NORMAL PLOTTING FUNCTIONALITY **************** //
240276
// *********************************************************** //
241-
int tempTime = millis();
277+
int tempTime = json.getInt( "t" );
242278

279+
JSONArray jsonGraphs = json.getJSONArray( "g" );
280+
243281
for ( int i = 0; i < numGraphs; i++ )
244282
{
245-
String[] arraySub = arrayMain[i+1].split( INNER_KEY );
283+
JSONArray data = jsonGraphs.getJSONObject( i ).getJSONArray( "d" );
246284

247-
double[] tempData = new double[ (arraySub.length - 5) / 3 ];
285+
double[] tempData = new double[ data.size() ];
248286

249287
// Update graph objects with new data
250-
int q = 0;
251-
for ( int j = 6; j < arraySub.length; j += 3 )
288+
for ( int j = 0; j < data.size(); j++ )
252289
{
253-
tempData[q] = Double.parseDouble( arraySub[j] );
254-
q++;
290+
tempData[j] = data.getDouble( j );
255291
}
256292
graphs.get( i ).Update( tempData, tempTime );
257293
}
@@ -261,6 +297,20 @@ void serialEvent( Serial ser )
261297
{
262298
logMessage( "Exception in serialEvent: " + e.toString(), true );
263299
}
300+
301+
// REMOVE ***** timing logic: end
302+
int end = millis();
303+
totalSerialEvent += ( end - start );
304+
count++;
305+
if ( count == countTo )
306+
{
307+
logMessage( "Time per " + count + " counts: overall: " + ( end - startTrial ) + ", in serialEvent: "
308+
+ totalSerialEvent + ", graphing: " + totalGraphing, false );
309+
count = 0;
310+
totalSerialEvent = 0;
311+
totalGraphing = 0;
312+
}
313+
// ***** REMOVE
264314
}
265315

266316
// Helper method to calculate bounds of graphs

plotter/Plotter.cpp

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929

3030
Plotter::Plotter()
3131
{
32-
Serial.begin(115200);
33-
head = NULL;
34-
tail = NULL;
35-
totalSize = 0;
36-
numGraphs = 0;
37-
maxPointsDisplayed = 0;
38-
lastUpdated = millis();
32+
Serial.begin( 115200 );
33+
head = NULL;
34+
tail = NULL;
35+
numGraphs = 0;
36+
lastUpdated = millis();
37+
counter = 0;
3938
}
4039

4140
Plotter::~Plotter()
@@ -65,13 +64,7 @@ void Plotter::AddGraphHelper( String title, VariableWrapper * wrappers, int sz,
6564
tail = temp;
6665
}
6766

68-
totalSize += sz;
69-
numGraphs++;
70-
if ( pointsDisplayed > maxPointsDisplayed )
71-
{
72-
maxPointsDisplayed = pointsDisplayed;
73-
}
74-
67+
numGraphs++;
7568
lastUpdated = millis();
7669
}
7770

@@ -99,7 +92,6 @@ bool Plotter::Remove( int index )
9992
}
10093
last->next = temp->next;
10194
numGraphs--;
102-
totalSize -= temp->size;
10395
delete temp;
10496
}
10597
lastUpdated = millis();
@@ -167,18 +159,35 @@ bool Plotter::SetColorHelper( int index, int sz, String * colors )
167159

168160
void Plotter::Plot()
169161
{
170-
String code = OUTER_KEY;
171-
code += ( numGraphs + INNER_KEY + totalSize + INNER_KEY
172-
+ maxPointsDisplayed + INNER_KEY + lastUpdated + INNER_KEY );
173-
Serial.print( code );
162+
bool config = counter == 0;
163+
164+
Serial.print( "{\"" + TIME_KEY + "\":" ); Serial.print( millis() );
165+
166+
if ( config )
167+
{
168+
Serial.print( ",\"" + NUM_GRAPH_KEY + "\":" ); Serial.print( numGraphs );
169+
Serial.print( ",\"" + LAST_UPDATED_KEY + "\":" ); Serial.print( lastUpdated );
170+
}
171+
172+
Serial.print( ",\"" + GRAPHS_KEY + "\":[" );
173+
174174
Graph * temp = head;
175-
while ( temp != NULL )
175+
while ( temp )
176176
{
177-
Serial.println();
178-
temp->Plot();
177+
temp->Plot( config );
179178
temp = temp->next;
179+
if ( temp )
180+
{
181+
Serial.print( "," );
182+
}
183+
}
184+
Serial.print( "]}" ); Serial.println( OUTER_KEY );
185+
186+
counter++;
187+
if ( counter >= CONFIG_INTERVAL )
188+
{
189+
counter = 0;
180190
}
181-
Serial.println( OUTER_KEY );
182191
}
183192

184193
// Graph
@@ -197,21 +206,50 @@ Plotter::Graph::~Graph()
197206
delete[] wrappers;
198207
}
199208

200-
void Plotter::Graph::Plot()
209+
void Plotter::Graph::Plot( bool config )
201210
{
202-
Serial.print( title ); Serial.print( INNER_KEY );
203-
Serial.print( xvy ); Serial.print( INNER_KEY );
204-
Serial.print( pointsDisplayed ); Serial.print( INNER_KEY );
205-
Serial.print( size ); Serial.print( INNER_KEY );
206-
211+
Serial.print( "{" );
212+
213+
if ( config )
214+
{
215+
Serial.print( "\"" + TITLE_KEY + "\":" ); Serial.print( "\"" + title + "\"" );
216+
Serial.print( ",\"" + XVY_KEY + "\":" ); Serial.print( xvy );
217+
Serial.print( ",\"" + POINTS_DISPLAYED_KEY + "\":" ); Serial.print( pointsDisplayed );
218+
Serial.print( ",\"" + SIZE_KEY + "\":" ); Serial.print( size );
219+
Serial.print( ",\"" + LABELS_KEY + "\":[" );
220+
for ( int i = 0; i < size; i++ )
221+
{
222+
Serial.print( "\"" + wrappers[i].GetLabel() + "\"" );
223+
if ( i + 1 < size )
224+
{
225+
Serial.print( "," );
226+
}
227+
}
228+
Serial.print( "],\"" + COLORS_KEY + "\":[" );
229+
for ( int i = 0; i < size; i++ )
230+
{
231+
Serial.print( "\"" + wrappers[i].GetColor() + "\"" );
232+
if ( i + 1 < size )
233+
{
234+
Serial.print( "," );
235+
}
236+
}
237+
Serial.print( "]," );
238+
}
239+
240+
Serial.print( "\"" + DATA_KEY + "\":[" );
207241
char val[15];
208242
for (int i = 0; i < size; i++)
209243
{
210-
Serial.print( wrappers[i].GetLabel() ); Serial.print( INNER_KEY );
211-
Serial.print( wrappers[i].GetColor() ); Serial.print( INNER_KEY );
212244
dtostrf( wrappers[i].GetValue(), 1, 7, val );
213-
Serial.print( val ); Serial.print( INNER_KEY );
245+
Serial.print( val );
246+
if ( i + 1 < size )
247+
{
248+
Serial.print( "," );
249+
}
214250
}
251+
252+
Serial.print( "]}" );
215253
}
216254

217255
bool Plotter::Graph::SetColor( int sz, String * colors )

0 commit comments

Comments
 (0)