aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Tools/tests-host/sf0x_test.cpp39
-rw-r--r--src/drivers/sf0x/sf0x_parser.cpp151
2 files changed, 102 insertions, 88 deletions
diff --git a/Tools/tests-host/sf0x_test.cpp b/Tools/tests-host/sf0x_test.cpp
index 6b98c2427..82d19fcbe 100644
--- a/Tools/tests-host/sf0x_test.cpp
+++ b/Tools/tests-host/sf0x_test.cpp
@@ -8,7 +8,8 @@
#include <drivers/sf0x/sf0x_parser.h>
-int main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
warnx("SF0X test started");
int ret = 0;
@@ -18,22 +19,22 @@ int main(int argc, char *argv[]) {
_linebuf[0] = '\0';
const char *lines[] = {"0.01\r\n",
- "0.02\r\n",
- "0.03\r\n",
- "0.04\r\n",
- "0",
- ".",
- "0",
- "5",
- "\r",
- "\n",
- "0",
- "3\r",
- "\n"
- "\r\n",
- "0.06",
- "\r\n"
- };
+ "0.02\r\n",
+ "0.03\r\n",
+ "0.04\r\n",
+ "0",
+ ".",
+ "0",
+ "5",
+ "\r",
+ "\n",
+ "0",
+ "3\r",
+ "\n"
+ "\r\n",
+ "0.06",
+ "\r\n"
+ };
enum SF0X_PARSE_STATE state = SF0X_PARSE_STATE0_UNSYNC;
float dist_m;
@@ -46,14 +47,14 @@ int main(int argc, char *argv[]) {
int parse_ret;
- for (int i = 0; i < strlen(lines[l]); i++)
- {
+ for (int i = 0; i < strlen(lines[l]); i++) {
parse_ret = sf0x_parser(lines[l][i], _parserbuf, &_parsebuf_index, &state, &dist_m);
if (parse_ret == 0) {
printf("\nparsed: %f %s\n", dist_m, (parse_ret == 0) ? "OK" : "");
}
}
+
printf("%s", lines[l]);
}
diff --git a/src/drivers/sf0x/sf0x_parser.cpp b/src/drivers/sf0x/sf0x_parser.cpp
index cddc3fe99..8e73b0ad3 100644
--- a/src/drivers/sf0x/sf0x_parser.cpp
+++ b/src/drivers/sf0x/sf0x_parser.cpp
@@ -47,7 +47,7 @@
#ifdef SF0X_DEBUG
#include <stdio.h>
-const char* parser_state[] = {
+const char *parser_state[] = {
"0_UNSYNC",
"1_SYNC",
"2_GOT_DIGIT0",
@@ -64,74 +64,87 @@ int sf0x_parser(char c, char *parserbuf, unsigned *parserbuf_index, enum SF0X_PA
char *end;
switch (*state) {
- case SF0X_PARSE_STATE0_UNSYNC:
- if (c == '\n') {
- *state = SF0X_PARSE_STATE1_SYNC;
- (*parserbuf_index) = 0;
- }
- break;
-
- case SF0X_PARSE_STATE1_SYNC:
- if (c >= '0' && c <= '9') {
- *state = SF0X_PARSE_STATE2_GOT_DIGIT0;
- parserbuf[*parserbuf_index] = c;
- (*parserbuf_index)++;
- }
- break;
-
- case SF0X_PARSE_STATE2_GOT_DIGIT0:
- if (c >= '0' && c <= '9') {
- *state = SF0X_PARSE_STATE2_GOT_DIGIT0;
- parserbuf[*parserbuf_index] = c;
- (*parserbuf_index)++;
- } else if (c == '.') {
- *state = SF0X_PARSE_STATE3_GOT_DOT;
- parserbuf[*parserbuf_index] = c;
- (*parserbuf_index)++;
- } else {
- *state = SF0X_PARSE_STATE0_UNSYNC;
- }
- break;
-
- case SF0X_PARSE_STATE3_GOT_DOT:
- if (c >= '0' && c <= '9') {
- *state = SF0X_PARSE_STATE4_GOT_DIGIT1;
- parserbuf[*parserbuf_index] = c;
- (*parserbuf_index)++;
- } else {
- *state = SF0X_PARSE_STATE0_UNSYNC;
- }
- break;
-
- case SF0X_PARSE_STATE4_GOT_DIGIT1:
- if (c >= '0' && c <= '9') {
- *state = SF0X_PARSE_STATE5_GOT_DIGIT2;
- parserbuf[*parserbuf_index] = c;
- (*parserbuf_index)++;
- } else {
- *state = SF0X_PARSE_STATE0_UNSYNC;
- }
- break;
-
- case SF0X_PARSE_STATE5_GOT_DIGIT2:
- if (c == '\r') {
- *state = SF0X_PARSE_STATE6_GOT_CARRIAGE_RETURN;
- } else {
- *state = SF0X_PARSE_STATE0_UNSYNC;
- }
- break;
-
- case SF0X_PARSE_STATE6_GOT_CARRIAGE_RETURN:
- if (c == '\n') {
- parserbuf[*parserbuf_index] = '\0';
- *dist = strtod(parserbuf, &end);
- *state = SF0X_PARSE_STATE1_SYNC;
- *parserbuf_index = 0;
- ret = 0;
- } else {
- *state = SF0X_PARSE_STATE0_UNSYNC;
- }
- break;
+ case SF0X_PARSE_STATE0_UNSYNC:
+ if (c == '\n') {
+ *state = SF0X_PARSE_STATE1_SYNC;
+ (*parserbuf_index) = 0;
+ }
+
+ break;
+
+ case SF0X_PARSE_STATE1_SYNC:
+ if (c >= '0' && c <= '9') {
+ *state = SF0X_PARSE_STATE2_GOT_DIGIT0;
+ parserbuf[*parserbuf_index] = c;
+ (*parserbuf_index)++;
+ }
+
+ break;
+
+ case SF0X_PARSE_STATE2_GOT_DIGIT0:
+ if (c >= '0' && c <= '9') {
+ *state = SF0X_PARSE_STATE2_GOT_DIGIT0;
+ parserbuf[*parserbuf_index] = c;
+ (*parserbuf_index)++;
+
+ } else if (c == '.') {
+ *state = SF0X_PARSE_STATE3_GOT_DOT;
+ parserbuf[*parserbuf_index] = c;
+ (*parserbuf_index)++;
+
+ } else {
+ *state = SF0X_PARSE_STATE0_UNSYNC;
+ }
+
+ break;
+
+ case SF0X_PARSE_STATE3_GOT_DOT:
+ if (c >= '0' && c <= '9') {
+ *state = SF0X_PARSE_STATE4_GOT_DIGIT1;
+ parserbuf[*parserbuf_index] = c;
+ (*parserbuf_index)++;
+
+ } else {
+ *state = SF0X_PARSE_STATE0_UNSYNC;
+ }
+
+ break;
+
+ case SF0X_PARSE_STATE4_GOT_DIGIT1:
+ if (c >= '0' && c <= '9') {
+ *state = SF0X_PARSE_STATE5_GOT_DIGIT2;
+ parserbuf[*parserbuf_index] = c;
+ (*parserbuf_index)++;
+
+ } else {
+ *state = SF0X_PARSE_STATE0_UNSYNC;
+ }
+
+ break;
+
+ case SF0X_PARSE_STATE5_GOT_DIGIT2:
+ if (c == '\r') {
+ *state = SF0X_PARSE_STATE6_GOT_CARRIAGE_RETURN;
+
+ } else {
+ *state = SF0X_PARSE_STATE0_UNSYNC;
+ }
+
+ break;
+
+ case SF0X_PARSE_STATE6_GOT_CARRIAGE_RETURN:
+ if (c == '\n') {
+ parserbuf[*parserbuf_index] = '\0';
+ *dist = strtod(parserbuf, &end);
+ *state = SF0X_PARSE_STATE1_SYNC;
+ *parserbuf_index = 0;
+ ret = 0;
+
+ } else {
+ *state = SF0X_PARSE_STATE0_UNSYNC;
+ }
+
+ break;
}
#ifdef SF0X_DEBUG