11/*
2- SparkFun SBF in SPARTN test example sketch
2+ SparkFun SBF-in- SPARTN test example sketch
33
44 The mosaic-X5 can output raw L-Band (LBandBeam1) data, interspersed with SBF messages
55
6- This example demonstrates how to use two parsers to extract the SBF from the L-Band stream
6+ This example demonstrates how to use two parsers to separate the SBF from the L-Band stream
7+ and extract SPARTN from the remaining raw L-Band
78
89 License: MIT. Please see LICENSE.md for more details
910*/
@@ -109,7 +110,6 @@ const uint8_t rawDataStream[] =
109110
110111uint32_t dataOffset1;
111112SEMP_PARSE_STATE *parse1;
112- uint32_t dataOffset2;
113113SEMP_PARSE_STATE *parse2;
114114
115115// ----------------------------------------
@@ -133,36 +133,30 @@ void setup()
133133 if (!parse1)
134134 reportFatalError (" Failed to initialize parser 1" );
135135
136+ sempEnableDebugOutput (parse1); // Debug - comment if desired
137+
138+ // Add the callback for invalid SBF data,
139+ // to allow it to be passed to the SPARTN parser
140+ sempSbfSetInvalidDataCallback (parse1, invalidSbfData);
141+
136142 parse2 = sempBeginParser (parserTable2, parserCount2,
137143 parser2Names, parser2NameCount,
138144 0 , BUFFER_LENGTH, processSpartnMessage, " SPARTN_Test" );
139145 if (!parse2)
140146 reportFatalError (" Failed to initialize parser 2" );
141147
148+ sempEnableDebugOutput (parse2); // Debug - comment if desired
149+
142150 // Obtain a raw data stream from somewhere
143151 Serial.printf (" Raw data stream: %d bytes\r\n " , RAW_DATA_BYTES);
144152
145153 // The raw data stream is passed to the SBF parser one byte at a time
146- sempEnableDebugOutput (parse1);
147-
148- // Any data which is not SBF is passed to the SPARTN parser
149- sempEnableDebugOutput (parse2);
150-
154+ // Any data which is not SBF is passed to the SPARTN parser via the callback
151155 for (dataOffset1 = 0 ; dataOffset1 < RAW_DATA_BYTES; dataOffset1++)
152156 {
153157 // Update the SBF parser state based on the incoming byte
158+ // Non-SBF data is parsed automatically by invalidSbfData
154159 sempParseNextByte (parse1, rawDataStream[dataOffset1]);
155-
156- // If the data is not SBF, the state will return to sempFirstByte
157- if (parse1->state == sempFirstByte)
158- {
159- // Data is not SBF, so pass it to the SPARTN parser
160- for (dataOffset2 = 0 ; dataOffset2 < parse1->length ; dataOffset2++)
161- {
162- // Update the SPARTN parser state based on the non-SBF byte
163- sempParseNextByte (parse2, parse1->buffer [dataOffset2]);
164- }
165- }
166160 }
167161
168162 // Done parsing the data
@@ -176,6 +170,18 @@ void loop()
176170 // Nothing to do here...
177171}
178172
173+ // Callback from within the SBF parser when invalid data is identified
174+ // The data is passed on to the SPARTN parser
175+ void invalidSbfData (SEMP_PARSE_STATE *parse)
176+ {
177+ // Data is not SBF, so pass it to the SPARTN parser
178+ for (uint32_t dataOffset = 0 ; dataOffset < parse->length ; dataOffset++)
179+ {
180+ // Update the SPARTN parser state based on the non-SBF byte
181+ sempParseNextByte (parse2, parse->buffer [dataOffset]);
182+ }
183+ }
184+
179185// Call back from within parser, for end of message
180186// Process a complete message incoming from parser
181187void processSbfMessage (SEMP_PARSE_STATE *parse, uint16_t type)
@@ -213,15 +219,13 @@ void processSpartnMessage(SEMP_PARSE_STATE *parse, uint16_t type)
213219 SEMP_SCRATCH_PAD *scratchPad = (SEMP_SCRATCH_PAD *)parse->scratchPad ;
214220
215221 static bool displayOnce = true ;
216- uint32_t offset;
217222
218223 // Display the raw message
219224 Serial.println ();
220- offset = dataOffset2 + 1 - parse->length ;
221- Serial.printf (" Valid SPARTN message, type %d, subtype %d : %d bytes at 0x%08lx (%ld)\r\n " ,
225+ Serial.printf (" Valid SPARTN message, type %d, subtype %d : %d bytes\r\n " ,
222226 scratchPad->spartn .messageType ,
223227 scratchPad->spartn .messageSubtype ,
224- parse->length , offset, offset );
228+ parse->length );
225229 dumpBuffer (parse->buffer , parse->length );
226230
227231 // Display the parser state
0 commit comments