From 4f81ea6c28445ef461c4a444b6a810b6080837a4 Mon Sep 17 00:00:00 2001 From: popkex Date: Tue, 9 Jun 2026 19:16:58 +0200 Subject: [PATCH] Add Controller and Updater classes for shutter control and updates --- Hard/src/controller.cpp | 93 +++++++++++++++++++++++++++++++++++++++++ Hard/src/controller.h | 31 ++++++++++++++ Hard/src/main.cpp | 15 +++++++ Hard/src/updater.h | 5 ++- 4 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 Hard/src/controller.cpp create mode 100644 Hard/src/controller.h diff --git a/Hard/src/controller.cpp b/Hard/src/controller.cpp new file mode 100644 index 0000000..7f296c2 --- /dev/null +++ b/Hard/src/controller.cpp @@ -0,0 +1,93 @@ +#include "Arduino.h" +#include "controller.h" +/* +shutterStates[n] = 0 => le volet est fermer (position basse) +shutterStates[n] = 100 => le volet est ouvert (position haute) +*/ + +Controller::Controller() { +} + +void Controller::initController() { + for (int i = 0; i < 5; i++) { + // créer le volet + unsigned short pinDown = 9 - (2 * i); + unsigned short pinUp = 8 - (2 * i); + + Shutter shutter{pinUp, pinDown, i}; // i est le nom + + pinMode(i, OUTPUT); + + // stock le volet + shutters[i] = &shutter; + } + + // Permet de correctement initaliser tout les volets + openExactePercentAllShutter(100); + openExactePercentAllShutter(0); +} + +void Controller::openExactePercentSpecificShutter(unsigned short shutter, float percent) { + if (percent < 0) + percent = 0; + else if (percent > 100) + percent = 100; + + if (shutter < 0 && shutter >= 5) + return; + + float percentOpen = shutters[shutter]->percentOpen; + // si le volet est deja ouvert a son pourcentage, ne rien faire + if (percent = percentOpen) + return; + + float deltaPercent = percentOpen - percent; + + if (deltaPercent < 0) // on doit fermet + digitalWrite(shutters[shutter]->pinDown, 1); + else // on doit ouvrir + digitalWrite(shutters[shutter]->pinUp, 1); + + // Arreter le volet au bon moment + delay((percent * 1000) / shutters[shutter]->TIMETOOPEN); + + digitalWrite(shutters[shutter]->pinUp, 0); + digitalWrite(shutters[shutter]->pinDown, 0); +} + +void Controller::openExactePercentAllShutter(float percent) { + for (int i = 0; i < 5; i++) { + openExactePercentSpecificShutter(i, percent); + } +} + +void Controller::checkManualOpen() { + // ouvre tous les volets du salon + if (analogRead(A7) > 512) { + openExactePercentSpecificShutter(0, 100); + openExactePercentSpecificShutter(1, 100); + openExactePercentSpecificShutter(2, 100); + } + // Ferme tous les volets du salon + if (analogRead(A6) > 512) { + openExactePercentSpecificShutter(0, 0); + openExactePercentSpecificShutter(1, 0); + openExactePercentSpecificShutter(2, 0); + } + + // Ouvre tous les volets de la cuisine + if (analogRead(A3) > 512) { + openExactePercentSpecificShutter(3, 100); + openExactePercentSpecificShutter(4, 100); + } + // Ferme tous les volets du salon + if (analogRead(A2) > 512) { + openExactePercentSpecificShutter(3, 0); + openExactePercentSpecificShutter(4, 0); + } + + if (analogRead(A1) > 512) + openExactePercentAllShutter(100); + if (analogRead(A0) > 512) + openExactePercentAllShutter(0); +} \ No newline at end of file diff --git a/Hard/src/controller.h b/Hard/src/controller.h new file mode 100644 index 0000000..b52d27f --- /dev/null +++ b/Hard/src/controller.h @@ -0,0 +1,31 @@ +#ifndef CONTROLER +#define CONTROLER + +class Controller { + public: + Shutter* shutters[5]; + + Controller(); + + void initController(); // permet de init le state de tout les volets /!\ bouge les volets IRL + + void openExactePercentSpecificShutter(unsigned short shutter, float percent); // Ouvrir un seul volet a x pourcent + void openExactePercentAllShutter(float percent); // ouvrir tous les volets a x pourcent + + void checkManualOpen(); +}; + +struct Shutter { + public: + const unsigned short pinUp; + const unsigned short pinDown; + const unsigned short name; + const unsigned short TIMETOOPEN = 10; // en secondes + + float percentOpen = 0.0f; // pourcentage d'ouverture du volet, 0: fermer | 100: ouvert + + // Constructeur + Shutter(unsigned short up, unsigned short down, unsigned short n) : pinUp(up), pinDown(down), name(n) {} +}; + +#endif \ No newline at end of file diff --git a/Hard/src/main.cpp b/Hard/src/main.cpp index 2a28309..459fab3 100644 --- a/Hard/src/main.cpp +++ b/Hard/src/main.cpp @@ -2,9 +2,24 @@ #include "updater.h" #include "wifiManager.h" +#include "controller.h" + +unsigned long lastTimeCheckUpdate; +const unsigned timeWaitRestart = 182.575 * 24 * 3600; // permet le redemarrage tous les 6mois +const unsigned timeWaitCheckUpdate = 24 * 3600; // permet la verfu des updates tous les jours + +WifiManager wifiManager; // se connecte automatiquement au WiFi +Updater updater; +Controller controller; void setup() { + updater.update(); // vérifie les updates + controller.initController(); } void loop() { + if (millis() > timeWaitRestart) // redemarrage tout les 6mois + ESP.restart(); + else if (lastTimeCheckUpdate - millis() > timeWaitCheckUpdate) + updater.update(); } \ No newline at end of file diff --git a/Hard/src/updater.h b/Hard/src/updater.h index 0432d54..b461a66 100644 --- a/Hard/src/updater.h +++ b/Hard/src/updater.h @@ -5,11 +5,14 @@ const int THISVERSION = 1; const char* FIRMWAREURL = "http://89.168.57.22:6543/"; const char* VERSIONURL = "http://89.168.57.22/6543/LastVersion.txt"; +// Permet la mise a jour a distance class Updater { + private: + bool checkUpdate(); + public: Updater(); - bool checkUpdate(); void update(); };