From d21162e8358fe38b870941911ac62494f577a782 Mon Sep 17 00:00:00 2001 From: Gaetan Bruyant Date: Fri, 2 May 2025 14:13:31 +0200 Subject: [PATCH] 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; +}