/* ============================================
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 {COMMANDE1 = 1, COMMANDE2 = 2, COMMANDE3 = 3, COMMANDE4 = 4, NON_UTILISE = 255};
const byte nombreMaxSwitches = 64;
uint8_t etatDesSwitches[nombreMaxSwitches];
void setup() {
for (byte i = 0; i < nombreMaxSwitches; i++) etatDesSwitches[i] = NON_UTILISE;
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 >= COMMANDE1 && valeurCommande <= COMMANDE4) {
etatDesSwitches[idSwitch - 1] = valeurCommande ;
} 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 < nombreMaxSwitches; i++) {
Serial.print("switch N° "); Serial.print(i + 1);
switch (etatDesSwitches[i]) {
case COMMANDE1: Serial.println(" => commande #1"); break;
case COMMANDE2: Serial.println(" => commande #2"); break;
case COMMANDE3: Serial.println(" => commande #3"); break;
case COMMANDE4: Serial.println(" => commande #4"); break;
case NON_UTILISE: Serial.println(" => non utilisé"); break;
}
}
}
void loop() {}