HFAC

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
hassan_elahi
Date:
Thu Jul 02 17:55:54 2020 +0000
Commit message:
HFAC

Changed in this revision

Functions.cpp Show annotated file Show diff for this revision Revisions of this file
Functions.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Functions.cpp	Thu Jul 02 17:55:54 2020 +0000
@@ -0,0 +1,154 @@
+#include "Functions.h"
+
+
+bool syncShockReady = false;
+
+//Delivering the HFAC and Dfeb Shock
+void Delivering (int amplitude, int duration)
+{
+
+    uint16_t sample = 0;
+    double step= (amplitude*220)/duration;
+
+    for(int HFAC_Count=1; HFAC_Count<= duration; ++HFAC_Count)
+    {
+        sample = (uint16_t)(offset + (step*HFAC_Count));
+        HFAC.write_u16(sample);
+        wait_us(500);
+        
+        //pc.printf("%d\r\n",duration - HFAC_Count);
+        if((duration - HFAC_Count) == 11){
+            led1 = 0;
+            SyncOut = 1;
+        }
+        
+        sample = (uint16_t)(offset - (step*HFAC_Count));
+        HFAC.write_u16(sample);
+        wait_us(500);
+    }
+    
+    HFAC_Relay = 0;
+    M_Relay = 1;
+    R_Relay = 0;
+    wait_ms(1);
+    M_Relay = 0;
+    SyncOut =0;
+    shock = 0;
+    HFAC.write_u16(offset);
+    buttonLEDs(shockButtonRedLED, Disable);
+    
+}
+
+//Delivering the HFAC and Dfeb Shock in the sync mode
+void SyncDelivering()
+{
+    if (byPassStatus)
+        Delivering (HFAC_Amplitude, HFAC_Duration);
+    else
+        Delivering (10, 100);
+
+    wait_ms(1000);
+}
+
+//Check the by Pass mode "HFAC ON or OFF"
+void checkByPass()
+{
+    wait_ms(100);
+    if (byPassButton){
+        LCD.putc('A');
+        byPassStatus = false;
+    }
+    else{
+        LCD.putc('B');
+        byPassStatus = true;
+    }
+    led4 = !led4;
+}
+
+//Interrupt function for R wave fall time 
+void  syncInFuncFall()
+{
+    wait_ms(100);
+    if (syncShock){
+            buttonLEDs(shockButtonGreenLED, Disable);
+    }
+}
+//Interrupt function for R wave raise time (Also calculating the average of 
+// HeartRateAvg samples of R wave signals
+void syncInFunc()
+{
+   if (syncShock) 
+        if(heartRateTimerStart){
+            if (heartMeanCounter < HeartRateAvg){
+                buttonLEDs(shockButtonGreenLED, Enable);
+                heartRateTemp += heartRateTimer.read_us();
+                //pc.printf("%d   ", heartMeanCounter); 
+                heartMeanCounter ++;
+            }
+            else{
+                syncShock = false;
+                buttonLEDs(shockButtonRedLED, Enable);
+
+                heartRate = heartRateTemp / HeartRateAvg;
+                heartMeanCounter = 0;
+                heartRateTemp = 0;
+                int fireTime =  heartRate - ((HFAC_Duration*1000) % heartRate)- 013.8* HFAC_Duration - 23625;//- ( -0.011*HFAC_Duration + 45.278)-10; 
+                //pc.printf("%d   ", heartRate);
+                //pc.printf("%d   ", HFAC_Duration);
+                //pc.printf("%d   ", fireTime); 
+                deliverTimeout.attach_us(&SyncDelivering, fireTime);
+                
+            } 
+            heartRateTimer.reset();
+        }
+        else{
+            heartRateTimerStart = true;
+            heartRateTimer.start();
+            heartMeanCounter = 0;
+        }
+
+    
+}
+
+//Shock and Charge button LEDS
+void buttonLEDs(int led, bool enable)
+{
+    switch (led) {
+        case 0:
+            if(enable) {
+                shockRedLED = 1;
+                shockGreenLED = 0;
+            } else {
+                shockRedLED = 0;
+                shockGreenLED = 0;
+            }
+            break;
+        case 1:
+            if(enable) {
+                shockRedLED = 0;
+                shockGreenLED = 1;
+            } else {
+                shockRedLED = 0;
+                shockGreenLED = 0;
+            }
+            break;
+        case 2:
+            if(enable) {
+                chargeRedLED = 1;
+                chargeGreenLED = 0;
+            } else {
+                chargeRedLED = 0;
+                chargeGreenLED = 0;
+            }
+            break;
+        case 3:
+            if(enable) {
+                chargeRedLED = 0;
+                chargeGreenLED = 1;
+            } else {
+                chargeRedLED = 0;
+                chargeGreenLED = 1;
+            }
+            break;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Functions.h	Thu Jul 02 17:55:54 2020 +0000
@@ -0,0 +1,74 @@
+#ifndef DFEB_Functions
+#define DFEB_Functions
+#include "mbed.h"
+
+#define HeartRateAvg  3
+
+#define shockButtonRedLED  0
+#define shockButtonGreenLED  1
+#define chargeButtonRedLED  2
+#define chargeButtonGreenLED  3
+#define Enable 1
+#define Disable 0
+
+
+
+extern DigitalIn shockButton;
+extern DigitalIn chargeButton;
+//extern InterruptIn byPassButton;
+extern DigitalIn byPassButton;
+
+
+extern DigitalOut shock;
+
+
+extern DigitalOut HFAC_Relay;
+extern DigitalOut M_Relay_B;
+extern DigitalOut M_Relay;
+extern DigitalOut R_Relay;
+
+extern DigitalOut SyncOut;
+
+extern AnalogOut HFAC;
+
+extern DigitalOut led1;
+extern DigitalOut led4;
+
+
+extern DigitalOut shockRedLED;
+extern DigitalOut shockGreenLED;
+
+extern DigitalOut chargeRedLED;
+extern DigitalOut chargeGreenLED;
+
+
+extern Serial LCD;
+extern Serial pc;
+extern int HFAC_Duration;
+extern int HFAC_Amplitude;
+
+
+const double offset = 65535/2;
+extern bool byPassStatus ;
+
+extern bool DfebCharged;
+
+extern Timer heartRateTimer; 
+extern Timeout deliverTimeout;
+extern bool heartRateTimerStart;
+extern int heartRate;
+extern int heartMeanCounter;
+extern int heartRateTemp;
+extern bool syncActivate;
+extern bool syncShock;
+
+void Delivering(int amplitude, int duration);
+void SyncDelivering();
+void checkByPass();
+void syncInFunc();
+void syncInFuncFall();
+void buttonLEDs(int led, bool enable); 
+
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 02 17:55:54 2020 +0000
@@ -0,0 +1,215 @@
+#include "Functions.h"
+
+
+DigitalIn shockButton(p16);
+DigitalIn chargeButton(p17);
+DigitalIn byPassButton(p32);
+
+//sync IN, ECG R wave In pin
+InterruptIn SyncIn(p14);
+//InterruptIn byPassButton(p15);
+
+//shock signal to defeb
+DigitalOut shock(p26);
+//charge signal to defeb
+DigitalOut charge(p25);
+
+//charge signal from defeb
+DigitalIn chargeLEDCheck(p30);
+
+// Relay to power Amplifier 
+DigitalOut HFAC_Relay(p5);
+//Middle Relay off
+DigitalOut M_Relay_B(p6);
+//Middle Relay On
+DigitalOut M_Relay(p7);
+// Resistor Relay : No need any more
+DigitalOut R_Relay(p8);
+
+//Sync out to dfeb to deliver the shock 
+DigitalOut SyncOut(p20);
+
+//HFAC  Analog out put
+AnalogOut HFAC(p18);
+
+//Test leds
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+//Shock button Leds
+DigitalOut shockRedLED(p34);
+DigitalOut shockGreenLED(p33);
+
+//Charge button Leds
+DigitalOut chargeRedLED(p39);
+DigitalOut chargeGreenLED(p38);
+
+// serial link for LCD
+Serial LCD(p9, p10);
+
+// serial link for PC
+Serial pc(USBTX, USBRX);
+
+uint16_t sample = 0;
+
+char temp;
+int HFAC_Duration = 1300;
+int HFAC_Amplitude = 100;
+int LCD_Counter = 0;
+int LCD_Message = 0;
+int LCD_Source = 0;
+
+bool byPassOldStatus = false;
+bool byPassStatus = false;
+
+//Timer for calculating the time bewteen each R wave signal
+Timer heartRateTimer;
+//TimeOut Timer for deleviring shock after detecting "HeartRateAvg + 1" R wave signal
+Timeout deliverTimeout;
+
+bool heartRateTimerStart = false;
+int heartRate = 0;
+int heartMeanCounter = 0;
+int heartRateTemp = 0;
+
+bool syncActivate = false;
+bool syncShock = false;
+
+bool DfebCharged = false;
+
+int main()
+{
+
+    ////////init//////////////
+    LCD.baud(115200);
+    shockButton.mode(PullUp);
+    chargeButton.mode(PullUp);
+    byPassButton.mode(PullUp);
+    chargeLEDCheck.mode(PullDown);
+    
+    SyncIn.mode(PullDown);
+    
+ //   byPassButton.rise(&checkByPass);
+ //   byPassButton.fall(&checkByPass);
+    
+    M_Relay = 1;
+    wait_ms(1);
+    M_Relay = 0;
+    HFAC_Relay = 0;
+    R_Relay = 0;
+    // Make the HFAC analog to VDD/2
+    HFAC.write_u16(offset);
+
+    led1 = 1;
+    SyncOut =0;
+    wait_ms(200);
+    
+    checkByPass();
+    
+    SyncIn.rise(&syncInFunc);
+    SyncIn.fall(&syncInFuncFall);
+
+    
+    while(1) {
+        //Check if the bypass  status has changed 
+        if (byPassOldStatus != byPassButton){
+            wait_ms(200);
+            checkByPass();
+        } 
+        
+        byPassOldStatus = byPassButton;
+        
+        //Check Charge button status 
+        if (!chargeButton){
+            charge =1; 
+            led3 = 1;
+            led4 = 0; 
+        
+            //R_Relay = 1;
+            
+            //M_Relay_B = 1;
+            //wait_ms(1);
+            //M_Relay_B = 0;
+            
+            //HFAC_Relay = 1;
+            wait_ms(100);
+            charge=0;         
+        }
+        if (chargeLEDCheck){
+            DfebCharged = true;
+        }
+        //Check Shock button status 
+        if (!shockButton) {
+            //pc.printf("shock");
+            wait_ms(50);
+            if (!shockButton){
+                led4 = 1;
+                led3 = 0;
+                shock = 1;
+                wait_ms(300);
+                            
+                M_Relay_B = 1;
+                wait_ms(1);
+                M_Relay_B = 0;
+                
+                HFAC_Relay = 1;
+                if (syncActivate){
+                    heartRateTimer.stop();
+                    heartRateTimer.reset(); 
+                    heartRateTimerStart = false;
+                    heartRate = 0;
+                    heartMeanCounter = 0;
+                    heartRateTemp = 0; 
+                    syncShock = true;
+                }
+                else{
+                    if (byPassStatus)
+                        Delivering (HFAC_Amplitude, HFAC_Duration);
+                    else
+                        Delivering (10, 100);
+             
+                    wait_ms(1000);
+                }
+            }
+        }
+        //Check if there is a message from LCD
+        // The LCD message is in format of: 'A' + 2 bytes  or 'B' + 2 bytes 
+        // 'C' and 'D' for Activiate or Deactivate the sync mode ( Shock on R wave)
+        if(LCD.readable()) {
+               
+            temp = LCD.getc();
+            led2 = !led2;
+            if (temp == 0x43)
+                syncActivate = true;
+            else if (temp == 0x44)
+                syncActivate = false;
+            
+                
+            if ((temp == 0x41 || temp == 0x42)&& !LCD_Counter){
+                LCD_Source =  temp;
+                LCD_Counter ++;
+            }
+            else if (LCD_Counter == 1){
+                LCD_Counter ++;
+                LCD_Message = temp;
+            }
+            else if (LCD_Counter == 2){
+                LCD_Counter ++;
+                LCD_Message = LCD_Message + (temp << 8);
+                LCD_Counter = 0;
+                if (LCD_Source == 0x41){
+                    HFAC_Duration = LCD_Message; 
+                    pc.printf("HF %d\n", HFAC_Duration);
+                }
+                else if (LCD_Source == 0x42){
+                    HFAC_Amplitude = LCD_Message ;
+                    pc.printf("AM %d\n", HFAC_Amplitude);
+                }
+            }
+            
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Jul 02 17:55:54 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file