97 lines
2.7 KiB
Plaintext
97 lines
2.7 KiB
Plaintext
#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;
|
|
}
|