new code main3 working well, with simulation of buzzer, vibreur and bluetooth sending message

This commit is contained in:
Gaetan Bruyant 2025-05-02 14:13:31 +02:00
parent 3e0937c6d0
commit d21162e835
5 changed files with 199 additions and 6 deletions

8
.vscode/launch.json vendored
View File

@ -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"
}
]
}

View File

@ -45,6 +45,7 @@
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"main3": "cpp"
}
}

View File

@ -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]";

96
main3 Normal file
View File

@ -0,0 +1,96 @@
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib> // rand()
#include <ctime> // time()
#include <unistd.h> // 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<unsigned int>(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<std::chrono::milliseconds>(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;
}

96
main3.cpp Normal file
View File

@ -0,0 +1,96 @@
#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib> // rand()
#include <ctime> // time()
#include <unistd.h> // 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<unsigned int>(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<std::chrono::milliseconds>(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;
}