Bonjour, je voulais avoir votre avis sur mon code. Le programme dans son ensemble est assez vaste et fonctionne. Il a pour but d'automatiser l'ouverture/fermeture des portes d'un poulailler. J'ai donc opté pour une approche orienté objet avec les classes suivantes : Bouton, Capteur_Lumiere, Ecran, Led, Porte, Temps et Son.
Je ne détaillerai que la classe Son (les modules étant bien séparés les uns des autres).
Le constructeur me permet de créer un objet hautParleur1 par exemple dans lequel je peux choisir un morceau à charger et un tempo dans un Musique.h.
Je peux ensuite faire grosso modo (v_SON_Lecture()) un hautParleur1.Lecture() qui me permet de lire le tableau en "const int" correspondant au morceau choisi.
Chaque mélodie est un tableau à 2 dimensions dans lequel repose dans chaque bloc la fréquence de la note de base, le rythme et l'octave. Un calcul ensuite me permet de retrouver la fréquence voulue.
J'ai mis 4 mélodies (dont une qui ne compte pas vraiment, c'est juste un test à 3 notes) et c'est très lourd :o
J'aurai bien voulu stocker ça différemment et charger le tableau au besoin mais j'ai déjà eu un mal de chien à mettre ça en place. Je m'étais embarqué sur un attribut melodie[][3] et je me suis cassé les dents à vouloir absolument intégrer des séries de notes, rythmes en fonction du choix de la musique. Et Monsieur C++ n'aime pas franchement que je déclare mon tableau dans le .h et que je l'initialise dans une fonction de mon .cpp ... J'ai pensé au tableau dynamique type vector par la suite, mais vu que c'est pas implanté nativement...
J'ai fait du javascript récemment et je me demandais s'il n'y avait pas une sorte de format json qui pouvait exister pour arduino ? Si vous avez des idées pour alléger ces séries de valeurs, je suis preneur.
Son.cpp :
#include "Son.h"
#include "Arduino.h"
#include "Musique.h"
#include <string.h>
// macro pour compter le nombre de lignes ou de colonnes dans un tableau quelque soit le type
#define COMPTE_LIGNES(tableau) (sizeof(tableau) / sizeof(*tableau))
#define COMPTE_COLONNES(tableau) (sizeof(tableau) / (sizeof(**tableau) * COMPTE_LIGNES(tableau)))
// Mélodies
const int SON_TEST[3][3]={
{DON,CROCHE,0},
{DON,CROCHE,2},
{DON,NOIRE,3}
};
const int SON_FRENCHCANCAN[44][3]={
{SON,NOIRE,3},{DON,BLANCHE,3},{REN,CROCHE,3},{FAN,CROCHE,3},
{MIN,CROCHE,3},{REN,CROCHE,3},{SON,NOIRE,3},{SON,NOIRE,3},
{SON,CROCHE,3},{LAN,CROCHE,3},{MIN,CROCHE,3},{FAN,CROCHE,3},
{REN,NOIRE,3},{REN,NOIRE,3},{REN,CROCHE,3},{FAN,CROCHE,3},
{MIN,CROCHE,3},{REN,CROCHE,3},{DON,CROCHE,3},{DON,CROCHE,4},
{SIN,CROCHE,3},{LAN,CROCHE,3},{SON,CROCHE,3},{FAN,CROCHE,3},
{MIN,CROCHE,3},{REN,CROCHE,3},{DON,BLANCHE,3},{REN,CROCHE,3},
{FAN,CROCHE,3},{MIN,CROCHE,3},{REN,CROCHE,3},{SON,NOIRE,3},
{SON,NOIRE,3},{SON,CROCHE,3},{LAN,CROCHE,3},{MIN,CROCHE,3},
{FAN,CROCHE,3},{REN,NOIRE,3},{REN,NOIRE,3},{REN,CROCHE,3},
{SON,CROCHE,3},{REN,CROCHE,3},{MIN,CROCHE,3},{DON,NOIRE,3}
};
const int SON_MARCHE_IMPERIALE[44][3]={
{SON,NOIRE,3},{SON,NOIRE,3},{SON,CROCHE,3},{RED,CROCHEPOINTEE,3},
{LAD,DOUBLECROCHE,3},{SON,NOIRE,3},{RED,CROCHEPOINTEE,3},{LAD,DOUBLECROCHE,3},
{SON,BLANCHE,3},{REN,NOIRE,4},{REN,NOIRE,4},{REN,NOIRE,4},
{RED,CROCHEPOINTEE,4},{LAD,DOUBLECROCHE,3},{FAD,NOIRE,3},{RED,CROCHEPOINTEE,3},
{LAD,DOUBLECROCHE,3},{SON,BLANCHE,3},{SON,NOIRE,4},{SON,CROCHEPOINTEE,3},
{SON,DOUBLECROCHE,3},{SON,NOIRE,4},{FAD,CROCHEPOINTEE,4},{FAN,DOUBLECROCHE,4},
{MIN,DOUBLECROCHE,4},{RED,DOUBLECROCHE,4},{MIN,CROCHE,4},{SIL,DEMISOUPIR,0},
{SON,CROCHE,3},{REN,NOIRE,4},{DON,CROCHEPOINTEE,4},{SIN,DOUBLECROCHE,3},
{LAD,DOUBLECROCHE,3},{LAN,DOUBLECROCHE,3},{LAD,CROCHE,3},{SIL,DEMISOUPIR,0},
{RED,CROCHE,3},{SON,NOIRE,3},{RED,CROCHEPOINTEE,3},{LAD,DOUBLECROCHE,3},
{SON,NOIRE,3},{RED,CROCHEPOINTEE,3},{LAD,DOUBLECROCHE,3},{SON,BLANCHE,3}
};
const int SON_FIREWORKS[72][3]={
{REN,NOIRE,3},{SON,CROCHE,3},{SON,CROCHE,3},{SON,CROCHE,3},
{SON,CROCHE,3},{SON,DOUBLECROCHE,3},{LAN,DOUBLECROCHE,3},{SIN,DOUBLECROCHE,3},
{LAN,DOUBLECROCHE,3},{SON,CROCHE,3},{SON,DOUBLECROCHE,3},{LAN,DOUBLECROCHE,3},
{SIN,CROCHE,3},{SIN,CROCHE,3},{SIN,CROCHE,3},{SIN,CROCHE,3},
{SIN,DOUBLECROCHE,3},{DON,DOUBLECROCHE,4},{REN,DOUBLECROCHE,4},{DON,DOUBLECROCHE,4},
{SIN,CROCHE,3},{SIN,DOUBLECROCHE,3},{DON,DOUBLECROCHE,4},{REN,CROCHE,4},
{REN,CROCHE,4},{REN,CROCHE,4},{REN,CROCHE,4},{REN,BLANCHE,4},
{SIL,DEMISOUPIR,0},{SON,CROCHE,4},{REN,CROCHE,4},{SIN,CROCHE,3},
{SIL,DEMISOUPIR,0},{SON,CROCHE,4},{REN,CROCHE,4},{SIN,CROCHE,3},
{SIL,DEMISOUPIR,0},{REN,NOIRE,4},{REN,NOIRE,4},{MIN,DOUBLECROCHE,4},
{REN,DOUBLECROCHE,4},{DON,CROCHE,4},{SIN,CROCHE,3},{LAN,BLANCHE,3},
{SIL,DEMISOUPIR,0},{REN,CROCHE,4},{REN,CROCHE,4},{MIN,DOUBLECROCHE,4},
{REN,DOUBLECROCHE,4},{DON,CROCHE,4},{SIN,CROCHE,3},{LAN,CROCHEPOINTEE,3},
{SIN,DOUBLECROCHE,3},{LAN,CROCHEPOINTEE,3},{SIN,DOUBLECROCHE,3},{LAN,DOUBLECROCHE,3},
{SIN,DOUBLECROCHE,3},{LAN,DOUBLECROCHE,3},{SIN,DOUBLECROCHE,3},{LAN,DOUBLECROCHE,3},
{SIN,DOUBLECROCHE,3},{LAN,DOUBLECROCHE,3},{SIN,DOUBLECROCHE,3},{LAN,CROCHEPOINTEE,3},
{LAN,DOUBLECROCHE,3},{SIN,DOUBLECROCHE,3},{DON,DOUBLECROCHE,4},{REN,DOUBLECROCHE,4},
{MIN,DOUBLECROCHE,4},{DOD,CROCHEPOINTEE,4},{REN,DOUBLECROCHE,4},{REN,BLANCHE,4}
};
C_SON::C_SON(byte b_pinSon,byte b_choixMusique,int i_tempo)
{
b_SON_pinSon = b_pinSon;
b_SON_choixMusique=b_choixMusique;
i_SON_tempo=i_tempo;
pinMode(b_SON_pinSon, OUTPUT);
}
int C_SON::i_SON_CalculFrequence(int i_frequenceOrigine,int i_octaveSouhaite){
return i_frequenceOrigine*pow(2,i_octaveSouhaite);
}
void C_SON::v_SON_Lecture(){
int i_melodie[100][3];
float f_tempo=0;
int lignes=0;
if(b_SON_choixMusique==TEST){
lignes=COMPTE_LIGNES(SON_TEST);
i_melodie[lignes][3];
memcpy(i_melodie, SON_TEST, sizeof(SON_TEST));
}
else if(b_SON_choixMusique==MARCHE_IMPERIALE){
lignes=COMPTE_LIGNES(SON_MARCHE_IMPERIALE);
i_melodie[lignes][3];
memcpy(i_melodie, SON_MARCHE_IMPERIALE, sizeof(SON_MARCHE_IMPERIALE));
}
else if(b_SON_choixMusique==FRENCHCANCAN){
lignes=COMPTE_LIGNES(SON_FRENCHCANCAN);
i_melodie[lignes][3];
memcpy(i_melodie, SON_FRENCHCANCAN, sizeof(SON_FRENCHCANCAN));
}
else if(b_SON_choixMusique==FIREWORKS){
lignes=COMPTE_LIGNES(SON_FIREWORKS);
i_melodie[lignes][3];
memcpy(i_melodie, SON_FIREWORKS, sizeof(SON_FIREWORKS));
}
f_tempo=(1000.0*60.0)/float(i_SON_tempo);
for (int n=0;n<lignes;n++){ // boucle de lecture du tableau
char note = i_melodie[n][0]; // on récupère la fréquence de base dans le tableau
char octave= i_melodie[n][2]; // on récupère l'octave
int frequence=i_SON_CalculFrequence(note,octave); //on calcule la bonne fréquence
Serial.println(frequence);
float duree=f_tempo*i_melodie[n][1]/32;// on multiplie la duree de base par la valeur de duree du tableau
Serial.println(duree);
tone (b_SON_pinSon,frequence); //on joue la note
delay(duree);
noTone(b_SON_pinSon);//on arrete la note
delay(10);// petite attente pour avoir l'impression d'attaquer la note
}
}
void C_SON::v_SON_SetMelodie(byte b_choixMusique,int i_tempo){
b_SON_choixMusique=b_choixMusique;
i_SON_tempo=i_tempo;
}
Son.h :
#ifndef DEF_SON
#define DEF_SON
#include "Arduino.h"
class C_SON
{
protected:
byte b_SON_pinSon=HIGH; // numero de pin
bool bo_SON_etatSon=false; // etat (si allume alors true)
int i_SON_tempo;
byte b_SON_choixMusique;
public:
C_SON();
C_SON(byte b_pinSon, byte b_choixMusique,int i_tempo);
int i_SON_CalculFrequence(int i_frequenceOrigine,int i_octaveSouhaite);
void v_SON_Lecture();
void v_SON_SetMelodie(byte b_choixMusique,int i_tempo);
};
#endif
Musique.h :
// liste musiques disponibles
#define TEST 0
#define MARCHE_IMPERIALE 1
#define FRENCHCANCAN 2
#define FIREWORKS 3
// Liste des notes
#define SIL 0
#define DON 65
#define DOD 69
#define REN 74
#define RED 78
#define MIN 83
#define FAN 87
#define FAD 93
#define SON 98
#define SOD 104
#define LAN 110
#define LAD 117
#define SIN 123
// liste des rythmes
#define QUINTUPLECROCHE 1
#define QUADRUPLECROCHE 2
#define TRIPLECROCHE 4
#define DOUBLECROCHE 8
#define CROCHE 16
#define CROCHEPOINTEE 24
#define NOIRE 32
#define NOIREPOINTEE 48
#define BLANCHE 64
#define BLANCHEPOINTEE 96
#define RONDE 128
// liste des rythmiques de silences
#define TRENTEDEUXIEMEDESOUPIR 1
#define SEIZIEMEDESOUPIR 2
#define HUITIEMEDESOUPIR 4
#define QUARDESOUPIR 8
#define DEMISOUPIR 16
#define SOUPIR 32
#define DEMIPAUSE 64
#define PAUSE 128
// Tempo
#define LENTO 60
#define ANDANTE 80
#define MODERATO 100
#define ALLEGRO 120
#define PRESTO 150
#define PRESTISSIMO 200