Skip to content

Commit 126fd21

Browse files
committed
Save another 1kB by changing the use of ArduinoJson
1 parent ecd13f0 commit 126fd21

File tree

1 file changed

+78
-67
lines changed

1 file changed

+78
-67
lines changed

src/lib/WIFI/devWIFI.cpp

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -270,9 +270,9 @@ static const char *GetConfigUidType(const JsonObject json)
270270

271271
static void GetConfiguration(AsyncWebServerRequest *request)
272272
{
273-
bool exportMode = request->hasArg("export");
273+
const bool exportMode = request->hasArg("export");
274274
auto *response = new AsyncJsonResponse();
275-
JsonObject json = response->getRoot();
275+
const auto json = response->getRoot();
276276

277277
if (!exportMode)
278278
{
@@ -281,7 +281,8 @@ static void GetConfiguration(AsyncWebServerRequest *request)
281281
json["options"] = options;
282282
}
283283

284-
JsonArray uid = json["config"]["uid"].to<JsonArray>();
284+
const auto cfg = json["config"].to<JsonObject>();
285+
const auto uid = cfg["uid"].to<JsonArray>();
285286
copyArray(UID, UID_LEN, uid);
286287

287288
#if defined(TARGET_TX)
@@ -293,66 +294,74 @@ static void GetConfiguration(AsyncWebServerRequest *request)
293294
for (int button=0 ; button<button_count ; button++)
294295
{
295296
const tx_button_color_t *buttonColor = config.GetButtonActions(button);
297+
const auto btn = cfg["button-actions"][button].to<JsonObject>();
296298
if (hardware_int(button == 0 ? HARDWARE_button_led_index : HARDWARE_button2_led_index) != -1) {
297-
json["config"]["button-actions"][button]["color"] = buttonColor->val.color;
299+
btn["color"] = buttonColor->val.color;
298300
}
299301
for (int pos=0 ; pos<button_GetActionCnt() ; pos++)
300302
{
301-
json["config"]["button-actions"][button]["action"][pos]["is-long-press"] = buttonColor->val.actions[pos].pressType ? true : false;
302-
json["config"]["button-actions"][button]["action"][pos]["count"] = buttonColor->val.actions[pos].count;
303-
json["config"]["button-actions"][button]["action"][pos]["action"] = buttonColor->val.actions[pos].action;
303+
const auto action = btn["action"][pos].to<JsonObject>();
304+
action["is-long-press"] = buttonColor->val.actions[pos].pressType ? true : false;
305+
action["count"] = buttonColor->val.actions[pos].count;
306+
action["action"] = buttonColor->val.actions[pos].action;
304307
}
305308
}
306309
if (exportMode)
307310
{
308-
json["config"]["fan-mode"] = config.GetFanMode();
309-
json["config"]["power-fan-threshold"] = config.GetPowerFanThreshold();
311+
cfg["fan-mode"] = config.GetFanMode();
312+
cfg["power-fan-threshold"] = config.GetPowerFanThreshold();
310313

311-
json["config"]["motion-mode"] = config.GetMotionMode();
314+
cfg["motion-mode"] = config.GetMotionMode();
312315

313-
json["config"]["vtx-admin"]["band"] = config.GetVtxBand();
314-
json["config"]["vtx-admin"]["channel"] = config.GetVtxChannel();
315-
json["config"]["vtx-admin"]["pitmode"] = config.GetVtxPitmode();
316-
json["config"]["vtx-admin"]["power"] = config.GetVtxPower();
317-
json["config"]["backpack"]["dvr-start-delay"] = config.GetDvrStartDelay();
318-
json["config"]["backpack"]["dvr-stop-delay"] = config.GetDvrStopDelay();
319-
json["config"]["backpack"]["dvr-aux-channel"] = config.GetDvrAux();
316+
const auto vtxAdmin = cfg["vtx-admin"].to<JsonObject>();
317+
vtxAdmin["band"] = config.GetVtxBand();
318+
vtxAdmin["channel"] = config.GetVtxChannel();
319+
vtxAdmin["pitmode"] = config.GetVtxPitmode();
320+
vtxAdmin["power"] = config.GetVtxPower();
321+
322+
const auto backpack = cfg["backpack"].to<JsonObject>();
323+
backpack["dvr-start-delay"] = config.GetDvrStartDelay();
324+
backpack["dvr-stop-delay"] = config.GetDvrStopDelay();
325+
backpack["dvr-aux-channel"] = config.GetDvrAux();
320326

321327
for (int model = 0 ; model < CONFIG_TX_MODEL_CNT ; model++)
322328
{
323329
const model_config_t &modelConfig = config.GetModelConfig(model);
324330
String strModel(model);
325-
JsonObject modelJson = json["config"]["model"][strModel].to<JsonObject>();
331+
const auto modelJson = cfg["model"][strModel].to<JsonObject>();
326332
modelJson["packet-rate"] = modelConfig.rate;
327333
modelJson["telemetry-ratio"] = modelConfig.tlm;
328334
modelJson["switch-mode"] = modelConfig.switchMode;
329-
modelJson["power"]["max-power"] = modelConfig.power;
330-
modelJson["power"]["dynamic-power"] = modelConfig.dynamicPower;
331-
modelJson["power"]["boost-channel"] = modelConfig.boostChannel;
332335
modelJson["model-match"] = modelConfig.modelMatch;
333336
modelJson["tx-antenna"] = modelConfig.txAntenna;
337+
const auto power = cfg["power"].to<JsonObject>();
338+
power["max-power"] = modelConfig.power;
339+
power["dynamic-power"] = modelConfig.dynamicPower;
340+
power["boost-channel"] = modelConfig.boostChannel;
334341
}
335342
}
336343
#endif /* TARGET_TX */
337344

338345
if (!exportMode)
339346
{
347+
const auto settings = json["settings"].to<JsonObject>();
340348
#if defined(TARGET_RX)
341-
json["config"]["serial-protocol"] = config.GetSerialProtocol();
349+
cfg["serial-protocol"] = config.GetSerialProtocol();
342350
#if defined(PLATFORM_ESP32)
343351
if ((GPIO_PIN_SERIAL1_RX != UNDEF_PIN && GPIO_PIN_SERIAL1_TX != UNDEF_PIN) || GPIO_PIN_PWM_OUTPUTS_COUNT > 0)
344352
{
345-
json["config"]["serial1-protocol"] = config.GetSerial1Protocol();
353+
cfg["serial1-protocol"] = config.GetSerial1Protocol();
346354
}
347355
#endif
348-
json["config"]["sbus-failsafe"] = config.GetFailsafeMode();
349-
json["config"]["modelid"] = config.GetModelId();
350-
json["config"]["force-tlm"] = config.GetForceTlmOff();
351-
json["config"]["vbind"] = config.GetBindStorage();
356+
cfg["sbus-failsafe"] = config.GetFailsafeMode();
357+
cfg["modelid"] = config.GetModelId();
358+
cfg["force-tlm"] = config.GetForceTlmOff();
359+
cfg["vbind"] = config.GetBindStorage();
352360
for (int ch=0; ch<GPIO_PIN_PWM_OUTPUTS_COUNT; ++ch)
353361
{
354-
json["config"]["pwm"][ch]["config"] = config.GetPwmChannel(ch)->raw;
355-
json["config"]["pwm"][ch]["pin"] = GPIO_PIN_PWM_OUTPUTS[ch];
362+
const auto channel = cfg["channel"][ch].to<JsonObject>();
363+
channel["config"] = config.GetPwmChannel(ch)->raw;
364+
channel["pin"] = GPIO_PIN_PWM_OUTPUTS[ch];
356365
uint8_t features = 0;
357366
auto pin = GPIO_PIN_PWM_OUTPUTS[ch];
358367
if (pin == U0TXD_GPIO_NUM) features |= 1; // SerialTX supported
@@ -367,44 +376,44 @@ static void GetConfiguration(AsyncWebServerRequest *request)
367376
else if ((GPIO_PIN_SERIAL1_RX == UNDEF_PIN || GPIO_PIN_SERIAL1_TX == UNDEF_PIN) &&
368377
(!(features & 1) && !(features & 2))) features |= 96; // Both Serial1 RX/TX supported (on any pin if not already featured for Serial 1)
369378
#endif
370-
json["config"]["pwm"][ch]["features"] = features;
379+
channel["features"] = features;
371380
}
372381
if (GPIO_PIN_RCSIGNAL_RX != UNDEF_PIN && GPIO_PIN_RCSIGNAL_TX != UNDEF_PIN)
373382
{
374-
json["settings"]["has_serial_pins"] = true;
383+
settings["has_serial_pins"] = true;
375384
}
376385
#endif
377-
json["settings"]["product_name"] = product_name;
378-
json["settings"]["lua_name"] = device_name;
379-
json["settings"]["uidtype"] = GetConfigUidType(json);
380-
json["settings"]["ssid"] = station_ssid;
381-
json["settings"]["mode"] = wifiMode == WIFI_STA ? "STA" : "AP";
382-
json["settings"]["custom_hardware"] = hardware_flag(HARDWARE_customised);
383-
json["settings"]["target"] = &target_name[4];
384-
json["settings"]["version"] = VERSION;
385-
json["settings"]["git-commit"] = commit;
386+
settings["product_name"] = product_name;
387+
settings["lua_name"] = device_name;
388+
settings["uidtype"] = GetConfigUidType(json);
389+
settings["ssid"] = station_ssid;
390+
settings["mode"] = wifiMode == WIFI_STA ? "STA" : "AP";
391+
settings["custom_hardware"] = hardware_flag(HARDWARE_customised);
392+
settings["target"] = &target_name[4];
393+
settings["version"] = VERSION;
394+
settings["git-commit"] = commit;
386395
#if defined(TARGET_TX)
387-
json["settings"]["module-type"] = "TX";
396+
settings["module-type"] = "TX";
388397
#endif
389398
#if defined(TARGET_RX)
390-
json["settings"]["module-type"] = "RX";
399+
settings["module-type"] = "RX";
391400
#endif
392401
#if defined(RADIO_SX128X)
393-
json["settings"]["radio-type"] = "SX128X";
394-
json["settings"]["has_low_band"] = false;
395-
json["settings"]["has_high_band"] = true;
396-
json["settings"]["reg_domain_high"] = FHSSconfig->domain;
402+
settings["radio-type"] = "SX128X";
403+
settings["has_low_band"] = false;
404+
settings["has_high_band"] = true;
405+
settings["reg_domain_high"] = FHSSconfig->domain;
397406
#elif defined(RADIO_SX127X)
398-
json["settings"]["radio-type"] = "SX127X";
399-
json["settings"]["has_low_band"] = true;
400-
json["settings"]["has_high_band"] = false;
401-
json["settings"]["reg_domain_low"] = FHSSconfig->domain;
407+
settings["radio-type"] = "SX127X";
408+
settings["has_low_band"] = true;
409+
settings["has_high_band"] = false;
410+
settings["reg_domain_low"] = FHSSconfig->domain;
402411
#elif defined(RADIO_LR1121)
403-
json["settings"]["radio-type"] = "LR1121";
404-
json["settings"]["has_low_band"] = POWER_OUTPUT_VALUES_COUNT != 0;
405-
json["settings"]["has_high_band"] = POWER_OUTPUT_VALUES_DUAL_COUNT != 0;
406-
json["settings"]["reg_domain_low"] = FHSSconfig->domain;
407-
json["settings"]["reg_domain_high"] = FHSSconfigDualBand->domain;
412+
settings["radio-type"] = "LR1121";
413+
settings["has_low_band"] = POWER_OUTPUT_VALUES_COUNT != 0;
414+
settings["has_high_band"] = POWER_OUTPUT_VALUES_DUAL_COUNT != 0;
415+
settings["reg_domain_low"] = FHSSconfig->domain;
416+
settings["reg_domain_high"] = FHSSconfigDualBand->domain;
408417
#endif
409418
}
410419

@@ -445,27 +454,29 @@ static void ImportConfiguration(AsyncWebServerRequest *request, JsonVariant &jso
445454
if (json["power-fan-threshold"].is<JsonVariant>()) config.SetPowerFanThreshold(json["power-fan-threshold"]);
446455
if (json["motion-mode"].is<JsonVariant>()) config.SetMotionMode(json["motion-mode"]);
447456

448-
if (json["vtx-admin"].is<JsonVariant>())
457+
if (json["vtx-admin"].is<JsonObject>())
449458
{
450-
if (json["vtx-admin"]["band"].is<JsonVariant>()) config.SetVtxBand(json["vtx-admin"]["band"]);
451-
if (json["vtx-admin"]["channel"].is<JsonVariant>()) config.SetVtxChannel(json["vtx-admin"]["channel"]);
452-
if (json["vtx-admin"]["pitmode"].is<JsonVariant>()) config.SetVtxPitmode(json["vtx-admin"]["pitmode"]);
453-
if (json["vtx-admin"]["power"].is<JsonVariant>()) config.SetVtxPower(json["vtx-admin"]["power"]);
459+
const auto vtxAdmin = json["vtx-admin"].as<JsonObject>();
460+
if (vtxAdmin["band"].is<JsonVariant>()) config.SetVtxBand(vtxAdmin["band"]);
461+
if (vtxAdmin["channel"].is<JsonVariant>()) config.SetVtxChannel(vtxAdmin["channel"]);
462+
if (vtxAdmin["pitmode"].is<JsonVariant>()) config.SetVtxPitmode(vtxAdmin["pitmode"]);
463+
if (vtxAdmin["power"].is<JsonVariant>()) config.SetVtxPower(vtxAdmin["power"]);
454464
}
455465

456466
if (json["backpack"].is<JsonVariant>())
457467
{
458-
if (json["backpack"]["dvr-start-delay"].is<JsonVariant>()) config.SetDvrStartDelay(json["backpack"]["dvr-start-delay"]);
459-
if (json["backpack"]["dvr-stop-delay"].is<JsonVariant>()) config.SetDvrStopDelay(json["backpack"]["dvr-stop-delay"]);
460-
if (json["backpack"]["dvr-aux-channel"].is<JsonVariant>()) config.SetDvrAux(json["backpack"]["dvr-aux-channel"]);
468+
const auto backpack = json["backpack"].as<JsonObject>();
469+
if (backpack["dvr-start-delay"].is<JsonVariant>()) config.SetDvrStartDelay(backpack["dvr-start-delay"]);
470+
if (backpack["dvr-stop-delay"].is<JsonVariant>()) config.SetDvrStopDelay(backpack["dvr-stop-delay"]);
471+
if (backpack["dvr-aux-channel"].is<JsonVariant>()) config.SetDvrAux(backpack["dvr-aux-channel"]);
461472
}
462473

463474
if (json["model"].is<JsonVariant>())
464475
{
465476
for(JsonPair kv : json["model"].as<JsonObject>())
466477
{
467-
uint8_t model = atoi(kv.key().c_str());
468-
JsonObject modelJson = kv.value();
478+
const uint8_t model = atoi(kv.key().c_str());
479+
const auto modelJson = kv.value().as<JsonObject>();
469480

470481
config.SetModelId(model);
471482
if (modelJson["packet-rate"].is<JsonVariant>()) config.SetRate(modelJson["packet-rate"]);
@@ -479,7 +490,7 @@ static void ImportConfiguration(AsyncWebServerRequest *request, JsonVariant &jso
479490
}
480491
if (modelJson["model-match"].is<JsonVariant>()) config.SetModelMatch(modelJson["model-match"]);
481492
// if (modelJson["tx-antenna"].is<JsonVariant>()) config.SetTxAntenna(modelJson["tx-antenna"]);
482-
// have to commmit after each model is updated
493+
// have to commit after each model is updated
483494
config.Commit();
484495
}
485496
}
@@ -501,7 +512,7 @@ static void WebUpdateButtonColors(AsyncWebServerRequest *request, JsonVariant &j
501512
*/
502513
static void JsonUidToConfig(JsonVariant &json)
503514
{
504-
JsonArray juid = json["uid"].as<JsonArray>();
515+
const auto juid = json["uid"].as<JsonArray>();
505516
size_t juidLen = constrain(juid.size(), 0, UID_LEN);
506517
uint8_t newUid[UID_LEN] = { 0 };
507518

0 commit comments

Comments
 (0)