refactor(server): ♻️ move global variables into a struct
This commit is contained in:
parent
9b055207d6
commit
477e85ba56
|
@ -23,10 +23,13 @@ const char *password = "test12345";
|
|||
int delayTime = 3;
|
||||
|
||||
// Variables de commande :
|
||||
bool commandRunning = false; // Une commande doit être exécutée
|
||||
bool stopCommand = false; // La commande doit être stoppée
|
||||
String globalCommand; // Nom de la commande
|
||||
int globalValue; // Valeur (nombre de pas à effectuer)
|
||||
struct command_t
|
||||
{
|
||||
bool running; // Une commande doit être exécutée
|
||||
bool stopped; // La commande doit être stoppée
|
||||
String name; // Nom de la commande
|
||||
int value; // Valeur (nombre de pas à effectuer)
|
||||
} command = {false, false};
|
||||
|
||||
// Création du serveur asynchrone :
|
||||
AsyncWebServer server(80);
|
||||
|
@ -55,28 +58,28 @@ void setup()
|
|||
void loop()
|
||||
{
|
||||
// Ne rien faire si aucune commande en cours
|
||||
if (!commandRunning)
|
||||
if (!command.running)
|
||||
return;
|
||||
|
||||
// Arrêter le robot si une commande est interrompue
|
||||
// ou si le nombre de pas a été effectué
|
||||
if (stopCommand || globalValue <= 0)
|
||||
if (command.stopped || command.value <= 0)
|
||||
{
|
||||
Serial.println("stop");
|
||||
commandRunning = false;
|
||||
stopCommand = false;
|
||||
command.running = false;
|
||||
command.stopped = false;
|
||||
stopMotors();
|
||||
return;
|
||||
}
|
||||
|
||||
// Sinon executer la commande en cours
|
||||
chooseCommand(); // Appeler la fonction "chooseCommand()"
|
||||
if (globalValue > 0)
|
||||
if (command.value > 0)
|
||||
{ // S'il reste des pas à effectuer alors on l'affiche et on enlève un pas
|
||||
Serial.print(globalCommand);
|
||||
Serial.print(command.name);
|
||||
Serial.print(": ");
|
||||
Serial.println(globalValue);
|
||||
globalValue--;
|
||||
Serial.println(command.value);
|
||||
command.value--;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,16 +88,16 @@ void serverConfig()
|
|||
{
|
||||
server.on("/get", HTTP_GET, [](AsyncWebServerRequest *request)
|
||||
{
|
||||
String command;
|
||||
float value;
|
||||
String commandName;
|
||||
float commandValue;
|
||||
|
||||
// Parse request params and update command and value
|
||||
parseRequestParams(request, command, value);
|
||||
parseRequestParams(request, commandName, commandValue);
|
||||
|
||||
Serial.print(command);
|
||||
Serial.print(commandName);
|
||||
Serial.print(": ");
|
||||
Serial.println(value);
|
||||
request->send(200, "text/plain", command); });
|
||||
Serial.println(commandValue);
|
||||
request->send(200, "text/plain", commandName); });
|
||||
}
|
||||
|
||||
void parseRequestParams(AsyncWebServerRequest *request, String &commandName, float &commandValue)
|
||||
|
@ -102,17 +105,17 @@ void parseRequestParams(AsyncWebServerRequest *request, String &commandName, flo
|
|||
// Stop here with error message if wrong command
|
||||
if (!request->hasParam("command"))
|
||||
{
|
||||
command = "No command provided";
|
||||
commandName = "No command provided";
|
||||
return;
|
||||
}
|
||||
|
||||
// Update command from params
|
||||
command = request->getParam("command")->value();
|
||||
commandName = request->getParam("command")->value();
|
||||
|
||||
// Update value from params and use "0" as default
|
||||
value = request->hasParam("value") ? request->getParam("value")->value().toFloat() : 0;
|
||||
commandValue = request->hasParam("value") ? request->getParam("value")->value().toFloat() : 0;
|
||||
|
||||
requestCheck(command, value);
|
||||
requestCheck(commandName, commandValue);
|
||||
}
|
||||
|
||||
// Fonction pour convertir une longueur en cm vers un nombre de pas moteur.
|
||||
|
@ -130,36 +133,38 @@ int convertRotToSteps(int rotation)
|
|||
}
|
||||
|
||||
// Lorsqu'une commande arrive au serveur, cette fonction permet
|
||||
void requestCheck(String command, float value)
|
||||
void requestCheck(String commandName, float commandValue)
|
||||
{
|
||||
if (command == "stop")
|
||||
if (commandName == "stop")
|
||||
{
|
||||
globalCommand = "stop";
|
||||
commandRunning = true;
|
||||
stopCommand = true;
|
||||
command.name = "stop";
|
||||
command.running = true;
|
||||
command.stopped = true;
|
||||
}
|
||||
else if setGlobals (command, value)
|
||||
;
|
||||
|
||||
if (value == 0)
|
||||
else
|
||||
{
|
||||
globalValue = 474;
|
||||
setGlobals(commandName, commandValue);
|
||||
}
|
||||
|
||||
if (commandValue == 0)
|
||||
{
|
||||
command.value = 474;
|
||||
}
|
||||
}
|
||||
|
||||
void setGlobals(String command, float value)
|
||||
void setGlobals(String commandName, float commandValue)
|
||||
{
|
||||
// Assert command before updating globals
|
||||
if (command != "forward" && command != "backward" && command != "left" && command != "right")
|
||||
if (commandName != "forward" && commandName != "backward" && commandName != "left" && commandName != "right")
|
||||
{
|
||||
return
|
||||
return;
|
||||
}
|
||||
|
||||
// Update globals
|
||||
globalCommand = command;
|
||||
commandRunning = true;
|
||||
stopCommand = false;
|
||||
globalValue = convertLengthToSteps(value);
|
||||
command.name = commandName;
|
||||
command.running = true;
|
||||
command.stopped = false;
|
||||
command.value = convertLengthToSteps(commandValue);
|
||||
}
|
||||
|
||||
void connectWiFi()
|
||||
|
@ -173,19 +178,19 @@ void connectWiFi()
|
|||
|
||||
void chooseCommand()
|
||||
{
|
||||
if (globalCommand == "forward")
|
||||
if (command.name == "forward")
|
||||
{
|
||||
forward();
|
||||
}
|
||||
else if (globalCommand == "left")
|
||||
else if (command.name == "left")
|
||||
{
|
||||
left();
|
||||
}
|
||||
else if (globalCommand == "right")
|
||||
else if (command.name == "right")
|
||||
{
|
||||
right();
|
||||
}
|
||||
else if (globalCommand == "backward")
|
||||
else if (command.name == "backward")
|
||||
{
|
||||
backward();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue