Breath

Dependencies:   NeoStrip mbed

Fork of Breath by Ryan Williams

Revision:
1:86d9b36d9132
Parent:
0:397523d4133e
--- a/windSensor.cpp	Thu Dec 01 00:42:08 2016 +0000
+++ b/windSensor.cpp	Thu Dec 01 03:04:38 2016 +0000
@@ -1,12 +1,16 @@
 #include "windSensor.h"
+#include "MPL3115.h"
 
-windSensor :: windSensor(PinName p, NeoStrip *_strip)
-    : sensor(p), strip(_strip)
+windSensor :: windSensor(PinName p1, NeoStrip *_strip, PinName p2, PinName p3, int addr)
+    : sensor(p1), strip(_strip), barometer(p2, p3, addr)
 {
     thresh = sensor.read()*1.4;
     isBreathing = false;
     breathCount = 0;
-    strip->setBrightness(0.1);
+    spike = false;
+    barometer.config();
+    //strip->setBrightness(0.1);
+
 }
 
 void windSensor::startReading()
@@ -37,6 +41,11 @@
     return isBreathing; //only return positive if we get the critical number of Breaths in the given time window
 }
 
+//call this member function to see if there is a pressure spike
+bool windSensor::pressureSpike()
+{
+    return spike; //return true if spike occured during give_breath. 
+}
 
 // THIS IS THE TIMER 2 INTERRUPT SERVICE ROUTINE.
 // Timer 2 makes sure that we take a reading every 2 miliseconds
@@ -66,21 +75,27 @@
 
 float windSensor::give_breath(void)
 {
-    strip->initialize();
+    spike = false;
+    //strip->initialize();
     c.clear();
     t.reset();
     q.reset();
-    //float a = 0.0;
+    float base = 0.0;
     float d = 0.0; //return time for fractional breathes
     float e = 0.0;
     int i;
     float thresh;
-    /*for(i = 0; i<20; i++) {
-        float val = sensor.read();
-        a = a + val;
+    t.start();
+    for(i = 0; i<10; i++) {
+        float tempPress = barometer.getPressure();
+        base = base + tempPress;
         //printf("%12.5f\n",val);
     }
-    a = a/20.0; //get first baseline reading
+    t.stop();
+    printf("%f\n", t.read());
+    t.reset();
+    base = base/10.0; //get first pressure baseline reading
+    printf("Baseline: %f\n", base);
     //thresh = a*3.0; //theshold to start breathe timer (ideal multiplier is between 2-3)*/
     thresh = 0.63;
     //printf("thresh:%f\n",thresh);
@@ -89,6 +104,7 @@
         if(t.read() <= 3.0) {
             wait(0.1); //necessary to give sensor time to be polled
             float val = sensor.read();
+            val = 0.7; //testing, delete after
             //printf("%15.5f\n",val);
             if(val > thresh) {
                 //printf("Made it here\n");
@@ -100,8 +116,10 @@
                     e = e + c[i];
                 }
                 e = e/10.0; //provides slope of 10 most recent samples
+                e = 1; //erase
                 float oldTimerVal = 0;
                 float timerVal = q.read();
+                printf("Start Breath");
                 while( (e > thresh) && (timerVal < 1.0)) {
 
                     pop_front(c);//remove the oldest element
@@ -110,16 +128,23 @@
                         e = e + c[i];
                     }
                     e = e/10.0; //provides slope of 10 most recent samples
+                    e = 1; //erase later
                     timerVal = q.read();
                     if(timerVal >= oldTimerVal + 0.25){
-                        strip->progress(timerVal);
+                        //strip->progress(timerVal);
                         oldTimerVal = timerVal;
                     }
+                    float pressure = barometer.getPressure();
+                    printf("Pressure: %f\n", pressure);
+                    if(pressure < .96*base){
+                        spike = true;
+                        printf("SPIKE");
+                    }
                 }
                 d = q.read();
                 if(q.read() > 1.0) { //breathe was greater than thresh and lasted more than 1 sec
                     t.stop(); q.stop();
-                    strip->progress(1);
+                    //strip->progress(1);
                     return 1.0; //full breath
                 } else {
                     t.stop(); q.stop();