@@ -35,7 +35,7 @@ import java.util.concurrent.TimeUnit;
3535import java.util.concurrent.TimeoutException ;
3636
3737// FLAG FOR DEBUG MODE
38- final boolean DEBUG = false ;
38+ final boolean DEBUG = true ;
3939
4040// CONSTANTS
4141final char OUTER_KEY = ' #' ;
@@ -94,6 +94,10 @@ void setup()
9494
9595void 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+
144160void 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
0 commit comments