From 366097db99256a6af88558fad86de3e28e9f1a50 Mon Sep 17 00:00:00 2001 From: Daniel A Date: Mon, 5 Jun 2023 14:32:37 -0400 Subject: [PATCH 1/2] DEV_Oxygen - Create Oxygen files --- include/Oxygen.h | 26 ++++++++++++ include/common.h | 5 ++- src/Oxygen.cpp | 103 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 1 + 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 include/Oxygen.h create mode 100644 src/Oxygen.cpp diff --git a/include/Oxygen.h b/include/Oxygen.h new file mode 100644 index 0000000..bb95844 --- /dev/null +++ b/include/Oxygen.h @@ -0,0 +1,26 @@ +#include +#ifdef OXYGEN + +#ifndef OXYGEN_FILE_H +#define OXYGEN_FILE_H + +#include + +/// @brief Setup function of module +void MySetup(); + +/// @brief Reset function of module +void ResetModule(); + +/// @brief Loop function of module +void MyLoop(); + +/// @brief Function called when a message is received +void Received(); + +/// @brief Function called when a message is send in close loop +void ServerSimulation(); + + +#endif // FILE_H +#endif // OXYGEN \ No newline at end of file diff --git a/include/common.h b/include/common.h index a89a0c6..0b2f240 100644 --- a/include/common.h +++ b/include/common.h @@ -15,12 +15,13 @@ // Define witch module is used (ONLY ONE) // #define MODEL // #define BREACH -#define ELECTRICITY +//#define ELECTRICITY // #define ENERGY // #define ENGINE // #define EXTINGUISHER // #define FIRE // #define NAVIGATION +#define OXYGEN // #define RADAR // #define SHIELD // #define TORPEDO @@ -46,6 +47,8 @@ #define TYPE ("FIR_" + std::to_string(NUMBER)).c_str() #elif defined(NAVIGATION) #define TYPE "NAV" +#elif defined(OXYGEN) +#define TYPE "OXY" #elif defined(RADAR) #define TYPE "RDR" #elif defined(SHIELD) diff --git a/src/Oxygen.cpp b/src/Oxygen.cpp new file mode 100644 index 0000000..9bfffe2 --- /dev/null +++ b/src/Oxygen.cpp @@ -0,0 +1,103 @@ +#include +#ifdef OXYGEN + +#include + +//////// Add new include library +//#include + +//////// Define global constantes (ALWAYS IN MAJ, use pin number and not name) +// const int TEST_IN = 10; +// const int TEST_OUT = 11; + +//////// Define global variables +int _testInt = 0; +bool _testBool = 0; + + +///////////////////////////////////////////////////////////////////////////////////////////// +// User function // +///////////////////////////////////////////////////////////////////////////////////////////// + +/// @brief Si je tape ///, il me propose de mettre des commentaires à la fonction +void my_function() +{ + // ... +} + + +///////////////////////////////////////////////////////////////////////////////////////////// +// Setup and reset // +///////////////////////////////////////////////////////////////////////////////////////////// + +/// @brief Setup function for the module +void MySetup() +{ + // Suround every "Serial" order between "#ifdef LOG" and "#endif" + #ifdef LOG + Serial.println("--- Model ---"); + #endif +} + + +/////////////////////////////// Reset all proprety of module //////////////////////////////// + +/// @brief Call after the config and when the module reset by the app +void ResetModule() +{ +} + + +///////////////////////////////// Write here the loop code ///////////////////////////////// + +/// @brief Call at the end of the main loop function +void MyLoop() +{ + // To send datas to the server, use the send function + comm.send("LED;R;1"); + + // It's possible to send with more then on line + comm.start(); // Open the buffer + comm.add("LED"); // Write String + comm.add(';'); // Add char + // comm.add(testInt); // Add from variable + comm.send(); // Send concatened variable +} + + +///////////////////////////////////////////////////////////////////////////////////////////// +// Communication // +///////////////////////////////////////////////////////////////////////////////////////////// + +/// @brief Call when a message is received from server (or serial) +void Received() +{ + + // If you want debug the received string, you can write this commande: + comm.Info_Received(); + + // The get the received code, use the function GetCode() + if (comm.GetCode() == "TST") + { + // If you need to check the number of parameter available, use the function comm.GetSize() + Serial.println("Number of received parameter :"); + Serial.println(comm.GetSize()); + + // If you want to read one parameter, you can use the function comm.GetParameter(x) + Serial.println("Value of parameter 1 :"); + Serial.println(comm.GetParameter(1)); + } +} + +/// @brief When a message is send without server, the message will be received here. You can close the loop to test the module +void ServerSimulation() +{ + // Update navigation status + if (comm.GetCode() == "NVC") + { + // DO something + Serial.println("WOOW, j'ai reçu un NVC en local !"); + } +} + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2f8fdad..219aec1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include From a62b9ab0560562fd075034a939bce94e2c79f312 Mon Sep 17 00:00:00 2001 From: Daniel A Date: Thu, 8 Jun 2023 11:51:17 -0400 Subject: [PATCH 2/2] Dev_Oxygen Write and test code --- include/common.h | 2 +- src/Oxygen.cpp | 127 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 104 insertions(+), 25 deletions(-) diff --git a/include/common.h b/include/common.h index 0b2f240..35e8297 100644 --- a/include/common.h +++ b/include/common.h @@ -10,7 +10,7 @@ #define LOG // Work without the server (put in comment to work with the server) -// #define STANDALONE + #define STANDALONE // Define witch module is used (ONLY ONE) // #define MODEL diff --git a/src/Oxygen.cpp b/src/Oxygen.cpp index 9bfffe2..b1b31bc 100644 --- a/src/Oxygen.cpp +++ b/src/Oxygen.cpp @@ -4,67 +4,140 @@ #include //////// Add new include library -//#include +#include //////// Define global constantes (ALWAYS IN MAJ, use pin number and not name) -// const int TEST_IN = 10; -// const int TEST_OUT = 11; + +const int POTPIN = A0; // Analog pin used to read the potentiometer +const int UPPERTHRESHOLD = 900; // Upper threshold to detect upward movement +const int LOWERTHRESHOLD = 10; // Lower threshold to detect downward movement + +const int INCREMENTATION = 10; // number of Back-and-Forth neeeded to add oxygen + +const int NUM_LEDS = 10; +#define LED_PIN 13 +#define BRIGHTNESS 200 +Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRBW + NEO_KHZ800); + +#define WORKALONE // add oxygen automaticly //////// Define global variables -int _testInt = 0; -bool _testBool = 0; +int potValue; // Variable to store the read value from the potentiometer +bool previousState = 0; // Variable to store the previous state of the potentiometer +int counter = 0; // Counter for the number of back-and-forth movements +int percent = 50; // Oxygen percent ///////////////////////////////////////////////////////////////////////////////////////////// // User function // ///////////////////////////////////////////////////////////////////////////////////////////// -/// @brief Si je tape ///, il me propose de mettre des commentaires à la fonction -void my_function() + +/// @brief Read Back-and-Forth from the potentiometer +void read_moves() +{ + potValue = analogRead(POTPIN); + + // Detect upward movement + if (potValue > UPPERTHRESHOLD && previousState == 0) + { + counter++; + previousState = 1; + +#ifdef LOG + Serial.print("Back-and-Forth: "); + Serial.println(counter); +#endif + } + // Detect downward movement + else if (potValue < LOWERTHRESHOLD && previousState == 1) + { + previousState = 0; + } +} + +/// @brief Send to server when the counter reach the right number for adding oxygen +void send_server() { - // ... + if (counter >= INCREMENTATION) + { + comm.send("OXY;1"); + counter = 0; + +#ifdef WORKALONE + + if (percent <= 90) + { + percent = percent + 10; +#ifdef LOG + Serial.print("Augmentation du pourcentage. Nouveau pourcentage : "); + Serial.println(percent); +#endif + } + else if (percent < 100 && percent > 90) + { + percent = 100; +#ifdef LOG + Serial.print("Augmentation du pourcentage. Nouveau pourcentage : "); + Serial.println(percent); +#endif + } + +#endif + + } } +/// @brief display oxygen percent with leds and red to green effect +void display_percent() +{ + for (int i = 0; i < NUM_LEDS; i++) + { + pixels.setPixelColor(i, pixels.Color(((10 - i) * 25), (i * 25), 0, 0)); + } + + /// @brief Turn off the leds depending percent + int ledpercent = percent / NUM_LEDS; + for (int j = (ledpercent); j < NUM_LEDS; j++) + { + pixels.setPixelColor(j, pixels.Color(0, 0, 0, 0)); + } + + pixels.show(); +} ///////////////////////////////////////////////////////////////////////////////////////////// // Setup and reset // ///////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Setup function for the module void MySetup() { - // Suround every "Serial" order between "#ifdef LOG" and "#endif" - #ifdef LOG - Serial.println("--- Model ---"); - #endif + pixels.begin(); + pixels.setBrightness(BRIGHTNESS); + pixels.clear(); + pixels.show(); } - /////////////////////////////// Reset all proprety of module //////////////////////////////// /// @brief Call after the config and when the module reset by the app void ResetModule() { + percent = 50 ; } - ///////////////////////////////// Write here the loop code ///////////////////////////////// /// @brief Call at the end of the main loop function void MyLoop() { - // To send datas to the server, use the send function - comm.send("LED;R;1"); - - // It's possible to send with more then on line - comm.start(); // Open the buffer - comm.add("LED"); // Write String - comm.add(';'); // Add char - // comm.add(testInt); // Add from variable - comm.send(); // Send concatened variable + read_moves(); + send_server(); + display_percent(); } - ///////////////////////////////////////////////////////////////////////////////////////////// // Communication // ///////////////////////////////////////////////////////////////////////////////////////////// @@ -87,6 +160,12 @@ void Received() Serial.println("Value of parameter 1 :"); Serial.println(comm.GetParameter(1)); } + + // get oxygen percent + if (comm.GetCode() == "OXP") + { + percent = comm.GetParameter(1).toInt(); + } } /// @brief When a message is send without server, the message will be received here. You can close the loop to test the module