// Projet de session 2022
// ==============================================
/*
--------------------------------------------------------------------------
Projet: Projet de session 2022
Auteur: Olivier Roux
Date: 2022.11.25
---------------------------------------------------------------------------
Description: Programme envoyant un message communiquant par UART à l'aide d'un ILI9341 ainsi qu'un écran lcd 20x04.
Permettant d'afficher l'heure actuelle exacte à l'aide d'un RTC et d'un 4 Digit Display,
À l'aide d'un détecteur de mouvement qui nous donne acces a l'écran pour entrée un code de
de sécurité à l'aide d'un key pad.
Affiche le temps actifs du projet ainsi que la date du jour le jour et le mois
---------------------------------------------------------------------------
Début du programme: 2022.12.05
Poursuite du programme : 2022.12.06
Poursuite du programme : 2022.12.07
Poursuite du programme : 2022.12.08
---------------------------------------------------------------------------
*/
// ==============================================
#include "mesincludes.h"
// Déclaration des objets à portée globale:
TM1637 affichageHorloge(CLK, DIO);
DS1307 horloge;
Adafruit_ILI9341 ecranCentraleSurveillance = Adafruit_ILI9341(TFT_CS, TFT_DC);
LiquidCrystal_I2C ecranPrincipal(ADRESSE_I2C,NB_COLONES_ECRAN_LCD,NB_LIGNES_ECRAN_LCD);
Keypad keypad = Keypad(makeKeymap(keys), colRows, colPins, ROWS, COLS);
// ********************************************************
void setup()
// ********************************************************
{
// Initialisation des modules
Serial.begin(9600);
Serial << (F(MSG_DEMARRAGE_DU_PROJET));
Serial << (F(MSG_PROJET_PAR));
Serial << (F(MSG_DEBUT_INIT));
initialisationDesAppareils();
Serial << (F(MSG_FIN_INIT));
afficherPageEcran(centraleEcranOuverture);
afficherPageEcran(ecranOuverture, UNE_SECONDE * 2);
afficherPageEcran(ecranInstruction);
afficherTemps();
// Début de Lecture du bouton et affichage de l'heure en mode non bloquant
int nbrFoisBtn = 0;
while(nbrFoisBtn == 0)
{
if (digitalRead(PIN_BTN))
{
Serial << "Bouton enfoncé\n";
delay(5);
// Permet d'éviter que le bouton reste enfoncé
nbrFoisBtn++;
while (digitalRead(PIN_BTN));
delay(5);
}
}
// Fin de Lecture du bouton
if (connexionALaCentrale()){
afficherPageEcran(systemeEnLigne);
afficherPageEcran(debutEtape02);
}
} // setup()
// ********************************************************
void loop()
// ********************************************************
{
afficherTemps();
afficherPageEcran(debutEtape02);
lectureSensor();
} // loop()
// ********************************************************
char* tempsActifs()
// ********************************************************
{
static char temps[14] = "";
ecranPrincipal.setCursor(7,3);
unsigned long secondes = millis() / 1000;
int jours = secondes / 86400;
secondes %= 86400;
byte heures = secondes / 3600;
secondes %= 3600;
byte minutes = secondes / 60;
secondes %= 60;
snprintf(temps, sizeof(temps), "%03d, %02d:%02d:%02d ", jours, heures, minutes, secondes);
return temps;
} // tempsActifs()
// ********************************************************
void afficherPageEcran(Ecrans ecran, int delai = 0)
// ********************************************************
{
switch(ecran)
{
// Affichage du premier écran
case ecranOuverture:
ecranPrincipal.backlight();
ecranPrincipal.setCursor(0,0);
ecranPrincipal.print(F(alarme));
ecranPrincipal.setCursor(0,1);
ecranPrincipal.print(F(ligne));
ecranPrincipal.setCursor(0,2);
ecranPrincipal.print(F(version2));
ecranPrincipal.setCursor(0,3);
ecranPrincipal.print(F(matricule));
Serial << "afficherPageEcran\n";
break;
// Affichage du premier écran sur le ILI9341
case centraleEcranOuverture:
Serial << "Debut ecranCentraleSurveillance\n";
ecranCentraleSurveillance.setCursor(26, 26);
ecranCentraleSurveillance.setTextColor(ILI9341_RED);
ecranCentraleSurveillance.setTextSize(3);
ecranCentraleSurveillance.print(MSG_CENTRALE_SURVEILLANCE);
ecranCentraleSurveillance.setCursor(10, 80);
ecranCentraleSurveillance.setTextColor(ILI9341_WHITE);
ecranCentraleSurveillance.setTextSize(2);
ecranCentraleSurveillance.print(LIGNE_ILI9341);
ecranCentraleSurveillance.setCursor(0, 110);
ecranCentraleSurveillance.setTextColor(ILI9341_YELLOW);
ecranCentraleSurveillance.print(MESSAGE_ATTENTE_CLIENT);
Serial << "Fin ecranCentraleSurveillance\n";
break;
// Affichage du deuxième écran du LCD
case ecranInstruction:
delay(DEUX_SECONDES);
Serial << "Debut ecranInstruction\n";
ecranPrincipal.clear();
ecranPrincipal.print(MESSAGE_LIGNE1_ECRAN2);
ecranPrincipal.setCursor(0,1);
ecranPrincipal.print(MESSAGE_LIGNE2_ECRAN2);
ecranPrincipal.setCursor(0,2);
ecranPrincipal.print(MESSAGE_LIGNE3_ECRAN2);
ecranPrincipal.setCursor(0,3);
ecranPrincipal.print(MESSAGE_LIGNE4_ECRAN2);
Serial << "Fin ecranInstruction\n";
break;
// Affichage de la troisième écran sur les deux dispositifs
case systemeEnLigne:
Serial << "Debut systemeEnLigne\n";
ecranPrincipal.clear();
ecranPrincipal.setCursor(2,0);
ecranPrincipal.print(MESSAGE_SYSTEME_EN_LIGNE);
ecranPrincipal.setCursor(0,1);
ecranPrincipal.print(ligne);
ecranPrincipal.setCursor(0,2);
ecranPrincipal.print(MESSAGE_FIN_ETAPE1);
ecranPrincipal.setCursor(0,3);
ecranPrincipal.print(ligne);
ecranCentraleSurveillance.setCursor(0, 158);
ecranCentraleSurveillance.print(MESSAGE_SYSTEME_EN_LIGNE);
delay(QUATRE_SECONDES);
Serial << "Fin systemeEnLigne\n";
break;
// Début de l'étape 02
case debutEtape02:
ecranPrincipal.print(F(alarme));
ecranPrincipal.setCursor(0,1);
ecranPrincipal.print(F(ligne));
ecranPrincipal.setCursor(0,2);
ecranPrincipal.print(nomDuJour);
ecranPrincipal.setCursor(8,2);
ecranPrincipal.print(" ");
ecranPrincipal.setCursor(9,2);
sprintf(date, "%2u" , horloge.dayOfMonth);
ecranPrincipal.print(date);
ecranPrincipal.setCursor(11,2);
ecranPrincipal.print(" ");
ecranPrincipal.setCursor(12,2);
ecranPrincipal.print(nomMois);
ecranPrincipal.setCursor(0,3);
ecranPrincipal.print(actif);
ecranPrincipal.print(tempsActifs());
break;
case detectionMouvement:
ecranPrincipal.clear();
ecranPrincipal.print(F(MESSAGE_DETECTION_MOUVEMENT));
ecranPrincipal.setCursor(0,1);
ecranPrincipal.print(F(ligne));
ecranPrincipal.setCursor(0,2);
ecranPrincipal.print(F(MESSAGE_CODE));
ecranPrincipal.setCursor(0,3);
ecranPrincipal.print("_ ");
ecranPrincipal.setCursor(1,3);
break;
} // switch(ecran)
} // afficherPageEcran()
// ********************************************************
void initialisationDesAppareils()
// ********************************************************
{
// Initialisation du UART de la centrale
Serial3.begin(UART_VITESSE_CONSOLE3);
// Initialisation du UART du système d alarme
Serial1.begin(UART_VITESSE_CONSOLE1);
// Initilisation du LCD
ecranPrincipal.begin(NB_COLONES_ECRAN_LCD, NB_LIGNES_ECRAN_LCD);
// Initialisation du TFT
ecranCentraleSurveillance.begin();
// Initialisation du bouton
pinMode(PIN_BTN, INPUT);
// Initialisation du 4-digit
affichageHorloge.init();
// Initialisation du RTC
horloge.begin();
// Initialisation de la led
pinMode(PIN_LED, OUTPUT);
// Initialisation Sensor
pinMode(PIN_DETECTEUR_MOUVEMENT, INPUT);
} // initialisationDesAppareils()
// ********************************************************
boolean connexionALaCentrale()
// ********************************************************
{
// Retourner 'true' ou 'false' selon le cas.
Serial1.write(1);
delay(1000);
if(Serial3.available())
{
return true;
}
return false;
} // connexionALaCentrale()
// ********************************************************
void afficherTemps()
// ********************************************************
{
horloge.fillByHMS(15, 24, 45);
horloge.fillByYMD(2022, 12, 7);
// horloge.setTime();
horloge.getTime();
affichageHorloge.set(BRIGHT_TYPICAL);
switch (horloge.dayOfWeek)
{
case 1:
nomDuJour = "Dimanche";
break;
case 2:
nomDuJour = "Lundi";
break;
case 3:
nomDuJour = "Mardi";
break;
case 4:
nomDuJour = "Mercredi";
break;
case 5:
nomDuJour = "Jeudi";
break;
case 6:
nomDuJour = "Vendredi";
break;
case 7:
nomDuJour = "Samedi";
break;
};
switch (horloge.month)
{
case 1:
nomMois = "JANVIER";
break;
case 2:
nomMois = "FEVRIER";
break;
case 3:
nomMois = "MARS";
break;
case 4:
nomMois = "AVRIL";
break;
case 5:
nomMois = "MAI";
break;
case 6:
nomMois = "JUIN";
break;
case 7:
nomMois = "JUILIET";
break;
case 8:
nomMois = "AOUT";
break;
case 9:
nomMois = "SEPTEMBRE";
break;
case 10:
nomMois = "OCTOBRE";
break;
case 11:
nomMois = "NOVEMBRE";
break;
case 12:
nomMois = "DECEMBRE";
break;
};
// Division du temps pour l'afficher correctement sur le 4 Digits
affichageHorloge.display(0,horloge.hour / 10);
affichageHorloge.display(1,horloge.hour % 10);
affichageHorloge.display(2,horloge.minute / 10);
affichageHorloge.display(3,horloge.minute % 10);
affichageHorloge.point(true);
} // afficherTemps()
// ********************************************************
void lectureSensor()
// ********************************************************
{
val = digitalRead(PIN_DETECTEUR_MOUVEMENT);
if (val == HIGH)
{
digitalWrite(PIN_LED, HIGH);
if (statusSensor == LOW)
{
Serial << "Mouvement détecté!\n";
afficherPageEcran(detectionMouvement);
statusSensor = HIGH;
lectureKeyPad();
}
}
}
// ********************************************************
void lectureKeyPad()
// ********************************************************
{
bool terminer = false;
while (terminer == false || trenteSecondePasser() == false)
{
// Initialisation du keypad
char key = keypad.getKey();
if (key != NO_KEY) // Si touche appuyée
{
valeurCle += key - 48; // convertir la valeur du caractère en valeur numérique. Par exemple, '0' vaut 48.
Serial << key;
ecranPrincipal.print(key);
compteur++;
}
if (compteur == 4)
{
Serial << CODE_ALARME;
Serial << resultatCodeAlarme;
Serial << VALIDATION;
if (valeurCle == resultatCodeAlarme)
{
Serial << VALIDE;
while (digitalRead(PIN_DETECTEUR_MOUVEMENT))
return;
}
else
{
Serial << INVALIDE;
while (digitalRead(PIN_DETECTEUR_MOUVEMENT))
return;
}
}
}
}
bool trenteSecondePasser()
{
static unsigned long momentPresent = millis();
if (millis() - momentPresent >= TRENTE_SECONDES)
{
momentPresent = millis();
return true;
}
return false;
}