/* ============================================
code is placed under the MIT license
Copyright (c) 2023 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include "Moteurs.h"
#include "Analyseur.hpp"
Moteur moteursOeilGauche[] = {{22, 23}, {24, 25}, {26, 27}, {28, 29}};
Moteur moteursOeilDroit[] = {{30, 31}, {32, 33}, {34, 35}, {36, 37}};
Moteur moteurOreilleGauche[] = {{38, 39}};
Moteur moteurOreilleDroite[] = {{40, 41}};
Moteur moteurLevreSup[] = {{42, 43}};
Moteur moteurLevreInf[] = {{44, 45}};
Moteur moteurMachoireInf[] = {{46, 47}, {48, 49}};
enum {OEIL_G, OEIL_D, OREILLE_G, OREILLE_D, LEVRE_SUP, LEVRE_INF, MACHOIRE_INF };
GroupeMoteur groupes[] = {
{"Oeil gauche", 4, moteursOeilGauche},
{"Oeil Droit", 4, moteursOeilDroit},
{"Oreille Gauche", 1, moteurOreilleGauche},
{"Oreille Droite", 1, moteurOreilleDroite},
{"Lèvre Supérieure", 1, moteurLevreSup},
{"Lèvre inférieure", 1, moteurLevreInf},
{"Machoire inférieure", 2, moteurMachoireInf},
};
const byte maxMoteursDansUnGroupe = 4;
const DescripteurCommande commandes[] = {
{OEIL_G, "OEG", VALEUR}, // oeil Gauche
{OEIL_D, "OED", VALEUR}, // oeil Droit
{OREILLE_G, "ORG", VALEUR}, // Oreille Gauche
{OREILLE_D, "ORD", VALEUR}, // Oreille Droite
{LEVRE_SUP, "LS", VALEUR}, // Lèvre Supérieure
{LEVRE_INF, "LI", VALEUR}, // Lèvre inférieure
{MACHOIRE_INF, "MI", VALEUR}, // Machoire inférieure
};
const size_t nbCommandes = sizeof commandes / sizeof * commandes;
Analyseur<50> analyseur(commandes, nbCommandes, Serial); // 50 caractères max dans une seule commande arrivant sur Serial
bool extrairePositions(long * tableau, char * liste, uint8_t n) {
uint8_t count = 0;
char * token = strtok(liste, ",");
while (token != nullptr) {
if (count >= n) return false; // Dépassement du tableau
char * endptr;
long valeur = strtol(token, &endptr, 10);
if (*endptr == '\0') {
tableau[count++] = valeur;
} else {
return false;
}
token = strtok(nullptr, ",");
}
return (count == n); // Extraction réussie
}
void gestionCommande() {
DetailCommande * uneCommande = analyseur.commandeRecue();
if (uneCommande != nullptr && uneCommande->valeur != nullptr) {
long positions[maxMoteursDansUnGroupe];
if (extrairePositions(positions, uneCommande->valeur, groupes[uneCommande->langage->identifiantCommande].nombreMoteurs)) {
Serial.println(groupes[uneCommande->langage->identifiantCommande].nomDuGroupe);
for (uint8_t i = 0; i < groupes[uneCommande->langage->identifiantCommande].nombreMoteurs; i++) Serial.println(positions[i]);
groupes[uneCommande->langage->identifiantCommande].moveTo(positions);
}
}
}
void action() {
for (auto &&g : groupes) g.run();
}
void setup() {
Serial.begin(115200);
analyseur.afficherCommandes();
}
void loop() {
gestionCommande();
action();
}
OEIL DROIT
OEIL GAUCHE
OREILLE GAUCHE
OREILLE DROITE
LEVRE SUPERIEURE
LEVRE INFERIEURE
MACHOIRE INFERIEURE