example MAX30100

Dependencies:   MAX30100 mbed

Committer:
AVELARDEV
Date:
Fri Nov 25 00:54:29 2016 +0000
Revision:
0:80ecccd27646
exmaple use MAX30100 library

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AVELARDEV 0:80ecccd27646 1 /*
AVELARDEV 0:80ecccd27646 2 Arduino-MAX30100 oximetry / heart rate integrated sensor library
AVELARDEV 0:80ecccd27646 3 Copyright (C) 2016 OXullo Intersecans <x@brainrapers.org>
AVELARDEV 0:80ecccd27646 4 This program is free software: you can redistribute it and/or modify
AVELARDEV 0:80ecccd27646 5 it under the terms of the GNU General Public License as published by
AVELARDEV 0:80ecccd27646 6 the Free Software Foundation, either version 3 of the License, or
AVELARDEV 0:80ecccd27646 7 (at your option) any later version.
AVELARDEV 0:80ecccd27646 8 This program is distributed in the hope that it will be useful,
AVELARDEV 0:80ecccd27646 9 but WITHOUT ANY WARRANTY; without even the implied warranty of
AVELARDEV 0:80ecccd27646 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
AVELARDEV 0:80ecccd27646 11 GNU General Public License for more details.
AVELARDEV 0:80ecccd27646 12 You should have received a copy of the GNU General Public License
AVELARDEV 0:80ecccd27646 13 along with this program. If not, see <http://www.gnu.org/licenses/>.
AVELARDEV 0:80ecccd27646 14 */
AVELARDEV 0:80ecccd27646 15
AVELARDEV 0:80ecccd27646 16 #include "MAX30100_PulseOximeter.h"
AVELARDEV 0:80ecccd27646 17
AVELARDEV 0:80ecccd27646 18 //#define REPORTING_PERIOD_MS 1000
AVELARDEV 0:80ecccd27646 19
AVELARDEV 0:80ecccd27646 20 Serial pc(USBTX, USBRX);
AVELARDEV 0:80ecccd27646 21 Timer t;
AVELARDEV 0:80ecccd27646 22
AVELARDEV 0:80ecccd27646 23 // PulseOximeter is the higher level interface to the sensor
AVELARDEV 0:80ecccd27646 24 // it offers:
AVELARDEV 0:80ecccd27646 25 // * beat detection reporting
AVELARDEV 0:80ecccd27646 26 // * heart rate calculation
AVELARDEV 0:80ecccd27646 27 // * SpO2 (oxidation level) calculation
AVELARDEV 0:80ecccd27646 28 PulseOximeter pox;
AVELARDEV 0:80ecccd27646 29
AVELARDEV 0:80ecccd27646 30 uint32_t tsLastReport = 0;
AVELARDEV 0:80ecccd27646 31
AVELARDEV 0:80ecccd27646 32 // Callback (registered below) fired when a pulse is detected
AVELARDEV 0:80ecccd27646 33
AVELARDEV 0:80ecccd27646 34 void onBeatDetected()
AVELARDEV 0:80ecccd27646 35 {
AVELARDEV 0:80ecccd27646 36 // pc.printf("Beat!\r\n");
AVELARDEV 0:80ecccd27646 37 }
AVELARDEV 0:80ecccd27646 38
AVELARDEV 0:80ecccd27646 39 bool setup()
AVELARDEV 0:80ecccd27646 40 {
AVELARDEV 0:80ecccd27646 41 pc.baud(115200);
AVELARDEV 0:80ecccd27646 42 pc.printf("Start program!\r\n");
AVELARDEV 0:80ecccd27646 43 //pox = new PulseOximeter (&pc);
AVELARDEV 0:80ecccd27646 44
AVELARDEV 0:80ecccd27646 45 // Initialize the PulseOximeter instance and register a beat-detected callback
AVELARDEV 0:80ecccd27646 46 if(!pox.begin())
AVELARDEV 0:80ecccd27646 47 return false;
AVELARDEV 0:80ecccd27646 48 pox.setOnBeatDetectedCallback(onBeatDetected);
AVELARDEV 0:80ecccd27646 49 return true;
AVELARDEV 0:80ecccd27646 50 }
AVELARDEV 0:80ecccd27646 51
AVELARDEV 0:80ecccd27646 52 bool newValueMAX30100 = 0;
AVELARDEV 0:80ecccd27646 53 float heartRate;
AVELARDEV 0:80ecccd27646 54 float finalHeartRate;
AVELARDEV 0:80ecccd27646 55 uint8_t sp02;
AVELARDEV 0:80ecccd27646 56 uint16_t finalSp02;
AVELARDEV 0:80ecccd27646 57 uint32_t REPORTING_PERIOD_MS = 1000;
AVELARDEV 0:80ecccd27646 58 std::vector<float> valuesHeartRate;
AVELARDEV 0:80ecccd27646 59 std::vector<uint8_t> valuesSp02;
AVELARDEV 0:80ecccd27646 60 uint8_t samplesMAX30100 = 10;
AVELARDEV 0:80ecccd27646 61 uint8_t counterMAX30100 = 0;;
AVELARDEV 0:80ecccd27646 62
AVELARDEV 0:80ecccd27646 63 void updateMAX30100 (){
AVELARDEV 0:80ecccd27646 64 // Make sure to call update as fast as possible
AVELARDEV 0:80ecccd27646 65 pox.update();
AVELARDEV 0:80ecccd27646 66
AVELARDEV 0:80ecccd27646 67 if (t.read_ms() > REPORTING_PERIOD_MS) {
AVELARDEV 0:80ecccd27646 68 heartRate = pox.getHeartRate();
AVELARDEV 0:80ecccd27646 69 sp02 = pox.getSpO2();
AVELARDEV 0:80ecccd27646 70
AVELARDEV 0:80ecccd27646 71 if(heartRate != 0 && sp02 != 0) {
AVELARDEV 0:80ecccd27646 72 pc.printf("Heart rate: %f",heartRate);
AVELARDEV 0:80ecccd27646 73 pc.printf(" bpm / SpO2: %d%\r\n",sp02);
AVELARDEV 0:80ecccd27646 74 valuesHeartRate.push_back(heartRate);
AVELARDEV 0:80ecccd27646 75 valuesSp02.push_back(sp02);
AVELARDEV 0:80ecccd27646 76 counterMAX30100 ++;
AVELARDEV 0:80ecccd27646 77 }else{
AVELARDEV 0:80ecccd27646 78 pc.printf("No finger\r\n");
AVELARDEV 0:80ecccd27646 79 }
AVELARDEV 0:80ecccd27646 80
AVELARDEV 0:80ecccd27646 81 if(samplesMAX30100 == counterMAX30100) {
AVELARDEV 0:80ecccd27646 82
AVELARDEV 0:80ecccd27646 83 finalHeartRate = 0;
AVELARDEV 0:80ecccd27646 84 finalSp02 = 0;
AVELARDEV 0:80ecccd27646 85 for(int i=0; i<samplesMAX30100; i++){
AVELARDEV 0:80ecccd27646 86 finalHeartRate += valuesHeartRate[i];
AVELARDEV 0:80ecccd27646 87 finalSp02 += valuesSp02[i];
AVELARDEV 0:80ecccd27646 88 }
AVELARDEV 0:80ecccd27646 89
AVELARDEV 0:80ecccd27646 90 finalHeartRate /= samplesMAX30100;
AVELARDEV 0:80ecccd27646 91 finalSp02 /= samplesMAX30100;
AVELARDEV 0:80ecccd27646 92
AVELARDEV 0:80ecccd27646 93 counterMAX30100 = 0;
AVELARDEV 0:80ecccd27646 94 valuesHeartRate.clear();
AVELARDEV 0:80ecccd27646 95 valuesSp02.clear();
AVELARDEV 0:80ecccd27646 96 newValueMAX30100 = true;
AVELARDEV 0:80ecccd27646 97 }
AVELARDEV 0:80ecccd27646 98
AVELARDEV 0:80ecccd27646 99 t.reset();
AVELARDEV 0:80ecccd27646 100 }
AVELARDEV 0:80ecccd27646 101 }
AVELARDEV 0:80ecccd27646 102
AVELARDEV 0:80ecccd27646 103 void loop()
AVELARDEV 0:80ecccd27646 104 {
AVELARDEV 0:80ecccd27646 105 updateMAX30100();
AVELARDEV 0:80ecccd27646 106
AVELARDEV 0:80ecccd27646 107 if(newValueMAX30100){
AVELARDEV 0:80ecccd27646 108 pc.printf("--->New Value\r\n");
AVELARDEV 0:80ecccd27646 109 pc.printf("Heart rate: %f",finalHeartRate);
AVELARDEV 0:80ecccd27646 110 pc.printf(" bpm / SpO2: %d%\r\n",finalSp02);
AVELARDEV 0:80ecccd27646 111 pc.printf("*************************\r\n");
AVELARDEV 0:80ecccd27646 112 newValueMAX30100 = false;
AVELARDEV 0:80ecccd27646 113 }
AVELARDEV 0:80ecccd27646 114
AVELARDEV 0:80ecccd27646 115 }
AVELARDEV 0:80ecccd27646 116
AVELARDEV 0:80ecccd27646 117 int main()
AVELARDEV 0:80ecccd27646 118 {
AVELARDEV 0:80ecccd27646 119 if(setup())
AVELARDEV 0:80ecccd27646 120 pc.printf("MAX30100 beginning\r\n");
AVELARDEV 0:80ecccd27646 121 else
AVELARDEV 0:80ecccd27646 122 return 0;
AVELARDEV 0:80ecccd27646 123
AVELARDEV 0:80ecccd27646 124 t.start();
AVELARDEV 0:80ecccd27646 125 while(1)
AVELARDEV 0:80ecccd27646 126 loop();
AVELARDEV 0:80ecccd27646 127 }