change the sleep method because not working in main3, better in main4
This commit is contained in:
parent
d21162e835
commit
ba1e357124
|
|
@ -0,0 +1,96 @@
|
|||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <cstdlib> // rand()
|
||||
#include <ctime> // time()
|
||||
#include <windows.h>
|
||||
|
||||
// === 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 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<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;
|
||||
}
|
||||
|
||||
Sleep(SAMPLE_INTERVAL_MS); // expects milliseconds
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in New Issue