#include "synthese.h"
// Projet de session 2022
// De: Félix Theoret
// Date : 2022-12-19
// Document de départ
// ==============================================
// Ce programme est une centrale de surveillance qui
// affiche si le detecteur de mouvement est en ligne
// ==============================================
// ********************************************************
// Début du programme
// ********************************************************
// ********************************************************
void setup()
// ********************************************************
{
#ifdef DEBUG
Serial.begin(UART_VITESSE_CONSOLE);
#endif
initialisationDesAppareils();
ecranPrincipal.setCursor(0,1);
ecranPrincipal << "Connexion ....";
if (true){
ecranPrincipal.setCursor(0,2);
ecranPrincipal << "Systeme en ligne";
}
}
// ********************************************************
void loop()
// ********************************************************
{
bool alarmeON = true;
afficherTempsEcoule();
if (digitalRead(DETECTEUR_MOUVEMENT_ENTREE)) {
#ifdef DEBUG
Serial << "DETECTEUR_MOUVEMENT_ENTREE\n";
#endif
digitalWrite(LED1, HIGH);
ecranPrincipal.clear();
ecranPrincipal << "Detection mouvement";
#ifdef DEBUG
Serial << "Aviser la centrale de l'intrusion\n";
#endif
if (envoyerMessageVersLaCentrale(evenement_detection_mouvement) == ACK_evenement_detection_mouvement)
{
digitalWrite(LED1, LOW);
}
while(digitalRead(DETECTEUR_MOUVEMENT_ENTREE)); // S'assurer que le détecteur est détendu.
ecranPrincipal.clear();
}
if(digitalRead(BOUTON))
{
while(digitalRead(BOUTON));
static unsigned long int momentPresent = millis();
#ifdef DEBUG
Serial << F("envoyerMessageVersLaCentrale") << endl;
Serial << F("UART_CENTRALE.available()") << endl;
Serial << F("La centrale a reçu le message: ");
Serial << F("evenement_bouton_panique") << endl;
#endif
UART_SYSTEME_ALARME.write("salut");
delay(DELAI_TRANSMISSION_MESSAGE);
if (UART_CENTRALE.available()){
#ifdef DEBUG
Serial << F("ACK_evenement_bouton_panique") << endl;
#endif
while(digitalRead(BOUTON) == false)
{
digitalWrite(LED2, HIGH);
delay(2000);
digitalWrite(LED2, LOW);
delay(2000);
}
}
}
} // loop()
// ********************************************************
void initialisationDesAppareils()
// ********************************************************
{
ecranPrincipal.begin(LCD_NB_COLONNE,LCD_NB_LIGNE);
ecranPrincipal.backlight();
ecranPrincipal << "Initialisation...";
UART_SYSTEME_ALARME.begin(UART_VITESSE_CENTRALE);
UART_CENTRALE.begin(UART_VITESSE_CENTRALE);
pinMode(DETECTEUR_MOUVEMENT_ENTREE, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(BOUTON, INPUT);
} // initialisationDesAppareils
// ********************************************************
int envoyerMessageVersLaCentrale(codesAlarme message)
// ********************************************************
{
byte codeAlarme;
bool connexion = false;
#ifdef DEBUG
Serial << F("Tentative de connexion à la centrale ...") << endl;
#endif
UART_SYSTEME_ALARME.write(evenement_systeme_enligne);
delay(DELAI_TRANSMISSION_MESSAGE);
if (UART_CENTRALE.available()){
#ifdef DEBUG
Serial << F("UART_CENTRALE.available()") << endl;
#endif
codeAlarme = UART_CENTRALE.read();
switch (codeAlarme) {
case evenement_systeme_enligne:
#ifdef DEBUG
Serial << F("evenement_systeme_enligne") << endl;
Serial << F("Succes: Connexion à la centrale ...") << endl;
#endif
//afficherPageEcran(centraleSystemeEnLigne);
connexion = true;
break;
default:
#ifdef DEBUG
Serial << "Evenement non traité" << endl;
#endif
break;
}
}
if(connexion == true)
{
#ifdef DEBUG
Serial << F("envoyerMessageVersLaCentrale") << endl;
#endif
UART_SYSTEME_ALARME.write(message);
delay(DELAI_TRANSMISSION_MESSAGE);
if (UART_CENTRALE.available()){
#ifdef DEBUG
Serial << F("UART_CENTRALE.available()") << endl;
Serial << F("La centrale a reçu le message: ");
#endif
codeAlarme = UART_CENTRALE.read();
switch (codeAlarme) {
case evenement_detection_mouvement:
#ifdef DEBUG
Serial << F("evenement_detection_mouvement") << endl;
#endif
UART_CENTRALE.write(ACK_evenement_detection_mouvement);
delay(DELAI_TRANSMISSION_MESSAGE);
if (UART_SYSTEME_ALARME.available())
{
codeAlarme = UART_SYSTEME_ALARME.read();
}
return codeAlarme;
break;
default:
#ifdef DEBUG
Serial << "Evenement non traité" << endl;
#endif
break;
} // switch
} // if available()
return CODE_INVALIDE;
}
} // connexionALaCentrale
// ***************************************************
void afficherTempsEcoule() {
// ***************************************************
static unsigned long depart = millis();
if (millis() - depart < UNE_SECONDE) return;
char bufferLigneLCD[21]; // Vecteur pour construire le message à afficher
depart = millis(); // Réinitialiser le temps qui passe ...
unsigned long tempsEcoule = millis() / 1000;
int secondes = tempsEcoule % 60;
int minutes = (tempsEcoule / 60 ) % 60;
int heures = (tempsEcoule / 3600 ) % 24;
int jours = 0;
snprintf(bufferLigneLCD, LCD_NB_COLONNE + 1, "Actif: %03d, %02d:%02d:%02d", 0, heures, minutes, secondes );
ecranPrincipal.setCursor(LCD_PREMIERE_COLONNE,LCD_LIGNE4);
ecranPrincipal.print(bufferLigneLCD);
} // afficherTemps()
// ********************************************************
// Fin du programme
// ********************************************************