weeb grammers / Mbed 2 deprecated Assignment2_ver6

Dependencies:   MotionSensor mbed

Fork of Assignment2_ver5 by weeb grammers

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hardware.cpp Source File

Hardware.cpp

00001 #include "Hardware.h"
00002 #include "mbed.h"
00003 
00004 #define LOW  0
00005 #define HIGH 1
00006 
00007 //#include "pinmap.h"
00008 //#include "FXOS8700Q.h"
00009 
00010 ////////////////////////////////////////////////////////
00011 //FXOS8700Q_acc acc(PTE25, PTE24, FXOS8700CQ_SLAVE_ADDR1);
00012 
00013 /* Constants and Declares */
00014 //int numCount;
00015 //int const MAX_NUM_COUNTS = 3;
00016 //int const TIMING_PERIOD = 20; // Sensor polling interval
00017 
00018 //uint8_t motion_exceeded_threshold = 0;
00019 ///////////////////////////////////////////
00020 DigitalOut output_pin_A(LED3);
00021 DigitalOut output_pin_V(LED2);
00022 /*=== Analog In ===*/
00023 AnalogIn atrialIn           (A0);      // Pin A0
00024 AnalogIn ventricleIn        (A1);      // Pin A1
00025 AnalogIn leadImpedence      (A2);      // Pin A2
00026 AnalogIn atr_rect_signal    (A3);      // Pin A3
00027 AnalogIn vent_rect_signal   (A4);      // Pin A4
00028 
00029 /*=== Digital In ===*/
00030 DigitalIn atria_cmp_detect  (PTC16);   // Pin D0
00031 DigitalIn vent_cmp_detect   (PTC17);   // Pin D1
00032 
00033 /*=== PWM Out ===*/
00034 /* DigitalOut is used for the REF Signal
00035  * due to absence of PWM capabilities of Pins D2 and D4
00036  * on the FRDM-K64F Board
00037  */
00038 DigitalOut pacing_ref_pwm   (PTB9);    // Pin D2 (PTB9)
00039 PwmOut vent_ref_pwm         (PTA1);    // Pin D3
00040 DigitalOut atria_ref_pwm    (PTB23);   // Pin D4
00041 
00042 
00043 /*=== Digital Out ===*/
00044 DigitalOut pace_charge_ctrl (PTA2);    // Pin D5
00045 DigitalOut z_atria_ctrl     (PTC2);    // Pin D6
00046 DigitalOut z_vent_ctrl      (PTC3);    // Pin D7
00047 
00048 DigitalOut atr_pace_ctrl    (PTC12);   // Pin D8
00049 DigitalOut vent_pace_ctrl   (PTC4);    // Pin D9
00050 DigitalOut pace_grnd_ctrl   (PTD0);    // Pin D10
00051 DigitalOut atr_grnd_ctrl    (PTD2);    // Pin D11
00052 DigitalOut vent_grnd_ctrl   (PTD3);    // Pin D12
00053 DigitalOut frontend_ctrl    (PTD1);    // Pin D13
00054 
00055 /*=== On-Board Tri-LED ===*/
00056 DigitalOut rled (LED_RED);
00057 DigitalOut gled (LED_GREEN);
00058 DigitalOut bled (LED_BLUE);
00059 
00060 
00061 void send_data(Serial &pc,PaceHeart &Pacer){
00062     
00063              pc.printf ("%c",(char)Pacer.get_p_pacingState());
00064              pc.printf ("%c",(char)Pacer.get_p_pacingMode());
00065              pc.printf ("%c",(char)Pacer.get_p_hysteresis());
00066              pc.printf ("%c%c",(char)((int)(Pacer.get_p_hysteresisInterval()/128)),(char)(Pacer.get_p_hysteresisInterval()%128));
00067              pc.printf ("%c%c",(char)((int)(Pacer.get_p_lowrateInterval()/128)),(char)((int)(Pacer.get_p_lowrateInterval())%128));
00068              pc.printf ("%c%c",(char)((int)(Pacer.get_p_vPaceAmp())/128),(char)((int)(Pacer.get_p_vPaceAmp())%128));
00069              pc.printf ("%c%c",(char)((int)(10.0*Pacer.get_p_vPaceWidth())/128),(char)((int)(10.0*Pacer.get_p_vPaceWidth())%128));//10*pace width
00070              pc.printf ("%c%c\n",(char)((int)(Pacer.get_p_VRP()/128)),(char)(Pacer.get_p_VRP()%128));
00071              return;
00072 }
00073 void request_data(Serial &pc,PaceHeart &Pacer){ //implement limit later
00074             char d[14];
00075             int e = 0;
00076             pc.scanf("%s",&d);
00077             pc.printf("%c",'e');//end
00078             e = (int)d[0];
00079             //Pacer.set_p_pacingState(e);
00080             e = (int)d[1];
00081            // Pacer.set_p_pacingMode(e);
00082             e = (int)d[2];
00083             Pacer.set_p_hysteresis(e);
00084             e = (int)d[3]*128+(int)d[4];
00085             Pacer.set_p_hysteresisInterval(e);
00086             e = (int)d[5]*128+(int)d[6];
00087             Pacer.set_p_lowrateInterval(e);
00088             e = (int)d[7]*128+(int)d[8];
00089             Pacer.set_p_vPaceAmp((double)e);
00090             e = (int)d[9]*128+(int)d[10];
00091             Pacer.set_p_vPaceWidth((double)e/10.0);
00092             e = (int)d[11]*128+(int)d[12];
00093             Pacer.set_p_VRP(e);
00094 }
00095 
00096 
00097 void stream_A(Serial &pc){
00098     int amp = (int)(3450*vent_rect_signal);
00099     pc.printf("%c%c\n",(char)(amp/128),(char)(amp%128));
00100 }
00101 
00102 void stream_V(Serial &pc){
00103     int amp = (int)(3450*ventricleIn);
00104     pc.printf("%c%c\n",(char)(amp/128),(char)(amp%128));
00105 }
00106 
00107 void stream(Serial &pc, char mode){
00108         char c;
00109     while(1){
00110         if(pc.readable()!=0){
00111                 c = pc.getc();
00112                 if(c=='q'){
00113                     stream_A(pc);
00114                     output_pin_V=!output_pin_V;
00115                 }
00116                 else if(c=='w'){
00117                 return;
00118                 }
00119             } 
00120         }
00121 }
00122 
00123 void Check_serial(Serial &pc,PaceHeart &Pacer){
00124             char c = pc.getc();
00125             if(c=='r'){//send params
00126                 send_data(pc,Pacer);
00127                 }
00128             else if(c=='s') {//set params
00129                 request_data(pc,Pacer);
00130             }
00131             else if(c=='q'){ //stream egram
00132                 stream(pc,c);
00133             }
00134 }
00135 
00136 
00137 void Output_A(double amp, double wid){
00138     vent_ref_pwm = 0.5f;
00139     vent_ref_pwm.pulsewidth(2);
00140     /*AnalogOut aout(p18);  //sets up pin 18 as an analogue output
00141         AnalogueIn ain(p15);  //sets up pin 15 and an analogue input
00142  
00143         int main(){
00144              aout=0.5;  //sets output to 0.5*VCC
00145             while(1){    //sets up a loop
00146              if (ain>0.3){   //tests whether the input is above 0.3
00147             aout=0;  //sets the output to 0
00148       }
00149       aout = 0.5;
00150       wait(1);
00151       aout = 0;*/
00152       // test the voltage on the initialized analog pin
00153         //  and if greater than 0.3 * VCC set the digital pin
00154         //  to a logic 1 otherwise a logic 0
00155 }
00156 void Output_V(double amp,double wid){
00157     //vent_ref_pwm = 0.5f;
00158     //vent_ref_pwm.pulsewidth(2);
00159     atr_grnd_ctrl  = LOW;
00160     vent_grnd_ctrl = LOW;
00161     
00162     /* Stage 1: Ref PWM On */
00163     pacing_ref_pwm = HIGH;
00164     wait_ms(10);
00165 
00166     /* Stage 2: Charge CTRL on */
00167     pace_charge_ctrl = HIGH;
00168     wait_ms(10);
00169     
00170     vent_pace_ctrl = HIGH;
00171     
00172     // Pace Duration
00173     wait_us(wid/10);
00174     
00175     // Shut off Pace
00176     vent_pace_ctrl = LOW;
00177     
00178     atr_pace_ctrl =  LOW;
00179     vent_pace_ctrl = LOW;
00180     atr_grnd_ctrl  = HIGH;
00181     vent_grnd_ctrl = HIGH;
00182     wait_ms(50);
00183     
00184 
00185     /* Stage 2: Ref PWM LOW */
00186     pacing_ref_pwm = LOW;
00187     wait_ms(100);
00188 
00189     /* Stage 3: Charge CTRL off */
00190     pace_charge_ctrl = LOW;
00191     wait_ms(10);
00192     
00193     /* Stage 4: Ground OFF */
00194     atr_grnd_ctrl  = LOW;
00195     vent_grnd_ctrl = LOW;
00196     
00197     output_pin_V=!output_pin_V;
00198     /*AnalogOut aout(p18);  //sets up pin 18 as an analogue output
00199         AnalogueIn ain(p15);  //sets up pin 15 and an analogue input
00200  
00201         int main(){
00202              aout=0.5;  //sets output to 0.5*VCC
00203             while(1){    //sets up a loop
00204              if (ain>0.3){   //tests whether the input is above 0.3
00205             aout=0;  //sets the output to 0
00206       }
00207       aout = 0.5;
00208       wait(1);
00209       aout = 0;*/
00210 }
00211 ///////////////////////////////////////////////////////////////////////////////////////////////
00212 
00213 /*void initialize_motion () {
00214     acc.enable();
00215 }
00216 
00217 bool isMotionThresholdExceeded () {
00218     return motion_exceeded_threshold;    
00219 }
00220 
00221 void resetMotionDetection () {
00222     motion_exceeded_threshold = 0;
00223 }
00224 
00225 /**** Function: a_count
00226  * return: void
00227  * parameters: none
00228  * A function called if motion detection interrupt flag is set.  Maintains
00229  * a global counter and sets a timer to keep track of number of flags within
00230  * timing limit.
00231  */ 
00232 /*void a_count(void) {
00233     /* step 1 increment the counter 
00234     numCount++;
00235     
00236 
00237     if (numCount >= MAX_NUM_COUNTS) {
00238         rled = !rled;   // toggle LEDs to show acceleration threshold reached
00239         gled = !gled;   // toggle LEDS to show acceleration threshold reached
00240         
00241         motion_exceeded_threshold = 1;
00242     }
00243 }
00244 
00245 void motion_thread () {
00246     while(true) {
00247 
00248         float xAcc, yAcc, zAcc;
00249         acc.getX(&xAcc);
00250         acc.getY(&yAcc);
00251         acc.getZ(&zAcc);
00252         float magtd = xAcc*xAcc + yAcc*yAcc + zAcc*zAcc;
00253         
00254         if (magtd > 3.0f) { // Greater than (1.5G of Accel.)^2
00255             a_count();      // increment acceleration event counter
00256         }
00257 
00258     }   
00259 }*/