@@ -39,7 +39,6 @@ final boolean DEBUG = false;
3939
4040// CONSTANTS
4141final char OUTER_KEY = ' #' ;
42- final String INNER_KEY = " @" ;
4342final int MARGIN_SZ = 20 ; // between plots
4443final int BG_COL = 75 ; // background
4544final int PORT_INTERVAL = 5000 ; // time to sit on each port
@@ -147,30 +146,41 @@ void serialEvent( Serial ser )
147146 try
148147 {
149148 String message = ser. readStringUntil( OUTER_KEY );
150- if ( message == null || message. isEmpty() || message. equals( " \n\n " + OUTER_KEY ) )
149+ if ( message == null || message. isEmpty() || message. equals( OUTER_KEY ) )
151150 {
152151 return ;
153152 }
154- String [] arrayMain = message. split( " \n " );
155-
153+
154+ JSONObject json = parseJSONObject( message );
155+
156+ if ( json == null )
157+ {
158+ return ;
159+ }
160+
156161 // ********************************************************* //
157162 // ************* PLOT SETUP FROM CONFIG CODE *************** //
158163 // ********************************************************* //
159-
164+
165+ String tempCode = " " ;
166+ boolean config = false ;
167+ if ( json. hasKey( " ng" ) && json. hasKey( " lu" ) )
168+ {
169+ tempCode = Integer . toString( json. getInt( " ng" ) ) + Integer . toString( json. getInt( " lu" ) );
170+ config = true ;
171+ }
172+
160173 // If config code has changed, need to go through setup again
161- if ( ! configCode. equals( arrayMain[ 0 ] ) )
174+ if ( config && ! configCode. equals( tempCode ) )
162175 {
163- String [] arraySub = arrayMain[0 ]. split( INNER_KEY );
176+ lastPortSwitch = millis (); // (likely on the right port, just need to reconfigure graph layout)
177+
164178 // 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 )
179+ numGraphs = json. getInt( " ng" );
180+
181+ JSONArray jsonGraphs = json. getJSONArray( " g" );
182+
183+ if ( jsonGraphs. size() != numGraphs )
174184 {
175185 return ;
176186 }
@@ -186,23 +196,26 @@ void serialEvent( Serial ser )
186196 // Iterate through the individual graph data blocks to get graph specific info
187197 for ( int i = 0 ; i < numGraphs; i++ )
188198 {
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 ] );
199+ JSONObject g = jsonGraphs. getJSONObject( i );
200+
201+ String title = g. getString( " t" );
202+ boolean xvyTemp = g. getInt( " xvy" ) == 1 ;
203+ int maxPoints = g. getInt( " pd" );
204+ int numVars = g. getInt( " sz" );
194205 String [] labelsTemp = new String [numVars];
195206 int [] colorsTemp = new int [numVars];
196207
197208 concatLabels += title;
198-
209+
210+ JSONArray l = g. getJSONArray( " l" );
211+ JSONArray c = g. getJSONArray( " c" );
199212 for ( int j = 0 ; j < numVars; j++ )
200213 {
201- labelsTemp[j] = arraySub[ 4 + 3 * j] ;
202- colorsTemp[j] = COLORMAP . get( arraySub[ 5 + 3 * j] );
214+ labelsTemp[j] = l . getString( j ) ;
215+ colorsTemp[j] = COLORMAP . get( c . getString( j ) );
203216 if ( colorsTemp[j] == 0 )
204217 {
205- logMessage( " Invalid color: " + arraySub[ 5 + 3 * j] + " , defaulting to green." , true );
218+ logMessage( " Invalid color: " + c . getString( j ) + " , defaulting to green." , true );
206219 colorsTemp[j] = COLORMAP . get( " green" );
207220 }
208221 concatLabels += labelsTemp[j];
@@ -222,7 +235,7 @@ void serialEvent( Serial ser )
222235 // Set new config code
223236 if ( concatLabels. equals( lastLabels ) ) // Only when we're sure on labels
224237 {
225- configCode = arrayMain[ 0 ] ;
238+ configCode = tempCode ;
226239 lastConfig = millis ();
227240 logMessage( " Configured " + graphs. size() + " graphs" , false );
228241 }
@@ -238,20 +251,20 @@ void serialEvent( Serial ser )
238251 // *********************************************************** //
239252 // ************ NORMAL PLOTTING FUNCTIONALITY **************** //
240253 // *********************************************************** //
241- int tempTime = millis ( );
254+ int tempTime = json . getInt( " t " );
242255
256+ JSONArray jsonGraphs = json. getJSONArray( " g" );
257+
243258 for ( int i = 0 ; i < numGraphs; i++ )
244259 {
245- String [] arraySub = arrayMain[i + 1 ] . split( INNER_KEY );
260+ JSONArray data = jsonGraphs . getJSONObject( i ) . getJSONArray( " d " );
246261
247- double [] tempData = new double [ (arraySub . length - 5 ) / 3 ];
262+ double [] tempData = new double [ data . size() ];
248263
249264 // Update graph objects with new data
250- int q = 0 ;
251- for ( int j = 6 ; j < arraySub. length; j += 3 )
265+ for ( int j = 0 ; j < data. size(); j++ )
252266 {
253- tempData[q] = Double . parseDouble( arraySub[j] );
254- q++ ;
267+ tempData[j] = data. getDouble( j );
255268 }
256269 graphs. get( i ). Update ( tempData, tempTime );
257270 }
0 commit comments