From 33c9789fb2cdafb267986baba931aad15cd7c930 Mon Sep 17 00:00:00 2001 From: Gaetan Bruyant Date: Wed, 23 Apr 2025 13:48:24 +0200 Subject: [PATCH 01/11] correction des bugs et debugging fonctionne --- .vscode/launch.json | 52 +++++++++++++++++++++---------------------- .vscode/settings.json | 49 +++++++++++++++++++++++++++++++++++++++- .vscode/tasks.json | 28 +++++++++++++++++++++++ main.cpp | 18 ++++++++++++--- 4 files changed, 116 insertions(+), 31 deletions(-) create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 01d967f..781cecf 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,28 +1,26 @@ - +{ + "version": "0.2.0", + "configurations": [ { - "version": "0.2.0", - "configurations": [ - { - "name": "Debug C++", - "type": "cppdbg", - "request": "launch", - "program": "${workspaceFolder}/main", - "args": [], - "stopAtEntry": false, - "cwd": "${workspaceFolder}", - "environment": [], - "externalConsole": true, - "MIMode": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ], - "preLaunchTask": "build", - "miDebuggerPath": "/usr/bin/gdb" // ou chemin vers gdb sous Windows - } - ] - } - + "name": "Debug C++", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/main", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": true, + "MIMode": "gdb", + "miDebuggerPath": "C:/MinGW/bin/gdb.exe", // <- adjust this path to your Windows gdb + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "C/C++: g++.exe build active file" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json index 53b0dab..19e7db3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,50 @@ { - "C_Cpp.default.compilerPath": "C:/MinGW/bin/g++.exe" + "C_Cpp.default.compilerPath": "C:/MinGW/bin/g++.exe", + "files.associations": { + "array": "cpp", + "atomic": "cpp", + "*.tcc": "cpp", + "cctype": "cpp", + "chrono": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "random": "cpp", + "ratio": "cpp", + "string": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "fstream": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "thread": "cpp", + "typeinfo": "cpp" + } } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..82ad581 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++.exe build active file", + "command": "C:/MinGW/bin/g++.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "C:/MinGW/bin" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "compiler: C:/MinGW/bin/g++.exe" + } + ] +} \ No newline at end of file diff --git a/main.cpp b/main.cpp index b7d1300..e6a34ae 100644 --- a/main.cpp +++ b/main.cpp @@ -2,16 +2,27 @@ #include #include #include +#include +#include +#include // Seuils de BPM const int BPM_MIN = 50; const int BPM_MAX = 120; +int fakeBPM = 60; +const int secondsBetweenMeasures = 10; +const int bufferSize = 60480; // une semaine à 1 mesure/10s +int historique[bufferSize]; +int index = 0; // === Simule la récupération de données du capteur === int acquerirDonnees() { // Ici tu brancherais ton ADC ou capteur réel // Simulation d'une fréquence cardiaque variable - static int fakeBPM = 70 + rand() % 60 - 30; + fakeBPM = fakeBPM - 5 + rand() % 11 ; + std::cout << "Le random BPM est "< bufferBPM; const int tailleBuffer = 5; + std::srand(std::time(0)); while (true) { int mesure = acquerirDonnees(); @@ -68,7 +80,7 @@ int main() { alerter(bpm); } - std::this_thread::sleep_for(std::chrono::seconds(1)); // simulation de délai entre mesures + sleep(1); // simulation de délai entre mesures } return 0; From 3e0937c6d0eb5b3690614d4defa734afe271cd34 Mon Sep 17 00:00:00 2001 From: Gaetan Bruyant Date: Tue, 29 Apr 2025 10:58:37 +0200 Subject: [PATCH 02/11] nouveau fichier main2 qui simule le cardiac sensor avec des centaines de mesures par seconde --- .vscode/launch.json | 8 ++--- main.cpp | 14 +++++++- main2.cpp | 80 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 main2.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index 781cecf..9be484f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -2,17 +2,16 @@ "version": "0.2.0", "configurations": [ { - "name": "Debug C++", + "name": "Debug main2.cpp", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/main", + "program": "${workspaceFolder}/main2.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", - "miDebuggerPath": "C:/MinGW/bin/gdb.exe", // <- adjust this path to your Windows gdb "setupCommands": [ { "description": "Enable pretty-printing for gdb", @@ -20,7 +19,8 @@ "ignoreFailures": true } ], - "preLaunchTask": "C/C++: g++.exe build active file" + "preLaunchTask": "build-main2", + "miDebuggerPath": "C:/MinGW/bin/gdb.exe" } ] } diff --git a/main.cpp b/main.cpp index e6a34ae..d5a1c83 100644 --- a/main.cpp +++ b/main.cpp @@ -15,6 +15,9 @@ const int bufferSize = 60480; // une semaine à 1 mesure/10s int historique[bufferSize]; int index = 0; +const int SAMPLE_RATE_MS = 10; // 100 Hz -> une mesure chaque 10 ms +const int THRESHOLD = 500; // seuil pour détecter un battement (à ajuster selon notre capteur) + // === Simule la récupération de données du capteur === int acquerirDonnees() { // Ici tu brancherais ton ADC ou capteur réel @@ -26,6 +29,15 @@ int acquerirDonnees() { return fakeBPM; } +// Vrai fonction qui marcherait avec le capteur +int acquerirSignal() { + // Simulation d'un signal : bruit + pics + static int t = 0; + t++; + if (t % 100 == 0) return 700; // simulate un pic toutes les 1 seconde + return 300 + rand() % 100; // bruit autour de 300-400 +} + // === Calcule le BPM à partir d'une série de battements === int calculerBPM(const std::vector& battements) { // Moyenne simple des valeurs simulées @@ -80,7 +92,7 @@ int main() { alerter(bpm); } - sleep(1); // simulation de délai entre mesures + usleep(2); // simulation de délai entre mesures: 500 mesures par seconde } return 0; diff --git a/main2.cpp b/main2.cpp new file mode 100644 index 0000000..b0137fb --- /dev/null +++ b/main2.cpp @@ -0,0 +1,80 @@ +#include +#include +#include +#include +#include // Pour rand() +#include // Pour srand() +#include // pour usleep() + +// === Paramètres === +const int BPM_MIN = 50; +const int BPM_MAX = 120; +const int SAMPLE_INTERVAL_MS = 10; // Prendre une mesure toutes les 10 ms +const int THRESHOLD = 550; // Seuil pour détecter un battement +const int NOISE_LEVEL = 100; // Niveau de bruit du signal + +// === Initialiser le générateur de nombres aléatoires === +void initialiserRandom() { + srand(static_cast(time(0))); +} + +// === Simule les données reçues du capteur cardiaque === +int simulerSignalCardiaque() { + static int t = 0; + t++; + + // Créer un pic toutes les 800 ms environ (~75 BPM) + if (t % 80 == 0) { + return 700 + rand() % 50; // Pic entre 700-750 + } + else { + return 300 + rand() % NOISE_LEVEL; // Bruit entre 300 et 400 + } +} + +// === Fonction principale === +int main() { + initialiserRandom(); + + bool battementDetecte = false; + auto dernierBattement = std::chrono::steady_clock::now(); + + std::cout << "Démarrage de la simulation du capteur cardiaque...\n"; + + while (true) { + int signal = simulerSignalCardiaque(); + + // Afficher la valeur brute du signal simulé (optionnel pour debug) + // std::cout << "Signal: " << signal << std::endl; + + // Détection de battement + if (signal > THRESHOLD && !battementDetecte) { + auto maintenant = std::chrono::steady_clock::now(); + auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + if (intervalle > 300) { // Filtrer les battements trop proches (<300 ms = >200 BPM) + int bpm = 60000 / intervalle; + std::cout << " BPM mesuré : " << bpm; + + if (bpm < BPM_MIN || bpm > BPM_MAX) { + std::cout << " [Anomalie]"; + } + std::cout << std::endl; + + dernierBattement = maintenant; + } + + battementDetecte = true; + } + + // Prêt pour détecter un prochain battement + if (signal < THRESHOLD) { + battementDetecte = false; + } + + // Pause entre deux lectures du capteur + usleep(SAMPLE_INTERVAL_MS); + } + + return 0; +} From d21162e8358fe38b870941911ac62494f577a782 Mon Sep 17 00:00:00 2001 From: Gaetan Bruyant Date: Fri, 2 May 2025 14:13:31 +0200 Subject: [PATCH 03/11] new code main3 working well, with simulation of buzzer, vibreur and bluetooth sending message --- .vscode/launch.json | 8 ++-- .vscode/settings.json | 3 +- main2.cpp | 2 +- main3 | 96 +++++++++++++++++++++++++++++++++++++++++++ main3.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 199 insertions(+), 6 deletions(-) create mode 100644 main3 create mode 100644 main3.cpp diff --git a/.vscode/launch.json b/.vscode/launch.json index 9be484f..f351788 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -2,16 +2,17 @@ "version": "0.2.0", "configurations": [ { - "name": "Debug main2.cpp", + "name": "Debug C++", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/main2.exe", + "program": "${workspaceFolder}/main3.cpp", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", + "miDebuggerPath": "C:/MinGW/bin/gdb.exe", // <- adjust this path to your Windows gdb "setupCommands": [ { "description": "Enable pretty-printing for gdb", @@ -19,8 +20,7 @@ "ignoreFailures": true } ], - "preLaunchTask": "build-main2", - "miDebuggerPath": "C:/MinGW/bin/gdb.exe" + "preLaunchTask": "C/C++: g++.exe build active file" } ] } diff --git a/.vscode/settings.json b/.vscode/settings.json index 19e7db3..5bd0edc 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -45,6 +45,7 @@ "stdexcept": "cpp", "streambuf": "cpp", "thread": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "main3": "cpp" } } \ No newline at end of file diff --git a/main2.cpp b/main2.cpp index b0137fb..a701e6d 100644 --- a/main2.cpp +++ b/main2.cpp @@ -54,7 +54,7 @@ int main() { if (intervalle > 300) { // Filtrer les battements trop proches (<300 ms = >200 BPM) int bpm = 60000 / intervalle; - std::cout << " BPM mesuré : " << bpm; + std::cout << " BPM mesure : " << bpm; if (bpm < BPM_MIN || bpm > BPM_MAX) { std::cout << " [Anomalie]"; diff --git a/main3 b/main3 new file mode 100644 index 0000000..25cea36 --- /dev/null +++ b/main3 @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include // rand() +#include // time() +#include // pour usleep() + +// === Parameters === +const int BPM_MIN = 50; +const int BPM_MAX = 120; +const int SAMPLE_INTERVAL_MS = 100; // Sampling every 10 ms +const int THRESHOLD = 550; // Threshold for heartbeat detection +const int NOISE_LEVEL = 100; // Simulated noise level + +// === Initialize random generator === +void initialiserRandom() { + srand(static_cast(time(0))); +} + +// === Simulate heart signal from sensor === +int simulerSignalCardiaque() { + static int t = 0; + t++; + + // Simulate a peak every ~800 ms (≈75 BPM) + if (t % 80 == 0) { + return 700 + rand() % 50; // Simulated peak + } else { + return 300 + rand() % NOISE_LEVEL; // Background noise + } +} + +// === Simulate buzzer activation === +void activerBuzzer() { + std::cout << "🔊 Buzzer activé (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate vibration motor activation === +void activerVibreur() { + std::cout << " Vibreur activé (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate Bluetooth transmission === +void envoyerMessageBluetooth(bool urgence) { + std::cout << " Message Bluetooth envoyé: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; +} + +// === Main program === +int main() { + initialiserRandom(); + + bool battementDetecte = false; + auto dernierBattement = std::chrono::steady_clock::now(); + + std::cout << " Démarrage de la simulation du capteur cardiaque...\n"; + + while (true) { + int signal = simulerSignalCardiaque(); + + // Détection de battement + if (signal > THRESHOLD && !battementDetecte) { + auto maintenant = std::chrono::steady_clock::now(); + auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + if (intervalle > 300) { // BPM > 200 => ignore + int bpm = 60000 / intervalle; + std::cout << " BPM mesuré : " << bpm; + + bool anomalie = (bpm < BPM_MIN || bpm > BPM_MAX); + if (anomalie) { + std::cout << " [ANOMALIE]"; + activerBuzzer(); + activerVibreur(); + envoyerMessageBluetooth(true); + } else { + envoyerMessageBluetooth(false); + } + std::cout << std::endl; + + dernierBattement = maintenant; + } + + battementDetecte = true; + } + + // Prêt pour détecter le prochain battement + if (signal < THRESHOLD) { + battementDetecte = false; + } + + usleep(SAMPLE_INTERVAL_MS); + } + + return 0; +} diff --git a/main3.cpp b/main3.cpp new file mode 100644 index 0000000..31042dd --- /dev/null +++ b/main3.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include // rand() +#include // time() +#include // pour usleep() + +// === Parameters === +const int BPM_MIN = 50; +const int BPM_MAX = 120; +const int SAMPLE_INTERVAL_MS = 100; // Sampling every 10 ms +const int THRESHOLD = 550; // Threshold for heartbeat detection +const int NOISE_LEVEL = 100; // Simulated noise level + +// === Initialize random generator === +void initialiserRandom() { + srand(static_cast(time(0))); +} + +// === Simulate heart signal from sensor === +int simulerSignalCardiaque() { + static int t = 0; + t++; + + // Simulate a peak every ~800 ms (≈75 BPM) + if (t % 80 == 0) { + return 700 + rand() % 50; // Simulated peak + } else { + return 300 + rand() % NOISE_LEVEL; // Background noise + } +} + +// === Simulate buzzer activation === +void activerBuzzer() { + std::cout << " Buzzer active (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate vibration motor activation === +void activerVibreur() { + std::cout << " Vibreur active (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate Bluetooth transmission === +void envoyerMessageBluetooth(bool urgence) { + std::cout << " Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; +} + +// === Main program === +int main() { + initialiserRandom(); + + bool battementDetecte = false; + auto dernierBattement = std::chrono::steady_clock::now(); + + std::cout << " Demarrage de la simulation du capteur cardiaque...\n"; + + while (true) { + int signal = simulerSignalCardiaque(); + + // Détection de battement + if (signal > THRESHOLD && !battementDetecte) { + auto maintenant = std::chrono::steady_clock::now(); + auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + if (intervalle > 300) { // BPM > 200 => ignore + int bpm = 60000 / intervalle; + std::cout << " BPM mesure : " << bpm; + + bool anomalie = (bpm < BPM_MIN || bpm > BPM_MAX); + if (anomalie) { + std::cout << " [ANOMALIE]"; + activerBuzzer(); + activerVibreur(); + envoyerMessageBluetooth(true); + } else { + envoyerMessageBluetooth(false); + } + std::cout << std::endl; + + dernierBattement = maintenant; + } + + battementDetecte = true; + } + + // Prêt pour détecter le prochain battement + if (signal < THRESHOLD) { + battementDetecte = false; + } + + usleep(SAMPLE_INTERVAL_MS); + } + + return 0; +} From ba1e357124f9cd5bb7201f045529e5b360406e63 Mon Sep 17 00:00:00 2001 From: laure Date: Sat, 3 May 2025 11:24:35 +0200 Subject: [PATCH 04/11] change the sleep method because not working in main3, better in main4 --- main4.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.cpp | 9 ++++++ 2 files changed, 105 insertions(+) create mode 100644 main4.cpp create mode 100644 test.cpp diff --git a/main4.cpp b/main4.cpp new file mode 100644 index 0000000..558b2f3 --- /dev/null +++ b/main4.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include // rand() +#include // time() +#include + +// === Parameters === +const int BPM_MIN = 50; +const int BPM_MAX = 120; +const int SAMPLE_INTERVAL_MS = 100; // Sampling every 10 ms +const int THRESHOLD = 550; // Threshold for heartbeat detection +const int NOISE_LEVEL = 100; // Simulated noise level + +// === Initialize random generator === +void initialiserRandom() { + srand(static_cast(time(0))); +} + +// === Simulate heart signal from sensor === +int simulerSignalCardiaque() { + static int t = 0; + t++; + + // Simulate a peak every ~800 ms (≈75 BPM) + if (t % 80 == 0) { + return 700 + rand() % 50; // Simulated peak + } else if (t % 10==0){ + return 300 + rand() % NOISE_LEVEL; // Background noise + } +} + +// === Simulate buzzer activation === +void activerBuzzer() { + std::cout << " Buzzer active (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate vibration motor activation === +void activerVibreur() { + std::cout << " Vibreur active (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate Bluetooth transmission === +void envoyerMessageBluetooth(bool urgence) { + std::cout << " Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; +} + +// === Main program === +int main() { + initialiserRandom(); + + bool battementDetecte = false; + auto dernierBattement = std::chrono::steady_clock::now(); + + std::cout << " Demarrage de la simulation du capteur cardiaque...\n"; + + while (true) { + int signal = simulerSignalCardiaque(); + //std::cout << "Signal: " << signal << std::endl; // verify the signal detected + + // Détection de battement + if (signal > THRESHOLD && !battementDetecte) { + auto maintenant = std::chrono::steady_clock::now(); + auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + if (intervalle > 300) { // BPM > 200 => ignore + int bpm = 60000 / intervalle; + std::cout << " BPM mesure : " << bpm; + + bool anomalie = (bpm < BPM_MIN || bpm > BPM_MAX); + if (anomalie) { + std::cout << " [ANOMALIE]"; + activerBuzzer(); + activerVibreur(); + envoyerMessageBluetooth(true); + } else { + envoyerMessageBluetooth(false); + } + std::cout << std::endl; + + dernierBattement = maintenant; + } + + battementDetecte = true; + } + + // Prêt pour détecter le prochain battement + if (signal < THRESHOLD) { + battementDetecte = false; + } + + Sleep(SAMPLE_INTERVAL_MS); // expects milliseconds + } + + return 0; +} diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..da9602f --- /dev/null +++ b/test.cpp @@ -0,0 +1,9 @@ +#include +#include +#include + +int main() { + std::cout << "Sleeping..." << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(1)); + std::cout << "Awake!" << std::endl; +} From 3233ed92454f38362599e3dd0e901fa069d1ca4b Mon Sep 17 00:00:00 2001 From: laure Date: Sun, 11 May 2025 15:04:05 +0200 Subject: [PATCH 05/11] new random to generate the bmp, only in main4 --- main4.cpp | 81 +++++++++++++++++++++++++++---------------------------- test.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 106 insertions(+), 46 deletions(-) diff --git a/main4.cpp b/main4.cpp index 558b2f3..e322fd8 100644 --- a/main4.cpp +++ b/main4.cpp @@ -6,28 +6,18 @@ #include // === Parameters === -const int BPM_MIN = 50; -const int BPM_MAX = 120; -const int SAMPLE_INTERVAL_MS = 100; // Sampling every 10 ms -const int THRESHOLD = 550; // Threshold for heartbeat detection -const int NOISE_LEVEL = 100; // Simulated noise level +//const int BPM_MIN = 50; +//const int BPM_MAX = 120; +const int SAMPLE_INTERVAL_MS = 1000; // Sampling every 10 ms +const int THRESHOLD_CARDIAC_ARREST = 2; +//const int NOISE_LEVEL = 100; // Simulated noise level -// === Initialize random generator === -void initialiserRandom() { - srand(static_cast(time(0))); -} -// === Simulate heart signal from sensor === -int simulerSignalCardiaque() { - static int t = 0; - t++; - - // Simulate a peak every ~800 ms (≈75 BPM) - if (t % 80 == 0) { - return 700 + rand() % 50; // Simulated peak - } else if (t % 10==0){ - return 300 + rand() % NOISE_LEVEL; // Background noise - } +// === Initialize random generator ===// === Simulate heart signal from sensor === +int generateRandomHeartRate() { + //int bpm = 30 + rand() % 111; // génère un BPM entre 30 et 140 + int bpm = rand() % 141; // génère un BPM entre 0 et 140 + return bpm; } // === Simulate buzzer activation === @@ -47,46 +37,55 @@ void envoyerMessageBluetooth(bool urgence) { // === Main program === int main() { - initialiserRandom(); bool battementDetecte = false; auto dernierBattement = std::chrono::steady_clock::now(); std::cout << " Demarrage de la simulation du capteur cardiaque...\n"; + int i=0; + int lastBPM; while (true) { - int signal = simulerSignalCardiaque(); + //int signal = simulerSignalCardiaque(); //std::cout << "Signal: " << signal << std::endl; // verify the signal detected // Détection de battement - if (signal > THRESHOLD && !battementDetecte) { + if (!battementDetecte) { //signal > THRESHOLD && auto maintenant = std::chrono::steady_clock::now(); auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + int bpm = generateRandomHeartRate(); + std::cout << " BPM mesure : " << bpm; - if (intervalle > 300) { // BPM > 200 => ignore - int bpm = 60000 / intervalle; - std::cout << " BPM mesure : " << bpm; - - bool anomalie = (bpm < BPM_MIN || bpm > BPM_MAX); - if (anomalie) { - std::cout << " [ANOMALIE]"; + if (bpm < 55 && bpm > 40){ + std::cout << " [BRADYCARDIA]" << std::endl; + activerVibreur(); + } else if(bpm < 140 && bpm > 110) { + std::cout << " [TACHYCARDIA]" << std::endl; + activerVibreur(); + }else if(bpm < 110 && bpm > 55){ + std::cout << " [NORMAL]" << std::endl; + envoyerMessageBluetooth(false); + } else if (bpm < 40){ + if (i>THRESHOLD_CARDIAC_ARREST && lastBPM < 40){ + i=0; + std::cout << " [CARDIAC ARREST]" << std::endl; activerBuzzer(); activerVibreur(); envoyerMessageBluetooth(true); - } else { - envoyerMessageBluetooth(false); - } - std::cout << std::endl; - - dernierBattement = maintenant; - } + }else{ + i+=1; + } + } + std::cout << std::endl; + dernierBattement = maintenant; battementDetecte = true; - } + lastBPM =bpm; + std::cout << lastBPM << std::endl; - // Prêt pour détecter le prochain battement - if (signal < THRESHOLD) { - battementDetecte = false; + }else { + battementDetecte = false;// Prêt pour détecter le prochain battement } Sleep(SAMPLE_INTERVAL_MS); // expects milliseconds diff --git a/test.cpp b/test.cpp index da9602f..b6eb9c5 100644 --- a/test.cpp +++ b/test.cpp @@ -1,9 +1,70 @@ #include -#include +#include #include +#include -int main() { - std::cout << "Sleeping..." << std::endl; - std::this_thread::sleep_for(std::chrono::seconds(1)); - std::cout << "Awake!" << std::endl; +// État du rythme cardiaque +enum class HeartRateState { + NORMAL, + BRADYCARDIA, + TACHYCARDIA +}; + +// Fonction pour générer un signal de battement cardiaque +std::vector generateHeartbeatSignal(HeartRateState state, int durationSeconds = 10, int sampleRate = 1000) { + int bpm; + switch (state) { + case HeartRateState::BRADYCARDIA: + bpm = 40 + rand() % 15; // 40-55 BPM + break; + case HeartRateState::TACHYCARDIA: + bpm = 110 + rand() % 30; // 110-140 BPM + break; + default: + bpm = 60 + rand() % 40; // 60-100 BPM + break; + } + + int totalSamples = durationSeconds * sampleRate; + int samplesPerBeat = (60.0 / bpm) * sampleRate; + + std::vector signal(totalSamples, 0.0f); + + for (int i = 0; i < totalSamples; i += samplesPerBeat) { + if (i < totalSamples) signal[i] = 1.0f; // Pic R + if (i + 1 < totalSamples) signal[i + 1] = 0.5f; + if (i + 2 < totalSamples) signal[i + 2] = 0.2f; + } + + return signal; } + +// Pause active sans utiliser thread +void waitMilliseconds(int ms) { + auto start = std::chrono::high_resolution_clock::now(); + while (std::chrono::duration_cast( + std::chrono::high_resolution_clock::now() - start).count() < ms) { + // boucle vide + } +} + +// Affichage du signal +void printSignal(const std::vector& signal, int sampleRate) { + for (size_t i = 0; i < signal.size(); ++i) { + std::cout << signal[i] << "\n"; + waitMilliseconds(1000 / sampleRate); + } +} + +// ----------- FONCTION PRINCIPALE ------------ +int main() { + std::cout << "Simulation d’un signal de battement cardiaque\n"; + + // Tu peux changer ici : NORMAL / BRADYCARDIA / TACHYCARDIA + HeartRateState state = HeartRateState::TACHYCARDIA; + + std::vector signal = generateHeartbeatSignal(state, 5, 100); // 5 secondes, 100 Hz + printSignal(signal, 100); + + return 0; +} \ No newline at end of file From c56a1b66a63945cf43d99f56d4134232637dea11 Mon Sep 17 00:00:00 2001 From: Gaetan Bruyant Date: Sun, 11 May 2025 17:29:31 +0200 Subject: [PATCH 06/11] =?UTF-8?q?pas=20touch=C3=A9=20au=20main4,=20je=20mo?= =?UTF-8?q?difie=20le=20main8=20pour=20am=C3=A9liorer=20le=20main4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 4 +- main5.cpp | 141 ++++++++++++++++++++++++++++++++++++++++++ main6.cpp | 103 ++++++++++++++++++++++++++++++ main7.cpp | 103 ++++++++++++++++++++++++++++++ main8.cpp | 116 ++++++++++++++++++++++++++++++++++ 5 files changed, 466 insertions(+), 1 deletion(-) create mode 100644 main5.cpp create mode 100644 main6.cpp create mode 100644 main7.cpp create mode 100644 main8.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 5bd0edc..ca0a096 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -46,6 +46,8 @@ "streambuf": "cpp", "thread": "cpp", "typeinfo": "cpp", - "main3": "cpp" + "main3": "cpp", + "cstring": "cpp", + "iomanip": "cpp" } } \ No newline at end of file diff --git a/main5.cpp b/main5.cpp new file mode 100644 index 0000000..61655ef --- /dev/null +++ b/main5.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include // rand() +#include // time() +#include // usleep() + +// === Parameters === +const int BPM_MIN = 50; +const int BPM_MAX = 120; +const int SAMPLE_INTERVAL_MS = 20; // Sampling every 100 ms +const int THRESHOLD = 550; // Threshold for heartbeat detection +const int NOISE_LEVEL = 100; // Simulated noise level + +// === Initialize random generator === +void initialiserRandom() { + srand(static_cast(time(0))); +} + +// === Simulate buzzer activation === +void activerBuzzer() { + std::cout << "Buzzer active (ALERTE CARDIAQUE)" << std::endl; +} + +// === Simulate vibration motor activation === +void activerVibreur() { + std::cout << "Vibreur active (ALERTE CARDIAQUE)" << std::endl; +} + +// === Simulate Bluetooth transmission === +void envoyerMessageBluetooth(bool urgence) { + std::cout << "Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; +} + +// === Simulate heart signal from sensor with variable BPM and 0-BPM possibility === +int simulerSignalCardiaque() { + static int t = 0; + static int nextPeakInterval = 0; + + static int targetBPM = 75; + static int cyclesPerPeak = 800 / SAMPLE_INTERVAL_MS; + static bool enArretCardiaque = false; + + t++; + + if (t == 1 || t % nextPeakInterval == 0) { + if (rand() % 100 < 5) { + targetBPM = 0; + enArretCardiaque = true; + std::cout << "[Simulating cardiac arrest: BPM = 0]" << std::endl; + } else { + int variation = (rand() % 21) - 10; + targetBPM += variation; + if (targetBPM < 30) targetBPM = 30; + if (targetBPM > 210) targetBPM = 210; + enArretCardiaque = false; + std::cout << "[Target BPM = " << targetBPM << "]" << std::endl; + } + + if (targetBPM == 0) { + nextPeakInterval = 10; // Keep short interval to simulate repeated 0 BPM checks + } else { + int interval_ms = 60000 / targetBPM; + cyclesPerPeak = interval_ms / SAMPLE_INTERVAL_MS; + nextPeakInterval = cyclesPerPeak; + } + + return (targetBPM == 0) ? 300 + : 700 + rand() % 50; + } else { + return 300; + } +} + +// === Main program === +int main() { + initialiserRandom(); + + bool battementDetecte = false; + auto dernierBattement = std::chrono::steady_clock::now(); + auto dernierMessageArret = std::chrono::steady_clock::now(); + bool enArret = false; + + std::cout << "Demarrage de la simulation du capteur cardiaque..." << std::endl; + + while (true) { + int signal = simulerSignalCardiaque(); + auto maintenant = std::chrono::steady_clock::now(); + + // Heartbeat detected + if (signal > THRESHOLD && !battementDetecte) { + auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + if (intervalle > 300) { + int bpm = 60000 / intervalle; + std::cout << "BPM mesure : " << bpm; + + bool anomalie = (bpm < BPM_MIN || bpm > BPM_MAX); + if (anomalie) { + std::cout << " [ANOMALIE]"; + activerBuzzer(); + activerVibreur(); + envoyerMessageBluetooth(true); + } else { + envoyerMessageBluetooth(false); + } + + enArret = false; + std::cout << std::endl; + dernierBattement = maintenant; + } + + battementDetecte = true; + } + + // Ready for next detection + if (signal < THRESHOLD) { + battementDetecte = false; + } + + // Handle 0 BPM condition (no heartbeat detected for > 1 sec) + auto tempsDepuisDernierBattement = std::chrono::duration_cast(maintenant - dernierBattement).count(); + if (tempsDepuisDernierBattement > 1000) { + if (!enArret) { + std::cout << "Aucune activite cardiaque detectee." << std::endl; + enArret = true; + } + + auto tempsDepuisDernierMessage = std::chrono::duration_cast(maintenant - dernierMessageArret).count(); + if (tempsDepuisDernierMessage >= 1000) { + envoyerMessageBluetooth(true); // Emergency + dernierMessageArret = maintenant; + } + } + + usleep(SAMPLE_INTERVAL_MS * 1000); + } + + return 0; +} diff --git a/main6.cpp b/main6.cpp new file mode 100644 index 0000000..615f5a9 --- /dev/null +++ b/main6.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include + +const int SAMPLE_RATE_MS = 20; +const int SAMPLES_PER_SECOND = 1000 / SAMPLE_RATE_MS; +const int TOTAL_SECONDS = 60; + +std::default_random_engine rng(std::random_device{}()); +std::uniform_real_distribution changeDist(-0.25, 0.25); +std::uniform_real_distribution driftDist(-0.05, 0.05); +std::uniform_real_distribution heartAttackChance(0.0, 1.0); +std::uniform_int_distribution startBpmDist(30, 210); + +double clampBPM(double bpm) { + if (bpm == 0.0) return 0.0; + if (bpm < 30.0) return 30.0; + if (bpm > 210.0) return 210.0; + return bpm; +} + +int main() { + std::vector bpmHistory; + double bpm = startBpmDist(rng); + double drift = driftDist(rng); + int outOfRangeSeconds = 0; + bool emergency = false; + bool emergencyNotified = false; + bool cardiacArrestInjected = false; + + std::vector currentSecondSamples; + int secondCounter = 0; + + std::cout << "Starting real-time heart rate monitoring...\n" << std::endl; + + while (secondCounter < TOTAL_SECONDS) { + double sum = 0.0; + + for (int i = 0; i < SAMPLES_PER_SECOND; ++i) { + // Inject cardiac arrest + if (!cardiacArrestInjected && secondCounter >= 20 && secondCounter < 40 && heartAttackChance(rng) < 0.005) { + bpm = 0.0; + cardiacArrestInjected = true; + } else if (bpm != 0.0) { + bpm += changeDist(rng) + drift; + bpm = clampBPM(bpm); + } + + currentSecondSamples.push_back(bpm); + std::this_thread::sleep_for(std::chrono::milliseconds(SAMPLE_RATE_MS)); + } + + drift = driftDist(rng); + + // Calculate 1-second average + double sumSecond = 0.0; + for (double val : currentSecondSamples) sumSecond += val; + int avgBPM = static_cast(std::round(sumSecond / currentSecondSamples.size())); + bpmHistory.push_back(avgBPM); + currentSecondSamples.clear(); + + // Print real-time 1-second BPM + std::cout << "Second " << secondCounter + 1 << " - Calculated Avg BPM: " << avgBPM << std::endl; + + // Emergency check + if (avgBPM < 40 || avgBPM > 200) { + outOfRangeSeconds++; + } else { + outOfRangeSeconds = 0; + } + + if (!emergency && outOfRangeSeconds >= 3) { + emergency = true; + emergencyNotified = false; + } + + if (emergency) { + if (!emergencyNotified) { + std::cout << "[buzzer active]" << std::endl; + std::cout << "[vibrator active]" << std::endl; + emergencyNotified = true; + } + std::cout << "[Bluetooth message sent: urgency = TRUE]" << std::endl; + } else if ((secondCounter + 1) % 10 == 0) { + int count = std::min(60, static_cast(bpmHistory.size())); + int sumLast = 0; + for (int i = bpmHistory.size() - count; i < bpmHistory.size(); ++i) { + sumLast += bpmHistory[i]; + } + int avgLast = sumLast / count; + std::cout << "[Bluetooth message sent: urgency = FALSE, avg BPM = " << avgLast << "]" << std::endl; + } + + secondCounter++; + } + + std::cout << "\nMonitoring session ended.\n"; + return 0; +} diff --git a/main7.cpp b/main7.cpp new file mode 100644 index 0000000..4bae44b --- /dev/null +++ b/main7.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include +#include +#include + +const int SAMPLE_RATE_MS = 20; +const int SAMPLES_PER_SECOND = 1000 / SAMPLE_RATE_MS; +const int TOTAL_SECONDS = 60; + +std::default_random_engine rng(std::random_device{}()); +std::uniform_real_distribution changeDist(-0.25, 0.25); +std::uniform_real_distribution driftDist(-0.05, 0.05); +std::uniform_real_distribution heartAttackChance(0.0, 1.0); +std::uniform_int_distribution startBpmDist(30, 210); + +double clampBPM(double bpm) { + if (bpm == 0.0) return 0.0; + if (bpm < 30.0) return 30.0; + if (bpm > 210.0) return 210.0; + return bpm; +} + +int main() { + std::vector bpmHistory; + double bpm = startBpmDist(rng); + double drift = driftDist(rng); + int outOfRangeSeconds = 0; + bool emergency = false; + bool emergencyNotified = false; + bool cardiacArrestInjected = false; + + std::vector currentSecondSamples; + int secondCounter = 0; + + std::cout << "Starting real-time heart rate monitoring...\n" << std::endl; + + while (secondCounter < TOTAL_SECONDS) { + double sum = 0.0; + + for (int i = 0; i < SAMPLES_PER_SECOND; ++i) { + // Inject cardiac arrest + if (!cardiacArrestInjected && secondCounter >= 20 && secondCounter < 40 && heartAttackChance(rng) < 0.005) { + bpm = 0.0; + cardiacArrestInjected = true; + } else if (bpm != 0.0) { + bpm += changeDist(rng) + drift; + bpm = clampBPM(bpm); + } + + currentSecondSamples.push_back(bpm); + std::this_thread::sleep_for(std::chrono::milliseconds(SAMPLE_RATE_MS)); + } + + drift = driftDist(rng); + + // Calculate 1-second average + double sumSecond = 0.0; + for (double val : currentSecondSamples) sumSecond += val; + int avgBPM = static_cast(std::round(sumSecond / currentSecondSamples.size())); + bpmHistory.push_back(avgBPM); + currentSecondSamples.clear(); + + // Print real-time 1-second BPM + std::cout << "Second " << secondCounter + 1 << " - Calculated Avg BPM: " << avgBPM << std::endl; + + // Emergency check + if (avgBPM < 40 || avgBPM > 200) { + outOfRangeSeconds++; + } else { + outOfRangeSeconds = 0; + } + + if (!emergency && outOfRangeSeconds >= 3) { + emergency = true; + emergencyNotified = false; + } + + if (emergency) { + if (!emergencyNotified) { + std::cout << "[buzzer active]" << std::endl; + std::cout << "[vibrator active]" << std::endl; + emergencyNotified = true; + } + std::cout << "[Bluetooth message sent: urgency = TRUE]" << std::endl; + } else if ((secondCounter + 1) % 10 == 0) { + int count = std::min(60, static_cast(bpmHistory.size())); + int sumLast = 0; + for (int i = bpmHistory.size() - count; i < bpmHistory.size(); ++i) { + sumLast += bpmHistory[i]; + } + int avgLast = sumLast / count; + std::cout << "[Bluetooth message sent: urgency = FALSE, avg BPM = " << avgLast << "]" << std::endl; + } + + secondCounter++; + } + + std::cout << "\nMonitoring session ended.\n"; + return 0; +} \ No newline at end of file diff --git a/main8.cpp b/main8.cpp new file mode 100644 index 0000000..ae6a192 --- /dev/null +++ b/main8.cpp @@ -0,0 +1,116 @@ +#include +#include +#include +#include // rand() +#include // time() +#include + +// === Parameters === +//const int BPM_MIN = 50; +//const int BPM_MAX = 120; +const int SAMPLE_INTERVAL_MS = 1000; // Sampling every 10 ms +const int THRESHOLD_CARDIAC_ARREST = 2; +//const int NOISE_LEVEL = 100; // Simulated noise level + + +// === Initialize random generator ===// === Simulate heart signal from sensor === +int generateRandomHeartRate() { + //int bpm = 30 + rand() % 111; // génère un BPM entre 30 et 140 + int bpm = rand() % 141; // génère un BPM entre 0 et 140 + return bpm; +} + +// === Simulate buzzer activation === +void activerBuzzer() { + std::cout << " Buzzer active (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate vibration motor activation === +void activerVibreur() { + std::cout << " Vibreur active (ALERTE CARDIAQUE)!" << std::endl; +} + +// === Simulate Bluetooth transmission === +void envoyerMessageBluetooth(bool urgence) { + std::cout << " Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; +} + +// send avg bvpm of the last minute +void sendAvgBPM(int avg){ + std::cout << " Average BPM on the last minute sent by bluetooth : " << avg; +} + +// === Main program === +int main() { + + bool battementDetecte = false; + auto dernierBattement = std::chrono::steady_clock::now(); + + std::cout << " Demarrage de la simulation du capteur cardiaque...\n"; + int i=0; + int lastBPM; + int j = 0; + int time = 0; + int lastTime = 0; + std::vector heartRateHistory; + heartRateHistory.reserve(1728000); //20*24*60*60 secondes de data (20 jours) + int avgBPM = 0; + + while (true) { + //int signal = simulerSignalCardiaque(); + //std::cout << "Signal: " << signal << std::endl; // verify the signal detected + + // Détection de battement + if (!battementDetecte) { //signal > THRESHOLD && + auto maintenant = std::chrono::steady_clock::now(); + auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + int bpm = generateRandomHeartRate(); + std::cout << " BPM mesure : " << bpm; + + if (bpm < 55 && bpm > 40){ + std::cout << " [BRADYCARDIA]" << std::endl; + activerVibreur(); + } else if(bpm < 140 && bpm > 110) { + std::cout << " [TACHYCARDIA]" << std::endl; + activerVibreur(); + }else if(bpm < 110 && bpm > 55){ + std::cout << " [NORMAL]" << std::endl; + envoyerMessageBluetooth(false); + } else if (bpm < 40){ + if (i>THRESHOLD_CARDIAC_ARREST && lastBPM < 40){ + i=0; + std::cout << " [CARDIAC ARREST]" << std::endl; + activerBuzzer(); + activerVibreur(); + envoyerMessageBluetooth(true); + }else{ + i+=1; + } + } + std::cout << std::endl; + + dernierBattement = maintenant; + battementDetecte = true; + heartRateHistory.push_back(bpm); + lastBPM =bpm; + + }else { + battementDetecte = false;// Prêt pour détecter le prochain battement + } + + time++; + + if (time>(lastTime - 60)){ + for (j = lastTime; j Date: Sun, 11 May 2025 19:20:02 +0200 Subject: [PATCH 07/11] main8 fonctionne, c'est une amelioration du main 4 --- main8.cpp | 83 +++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/main8.cpp b/main8.cpp index ae6a192..49ae1bb 100644 --- a/main8.cpp +++ b/main8.cpp @@ -6,102 +6,101 @@ #include // === Parameters === -//const int BPM_MIN = 50; -//const int BPM_MAX = 120; -const int SAMPLE_INTERVAL_MS = 1000; // Sampling every 10 ms -const int THRESHOLD_CARDIAC_ARREST = 2; -//const int NOISE_LEVEL = 100; // Simulated noise level +const int BPM_MIN = 55; // Minimum Normal BPM +const int BPM_MAX = 110; // Maximum Normal BPM +const int BPM_TACHYCARDIA = 40; // Minimum BPM for Tachycardia +const int BPM_BRADYCARDIA = 140; // Maximum BPM for Bradycardia +const int SAMPLE_INTERVAL_MS = 1000; // Sampling every 1s +const int THRESHOLD_CARDIAC_ARREST = 5; // === Initialize random generator ===// === Simulate heart signal from sensor === int generateRandomHeartRate() { - //int bpm = 30 + rand() % 111; // génère un BPM entre 30 et 140 - int bpm = rand() % 141; // génère un BPM entre 0 et 140 + int bpm = rand() % BPM_BRADYCARDIA + 1; // génère un BPM entre 0 et 140 return bpm; } // === Simulate buzzer activation === -void activerBuzzer() { +void activateBuzzer() { std::cout << " Buzzer active (ALERTE CARDIAQUE)!" << std::endl; } // === Simulate vibration motor activation === -void activerVibreur() { +void activateVibration() { std::cout << " Vibreur active (ALERTE CARDIAQUE)!" << std::endl; } // === Simulate Bluetooth transmission === -void envoyerMessageBluetooth(bool urgence) { +void sendMessageBluetooth(bool urgence) { std::cout << " Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; } // send avg bvpm of the last minute void sendAvgBPM(int avg){ - std::cout << " Average BPM on the last minute sent by bluetooth : " << avg; + std::cout << " \n [BLUETOOTH MESSAGE] Average BPM over the last minute : " << avg; + std::cout << "\n\n" < heartRateHistory; - heartRateHistory.reserve(1728000); //20*24*60*60 secondes de data (20 jours) + heartRateHistory.reserve(1728000); //20*24*60*60 secondes de data (20 days) int avgBPM = 0; while (true) { - //int signal = simulerSignalCardiaque(); - //std::cout << "Signal: " << signal << std::endl; // verify the signal detected - // Détection de battement - if (!battementDetecte) { //signal > THRESHOLD && - auto maintenant = std::chrono::steady_clock::now(); - auto intervalle = std::chrono::duration_cast(maintenant - dernierBattement).count(); + + // DHeartrate detection + if (!pulseDetected) { + auto now = std::chrono::steady_clock::now(); + auto intervalle = std::chrono::duration_cast(now - lastPulse).count(); int bpm = generateRandomHeartRate(); - std::cout << " BPM mesure : " << bpm; - - if (bpm < 55 && bpm > 40){ + time++; + std::cout << " \n -------------- \n Time since device turned ON (in seconds) " << time; + std::cout << " \n BPM measured : " << bpm; + if (bpm < BPM_MIN && bpm > BPM_TACHYCARDIA){ std::cout << " [BRADYCARDIA]" << std::endl; - activerVibreur(); - } else if(bpm < 140 && bpm > 110) { + activateVibration(); + } else if(bpm < BPM_BRADYCARDIA && bpm > BPM_MAX) { std::cout << " [TACHYCARDIA]" << std::endl; - activerVibreur(); - }else if(bpm < 110 && bpm > 55){ + activateVibration(); + }else if(bpm < BPM_MAX && bpm > BPM_MIN){ std::cout << " [NORMAL]" << std::endl; - envoyerMessageBluetooth(false); - } else if (bpm < 40){ - if (i>THRESHOLD_CARDIAC_ARREST && lastBPM < 40){ + } else if (bpm < BPM_TACHYCARDIA){ + if (i>=THRESHOLD_CARDIAC_ARREST){ i=0; - std::cout << " [CARDIAC ARREST]" << std::endl; - activerBuzzer(); - activerVibreur(); - envoyerMessageBluetooth(true); + std::cout << " \n !! Number = 5 [HEART ATTACK]" << std::endl; + activateBuzzer(); + activateVibration(); + sendMessageBluetooth(true); }else{ + std::cout << " \n !! Low BPM, emergency alert if number gets to 5: "<(lastTime - 60)){ + if (time>(lastTime + 60)){ for (j = lastTime; j Date: Sun, 11 May 2025 20:39:25 +0200 Subject: [PATCH 08/11] last modification, i hope --- main8.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/main8.cpp b/main8.cpp index 49ae1bb..976e127 100644 --- a/main8.cpp +++ b/main8.cpp @@ -69,16 +69,20 @@ int main() { time++; std::cout << " \n -------------- \n Time since device turned ON (in seconds) " << time; std::cout << " \n BPM measured : " << bpm; + if (bpm < BPM_MIN && bpm > BPM_TACHYCARDIA){ std::cout << " [BRADYCARDIA]" << std::endl; activateVibration(); + } else if(bpm < BPM_BRADYCARDIA && bpm > BPM_MAX) { std::cout << " [TACHYCARDIA]" << std::endl; activateVibration(); + }else if(bpm < BPM_MAX && bpm > BPM_MIN){ std::cout << " [NORMAL]" << std::endl; + } else if (bpm < BPM_TACHYCARDIA){ - if (i>=THRESHOLD_CARDIAC_ARREST){ + if (i>=THRESHOLD_CARDIAC_ARREST && lastBPM < BPM_TACHYCARDIA){ i=0; std::cout << " \n !! Number = 5 [HEART ATTACK]" << std::endl; activateBuzzer(); @@ -89,6 +93,7 @@ int main() { i+=1; } } + std::cout << std::endl; lastPulse = now; From 00b5d65cbb0e50a168cf6a0ff1551709fc43d911 Mon Sep 17 00:00:00 2001 From: laure Date: Sun, 11 May 2025 22:00:22 +0200 Subject: [PATCH 09/11] well it wasn't the last one --- main8.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/main8.cpp b/main8.cpp index 976e127..c2c3066 100644 --- a/main8.cpp +++ b/main8.cpp @@ -11,7 +11,7 @@ const int BPM_MAX = 110; // Maximum Normal BPM const int BPM_TACHYCARDIA = 40; // Minimum BPM for Tachycardia const int BPM_BRADYCARDIA = 140; // Maximum BPM for Bradycardia const int SAMPLE_INTERVAL_MS = 1000; // Sampling every 1s -const int THRESHOLD_CARDIAC_ARREST = 5; +const int THRESHOLD_CARDIAC_ARREST = 2; // === Initialize random generator ===// === Simulate heart signal from sensor === @@ -22,17 +22,17 @@ int generateRandomHeartRate() { // === Simulate buzzer activation === void activateBuzzer() { - std::cout << " Buzzer active (ALERTE CARDIAQUE)!" << std::endl; + std::cout << " Buzzer activated (CARDIAC ALERT)!" << std::endl; } // === Simulate vibration motor activation === void activateVibration() { - std::cout << " Vibreur active (ALERTE CARDIAQUE)!" << std::endl; + std::cout << " Vibration activated (CARDIAC ALERT)!" << std::endl; } // === Simulate Bluetooth transmission === void sendMessageBluetooth(bool urgence) { - std::cout << " Message Bluetooth envoye: " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; + std::cout << " Message Bluetooth send : " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; } // send avg bvpm of the last minute @@ -73,24 +73,27 @@ int main() { if (bpm < BPM_MIN && bpm > BPM_TACHYCARDIA){ std::cout << " [BRADYCARDIA]" << std::endl; activateVibration(); + i=0; } else if(bpm < BPM_BRADYCARDIA && bpm > BPM_MAX) { std::cout << " [TACHYCARDIA]" << std::endl; activateVibration(); + i=0; }else if(bpm < BPM_MAX && bpm > BPM_MIN){ std::cout << " [NORMAL]" << std::endl; + i=0; } else if (bpm < BPM_TACHYCARDIA){ - if (i>=THRESHOLD_CARDIAC_ARREST && lastBPM < BPM_TACHYCARDIA){ + if (i=THRESHOLD_CARDIAC_ARREST && lastBPM < BPM_TACHYCARDIA){ i=0; - std::cout << " \n !! Number = 5 [HEART ATTACK]" << std::endl; + std::cout << " \n !! Number = 2 [HEART ATTACK]" << std::endl; activateBuzzer(); activateVibration(); sendMessageBluetooth(true); }else{ - std::cout << " \n !! Low BPM, emergency alert if number gets to 5: "< Date: Mon, 12 May 2025 00:00:12 +0200 Subject: [PATCH 10/11] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f0886e3..0e97cf8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # IP_HeartBand +To launch the code main8.cpp which is the final code, write this on the terminal: + +g++ main8.cpp -o main8.exe +./main8.exe \ No newline at end of file From 44dff6bfef1b889afd5aa69caf1f6fadd13f9038 Mon Sep 17 00:00:00 2001 From: laure Date: Mon, 12 May 2025 20:41:52 +0200 Subject: [PATCH 11/11] the end --- .vscode/settings.json | 3 ++- main8.cpp | 34 +++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index ca0a096..678b745 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -49,5 +49,6 @@ "main3": "cpp", "cstring": "cpp", "iomanip": "cpp" - } + }, + "C_Cpp.errorSquiggles": "disabled" } \ No newline at end of file diff --git a/main8.cpp b/main8.cpp index c2c3066..aeca474 100644 --- a/main8.cpp +++ b/main8.cpp @@ -21,18 +21,31 @@ int generateRandomHeartRate() { } // === Simulate buzzer activation === -void activateBuzzer() { - std::cout << " Buzzer activated (CARDIAC ALERT)!" << std::endl; +void activateBuzzer(bool activationBuz) { + if (activationBuz) { + std::cout << " Buzzer activated (CARDIAC ALERT)!" << std::endl; + } else { + std::cout << " Buzzer desactivated" << std::endl; + } } + // === Simulate vibration motor activation === -void activateVibration() { - std::cout << " Vibration activated (CARDIAC ALERT)!" << std::endl; +void activateVibration(bool activationVib) { + if (activationVib) { + std::cout << " Vibration activated (CARDIAC ALERT)!" << std::endl; + } else { + std::cout << " Vibration desactivated" << std::endl; + } } // === Simulate Bluetooth transmission === void sendMessageBluetooth(bool urgence) { - std::cout << " Message Bluetooth send : " << (urgence ? "TRUE (urgence)" : "FALSE (normal)") << std::endl; + if (urgence) { + std::cout << " Message Bluetooth send : TRUE (urgence)" << std::endl; + } else { + std::cout << " Message Bluetooth send : FALSE (normal)" << std::endl; + } } // send avg bvpm of the last minute @@ -72,24 +85,27 @@ int main() { if (bpm < BPM_MIN && bpm > BPM_TACHYCARDIA){ std::cout << " [BRADYCARDIA]" << std::endl; - activateVibration(); + activateVibration(true); i=0; } else if(bpm < BPM_BRADYCARDIA && bpm > BPM_MAX) { std::cout << " [TACHYCARDIA]" << std::endl; - activateVibration(); + activateVibration(true); i=0; }else if(bpm < BPM_MAX && bpm > BPM_MIN){ std::cout << " [NORMAL]" << std::endl; + activateBuzzer(false); + activateVibration(false); + sendMessageBluetooth(false); i=0; } else if (bpm < BPM_TACHYCARDIA){ if (i=THRESHOLD_CARDIAC_ARREST && lastBPM < BPM_TACHYCARDIA){ i=0; std::cout << " \n !! Number = 2 [HEART ATTACK]" << std::endl; - activateBuzzer(); - activateVibration(); + activateBuzzer(true); + activateVibration(true); sendMessageBluetooth(true); }else{ i+=1;