Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbarker committed Feb 1, 2024
1 parent 350b6de commit a5b61f4
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 70 deletions.
36 changes: 36 additions & 0 deletions libraries/AP_JSONParser/AP_JSONParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;i<n_object_kv_pairs; i++) {
char n[50] {};
if (!shift_token_string(n, ARRAY_SIZE(n))) {
return false;
}
for (auto &bit : bits) {
if (!streq(n, bits->label)) {
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
11 changes: 11 additions & 0 deletions libraries/AP_JSONParser/AP_JSONParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
82 changes: 12 additions & 70 deletions libraries/SITL/SIM_XPlane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,43 +141,13 @@ bool XPlane::handle_axis(const char *label, class ::AP_JSONParser &json_parser)
return false;
}

for (uint8_t i=0;i<n_object_kv_pairs; i++) {
char n[50] {};
if (!json_parser.shift_token_string(n, ARRAY_SIZE(n))) {
return false;
}
if (streq(n, "channel")) {
int32_t value;
if (!json_parser.shift_token_int32_t(value)) {
return false;
}
j->channel = 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;
}

Expand All @@ -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;i<n_object_kv_pairs; i++) {
char n[50] {};
if (!json_parser.shift_token_string(n, ARRAY_SIZE(n))) {
return false;
}
if (streq(n, "channel")) {
int32_t value;
if (!json_parser.shift_token_int32_t(value)) {
return false;
}
j->channel = 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;
}

Expand Down

0 comments on commit a5b61f4

Please sign in to comment.