@@ -39,7 +39,6 @@ final boolean DEBUG = false;
39
39
40
40
// CONSTANTS
41
41
final char OUTER_KEY = ' #' ;
42
- final String INNER_KEY = " @" ;
43
42
final int MARGIN_SZ = 20 ; // between plots
44
43
final int BG_COL = 75 ; // background
45
44
final int PORT_INTERVAL = 5000 ; // time to sit on each port
@@ -147,30 +146,41 @@ void serialEvent( Serial ser )
147
146
try
148
147
{
149
148
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 ) )
151
150
{
152
151
return ;
153
152
}
154
- String [] arrayMain = message. split( " \n " );
155
-
153
+
154
+ JSONObject json = parseJSONObject( message );
155
+
156
+ if ( json == null )
157
+ {
158
+ return ;
159
+ }
160
+
156
161
// ********************************************************* //
157
162
// ************* PLOT SETUP FROM CONFIG CODE *************** //
158
163
// ********************************************************* //
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
+
160
173
// If config code has changed, need to go through setup again
161
- if ( ! configCode. equals( arrayMain[ 0 ] ) )
174
+ if ( config && ! configCode. equals( tempCode ) )
162
175
{
163
- String [] arraySub = arrayMain[0 ]. split( INNER_KEY );
176
+ lastPortSwitch = millis (); // (likely on the right port, just need to reconfigure graph layout)
177
+
164
178
// 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 )
174
184
{
175
185
return ;
176
186
}
@@ -186,23 +196,26 @@ void serialEvent( Serial ser )
186
196
// Iterate through the individual graph data blocks to get graph specific info
187
197
for ( int i = 0 ; i < numGraphs; i++ )
188
198
{
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" );
194
205
String [] labelsTemp = new String [numVars];
195
206
int [] colorsTemp = new int [numVars];
196
207
197
208
concatLabels += title;
198
-
209
+
210
+ JSONArray l = g. getJSONArray( " l" );
211
+ JSONArray c = g. getJSONArray( " c" );
199
212
for ( int j = 0 ; j < numVars; j++ )
200
213
{
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 ) );
203
216
if ( colorsTemp[j] == 0 )
204
217
{
205
- logMessage( " Invalid color: " + arraySub[ 5 + 3 * j] + " , defaulting to green." , true );
218
+ logMessage( " Invalid color: " + c . getString( j ) + " , defaulting to green." , true );
206
219
colorsTemp[j] = COLORMAP . get( " green" );
207
220
}
208
221
concatLabels += labelsTemp[j];
@@ -222,7 +235,7 @@ void serialEvent( Serial ser )
222
235
// Set new config code
223
236
if ( concatLabels. equals( lastLabels ) ) // Only when we're sure on labels
224
237
{
225
- configCode = arrayMain[ 0 ] ;
238
+ configCode = tempCode ;
226
239
lastConfig = millis ();
227
240
logMessage( " Configured " + graphs. size() + " graphs" , false );
228
241
}
@@ -238,20 +251,20 @@ void serialEvent( Serial ser )
238
251
// *********************************************************** //
239
252
// ************ NORMAL PLOTTING FUNCTIONALITY **************** //
240
253
// *********************************************************** //
241
- int tempTime = millis ( );
254
+ int tempTime = json . getInt( " t " );
242
255
256
+ JSONArray jsonGraphs = json. getJSONArray( " g" );
257
+
243
258
for ( int i = 0 ; i < numGraphs; i++ )
244
259
{
245
- String [] arraySub = arrayMain[i + 1 ] . split( INNER_KEY );
260
+ JSONArray data = jsonGraphs . getJSONObject( i ) . getJSONArray( " d " );
246
261
247
- double [] tempData = new double [ (arraySub . length - 5 ) / 3 ];
262
+ double [] tempData = new double [ data . size() ];
248
263
249
264
// 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++ )
252
266
{
253
- tempData[q] = Double . parseDouble( arraySub[j] );
254
- q++ ;
267
+ tempData[j] = data. getDouble( j );
255
268
}
256
269
graphs. get( i ). Update ( tempData, tempTime );
257
270
}
0 commit comments