Hopkins (Henry) / Mbed 2 deprecated DFEB_New_Sync

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "Functions.h"
00002 
00003 
00004 DigitalIn shockButton(p16);
00005 DigitalIn chargeButton(p17);
00006 DigitalIn byPassButton(p32);
00007 
00008 //sync IN, ECG R wave In pin
00009 InterruptIn SyncIn(p14);
00010 //InterruptIn byPassButton(p15);
00011 
00012 //shock signal to defeb
00013 DigitalOut shock(p26);
00014 //charge signal to defeb
00015 DigitalOut charge(p25);
00016 
00017 //charge signal from defeb
00018 DigitalIn chargeLEDCheck(p30);
00019 
00020 // Relay to power Amplifier 
00021 DigitalOut HFAC_Relay(p5);
00022 //Middle Relay off
00023 DigitalOut M_Relay_B(p6);
00024 //Middle Relay On
00025 DigitalOut M_Relay(p7);
00026 // Resistor Relay : No need any more
00027 DigitalOut R_Relay(p8);
00028 
00029 //Sync out to dfeb to deliver the shock 
00030 DigitalOut SyncOut(p20);
00031 
00032 //HFAC  Analog out put
00033 AnalogOut HFAC(p18);
00034 
00035 //Test leds
00036 DigitalOut led1(LED1);
00037 DigitalOut led2(LED2);
00038 DigitalOut led3(LED3);
00039 DigitalOut led4(LED4);
00040 
00041 //Shock button Leds
00042 DigitalOut shockRedLED(p34);
00043 DigitalOut shockGreenLED(p33);
00044 
00045 //Charge button Leds
00046 DigitalOut chargeRedLED(p39);
00047 DigitalOut chargeGreenLED(p38);
00048 
00049 // serial link for LCD
00050 Serial LCD(p9, p10);
00051 
00052 // serial link for PC
00053 Serial pc(USBTX, USBRX);
00054 
00055 uint16_t sample = 0;
00056 
00057 char temp;
00058 int HFAC_Duration = 1300;
00059 int HFAC_Amplitude = 100;
00060 int LCD_Counter = 0;
00061 int LCD_Message = 0;
00062 int LCD_Source = 0;
00063 
00064 bool byPassOldStatus = false;
00065 bool byPassStatus = false;
00066 
00067 //Timer for calculating the time bewteen each R wave signal
00068 Timer heartRateTimer;
00069 //TimeOut Timer for deleviring shock after detecting "HeartRateAvg + 1" R wave signal
00070 Timeout deliverTimeout;
00071 
00072 bool heartRateTimerStart = false;
00073 int heartRate = 0;
00074 int heartMeanCounter = 0;
00075 int heartRateTemp = 0;
00076 
00077 bool syncActivate = false;
00078 bool syncShock = false;
00079 
00080 bool DfebCharged = false;
00081 
00082 int main()
00083 {
00084 
00085     ////////init//////////////
00086     LCD.baud(115200);
00087     shockButton.mode(PullUp);
00088     chargeButton.mode(PullUp);
00089     byPassButton.mode(PullUp);
00090     chargeLEDCheck.mode(PullDown);
00091     
00092     SyncIn.mode(PullDown);
00093     
00094  //   byPassButton.rise(&checkByPass);
00095  //   byPassButton.fall(&checkByPass);
00096     
00097     M_Relay = 1;
00098     wait_ms(1);
00099     M_Relay = 0;
00100     HFAC_Relay = 0;
00101     R_Relay = 0;
00102     // Make the HFAC analog to VDD/2
00103     HFAC.write_u16(offset);
00104 
00105     led1 = 1;
00106     SyncOut =0;
00107     wait_ms(200);
00108     
00109     checkByPass();
00110     
00111     SyncIn.rise(&syncInFunc);
00112     SyncIn.fall(&syncInFuncFall);
00113 
00114     
00115     while(1) {
00116         //Check if the bypass  status has changed 
00117         if (byPassOldStatus != byPassButton){
00118             wait_ms(200);
00119             checkByPass();
00120         } 
00121         
00122         byPassOldStatus = byPassButton;
00123         
00124         //Check Charge button status 
00125         if (!chargeButton){
00126             charge =1; 
00127             led3 = 1;
00128             led4 = 0; 
00129         
00130             //R_Relay = 1;
00131             
00132             //M_Relay_B = 1;
00133             //wait_ms(1);
00134             //M_Relay_B = 0;
00135             
00136             //HFAC_Relay = 1;
00137             wait_ms(100);
00138             charge=0;         
00139         }
00140         if (chargeLEDCheck){
00141             DfebCharged = true;
00142         }
00143         //Check Shock button status 
00144         if (!shockButton) {
00145             //pc.printf("shock");
00146             wait_ms(50);
00147             if (!shockButton){
00148                 led4 = 1;
00149                 led3 = 0;
00150                 shock = 1;
00151                 wait_ms(300);
00152                             
00153                 M_Relay_B = 1;
00154                 wait_ms(1);
00155                 M_Relay_B = 0;
00156                 
00157                 HFAC_Relay = 1;
00158                 if (syncActivate){
00159                     heartRateTimer.stop();
00160                     heartRateTimer.reset(); 
00161                     heartRateTimerStart = false;
00162                     heartRate = 0;
00163                     heartMeanCounter = 0;
00164                     heartRateTemp = 0; 
00165                     syncShock = true;
00166                 }
00167                 else{
00168                     if (byPassStatus)
00169                         Delivering (HFAC_Amplitude, HFAC_Duration);
00170                     else
00171                         Delivering (10, 100);
00172              
00173                     wait_ms(1000);
00174                 }
00175             }
00176         }
00177         //Check if there is a message from LCD
00178         // The LCD message is in format of: 'A' + 2 bytes  or 'B' + 2 bytes 
00179         // 'C' and 'D' for Activiate or Deactivate the sync mode ( Shock on R wave)
00180         if(LCD.readable()) {
00181                
00182             temp = LCD.getc();
00183             led2 = !led2;
00184             if (temp == 0x43)
00185                 syncActivate = true;
00186             else if (temp == 0x44)
00187                 syncActivate = false;
00188             
00189                 
00190             if ((temp == 0x41 || temp == 0x42)&& !LCD_Counter){
00191                 LCD_Source =  temp;
00192                 LCD_Counter ++;
00193             }
00194             else if (LCD_Counter == 1){
00195                 LCD_Counter ++;
00196                 LCD_Message = temp;
00197             }
00198             else if (LCD_Counter == 2){
00199                 LCD_Counter ++;
00200                 LCD_Message = LCD_Message + (temp << 8);
00201                 LCD_Counter = 0;
00202                 if (LCD_Source == 0x41){
00203                     HFAC_Duration = LCD_Message; 
00204                     pc.printf("HF %d\n", HFAC_Duration);
00205                 }
00206                 else if (LCD_Source == 0x42){
00207                     HFAC_Amplitude = LCD_Message ;
00208                     pc.printf("AM %d\n", HFAC_Amplitude);
00209                 }
00210             }
00211             
00212         }
00213     }
00214 
00215 }