practical / Mbed 2 deprecated updatedPatient2

Dependencies:   mbed

Revision:
1:417090fd8386
Parent:
0:c837d6abadc5
diff -r c837d6abadc5 -r 417090fd8386 main.cpp
--- a/main.cpp	Sat Apr 13 23:14:31 2019 +0000
+++ b/main.cpp	Sun Apr 14 02:07:37 2019 +0000
@@ -3,10 +3,11 @@
 #include "mbed.h"
 #include "algorithm.h"
 #include "MAX30102.h"
+#include "MAX30101.h"
 
 #define MAX_BRIGHTNESS 255
 Serial pc(USBTX, USBRX);
-Serial esp(p9, p10); 
+Serial esp(p13, p14); 
 //Serial esp(p28, p27); // tx, rx
 
 
@@ -18,9 +19,10 @@
 
 // some test values to show on web page
 AnalogIn   Ain1(p18);
-AnalogIn   Ain2(p19);
-AnalogIn LM35(p20);
-DigitalIn INT(p8);
+AnalogIn   LM35B(p19);
+AnalogIn   LM35A(p20);
+DigitalIn  INT(p8);
+DigitalIn  INT1(p7);
 /*
 char ssid[32] = "hsd";     // enter WiFi router ssid inside the quotes
 char pwd [32] = "austin123"; // enter WiFi router password inside the quotes
@@ -34,15 +36,38 @@
 int32_t n_heart_rate;   //heart rate value
 int8_t  ch_hr_valid;    //indicator to show if the heart rate calculation is valid
 uint8_t uch_dummy;
+
+uint32_t aun_ir_buffer1[500]; //IR LED sensor data
+int32_t n_ir_buffer_length1;    //data length
+uint32_t aun_red_buffer1[500];    //Red LED sensor data
+int32_t n_sp021; //SPO2 value
+int8_t ch_spo2_valid1;   //indicator to show if the SP02 calculation is valid
+int32_t n_heart_rate1;   //heart rate value
+int8_t  ch_hr_valid1;    //indicator to show if the heart rate calculation is valid
+uint8_t uch_dummy1;
+
+
 char uploadTimeOut=1;
 Timer timer1second;
 
 float temperature, AdcIn, Ht;
 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
 char Vcc[10];
-char Temp[10];
+char TempA[10];
+char TempB[10];
 char Null[10];
 
+float f_temp;
+uint32_t un_min, un_max, un_prev_data;  //variables to calculate the on-board LED brightness that reflects the heartbeats
+
+float f_temp1;
+uint32_t un_min1, un_max1, un_prev_data1;  //variables to calculate the on-board LED brightness that reflects the heartbeats
+int i1;
+int32_t n_brightness1;
+
+int i;
+int32_t n_brightness;
+
 // things for sending/receiving data over serial
 volatile int tx_in=0;
 volatile int tx_out=0;
@@ -77,28 +102,47 @@
 int month       =4;     // 1-12
 int year        =19;    // last 2 digits
 
-int main()
+void gettemp()
 {
-    float f_temp;
-    uint32_t un_min, un_max, un_prev_data;  //variables to calculate the on-board LED brightness that reflects the heartbeats
-    int i;
-    int32_t n_brightness;
+    float temp_C , temp_F, average[10], averageTemp;
+    float tempB_C , tempB_F, averageB[10], averageTempB;
+    int index;
     
-    pc.baud(115200);
-    esp.baud(9600);
-    led1=1,led2=0,led3=0, led4=0;
-    // Setup a serial interrupt function to receive data
-    esp.attach(&Rx_interrupt, Serial::RxIrq);
-    // Setup a serial interrupt function to transmit data
-    esp.attach(&Tx_interrupt, Serial::TxIrq);
-    if (time(NULL) < 1420070400) {
-        setRTC();
-    }
-    startserver();
-    DataRX=0;
-    count=0;
-     
+    averageTemp=0;
+            for(index=0; index<10; index++)
+            {
+                average[index]=LM35A.read();
+                     //wait(0.02);
+            }
+            for(index=0; index<10; index++)
+            {
+                averageTemp  = (averageTemp +(average[index]/10));
+            }
+            temp_C=(averageTemp*3.685*100);
+            temp_F=(((9.0*temp_C)/5.0) + 32.0);
+            sprintf(TempA,"%2.3f",temp_C);
+            
+    averageTempB=0;  
+            for(index=0; index<10; index++)
+            {
+                average[index]=LM35B.read();
+                     //wait(0.02);
+            }
+            for(index=0; index<10; index++)
+            {
+                averageTempB  = (averageTempB +(averageB[index]/10));
+            }
+            tempB_C=(averageTempB*3.685*100);
+            tempB_F=(((9.0*tempB_C)/5.0) + 32.0);
+            sprintf(TempB,"%2.3f",tempB_C);
+            
+    pc.printf("Temperature of Patient 1 is %.2f C, %.2f F \n\r ", temp_C,temp_F);
+    pc.printf("Temperature of Patient 1 is %.2f C, %.2f F \n\r ", tempB_C,tempB_F);
+}
 
+
+void heartbeatInitA()
+{
      maxim_max30102_reset(); //resets the MAX30102
      wait(1);
      //read and clear status register
@@ -123,7 +167,7 @@
     //read the first 500 samples, and determine the signal range
     for(i=0;i<n_ir_buffer_length;i++)
     {
-        //while(INT.read()==1);   //wait until the interrupt pin asserts
+        while(INT.read()==1);   //wait until the interrupt pin asserts
         
         maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));  //read from MAX30102 FIFO
             
@@ -139,46 +183,50 @@
     un_prev_data=aun_red_buffer[i];
 
     //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
-    maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid); 
+    maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid);     
      
-    timer1second.start(); 
-    while(1) {
-        if(DataRX==1) {
-            ReadWebData();
-            esp.attach(&Rx_interrupt, Serial::RxIrq);
-        }
-    if(n_heart_rate <=10)
-        {
-        maxim_max30102_init();  //initializes the MAX30102
+}
+
+
+void heartbeatInitB()
+{
+ maxim_max30101_reset(); //resets the MAX30102
+     wait(1);
+     //read and clear status register
+  
+    
+    maxim_max30101_init();  //initializes the MAX30102
           
-    n_brightness=0;
-    un_min=0x3FFFF;
-    un_max=0;
+    n_brightness1=0;
+    un_min1=0x3FFFF;
+    un_max1=0;
   
-   // n_ir_buffer_length=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
+    n_ir_buffer_length1=500; //buffer length of 100 stores 5 seconds of samples running at 100sps
     
     //read the first 500 samples, and determine the signal range
-    for(i=0;i<n_ir_buffer_length;i++)
+    for(i=0;i<n_ir_buffer_length1;i++)
     {
-        //while(INT.read()==1);   //wait until the interrupt pin asserts
+        while(INT1.read()==1);   //wait until the interrupt pin asserts
         
-        maxim_max30102_read_fifo((aun_red_buffer+i), (aun_ir_buffer+i));  //read from MAX30102 FIFO
+        maxim_max30101_read_fifo(((aun_red_buffer1)+i), ((aun_ir_buffer1)+i));  //read from MAX30102 FIFO
             
-        if(un_min>aun_red_buffer[i])
-            un_min=aun_red_buffer[i];    //update signal min
-        if(un_max<aun_red_buffer[i])
-            un_max=aun_red_buffer[i];    //update signal max
+        if(un_min1>aun_red_buffer1[i])
+            un_min1=aun_red_buffer1[i];    //update signal min
+        if(un_max1<aun_red_buffer1[i])
+            un_max1=aun_red_buffer1[i];    //update signal max
         pc.printf("red=");
-        pc.printf("%i", aun_red_buffer[i]);
+        pc.printf("%i", aun_red_buffer1[i]);
         pc.printf(", ir=");
-        pc.printf("%i\n\r", aun_ir_buffer[i]);
+        pc.printf("%i\n\r", aun_ir_buffer1[i]);
     }
-    un_prev_data=aun_red_buffer[i];
+    un_prev_data1=aun_red_buffer1[i];
 
     //calculate heart rate and SpO2 after first 500 samples (first 5 seconds of samples)
-    maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid); 
-     
-        }
+    maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer1, n_ir_buffer_length1, aun_red_buffer1, &n_sp021, &ch_spo2_valid1, &n_heart_rate1, &ch_hr_valid1);      
+}
+
+void heartbeatUpdateA()
+{
         i=0;
         un_min=0x3FFFF;
         un_max=0;
@@ -194,6 +242,7 @@
             un_min=aun_red_buffer[i];
             if(un_max<aun_red_buffer[i])
             un_max=aun_red_buffer[i];
+            
         }
         
         //take 100 sets of samples before calculating the heart rate.
@@ -225,29 +274,101 @@
          
         }
         maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer, n_ir_buffer_length, aun_red_buffer, &n_sp02, &ch_spo2_valid, &n_heart_rate, &ch_hr_valid); 
-    //eo while 
     sprintf(Vcc,"%i",n_heart_rate);
-           // 
+                
+}
+
+void heartbeatUpdateB()
+{
+        i=0;
+        un_min1=0x3FFFF;
+        un_max1=0;
+
+        //dumping the first 100 sets of samples in the memory and shift the last 400 sets of samples to the top
+        for(i=100;i<500;i++)
+        {
+            aun_red_buffer1[i-100]=aun_red_buffer1[i];
+            aun_ir_buffer1[i-100]=aun_ir_buffer1[i];
+            
+            //update the signal min and max
+            if(un_min1>aun_red_buffer1[i])
+            un_min1=aun_red_buffer1[i];
+            if(un_max1<aun_red_buffer1[i])
+            un_max1=aun_red_buffer1[i];
+        }
+        
+        //take 100 sets of samples before calculating the heart rate.
+        for(i=400;i<500;i++)
+        {
+            un_prev_data1=aun_red_buffer1[i-1];
+            while(INT1.read()==1);
+            maxim_max30101_read_fifo((aun_red_buffer1+i), (aun_ir_buffer1+i));
+        
+            if(aun_red_buffer1[i]>un_prev_data1)
+            {
+                f_temp1=aun_red_buffer1[i]-un_prev_data1;
+                f_temp1/=(un_max1-un_min1);
+                f_temp1*=MAX_BRIGHTNESS;
+                n_brightness1-=(int)f_temp1;
+                if(n_brightness1<0)
+                    n_brightness1=0;
+            }
+            else
+            {
+                f_temp1=un_prev_data1-aun_red_buffer1[i];
+                f_temp1/=(un_max1-un_min1);
+                f_temp1*=MAX_BRIGHTNESS;
+                n_brightness1+=(int)f_temp1;
+                if(n_brightness1>MAX_BRIGHTNESS)
+                    n_brightness1=MAX_BRIGHTNESS;
+            }
+        //pc.printf("red=%i, ir=%i, HR=%i,HRvalid=%i,SpO2=%i,SPO2Valid=%i\n\r",aun_red_buffer[i],aun_ir_buffer[i],n_heart_rate, ch_hr_valid, n_sp02, ch_spo2_valid);
+         
+        }
+        maxim_heart_rate_and_oxygen_saturation(aun_ir_buffer1, n_ir_buffer_length1, aun_red_buffer1, &n_sp021, &ch_spo2_valid1, &n_heart_rate1, &ch_hr_valid1); 
+        sprintf(Vcc,"%i",n_heart_rate1);
+                
+}
+
+int main()
+{
+    pc.baud(115200);
+    esp.baud(9600);
+    led1=1,led2=0,led3=0, led4=0;
+    // Setup a serial interrupt function to receive data
+    esp.attach(&Rx_interrupt, Serial::RxIrq);
+    // Setup a serial interrupt function to transmit data
+    esp.attach(&Tx_interrupt, Serial::TxIrq);
+    if (time(NULL) < 1420070400) {
+        setRTC();
+    }
+    startserver();
+    DataRX=0;
+    count=0;
+    heartbeatInitB();
+    heartbeatInitA();
+    timer1second.start(); 
+    while(1) {
+        if(DataRX==1) 
+        {
+            ReadWebData();
+            esp.attach(&Rx_interrupt, Serial::RxIrq);
+        }
+
+        heartbeatUpdateB();
+        heartbeatUpdateA();
         
         if(timer1second.read()>2)
         {   
             timer1second.reset();
-            // get new values
+         
             gettime();
             gettemp();
            // getbattery();
            
-            // send new values
-           /* if(n_heart_rate>=499 || n_heart_rate <=30)
-            {   
-                sprintf(Null,"%s","Null");
-                sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Null);
-            }
-            else*/
-            {
-                count++;
-                sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc);
-            }
+            count++;
+            sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,TempA,Vcc);
+            
             SendCMD();
             getreply();
             
