breath sensor library

Files at this revision

API Documentation at this revision

Comitter:
otis22894
Date:
Sun Dec 11 21:12:18 2016 +0000
Parent:
0:397523d4133e
Commit message:
First commit

Changed in this revision

windSensor.cpp Show annotated file Show diff for this revision Revisions of this file
windSensor.h Show annotated file Show diff for this revision Revisions of this file
diff -r 397523d4133e -r d07f80ef7abc windSensor.cpp
--- a/windSensor.cpp	Thu Dec 01 00:42:08 2016 +0000
+++ b/windSensor.cpp	Sun Dec 11 21:12:18 2016 +0000
@@ -1,17 +1,31 @@
 #include "windSensor.h"
 
-windSensor :: windSensor(PinName p, NeoStrip *_strip)
-    : sensor(p), strip(_strip)
+windSensor :: windSensor(PinName p, NeoStrip *_strip, PinName p2, PinName p3)
+    : sensor(p), strip(_strip), barometer(p2, p3, (0x60<<1))
 {
-    thresh = sensor.read()*1.4;
+    //thresh = sensor.read()*1.4;
+    thresh = 0.63;
     isBreathing = false;
     breathCount = 0;
     strip->setBrightness(0.1);
+    strip->clear();
+    spike = false;
+    barometer.config();
+    wait(0.1);
+    int i; 
+    float base = 0.0;
+    for(i = 0; i<10; i++) {
+        float tempPress = barometer.getPressure();
+        base = base + tempPress;
+        //printf("%12.5f\n",val);
+    }
+    base = base/10.0; //get first pressure baseline reading
+    pressure_threshold = 0.96*base;
 }
 
 void windSensor::startReading()
 {
-    breathReader.attach(this, &windSensor::sample, .002f);
+    breathReader.attach(this, &windSensor::sample, .02f);
     //volatile float a = sensor;
     //printf("\nwindsensor reads: %f", a);
     //printf("\nthresh is: %f", thresh);
@@ -44,11 +58,11 @@
 {
     __disable_irq();
     float readVal = sensor.read();                          // sample
-
-
+    //printf("%f\n",readVal);
     //check to see if victim is breathing at all above natural variation in sensor noise
     if(readVal >= thresh) {
         breathCount++;                         //increment global counter
+        printf("Found breath");
     }
     if(breathCount >= 3) { //should set this higher if want more certainty in victim breathing
         isBreathing = true;
@@ -66,11 +80,13 @@
 
 float windSensor::give_breath(void)
 {
+    spike = false;
     strip->initialize();
     c.clear();
     t.reset();
     q.reset();
     //float a = 0.0;
+    
     float d = 0.0; //return time for fractional breathes
     float e = 0.0;
     int i;
@@ -100,7 +116,7 @@
                     e = e + c[i];
                 }
                 e = e/10.0; //provides slope of 10 most recent samples
-                float oldTimerVal = 0;
+                float oldTimerVal = 0, pressure;
                 float timerVal = q.read();
                 while( (e > thresh) && (timerVal < 1.0)) {
 
@@ -115,6 +131,14 @@
                         strip->progress(timerVal);
                         oldTimerVal = timerVal;
                     }
+                    
+                    pressure = barometer.getPressure();
+                    //printf("Pressure: %f\n", pressure);
+                    if(pressure < pressure_threshold){
+                        spike = true;
+                        //printf("SPIKE");
+                    }
+                    
                 }
                 d = q.read();
                 if(q.read() > 1.0) { //breathe was greater than thresh and lasted more than 1 sec
@@ -131,7 +155,12 @@
             return 0.0; //no breath in 3 seconds
         }
     }
+}
 
+//call this member function to see if there is a pressure spike
+bool windSensor::pressureSpikeDetected()
+{
+    return spike; //return true if spike occured during give_breath. 
 }
 
 
diff -r 397523d4133e -r d07f80ef7abc windSensor.h
--- a/windSensor.h	Thu Dec 01 00:42:08 2016 +0000
+++ b/windSensor.h	Sun Dec 11 21:12:18 2016 +0000
@@ -1,10 +1,11 @@
 #include "mbed.h"
 #include "NeoStrip.h"
+#include "MPL3115.h"
 #include <vector>
 
 class windSensor {
 public:
-    windSensor(PinName p, NeoStrip *_strip);
+    windSensor(PinName p, NeoStrip *_strip, PinName p2, PinName p3);
     /* Start victim breath */
     void sample();
     void startReading();
@@ -15,6 +16,7 @@
     
     /* start give breath */
     float give_breath(void);
+    bool pressureSpikeDetected();
     /* end give breath */
     
 private:
@@ -34,4 +36,7 @@
     /* end give breath */
     vector<float> c;
     NeoStrip *strip;
+    MPL3115 barometer;
+    bool spike;
+    float pressure_threshold;
 };
\ No newline at end of file