From a5b61f465defa0c0fe46b970868c1b2fd6f02fef Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 1 Feb 2024 17:24:03 +1100 Subject: [PATCH] x --- libraries/AP_JSONParser/AP_JSONParser.cpp | 36 ++++++++++ libraries/AP_JSONParser/AP_JSONParser.h | 11 +++ libraries/SITL/SIM_XPlane.cpp | 82 ++++------------------- 3 files changed, 59 insertions(+), 70 deletions(-) diff --git a/libraries/AP_JSONParser/AP_JSONParser.cpp b/libraries/AP_JSONParser/AP_JSONParser.cpp index 2e4f4f323bf4aa..475e695a5c57c2 100644 --- a/libraries/AP_JSONParser/AP_JSONParser.cpp +++ b/libraries/AP_JSONParser/AP_JSONParser.cpp @@ -225,4 +225,40 @@ bool AP_JSONParser::shift_token_object(uint16_t &ret) return true; } +bool AP_JSONParser::shift_struct(const ObjectBits &bits) +{ + uint16_t n_object_kv_pairs; + if (!shift_token_object(n_object_kv_pairs)) { + return false; + } + + for (uint8_t i=0;ilabel)) { + continue; + } + switch (bits->type) { + case Type::STRING: + char unused[50] {}; + if (!shift_token_string((char*)bits->ptr, bits->ptrsize)) { + return false; + } + break; + case Type::INT32: + if (!shift_token_int32_t(*(int32_t*)(bits->ptr))) { + return false; + } + case Type::FLOAT: + if (!shift_token_float(*(float*)(bits->ptr))) { + return false; + } + } + } + } +} + #endif // AP_JSONPARSER_ENABLED diff --git a/libraries/AP_JSONParser/AP_JSONParser.h b/libraries/AP_JSONParser/AP_JSONParser.h index e0b2543d9d212d..b8dcb788af970a 100644 --- a/libraries/AP_JSONParser/AP_JSONParser.h +++ b/libraries/AP_JSONParser/AP_JSONParser.h @@ -57,6 +57,17 @@ class AP_JSONParser bool shift_token_vector3f(Vector3f &ret) WARN_IF_UNUSED; bool shift_token_object(uint16_t &count) WARN_IF_UNUSED; + struct ObjectBits { + enum class Type { + STRING, + INT32, + FLOAT, + }; + const char *label; // e.g. "channel", "mask", "type" + int8_t jsmn_type; + void *data_ptr; // this is where the data goes... + }; + private: // this object may be declared on the stack, so initialise: diff --git a/libraries/SITL/SIM_XPlane.cpp b/libraries/SITL/SIM_XPlane.cpp index ae8328d04dd2b6..643d69eb8df731 100644 --- a/libraries/SITL/SIM_XPlane.cpp +++ b/libraries/SITL/SIM_XPlane.cpp @@ -141,43 +141,13 @@ bool XPlane::handle_axis(const char *label, class ::AP_JSONParser &json_parser) return false; } - for (uint8_t i=0;ichannel = value; - continue; - } - if (streq(n, "input_min")) { - float value; - if (!json_parser.shift_token_float(value)) { - return false; - } - j->input_min = value; - continue; - } - if (streq(n, "input_max")) { - float value; - if (!json_parser.shift_token_float(value)) { - return false; - } - j->input_max = value; - continue; - } - if (streq(n, "type")) { - char unused[50] {}; - if (!json_parser.shift_token_string(unused, ARRAY_SIZE(unused))) { - return false; - } - continue; - } - ::printf("Unknown axis nv %s\n", n); + const struct JSONParser::ObjectBits button_bits { + { "channel", JSONParser::ObjectBits::Type::INT32, (void*)&j->channel }, + { "input_min", JSONParser::ObjectBits::Type::FLAOT, (void*)&j->input_min }, + { "input_max", JSONParser::ObjectBits::Type::FLAOT, (void*)&j->input_max }, + }; + + if (!json_parser->shift_struct(button_bits)) { return false; } @@ -197,40 +167,12 @@ bool XPlane::handle_button(const char *label, class ::AP_JSONParser &json_parser } j->type = JoyType::BUTTON; - uint16_t n_object_kv_pairs; - if (!json_parser.shift_token_object(n_object_kv_pairs)) { - return false; - } + const struct JSONParser::ObjectBits button_bits { + { "channel", JSONParser::ObjectBits::Type::INT32, (void*)&j->channel }, + { "mask", JSONParser::ObjectBits::Type::INT32, (void*)&j->mask }, + }; - for (uint8_t i=0;ichannel = value; - continue; - } - if (streq(n, "mask")) { - int32_t value; - if (!json_parser.shift_token_int32_t(value)) { - return false; - } - j->mask = value; - continue; - } - if (streq(n, "type")) { - char unused[50] {}; - if (!json_parser.shift_token_string(unused, ARRAY_SIZE(unused))) { - return false; - } - continue; - } - ::printf("Unknown button nv %s\n", n); + if (!json_parser->shift_struct(button_bits)) { return false; }