@@ -303,7 +424,7 @@
     pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
 
     // initial values
-    sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
+    sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,TempA,Vcc);
     SendCMD();
     getreply();
     wait(0.5);
@@ -548,43 +669,7 @@
 // Analog in example
 void getbattery()
 {
-    AdcIn=Ain1.read();
-    Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
-   // sprintf(Vcc,"%2.3f",Ht);
     
-    //Continuously taking samples from MAX30102.  Heart rate and SpO2 are calculated every 1 second
-    
-    //timer1second.start();
-    //timer20ms.start();
-    
-     
-       
 }
 // Temperature example
-void gettemp()
-{
- 
-  //float f_temp;
-     float temp_C , temp_F, average[10], averageTemp;
-    int index;
-    AdcIn=Ain2.read();
-    //Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy  
-    
-    
-    averageTemp=0;
-            for(index=0; index<10; index++)
-            {
-                average[index]=LM35.read();
-                     //wait(0.02);
-            }
-            for(index=0; index<10; index++)
-            {
-                averageTemp  = (averageTemp +(average[index]/10));
-            }
-            temp_C=(averageTemp*3.685*100);
-            temp_F=(((9.0*temp_C)/5.0) + 32.0);
-            sprintf(Temp,"%2.3f",temp_C);
-            
-            pc.printf("Temperature is %.2f C, %.2f F \n\r ", temp_C,temp_F);
 
-}
\ No newline at end of file