Shinnyei PPD42NJ particles sensor reading

Dependents:   PwmReaderTest

Library to read values out of a Shinyei PPD42NJ particles sensor

Files at this revision

API Documentation at this revision

Comitter:
abouillot
Date:
Thu Jan 26 15:07:26 2017 +0000
Commit message:
Initial commit

Changed in this revision

Shinyei.cpp Show annotated file Show diff for this revision Revisions of this file
Shinyei.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 995d42a37328 Shinyei.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Shinyei.cpp	Thu Jan 26 15:07:26 2017 +0000
@@ -0,0 +1,65 @@
+/*!
+ * Shinyei.cpp
+ *
+ * Read the particle concentration using a Shinyei PPD42NJ sensor
+ *
+ * Copyright (c) 2017 -  Alexandre Bouillot github.com/abouillot
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documnetation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to  whom the Software is
+ * furished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+ 
+#include "Shinyei.h"
+
+void Shinyei::startSampling(int duration)
+{
+    _samplingTimer.attach(callback(this, &Shinyei::samplingComplete), (float)duration);
+    _dataReady = false;
+    _callbackSet = false;
+    _pwm.start();
+}
+
+void Shinyei::startSampling(Callback<void()> function, int duration)
+{
+    _samplingTimer.attach(callback(this, &Shinyei::samplingComplete), (float)duration);
+    _dataReady = false;
+    _callbackSet = true;
+    _callback = function;
+    _pwm.start();
+}
+
+
+void Shinyei::stopSampling()
+{
+    _pwm.stop();
+//    _callbackSet = false;
+// Need to cancel the timer
+//    _samplingTimer.attach(callback(this, &samplingComplete), (float)duration);
+    _dataReady = false;
+    _pwm.start();
+}
+
+void Shinyei::samplingComplete()
+{
+    _pwm.stop();
+    float ratio = _pwm.occupacyLow();
+    _concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
+    _dataReady = true;
+    if (_callbackSet)
+        _callback();
+}
\ No newline at end of file
diff -r 000000000000 -r 995d42a37328 Shinyei.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Shinyei.h	Thu Jan 26 15:07:26 2017 +0000
@@ -0,0 +1,56 @@
+/*!
+ * Shinyei.cpp
+ *
+ * Read the particle concentration using a Shinyei PPD42NJ sensor
+ *
+ * Copyright (c) 2017 -  Alexandre Bouillot github.com/abouillot
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documnetation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to  whom the Software is
+ * furished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+ 
+#include "mbed.h"
+#include "PwmReader.h"
+
+class Shinyei
+{
+public:
+    Shinyei(PinName pin) : _pwm(pin), _dataReady(false) {
+    };
+    void startSampling(int duration = 30);
+    void startSampling(Callback< void()>, int duration = 30);
+    void stopSampling();
+    bool dataReady() {
+        return _dataReady;
+    };
+    float concentration() {
+        if (_dataReady)
+            return _concentration;
+        else
+            return -1;
+    };
+private:
+    void samplingComplete();
+    void init();
+    PwmReader _pwm;
+    bool _dataReady;
+    Timeout _samplingTimer;
+    float _concentration;
+    Callback<void()> _callback;
+    bool _callbackSet;
+};
\ No newline at end of file