1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| #include <I2C_eeprom.h> // https://github.com/RobTillaart/I2C_EEPROM/blob/master/I2C_eeprom.cpp
I2C_eeprom memoire(0x50, I2C_DEVICESIZE_24LC256); // AT24C256 en 0x50
/* METHODES PRATIQUES (un retour de 0 veut dire OK pour les fonctions d'écriture)
bool begin();
bool isConnected();
int writeByte(const uint16_t memoryAddress, const uint8_t value);
int writeBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length);
int setBlock(const uint16_t memoryAddress, const uint8_t value, const uint16_t length);
uint8_t readByte(const uint16_t memoryAddress);
uint16_t readBlock(const uint16_t memoryAddress, uint8_t* buffer, const uint16_t length);
int updateByte(const uint16_t memoryAddress, const uint8_t value);
int updateBlock(const uint16_t memoryAddress, const uint8_t* buffer, const uint16_t length);
*/
const uint16_t adresseMotMagique = 0;
const uint32_t motMagique = 0xB0CAD0;
const uint16_t adresseDebut = adresseMotMagique + sizeof motMagique;
const uint16_t tailleRequise = 10080; // 7 jours * 24 heures * 60 minutes
enum tRelais : byte {AucunRelais = 0, Relais0 = 1, Relais1 = 2, Relais2 = 4, Relais3 = 8, Relais4 = 16, Relais5 = 32, Relais6 = 64, Relais7 = 128};
enum tJour : byte {Lundi = 0, Mardi, Mercredi, Jeudi, Vendredi, Samedi, Dimanche};
const char * jours[] = {"Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"};
inline bool premierUsage() {
uint32_t motTest;
memoire.readBlock(adresseMotMagique, (uint8_t*) &motTest, sizeof motTest);
return (motTest != motMagique);
}
void formater() {
memoire.setBlock(adresseDebut, 0, tailleRequise); // on met tout à 0
memoire.writeBlock(adresseMotMagique, (uint8_t*) &motMagique, sizeof motMagique);
}
inline byte lireEtat(tJour jour, byte heure, byte minute) {
return memoire.readByte(adresseDebut + 1440u * jour + 60u * heure + minute);
}
inline bool ecrireEtat(byte etat, tJour jour, byte heure, byte minute) {
return memoire.updateByte(adresseDebut + 1440u * jour + 60u * heure + minute, etat) == 0;
}
inline bool activerRelais(byte relais, tJour jour, byte heure, byte minute) {
byte etat = lireEtat(jour, heure, minute);
etat |= relais;
return ecrireEtat(etat, jour, heure, minute);
}
inline bool desactiverRelais(byte relais, tJour jour, byte heure, byte minute) {
byte etat = lireEtat(jour, heure, minute);
etat &= ~relais;
return ecrireEtat(etat, jour, heure, minute);
}
void afficher(tJour jour, byte heure, byte minute) {
byte etat = lireEtat(jour, heure, minute);
Serial.print(jours[jour]); Serial.write(' ');
Serial.print(heure); Serial.write(':');
if (minute < 10) Serial.write('0');
Serial.print(minute);
for (byte relais = 0; relais < 8; relais++) {
Serial.print(F("\tR")); Serial.print(relais); Serial.write(':');
Serial.print(bitRead(etat, relais) ? F("ON") : F("OFF"));
}
Serial.println();
}
void setup() {
Serial.begin(115200);
memoire.begin();
if (! memoire.isConnected()) {
Serial.println(F("EEPROM absente."));
while (true);
}
if (premierUsage()) formater();
// --------------- POUR TESTER --------------
// activer uniquement les relais 1,3,5 et 7 le Lundi à 17h30
ecrireEtat(Relais1 | Relais3 | Relais5 | Relais7, Lundi, 17, 30);
afficher(Lundi, 17, 30); // vérification
// on active en plus le relais 0
activerRelais(Relais0, Lundi, 17, 30);
afficher(Lundi, 17, 30); // vérification
// on désactive les relais 5 et 7
desactiverRelais( Relais5 | Relais7, Lundi, 17, 30);
afficher(Lundi, 17, 30); // vérification
}
void loop() {} |
Partager