Shinnyei PPD42NJ particles sensor reading

Dependents:   PwmReaderTest

Library to read values out of a Shinyei PPD42NJ particles sensor

Committer:
abouillot
Date:
Thu Jan 26 15:07:26 2017 +0000
Revision:
0:995d42a37328
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
abouillot 0:995d42a37328 1 /*!
abouillot 0:995d42a37328 2 * Shinyei.cpp
abouillot 0:995d42a37328 3 *
abouillot 0:995d42a37328 4 * Read the particle concentration using a Shinyei PPD42NJ sensor
abouillot 0:995d42a37328 5 *
abouillot 0:995d42a37328 6 * Copyright (c) 2017 - Alexandre Bouillot github.com/abouillot
abouillot 0:995d42a37328 7 *
abouillot 0:995d42a37328 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
abouillot 0:995d42a37328 9 * of this software and associated documnetation files (the "Software"), to deal
abouillot 0:995d42a37328 10 * in the Software without restriction, including without limitation the rights
abouillot 0:995d42a37328 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
abouillot 0:995d42a37328 12 * copies of the Software, and to permit persons to whom the Software is
abouillot 0:995d42a37328 13 * furished to do so, subject to the following conditions:
abouillot 0:995d42a37328 14 *
abouillot 0:995d42a37328 15 * The above copyright notice and this permission notice shall be included in
abouillot 0:995d42a37328 16 * all copies or substantial portions of the Software.
abouillot 0:995d42a37328 17 *
abouillot 0:995d42a37328 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
abouillot 0:995d42a37328 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
abouillot 0:995d42a37328 20 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
abouillot 0:995d42a37328 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
abouillot 0:995d42a37328 22 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
abouillot 0:995d42a37328 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
abouillot 0:995d42a37328 24 * THE SOFTWARE.
abouillot 0:995d42a37328 25 */
abouillot 0:995d42a37328 26
abouillot 0:995d42a37328 27 #include "Shinyei.h"
abouillot 0:995d42a37328 28
abouillot 0:995d42a37328 29 void Shinyei::startSampling(int duration)
abouillot 0:995d42a37328 30 {
abouillot 0:995d42a37328 31 _samplingTimer.attach(callback(this, &Shinyei::samplingComplete), (float)duration);
abouillot 0:995d42a37328 32 _dataReady = false;
abouillot 0:995d42a37328 33 _callbackSet = false;
abouillot 0:995d42a37328 34 _pwm.start();
abouillot 0:995d42a37328 35 }
abouillot 0:995d42a37328 36
abouillot 0:995d42a37328 37 void Shinyei::startSampling(Callback<void()> function, int duration)
abouillot 0:995d42a37328 38 {
abouillot 0:995d42a37328 39 _samplingTimer.attach(callback(this, &Shinyei::samplingComplete), (float)duration);
abouillot 0:995d42a37328 40 _dataReady = false;
abouillot 0:995d42a37328 41 _callbackSet = true;
abouillot 0:995d42a37328 42 _callback = function;
abouillot 0:995d42a37328 43 _pwm.start();
abouillot 0:995d42a37328 44 }
abouillot 0:995d42a37328 45
abouillot 0:995d42a37328 46
abouillot 0:995d42a37328 47 void Shinyei::stopSampling()
abouillot 0:995d42a37328 48 {
abouillot 0:995d42a37328 49 _pwm.stop();
abouillot 0:995d42a37328 50 // _callbackSet = false;
abouillot 0:995d42a37328 51 // Need to cancel the timer
abouillot 0:995d42a37328 52 // _samplingTimer.attach(callback(this, &samplingComplete), (float)duration);
abouillot 0:995d42a37328 53 _dataReady = false;
abouillot 0:995d42a37328 54 _pwm.start();
abouillot 0:995d42a37328 55 }
abouillot 0:995d42a37328 56
abouillot 0:995d42a37328 57 void Shinyei::samplingComplete()
abouillot 0:995d42a37328 58 {
abouillot 0:995d42a37328 59 _pwm.stop();
abouillot 0:995d42a37328 60 float ratio = _pwm.occupacyLow();
abouillot 0:995d42a37328 61 _concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // using spec sheet curve
abouillot 0:995d42a37328 62 _dataReady = true;
abouillot 0:995d42a37328 63 if (_callbackSet)
abouillot 0:995d42a37328 64 _callback();
abouillot 0:995d42a37328 65 }