final

Dependencies:   MAX30003 max32630fthr DS1307

Revision:
25:06ba30a29754
Parent:
24:98332ec40aff
Child:
26:c41e561a02de
--- a/main.cpp	Tue Aug 17 16:39:40 2021 +0000
+++ b/main.cpp	Mon Nov 22 17:44:20 2021 +0000
@@ -10,14 +10,14 @@
 #define TARGET_TX_PIN P3_1
 #define TARGET_RX_PIN P3_0
  
-#define SDA P3_4
-#define SCL P3_5
+//#define SDA P3_4
+//#define SCL P3_5
 
 
-#define I2C_SDA P5_7
-#define I2C_SCL P6_0
+//#define I2C_SDA P5_7
+//#define I2C_SCL P6_0
 
- Ds3231 rtc(SDA, SCL); 
+ //Ds3231 rtc(SDA, SCL); 
 
 //Timer timer_fast;
 Timer t;
@@ -26,15 +26,23 @@
 void task_fast(void);
 //DigitalOut ledA(LED2);
  
-DigitalOut S0(P4_0);
-DigitalOut S1(P5_6);
+DigitalOut S0(P0_4);
+DigitalOut S1(P0_5);
 //DigitalOut S2(P7_2);
 //DigitalOut S3(P7_2);
 
-//channel feedback
-//DigitalIn A(P6_0); //P17
-//DigitalIn B(P3_5); //P18
-//DigitalIn C(P3_4); //P19
+  // Input pins
+DigitalIn A(P1_3); //P6_3
+DigitalIn B(P1_5); //P1_5
+DigitalIn C(P1_4); //P1_4
+//DigitalIn D(6_3); //P6_3 for hardware RTC
+
+const float battery_max = 4.20; //maximum voltage of battery
+const float battery_min = 3.0;  //minimum voltage of battery before shutdown
+uint8_t output = 0;
+
+//AnalogIn   ain_4(AIN_4);
+AnalogIn   ain_5(AIN_5);
 
  
 void ecg_config(MAX30003 &ecgAFE);
@@ -73,20 +81,6 @@
 {   
  uint16_t tic=0;
  
- /* S1S0
-    00 - C0
-    01 - C1
-    10 - C2
-    11 - C3 */
-    
- 
-     S0=0;    //C0
-     S1=0;
-  // S2=0;
-  // S3=0;
-   
- 
- 
    // Constants
     const int EINT_STATUS_MASK =  1 << 23;
     const int FIFO_OVF_MASK =  0x7;
@@ -95,7 +89,20 @@
     const int ETAG_BITS_MASK = 0x7;
     
   //  timer_fast.start();
-    DigitalOut rLed(LED2, LED_ON);
+    DigitalOut RGBr(P1_2);
+    DigitalOut RGBg(P1_1);
+    DigitalOut RGBb(P1_0);
+    
+    DigitalOut rLed(P0_6);
+    
+    rLed=1;
+    
+    S0=0;   
+    S1=0;
+    RGBr=0;
+    RGBg=1;
+    RGBb=1;
+    
 //    pc.baud(9600);
     
     //pc.set_baud(115200);                    // Baud rate = 115200
@@ -126,8 +133,8 @@
     int16_t ecgSample[32];
     //bool timerflag = false;
     int16_t ecgSample_1sec[256];
- //   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;
    
@@ -148,16 +155,17 @@
     uint32_t packet_1 = 0;
     uint8_t cksm_1 = 0;
     uint8_t cksm_2 = 0;
-    uint8_t header_device_id[3] = {0,0,210};
-    //uint8_t header_device_id[3] = {0,0,1};     //for rahul
+    uint8_t header_device_id[3] = {0,0,210};   //OLD device ID
+    //uint8_t header_device_id[3] = {0,0,12};     //
     uint8_t header_packet_type[2] = {0,2};
+    uint8_t battery_level[1] ={0};
     
     uint8_t ending[5] = {'@','#','%','!','7'};
     
     bool flip = true;
      int sampleps = 0;
      
-    char buf[20];
+    char buf[30];
    // pc.write("Welcome",8*sizeof(char));
    // printf("In the main loop"); //printf("In the main loop");
     bool flag_first = false;
@@ -201,13 +209,23 @@
      }   while (timestamp_reader==false);  // take the third timestamp 
     
 //---------------------------------software RTC End---------------------------------------------//
- 
+ rLed=0;
   
 while(1) 
     {
-       
     
-    if ((onesec_counter>=112))
+         unsigned short raw;
+      raw = ain_5.read_u16();
+      float vin;
+      vin = ((raw*5*1.2)/ 65535.0);
+      output = ((vin - battery_min) / (battery_max - battery_min)) * 100;
+      
+    if (output < 100)
+       battery_level[0] = {output}; 
+    else
+        battery_level[0] ={99};
+    
+    if ((onesec_counter>=125))
     {
       tic++;
         
@@ -233,6 +251,8 @@
       
       pc.write((uint8_t *)channel_num, sizeof(channel_num));
       
+      pc.write((uint8_t *)battery_level, sizeof(battery_level));
+      
       onesec_counter_temp = onesec_counter * 2;
       data_len_1 = onesec_counter_temp & 0xff;
       data_len_2 = (onesec_counter_temp >> 8) & 0xff;
@@ -262,20 +282,22 @@
     //  timer_fast.reset();
     
     
-       if (tic==10)       //C1    
+      if (tic==10)       //C1    
      {
       S0=1; 
       S1=0;
-   //   S2=0;
-   //   S3=0;
+        RGBr=1;
+        RGBg=0;
+        RGBb=1;
      channel_num[0]= {1};
      } 
      if (tic==20)    //C2
      {
       S0=0; 
       S1=1;
-    //  S2=0;
-    //  S3=0;
+        RGBr=1;
+        RGBg=1;
+        RGBb=0;
       channel_num[0]= {2};
       }
          
@@ -283,18 +305,23 @@
      {
       S0=0; 
       S1=0;
-    // S2=0;
-    // S3=0;
+        RGBr=0;
+        RGBg=1;
+        RGBb=1;
      channel_num[0]= {0};
      tic=0;} 
      
      
      // for channel selection
      
-  /*      if (A==0)  //
+       if (A==0)  //
     {tic=50; 
       S0=0; 
       S1=0;
+        RGBr=0;
+        RGBg=1;
+        RGBb=1;
+      
     //  S2=0;
    //  S3=0;
      channel_num[0]= {0};}
@@ -303,6 +330,10 @@
      {tic=60;
       S0=1; 
       S1=0;
+        RGBr=1;
+        RGBg=0;
+        RGBb=1;
+      
    //   S2=0;
     //  S3=0;
       channel_num[0]= {1};}
@@ -311,11 +342,13 @@
      {tic=70; 
       S0=0; 
       S1=1;
+        RGBr=1;
+        RGBg=1;
+        RGBb=0;
+      
    //   S2=0;
     //  S3=0;
-      channel_num[0]= {2};}
-    */
-    
+      channel_num[0]= {2};}    
     
     
        }