/* ============================================
code is placed under the MIT license
Copyright (c) 2024 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 <SdFat.h>
#define VITESSE_SPI SD_SCK_MHZ(4)
#define PIN_CS 10
const byte tailleBuffer = 50;
SdFat carteSd;
File fichierConfig;
enum : uint8_t {AUCUNE_COMMANDE = 0, COMMANDE1 = 1, COMMANDE2 = 2, COMMANDE3 = 3};
struct CommandeSwitch {
uint8_t idSwitch : 6; // le numéro du switch de 0 à 63
uint8_t commande : 2; // la commande de 0 à 3
};
const byte nombreMaxSwitches = 64;
CommandeSwitch lesSwitches[nombreMaxSwitches];
byte nombreDeSwitches = 0;
void setup() {
Serial.begin(115200);
if (!carteSd.begin(PIN_CS, VITESSE_SPI)) {
if (carteSd.card()->errorCode()) {
Serial.println("Échec de l'initialisation de la carte SD.");
} else if (carteSd.vol()->fatType() == 0) {
Serial.println("Partition FAT16/FAT32 non valide introuvable.");
} else {
Serial.println("Type d'erreur indéterminé");
}
while (true) yield();
}
fichierConfig = carteSd.open("config.txt", FILE_READ);
if (!fichierConfig) {
Serial.println("Erreur d'ouverture du fichier config.txt.");
while (true) yield();
}
char buffer[tailleBuffer];
int idSwitch;
int valeurCommande;
bool erreur = false;
while (fichierConfig.fgets(buffer, tailleBuffer)) {
char *debut = buffer;
while (isspace(*debut)) debut++; // Ignorer les espaces initiaux
if (*debut == '\0' || *debut == '#') continue; // la ligne était vide ou commentaire
if (sscanf(buffer, " SW %d = %d", &idSwitch, &valeurCommande) == 2) {
if (idSwitch > 0 && idSwitch <= nombreMaxSwitches && valeurCommande >= AUCUNE_COMMANDE + 1 && valeurCommande <= COMMANDE3 + 1) {
if (nombreDeSwitches >= nombreMaxSwitches) {
Serial.println("Trop d'entrées dnas le fichier config. Arrêt du traitement.");
erreur = true;
break;
}
lesSwitches[nombreDeSwitches].idSwitch = idSwitch - 1;
lesSwitches[nombreDeSwitches].commande = valeurCommande - 1;
nombreDeSwitches++;
} else {
Serial.println("Valeur de commande ou numéro de switch invalide. Arrêt du traitement.");
erreur = true;
break;
}
} else {
Serial.println("Erreur de format dans le fichier. Arrêt du traitement.");
erreur = true;
break;
}
}
fichierConfig.close();
if (erreur) {
while (true) yield();
}
for (byte i = 0; i < nombreDeSwitches; i++) {
Serial.print("switch N° "); Serial.print(lesSwitches[i].idSwitch + 1);
Serial.print(" a pour valeur "); Serial.println(lesSwitches[i].commande + 1);
}
}
void loop() {}