Channel_ID

Dependencies:   MAX30003 max32630fthr DS1307

Revision:
16:8fc65c8fa0e8
Parent:
15:0356e9454a60
--- a/main.cpp	Thu Mar 11 21:22:19 2021 +0000
+++ b/main.cpp	Tue May 18 23:43:34 2021 +0000
@@ -19,11 +19,13 @@
 
 void ecg_config(MAX30003 &ecgAFE);
 //BufferedSerial pc(P3_1,P3_0);            // Use USB debug probe for serial link static Unbuffered
-static BufferedSerial pc(TARGET_TX_PIN, TARGET_RX_PIN, 230400); // 230400 works well
+BufferedSerial pc(P0_1,P0_0);
+
+//atic BufferedSerial pc(TARGET_TX_PIN, TARGET_RX_PIN, 230400); // 230400 works well
 
 //Serial uart_1(USBTX, USBRX);            // Use USB debug probe for serial link static Unbuffered
 //
-//Serial pc(P3_1,P3_0);
+//rial pc(P0_1,P0_0);
 volatile bool ecgFIFOIntFlag = 0; 
 volatile bool timerflag = 0;
 
@@ -57,8 +59,10 @@
     const int FIFO_VALID_SAMPLE_MASK =  0x0;
     const int FIFO_FAST_SAMPLE_MASK =  0x1;
     const int ETAG_BITS_MASK = 0x7;
+    
     timer_fast.start();
     DigitalOut rLed(LED2, LED_ON);
+    
 //    pc.baud(9600);
     
     //pc.set_baud(115200);                    // Baud rate = 115200
@@ -83,13 +87,20 @@
     
     uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status;
     int16_t ecgSample[32];
+//-----------------------------------------------------------------------------------------------// 
+    
     //bool timerflag = false;
     int16_t ecgSample_1sec[200];
-    uint8_t ecg_1 = 0;
-    uint8_t ecg_2 = 0;
+    //uint8_t ecg_1 = 0;
+  //  uint8_t ecg_2 = 0;
    uint16_t onesec_counter = 0;
    uint16_t onesec_counter_temp = 0;
    
+   uint8_t channel_ID[3]= {1,2,3};
+   uint8_t ecg_1 = 0;
+   uint8_t ecg_2 = 0;
+   uint8_t ecg_3 = 0;
+   
     int16_t sample = 300;
     uint8_t final[10];
     
@@ -115,21 +126,24 @@
      int sampleps = 0;
      
     char buf[20];
+    
+    
+    
    // pc.write("Welcome",8*sizeof(char));
    // printf("In the main loop"); //printf("In the main loop");
      bool flag_first = false;
