1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
| /*
pour un gain de memoire et eviter les doublon , les variable globale son limiter aux objets et pointeurs
les constantes sont donc directement definient dans le code dans la mesure du possible
les numero de Pin sont donc eux aussi directement ecrit dans le code
néamois ceux si sont defini quoi qu'il se passe dans le setup
*/
//permet l'utilisation de string litteral depuis la memoire flash et non depuis la memoire RAM
#define FlashString(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
//librairie de management de pointeur partager
#include <SharedPtr.hpp>
//https://www.arduino.cc/en/reference/wire
//gestion I2C
#include <Wire.h>
// https://github.com/sparkfun/SparkFun_MAX3010x_Sensor_Library/blob/master/examples/Example5_HeartRate/Example5_HeartRate.ino
#include <MAX30105.h>
#include <spo2_algorithm.h>
#include <heartRate.h>
//https://passionelectronique.fr/carte-sd-arduino/
//https://docs.arduino.cc/learn/programming/sd-guide
#include <SPI.h>
#include <SD.h>
//https://www.arduino.cc/reference/en/libraries/ds3231/
//gestion RTC
#include <DS3231.h>
/*
Adafruit_MLX90614
https://github.com/adafruit/Adafruit_BusIO/blob/master/Adafruit_I2CDevice.h
https://github.com/adafruit/Adafruit-MLX90614-Library/blob/master/examples/mlxtest/mlxtest.ino
*/
#include <Adafruit_MLX90614.h>
// definition des constante et predefinie
#define BR_Serial 9600 //definis la vitesse de transmission usb
// utilisation d'un seul octet pour la definition d'un pin via pin_t
typedef byte const pin_t;
//tamplate = type de variable non definit
//class de stockage de seuils
template<typename T>class Seuil
{
public:
Seuil(void){}
Seuil(T _low , T _high)
{
this->low = _low ;
this->high = _high;
}
void setSeuil(Seuil const & _seuil)
{
this->low = _seuil.low ;
this->high = _seuil.high;
}
virtual ~Seuil(void){}
protected :
T high;
T low;
};
//class de gestion d'alarm
template<typename T> class Alarm : public Seuil<T>
{
public :
Alarm(void):Seuil<T>()
{
this->inclusive = true;
}
Alarm(Seuil<T> const & _Seuil , bool _inculsive = true):Seuil<T>()
{
this->setSeuil(_Seuil);
this->inclusive = _inculsive;
}
void setAlarm(Seuil<T> const & _Seuil , bool _inculsive = true)
{
this->setSeuil(_Seuil);
this->inclusive = _inculsive;
}
virtual ~Alarm(void)
{
}
protected:
//renvoi vrai si valeur hors tolerance
bool getState(T const value) const
{
return value >= this->low && value <= this->high ? this->inclusive : !this->inclusive;
}
// definit si l'alarme est hors des seuils ou entre les seuils
bool inclusive;
};
//class de gestin du capteur thermique
class ThermalSensor : public Adafruit_MLX90614 , public Alarm<float>
{
public:
float value;
ThermalSensor( ):Adafruit_MLX90614(),Alarm<float>()
{
this->value = 0.0f;
while (!this->begin())
{
Serial.println(FlashString("Error connecting to MLX sensor. Check wiring."));
delay(5000);
}
}
virtual ~ThermalSensor(void)
{
}
template<int averageCount> void read(void)
{
this->value = 0;
for(auto i = averageCount ; i > 0 ; i--)
{
this->value += this->readAmbientTempC();
delay(1);
}
this->value = this->value/(float)averageCount;
}
bool alarm(void)
{
return this->getState(this->value);
}
private :
};
//class de gestion du capteur spo2 et battement par minute
class MAX30102 : public MAX30105 , public Alarm<float>
{
private:
public:
int32_t spo2; //SPO2 value
int8_t validSPO2; //indicator to show if the SPO2 calculation is valid
int32_t heartRate; //heart rate value
int8_t validHeartRate; //indicator to show if the heart rate calculation is valid
MAX30102(void):MAX30105(),Alarm<float>()
{
this->validHeartRate = 0;
this->validSPO2 = 0;
this->heartRate = 0;
this->spo2 = 0;
while( !this->begin() )
{
Serial.println(FlashString("MAX30105 was not found. Please check wiring/power. "));
delay(5000);
}
byte ledBrightness = 60; //Options: 0=Off to 255=50mA
byte sampleAverage = 8; //Options: 1, 2, 4, 8, 16, 32
byte ledMode = 2; //Options: 1 = Red only, 2 = Red + IR, 3 = Red + IR + Green
byte sampleRate = 100; //Options: 50, 100, 200, 400, 800, 1000, 1600, 3200
int pulseWidth = 411; //Options: 69, 118, 215, 411
int adcRange = 4096; //Options: 2048, 4096, 8192, 16384
this->setup(ledBrightness, sampleAverage, ledMode, sampleRate, pulseWidth, adcRange);
}
virtual ~MAX30102(void)
{
}
template<typename T ,int bufferLength> void read(void)
{
T irBuffer[bufferLength]; //infrared LED sensor data
T redBuffer[bufferLength]; //red LED sensor data
for (byte i = 0 ; i < bufferLength ; i++) //incrémente pour voir si y'a de nouvelle data
{
while (this->available() == false)
{
//do we have new data?
this->check(); //Check the sensor for new data
}
redBuffer[i] = ( this->getRed() + this->getRed() )* 0.5 ;
irBuffer[i] = (this->getIR() + this->getIR()) * 0.5 ;
this->nextSample(); //We're finished with this sample so move to next sample
}
int32_t spo2_last = this->spo2; //SPO2 value
int32_t heartRate_last = this->heartRate; //heart rate value
maxim_heart_rate_and_oxygen_saturation(irBuffer, bufferLength, redBuffer, &this->spo2, &this->validSPO2, &this->heartRate, &this->validHeartRate);
if( this->validSPO2 )
{
this->spo2 = (this->spo2 + spo2_last) * 0.5 ;// permet d'enregistrer la derniere valeur?
}
else
{
this->spo2 = spo2_last ;
}
if(this->validHeartRate )
{
this->spo2 = (this->heartRate + heartRate_last ) * 0.5 ;// permet d'enregistrer la derniere valeur?
}
else
{
this->heartRate = heartRate_last ;// si pas de nouvelle donnée on reprend donnée -1
}
}
bool spoAlarm()// alarme = à 1 ou 0
{
return this->getState(this->spo2);// renvoie le résultat
}
bool heartRateAlarm( )// alarme = à 1 ou 0
{
return this->getState(this->heartRate); // renvoie le résultat
}
};
//construit la date en une seul chaine de caractere depuis le rtc
String getDate(SharedPtr<DS3231> & _rtc)
{
bool century = false;
bool h12 = true;
bool pm = true;
SharedPtr<String> res(new String(""));
*res.get() = String(_rtc->getYear() , DEC);
*res.get() +="/";
*res.get() +=String( _rtc->getMonth(century) , DEC);
*res.get() +="/";
*res.get() +=String( _rtc->getDate() , DEC);
*res.get() +=" ";
*res.get() +=String( _rtc->getHour(h12,pm) , DEC);
*res.get() +=":";
*res.get() +=String( _rtc->getMinute() , DEC);
*res.get() +=":";
*res.get() +=String( _rtc->getSecond() , DEC);
return *res.get() ;
}
template<char separator> void WriteCSV(SharedPtr<DS3231> & _rtc , SharedPtr<ThermalSensor> & _thermalSensored, SharedPtr<MAX30102> &_particleSensor)
{
//si le fichier n'existe pas set un flag
bool firstWrite = !SD.exists("log.csv") ;
//ouvre en ecriture un fichier
File file = SD.open("log.csv", FILE_WRITE);
if(!file)
{
Serial.println(FlashString("error for open file"));
return ;
}
// va a la fin du fichier
file.seek(file.size() - 1);
if(firstWrite)
{
// ecrit la ligne d'entete
file.print("date");
file.print(separator);
file.print("heartRate");
file.print(separator);
file.print("SPO2");
file.print(separator);
file.print("thermalValue");
file.print(separator);
file.print("heartRate alarm");
file.print(separator);
file.print("SPO2 alarm");
file.print(separator);
file.print("thermalValue alarm");
file.print("\n\r");
}
//prepare l'ecriture sur la carte SD en memoire RAM
file.print( getDate( _rtc ) );
//ecrit la ligne de donné
file.print(separator);
file.print(_particleSensor->heartRate);
file.print(separator);
file.print(_particleSensor->spo2);
file.print(separator);
file.print(_thermalSensored->value);
file.print(separator);
_particleSensor->setAlarm( Seuil<float>(50,150) , false);
file.print(_particleSensor->heartRateAlarm());
file.print(separator);
_particleSensor->setAlarm( Seuil<float>(0.95,1) , false);
file.print(_particleSensor->spoAlarm());
file.print(separator);
file.print(_thermalSensored->alarm());
file.print("\n\r");
file.print("\n\r");
// ferme le fichier et commence le transfere de memoire RAM a la mamoire de la SD
file.close();
//laisse le temps audonné de s'ecrire et libéré de la RAM
delay(50);
}
// variable globale ( pointeur partagable) des différent objet utile
SharedPtr<DS3231> rtc;
SharedPtr<ThermalSensor> thermalSensored;
SharedPtr<MAX30102> particleSensor;
//SharedPtr< TFT > tftScreen;
//variable de temps pour gestion asyncrone
uint32_t Async1;
//fonction d'initialisation arduino
void setup()
{
//init de la variable de temps a la valeur de l'horloge ( quartz ) actuel
Async1 = millis();
//initilisation de la communication serie
Serial.begin( BR_Serial) ;
//I2C en maitre
Wire.begin();
delay(20);
//intilise l'ecran tactile
//(_cs, _dc, _mosi, _sclk, _rst, _miso)
// A3 = CS , A2 = CD , A1 = WR , A0 = RD , A4 = RST
//tftScreen = SharedPtr< TFT >(new TFT(A3,A2,A1,A0,A4) );
//ecrit Bienvenue
//tftScreen->setCursor(100,180);
//tftScreen->setTextColor(0xFFFF);
//tftScreen->setTextSize(3);
//tftScreen->print(FlashString("Bienvenue"));
//initialise le rtc
rtc = SharedPtr<DS3231>(new DS3231());
//initialise le catpeur de temperature
thermalSensored = SharedPtr<ThermalSensor>(new ThermalSensor() );
//definition des borne d'alarm pour la temperature
thermalSensored->setAlarm( Seuil<float>(34 ,40 ),false);
//initilaise Le capteur SPO2 / Battement par minute
particleSensor = SharedPtr<MAX30102>( new MAX30102() );
if ( !SD.begin( 53 ) )
{
Serial.println(" sd not detected ");
}
else
{
Serial.println(" sd init ok ");
}
Serial.println(FlashString("init end , start main"));
}
//Boucle principal arduino
void loop()
{
// on ecrit la date ( toute les 500 ms)
//tftScreen->WriteDate(StartPosDate , getDate( rtc ) );
//Serial.println(particleSensor->getIR());
//toute les 10s effectue une mesure des capteurs si un doigt est detecter
if(millis() - Async1 >= 1000 && particleSensor->getIR() > 100000 )
{
Serial.println( "doigt détecté , calcule en cour ... " );
//lecture des capteur
particleSensor->read<uint32_t , 100 >();
thermalSensored->read< 25 >();
Serial.println( particleSensor->spo2 );
Serial.println( particleSensor->heartRate );
Serial.println( thermalSensored->value );
Serial.println( getDate( rtc ) );
//ecriture des valeur sur l'ecran
//tftScreen->WriteSPO2(StartPosSPO , particleSensor->spo2 );
//tftScreen->WriteHeartRate(StartPosHeartRate , particleSensor->heartRate );
//tftScreen->WriteThermal(StartPosThermal , thermalSensored->value );
//ecriture des donné sur la carte SD
WriteCSV<'\t'>(rtc , thermalSensored , particleSensor );
Async1 = millis();
}
//attant 500 ms
delay(500);
} |
Partager