Added parameters for motor config.
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
namespace
|
||||
{
|
||||
const char *kConfigPath = "/config.json";
|
||||
const size_t kConfigJsonCapacity = 4096;
|
||||
|
||||
WebServer server(80);
|
||||
bool configStoreReady = false;
|
||||
@@ -188,7 +189,6 @@ async function pollLogs() {
|
||||
return;
|
||||
}
|
||||
|
||||
Log(String("Config gespeichert: penUpPos=") + g_iPenUpPos + ", penDownPos=" + g_iPenDownPos);
|
||||
server.send(200, "application/json", buildConfigJson());
|
||||
}
|
||||
|
||||
@@ -214,8 +214,17 @@ async function pollLogs() {
|
||||
} // namespace
|
||||
|
||||
ConfigParameter configParameters[] = {
|
||||
// {"penUpPos", &g_iPEN_UP_POS, "Pen Up Position", 5},
|
||||
// {"penDownPos", &g_iPEN_DOWN_POS, "Pen Down Position", 20},
|
||||
{"penUpPos", &g_iPenUpPos, "Pen Up Position", 40},
|
||||
{"penDownPos", &g_iPenDownPos, "Pen Down Position", 10},
|
||||
{"rotStepPin", &g_iRotStepPin, "Rotational Stepper Step Pin", kDefaultRotStepPin},
|
||||
{"rotDirPin", &g_iRotDirPin, "Rotational Stepper Direction Pin", kDefaultRotDirPin},
|
||||
{"rotEnablePin", &g_iRotEnablePin, "Rotational Stepper Enable Pin", kDefaultRotEnablePin},
|
||||
{"penStepPin", &g_iPenStepPin, "Pen Stepper Step Pin", kDefaultPenStepPin},
|
||||
{"penDirPin", &g_iPenDirPin, "Pen Stepper Direction Pin", kDefaultPenDirPin},
|
||||
{"penEnablePin", &g_iPenEnablePin, "Pen Stepper Enable Pin", kDefaultPenEnablePin},
|
||||
{"rotMicrostep", &g_iRotMicrostep, "Rotational Stepper Microsteps", kDefaultRotMicrostep},
|
||||
{"penMicrostep", &g_iPenMicrostep, "Pen Stepper Microsteps", kDefaultPenMicrostep},
|
||||
{"servoPin", &g_iServoPin, "Servo Pin", kDefaultServoPin},
|
||||
};
|
||||
|
||||
const size_t configParameterCount = sizeof(configParameters) / sizeof(configParameters[0]);
|
||||
@@ -242,12 +251,12 @@ bool loadConfigFromFile()
|
||||
return saveConfigToFile();
|
||||
}
|
||||
|
||||
StaticJsonDocument<1024> doc;
|
||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
||||
DeserializationError err = deserializeJson(doc, file);
|
||||
file.close();
|
||||
if (err)
|
||||
{
|
||||
Log("config.json ist ungueltig, defaults werden gespeichert");
|
||||
Log(String("config.json ist ungueltig (") + err.c_str() + "), defaults werden gespeichert");
|
||||
return saveConfigToFile();
|
||||
}
|
||||
|
||||
@@ -273,6 +282,7 @@ bool loadConfigFromFile()
|
||||
param->description = item["description"].as<String>();
|
||||
}
|
||||
}
|
||||
updateStepCorrectionFactors();
|
||||
Log(String("Config geladen: penUpPos=") + g_iPenUpPos + ", penDownPos=" + g_iPenDownPos);
|
||||
|
||||
return true;
|
||||
@@ -287,7 +297,7 @@ bool saveConfigToFile()
|
||||
return false;
|
||||
}
|
||||
|
||||
StaticJsonDocument<1024> doc;
|
||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
||||
JsonArray params = doc.createNestedArray("parameters");
|
||||
for (size_t i = 0; i < configParameterCount; ++i)
|
||||
{
|
||||
@@ -296,6 +306,12 @@ bool saveConfigToFile()
|
||||
item["value"] = *configParameters[i].value;
|
||||
item["description"] = configParameters[i].description;
|
||||
}
|
||||
if (doc.overflowed())
|
||||
{
|
||||
Log("Config JSON Dokument zu klein beim Speichern");
|
||||
file.close();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok = serializeJsonPretty(doc, file) > 0;
|
||||
file.flush();
|
||||
@@ -309,7 +325,7 @@ bool saveConfigToFile()
|
||||
|
||||
String buildConfigJson()
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
||||
JsonArray params = doc.createNestedArray("parameters");
|
||||
for (size_t i = 0; i < configParameterCount; ++i)
|
||||
{
|
||||
@@ -318,6 +334,11 @@ String buildConfigJson()
|
||||
item["value"] = *configParameters[i].value;
|
||||
item["description"] = configParameters[i].description;
|
||||
}
|
||||
if (doc.overflowed())
|
||||
{
|
||||
Log("Config JSON Dokument zu klein beim Lesen");
|
||||
return String("{\"parameters\":[]}");
|
||||
}
|
||||
|
||||
String output;
|
||||
serializeJson(doc, output);
|
||||
@@ -326,7 +347,7 @@ String buildConfigJson()
|
||||
|
||||
bool applyConfigJson(const String &payload, String &errorMessage)
|
||||
{
|
||||
StaticJsonDocument<1024> doc;
|
||||
StaticJsonDocument<kConfigJsonCapacity> doc;
|
||||
DeserializationError err = deserializeJson(doc, payload);
|
||||
if (err)
|
||||
{
|
||||
@@ -361,6 +382,7 @@ bool applyConfigJson(const String &payload, String &errorMessage)
|
||||
param->description = item["description"].as<String>();
|
||||
}
|
||||
}
|
||||
updateStepCorrectionFactors();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user