-       bool timestamp_reader = false;
+     bool timestamp_reader = false;
       do
       {
-        if (pc.readable()) 
+        if (pc.readable())  // For nordic - Readiing time
         {     
         scanf("%s",buf);
      //printf("The entered string is %s : \n ",buf);
      
-     packet_1 = atoi(buf);
-     set_time(packet_1);
+     packet_1 = atoi(buf);  // converting ascii to integer
+     set_time(packet_1);   // check unit in packet_1 ??
      
-     if ((packet_1 % 60) == 0)
+     if ((packet_1 % 60) == 0)  // tim stamp at the beginning of time
          { 
            /*  for (int u = 0;u<3;u++)
              {
@@ -153,50 +167,71 @@
         }
         
         }while (timestamp_reader==false); // take the third timestamp
-         while(1) 
+         
+         
+        
+    while(1)        
     {
        
-    
-    if ((onesec_counter>=125))
+    if ((onesec_counter>=125)) //125 sample rate
     {
      //t.start();
      //ledA = !ledA;   
      
-      
-      pc.write((uint8_t *)header_device_id, sizeof(header_device_id));
+      //pc.write((uint8_t *)header_device_id, sizeof(header_device_id));                                                      // sensor ID
       
-      pc.write((uint8_t *)header_packet_type, sizeof(header_packet_type));
-      time_t seconds = time(NULL);
+     // pc.write((uint8_t *)header_packet_type, sizeof(header_packet_type));                                                  // packet type
+      
+      time_t seconds = time(NULL); // setting zero
       packet_1 = seconds;
       //packet_1 ++;
      // printf("Time as seconds since January 1, 1970 = %d\n", packet_1);
-      p_1 = packet_1 & 0xff;
+      p_1 = packet_1 & 0xff; 
       p_2 = (packet_1 >> 8) & 0xff;
       p_3 = (packet_1 >> 16) & 0xff;
       p_4 = (packet_1 >> 24) & 0xff;
      // checksum_  = checksum_ + (packet_1 & 0xffff) + ((packet_1 >> 16) & 0xffff) ;
      checksum_  = checksum_ + p_1 + p_2 + p_3 + p_4 ;
      uint8_t header_packet_id[4] = {p_4,p_3,p_2,p_1};
-      pc.write((uint8_t *)header_packet_id, sizeof(header_packet_id));
+      //pc.write((uint8_t *)header_packet_id, sizeof(header_packet_id));                                                      //packet ID
+      
+          
+      uint8_t Channel_Num = 0;
+      if(ecg_1 == 1)
+        Channel_Num = channel_ID[0];
+      else if (ecg_2 == 1)
+            Channel_Num = channel_ID[1];
+      else if (ecg_3 == 1)
+            Channel_Num = channel_ID[2];
+      else
+            Channel_Num = 0;      
+      //pc.write((uint8_t *)Channel_Num, sizeof(Channel_Num));     
+      printf("%6d\r\n", Channel_Num);                                                    //Channel no
+      
       onesec_counter_temp = onesec_counter * 2;
       data_len_1 = onesec_counter_temp & 0xff;
       data_len_2 = (onesec_counter_temp >> 8) & 0xff;
-      checksum_  = checksum_ + data_len_1 + data_len_2;
+      checksum_  = checksum_ + data_len_1 + data_len_2;  
       //pc.write((uint32_t *)packet_1, sizeof(packet_1));
       uint8_t header_ecg_datalen[2] = {data_len_2,data_len_1};
-      pc.write((uint8_t *)header_ecg_datalen, sizeof(header_ecg_datalen));
+     // pc.write((uint8_t *)header_ecg_datalen, sizeof(header_ecg_datalen));                                                  // Data length
+      
+      
       mod_checksum = checksum_ % 65536 ;
       cksm_1 = mod_checksum & 0xff;
       cksm_2 = (mod_checksum >> 8) & 0xff;
       uint8_t header_ecg_checksum[2] = {cksm_2,cksm_1};
-      pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum));
+      //pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum));                                                // checksum
       
-      pc.write((int16_t *)ecgSample_1sec,onesec_counter * sizeof(int16_t));
-     // printf("Samples per second  %d \n", (onesec_counter));
+    //  pc.write((int16_t *)ecgSample_1sec,onesec_counter * sizeof(int16_t));                                                 // ECG Data
+      printf("Samples per second  %d \n", (onesec_counter));
+     
+      
       onesec_counter = 0;
        // memset(ecgSample_1sec, 0, sizeof(ecgSample_1sec));
               
-        pc.write((uint8_t *)ending, sizeof(ending));
+       // pc.write((uint8_t *)ending, sizeof(ending));                                                                        // Ending of data
+        
         checksum_ = 0;
         //t.stop();
        // auto us = t.elapsed_time().count();
@@ -205,10 +240,12 @@
       // t.reset();
     //  timer_fast.reset();
        } 
-        // Read back ECG samples from the FIFO 
-        else if( (ecgFIFOIntFlag==1))// && (timerflag == 0)) 
+       
+
+       // Read back ECG samples from the FIFO        
+       else if((ecgFIFOIntFlag==1))// && (timerflag == 0)) 
         {
-            
+            ecg_1 = 1;
             ecgFIFOIntFlag = 0; 
             status = ecgAFE.readRegister( MAX30003::STATUS );      // Read the STATUS register