Skip to content

Commit 56dbf02

Browse files
authored
Merge pull request #22 from LeeLeahy2/user-debug
Provide debugging examples for the user parser
2 parents 91f6bb0 + 7eef4be commit 56dbf02

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

Examples/User_Parser/User_Parser.ino

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@ bool userPreamble(SEMP_PARSE_STATE *parse, uint8_t data)
7676
return true;
7777
}
7878

79+
// Translates state value into an string, returns nullptr if not found
80+
const char * userParserStateName(const SEMP_PARSE_STATE *parse)
81+
{
82+
if (parse->state == userPreamble)
83+
return "userPreamble";
84+
if (parse->state == userSecondPreambleByte)
85+
return "userSecondPreambleByte";
86+
if (parse->state == userFindNumber)
87+
return "userFindNumber";
88+
return nullptr;
89+
}
90+
7991
//----------------------------------------
8092
// Constants
8193
//----------------------------------------
@@ -135,13 +147,12 @@ SEMP_PARSE_STATE *parse;
135147
void setup()
136148
{
137149
int dataIndex;
138-
int rawDataBytes;
139150

140151
delay(1000);
141152

142153
Serial.begin(115200);
143154
Serial.println();
144-
Serial.println("Muliple_Parser example sketch");
155+
Serial.println("User_Parser example sketch");
145156
Serial.println();
146157

147158
// Initialize the parser
@@ -153,13 +164,31 @@ void setup()
153164
reportFatalError("Failed to initialize the user parser");
154165

155166
// Obtain a raw data stream from somewhere
156-
Serial.printf("Raw data stream: %d bytes\r\n", rawDataBytes);
167+
Serial.printf("Raw data stream: %d bytes\r\n", RAW_DATA_BYTES);
157168

158169
// The raw data stream is passed to the parser one byte at a time
159170
sempSetPrintDebug(parse, &Serial);
160171
for (dataOffset = 0; dataOffset < RAW_DATA_BYTES; dataOffset++)
172+
{
173+
uint8_t data;
174+
const char * endState;
175+
const char * startState;
176+
177+
// Get the parse state before entering the parser to enable
178+
// printing of the parser transition
179+
startState = getParseStateName(parse);
180+
161181
// Update the parser state based on the incoming byte
162-
sempParseNextByte(parse, rawDataStream[dataOffset]);
182+
data = rawDataStream[dataOffset];
183+
sempParseNextByte(parse, data);
184+
185+
// Print the parser transition
186+
endState = getParseStateName(parse);
187+
Serial.printf("0x%02x (%c), state: (%p) %s --> %s (%p)\r\n",
188+
rawDataStream[dataOffset],
189+
((data >= ' ') && (data < 0x7f)) ? data : '.',
190+
startState, startState, endState, endState);
191+
}
163192

164193
// Done parsing the data
165194
sempStopParser(&parse);
@@ -195,6 +224,21 @@ void userMessage(SEMP_PARSE_STATE *parse, uint16_t type)
195224
}
196225
}
197226

227+
// Translate the state value into an ASCII state name
228+
const char *getParseStateName(SEMP_PARSE_STATE *parse)
229+
{
230+
const char *name;
231+
232+
do
233+
{
234+
name = userParserStateName(parse);
235+
if (name)
236+
break;
237+
name = sempGetStateName(parse);
238+
} while (0);
239+
return name;
240+
}
241+
198242
// Display the contents of a buffer
199243
void dumpBuffer(const uint8_t *buffer, uint16_t length)
200244
{

0 commit comments

Comments
 (0)