IP_HeartBand/main5.cpp

142 lines
4.5 KiB
C++

#include <iostream>
#include <vector>
#include <chrono>
#include <thread>
#include <cstdlib> // rand()
#include <ctime> // time()
#include <unistd.h> // 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<unsigned int>(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<std::chrono::milliseconds>(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<std::chrono::milliseconds>(maintenant - dernierBattement).count();
if (tempsDepuisDernierBattement > 1000) {
if (!enArret) {
std::cout << "Aucune activite cardiaque detectee." << std::endl;
enArret = true;
}
auto tempsDepuisDernierMessage = std::chrono::duration_cast<std::chrono::milliseconds>(maintenant - dernierMessageArret).count();
if (tempsDepuisDernierMessage >= 1000) {
envoyerMessageBluetooth(true); // Emergency
dernierMessageArret = maintenant;
}
}
usleep(SAMPLE_INTERVAL_MS * 1000);
}
return 0;
}