From 5907c6f197b91a5bd710d9f1107d5227ba43702f Mon Sep 17 00:00:00 2001 From: Kosmo Obermayer Date: Wed, 6 Dec 2023 08:02:16 +0100 Subject: [PATCH] Initial commit --- .gitignore | 5 ++ .vscode/extensions.json | 10 +++ include/README | 39 ++++++++++ lib/README | 46 +++++++++++ platformio.ini | 15 ++++ src/BACKUp_BLACKWHITE.cpp | 37 +++++++++ src/OLD_PROGRAMM.cpp | 150 +++++++++++++++++++++++++++++++++++ src/Variablen.h | 18 +++++ src/Werte.cpp | 116 +++++++++++++++++++++++++++ src/Werte.h | 35 +++++++++ src/main.cpp | 41 ++++++++++ src/mainFIRSTATTEMPT.cpp | 159 ++++++++++++++++++++++++++++++++++++++ test/README | 11 +++ 13 files changed, 682 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/BACKUp_BLACKWHITE.cpp create mode 100644 src/OLD_PROGRAMM.cpp create mode 100644 src/Variablen.h create mode 100644 src/Werte.cpp create mode 100644 src/Werte.h create mode 100644 src/main.cpp create mode 100644 src/mainFIRSTATTEMPT.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..7fdd5ed --- /dev/null +++ b/platformio.ini @@ -0,0 +1,15 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32-s2-saola-1] +platform = espressif32 +board = esp32-s2-saola-1 +framework = arduino +monitor_speed = 115200 diff --git a/src/BACKUp_BLACKWHITE.cpp b/src/BACKUp_BLACKWHITE.cpp new file mode 100644 index 0000000..d69ea8d --- /dev/null +++ b/src/BACKUp_BLACKWHITE.cpp @@ -0,0 +1,37 @@ +/* + +#include + +#define AO 14 // Analog Output // +#define DO 13 // Digital Output // +#define CLK 10 // display as usual // +#define DIO 11 + +class VALUES +{ + public: + int DigVal; + int AnlVal; +}; + +void setup() +{ + pinMode(AO, INPUT); + pinMode(DO, INPUT); + + Serial.begin(9600); +} + +void loop() +{ + VALUES Wert; + Wert.DigVal = analogRead(AO); + Wert.AnlVal = digitalRead(DO); + + Serial.printf("Analog: %d Digital: %d\n", Wert.AnlVal, Wert.DigVal); + + + +} + +*/ \ No newline at end of file diff --git a/src/OLD_PROGRAMM.cpp b/src/OLD_PROGRAMM.cpp new file mode 100644 index 0000000..4848ae3 --- /dev/null +++ b/src/OLD_PROGRAMM.cpp @@ -0,0 +1,150 @@ +/* + +#include + +#define AnalogPIN 14 +#define DigitalPIN + +class WERTE +{ + public: + int ANL = 0; + + unsigned long int NewTime = 0; + unsigned long int NTBackup = 0; + unsigned long int OldTime = 0; + unsigned long int Delta = 0; + + double IN_STUNDEN = 0; + double WATT = 0; +}Mess; + +class VARIABLEN +{ + public: + int COUNT = 0; + int TIMESTAMP = 0; + int CAST = 0; // Für delta -> Watt // + double PLACE = 0; + + bool FLAG = false; + bool SIGNAL = false; +}VAR; + +// Funktionen_prototypen: // +void nochimmer(); +void check(); +void calc(); + +void setup() +{ + void IRAM_ATTR onTimer(); + + hw_timer_t *My_timer = 0; + My_timer = timerBegin(0, 80, true); + timerAttachInterrupt(My_timer, &onTimer, true); + timerAlarmWrite(My_timer, 5000, true); + timerAlarmEnable(My_timer); + + Serial.begin(115200); +} + +void loop() +{ + //Serial.println(Mess.ANL); + if(Mess.ANL >= 450) + { + check(); // Zeit New & Old richtig einlesen && Aufrufen von NOCHIMMER() // + } + +} + +void IRAM_ATTR onTimer() +{ + Mess.ANL = analogRead(AnalogPIN); +} + +void nochimmer() +{ + VAR.COUNT = 0; + + while(1) + { + + if(VAR.COUNT > 15) + { + break; + } + + VAR.TIMESTAMP = millis() + 6; + while((VAR.TIMESTAMP - millis()) > 0){millis();}; + if(Mess.ANL < 430) + { + VAR.COUNT++; + } + else + { + continue; + } + } + Serial.printf("\n\n\n\nSUCCESS\n\n\n\n"); + + calc(); // Ausrechnen von Delta, Watt und eventueller Output // +} + +void check() +{ + Mess.NTBackup = Mess.NewTime; + Mess.NewTime = millis(); + + + VAR.COUNT = 0; + VAR.FLAG = false; + + while(VAR.COUNT != 4 && VAR.FLAG != true) + { + VAR.TIMESTAMP = millis() + 6; + while((VAR.TIMESTAMP - millis()) > 0){millis();}; // Warte 5ms auf neuen Wert // + // Serial.printf("ANL: %d\n", Mess.ANL); + if(Mess.ANL >= 450) + { + VAR.COUNT++; + } + else + { + VAR.FLAG = true; + } + } + + Serial.println("-----------------------------------"); + + if(VAR.FLAG == true) + { + Mess.NewTime = Mess.NTBackup; + // Serial.println("..."); + } + else + { + Mess.OldTime = Mess.NTBackup; + // Serial.printf("TIME: %d\n", Mess.NewTime); + nochimmer(); // Überprüfen, ob statement noch immer zutrifft // + } +} + +void calc() +{ + if(Mess.OldTime != 0) + { + Mess.Delta = Mess.NewTime - Mess.OldTime; + + VAR.CAST = Mess.Delta; + VAR.PLACE = VAR.CAST / 1000; + Mess.IN_STUNDEN = 60 * (60 / VAR.PLACE); + Mess.WATT = (1000 * Mess.IN_STUNDEN) / 960; + + // OUTPUT // + Serial.printf("Momentanverbauch: DELTA: %d WATT: %0.2f\n", Mess.Delta, Mess.WATT); + } +} + +*/ \ No newline at end of file diff --git a/src/Variablen.h b/src/Variablen.h new file mode 100644 index 0000000..02b5d30 --- /dev/null +++ b/src/Variablen.h @@ -0,0 +1,18 @@ +#ifndef VARIABLEN_H +#define VARIABLEN_H + +#include + +class Variablen +{ + protected: + static int _count; + static int _timestamp; + static int _cast; // Für delta -> Watt // + static double _place; // placeholder // + + // boolsche Attribute // + static bool _flag; +}; + +#endif \ No newline at end of file diff --git a/src/Werte.cpp b/src/Werte.cpp new file mode 100644 index 0000000..626e60a --- /dev/null +++ b/src/Werte.cpp @@ -0,0 +1,116 @@ +#include +#include + +// 1st initialization: Werte // +int Werte::_anl = 0; +unsigned long Werte::_new_time = 0; +unsigned long Werte::_nt_backup = 0; +unsigned long Werte::_old_time = 0; +unsigned long Werte::_delta = 0; +double Werte::_watt = 0; +double Werte::_in_stunden = 0; + +// 1st initialization: Variablen // +int Variablen::_count = 0; +int Variablen::_cast = 0; +int Variablen::_timestamp = 0; +double Variablen::_place = 0; +bool Variablen::_flag = false; + +void Werte::set_ANL(int var) +{ + _anl = var; +} + +int Werte::get_ANL() +{ + return _anl; +} + +// Checkt ab, ob der Wert kein Fehlwert war // +void Werte::check() +{ + _nt_backup = _new_time; + _new_time = millis(); + + _count = 0; + _flag = false; + + while(_count != 4 && _flag != true) + { + _timestamp = millis() + 6; + + // Warte 5ms auf neuen Wert // + while((_timestamp - millis()) > 0){millis();}; + if(get_ANL() >= 450) + { + _count++; + } + else + { + // Falschwert erfasst // + _flag = true; + } + } + + Serial.println("-----------------------------------"); + + if(_flag == true) + { + // Alten Wert wiederherstellen // + _new_time = _nt_backup; + } + else + { + _old_time = _nt_backup; + // Serial.printf("TIME: %d\n", Mess.NewTime); Überprüfe: + nochimmer(); + } +} + +// Überprüfen, ob statement noch immer zutrifft // +void Werte::nochimmer() +{ + _count = 0; + + while(1) + { + + if(_count > 15) + { + break; + } + + _timestamp = millis() + 6; + while((_timestamp - millis()) > 0){millis();}; + if(get_ANL() < 430) + { + _count++; + } + else + { + continue; + } + } + Serial.printf("\n\n\n\nSUCCESS\n\n\n\n"); + + calc(); +} + +// Ausrechnen von Delta, Watt und eventueller Output // +void Werte::calc() +{ + if(_old_time != 0) + { + _delta = _new_time - _old_time; + + // Casting zu int ftf // + _cast = _delta; + _place = _cast / 1000; + _in_stunden = 60 * (60 / _place); + _watt = (1000 * _in_stunden) / 960; + + // OUTPUT // + Serial.printf("Momentanverbauch: DELTA: %d WATT: %0.2f\n", _delta, _watt); + } +} \ No newline at end of file diff --git a/src/Werte.h b/src/Werte.h new file mode 100644 index 0000000..88ae5a0 --- /dev/null +++ b/src/Werte.h @@ -0,0 +1,35 @@ +#ifndef WERTE_H +#define WERTE_H + +#include +#include + +class Werte : protected Variablen +{ + public: + Werte(){}; + ~Werte(){}; + + // Interaktion mit Timer: // + static void set_ANL(int var); + static int get_ANL(); + + // Trigger Methode // + static void check(); + + private: + static int _anl; + + static unsigned long int _new_time; + static unsigned long int _nt_backup; + static unsigned long int _old_time; + static unsigned long int _delta; + + static double _in_stunden; + static double _watt; + + static void nochimmer(); + static void calc(); +}; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..1a36cd9 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +#define AnalogPIN 14 +#define DigitalPIN + +void setup() +{ + // Initialisiere den Timer // + void IRAM_ATTR onTimer(); + + hw_timer_t *My_timer = 0; + My_timer = timerBegin(0, 80, true); + timerAttachInterrupt(My_timer, &onTimer, true); + timerAlarmWrite(My_timer, 5000, true); + timerAlarmEnable(My_timer); + + // Serielle Kommunikation // + Serial.begin(115200); +} + +void loop() +{ + // Serial.println(Werte::get_ANL()); + // ^ zum Überprüfen der Analog-Werte // + + // Wert möglicherweise vom Strich? // + if(Werte::get_ANL() >= 450) + { + // Zeit New & Old richtig einlesen && Aufrufen von NOCHIMMER() // + Werte::check(); + } + +} + +void IRAM_ATTR onTimer() +{ + // Liest Wert ein ca jede 5ms // + Werte::set_ANL(analogRead(AnalogPIN)); +} \ No newline at end of file diff --git a/src/mainFIRSTATTEMPT.cpp b/src/mainFIRSTATTEMPT.cpp new file mode 100644 index 0000000..0cc0ceb --- /dev/null +++ b/src/mainFIRSTATTEMPT.cpp @@ -0,0 +1,159 @@ +/* + +#include + +#define AO 14 // Analog Output // +#define DO 13 // Digital Output // +#define CLK 10 // display as usual // +#define DIO 11 + +class VALUES +{ + public: + int DigVal; + int AnlVal =0 ; + unsigned long int NewT = 0; + unsigned long int delta = 0; + unsigned long int BACKUPdelta = 0; + unsigned long int BACKUPproz; + unsigned long int OldT = 0; + unsigned long int BACKUPtwice = 0; +}; +VALUES Wert; + +void setup() +{ + void IRAM_ATTR onTimer(); + + hw_timer_t *My_timer = NULL; + + pinMode(AO, INPUT); + //pinMode(DO, INPUT); + + Serial.begin(115200); + + My_timer = timerBegin(0, 80, true); + timerAttachInterrupt(My_timer, &onTimer, true); + timerAlarmWrite(My_timer, 50000, true); + timerAlarmEnable(My_timer); +} + +int CHECK(int VAR); +void CALC(int TurnTime); +void COMPARE(int First, int Second); +void FILTERING(); + +int FLAG = 0; +int FLECK = 0; +int COUNTER = 0; +int CHECKING = 0; +int VAR = 0; +int LATE = 0; +double Watt; +double Var; +double INT; +int ARR[3]; +bool SCREAM = 0; + +void loop() +{ + Wert.AnlVal = VAR; + //Serial.println(Wert.AnlVal); + //Serial.println(Wert.delta); + + + if (Wert.AnlVal > 450 && FLAG == 0) + { + + Serial.println("JUMP"); + + Wert.NewT = millis(); + + if(SCREAM == 0) + { + Wert.BACKUPdelta = Wert.delta; + } + Wert.delta = Wert.NewT - Wert.OldT; + Wert.BACKUPproz = Wert.BACKUPdelta - (Wert.BACKUPdelta * 0.25); // ^ ?? + + if(Wert.delta <= Wert.BACKUPproz && FLECK != 0) + { + Serial.printf("Falschwert?? DELTA: %d\n", Wert.delta); + + + } + else + { + SCREAM = 1; + CHECKING = 0; + Wert.OldT = Wert.NewT; + Wert.BACKUPdelta = Wert.delta; + Serial.printf("NEWT: %d Delta: %d\n", Wert.NewT, Wert.delta); + CALC(Wert.delta); + Serial.printf("Momentan Verbrauch: %0.2f Watt\n", Watt); + } + + FLAG = 1; + FLECK = 1; + + } + + + else if (Wert.AnlVal > 450) + { + while(1) + { + Wert.AnlVal = VAR; + if(CHECK(VAR) == 1) + { + FLAG = 0; + break; + } + } + } + + else if(Wert.AnlVal < 450) + { + FLAG = 0; + } + + + + // NEW ASSIGNED TO OLD BTW INCASE THIS WONT WORK // + +}//------------------------------------------------------------------------------------------------------------------- + +int CHECK(int VAR) +{ + if(COUNTER == 10) + { + COUNTER = 0; + return 1; + } + + if(VAR < 450) + { + COUNTER++; + return 0; + } + else + { + COUNTER = 0; + return 0; + } +} + +void IRAM_ATTR onTimer() +{ + VAR = analogRead(AO); +} + +void CALC(int TurnTime) +{ + INT = TurnTime; + Var = INT / 1000; + double InStd = 60 * (60 / Var); + Watt = (1000 * InStd) / 960; +} + +*/ \ No newline at end of file diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html