stephen mathenge / Mbed OS Level_estimation_Maesurement

Dependencies:   Cayenne-LPP SDBlockDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dust_sensor.h Source File

dust_sensor.h

00001 #ifndef _DUST_SENSOR_H
00002 #define _DUST_SENSOR_H
00003 
00004 #include "mbed.h"
00005 
00006 class DustSensor : public NonCopyable<DustSensor> {
00007 public:
00008     DustSensor(PinName pin) : _input(pin), _cb(NULL), _busy(false) {
00009 
00010     }
00011 
00012     bool measure(Callback<void(int, float, float)> cb) {
00013         if (_busy) {
00014             return false;
00015         }
00016 
00017         _cb = cb;
00018 
00019         _busy = true;
00020 
00021         m.attach(callback(this, &DustSensor::calculate), 30.0f);
00022         t.reset(); // reset mesurement every 30 seconds
00023         _input.fall(callback(this, &DustSensor::start)); // start measuring when signal is low
00024         _input.rise(callback(this, &DustSensor::stop));  // stop measuring when signal is high
00025 
00026         return true;
00027     }
00028 
00029     bool is_busy() {
00030         return _busy;
00031     }
00032 
00033 private:
00034     // Start Timer
00035     void start(void) {
00036         t.start();
00037     }
00038 
00039     // Stop Timer
00040     void stop(void) {
00041         t.stop();
00042     }
00043 
00044     // calculate sensor value
00045     void calculate() {
00046         // run measurements
00047         int lpo = t.read_us();
00048         float ratio = t.read_us() / (30000 * 10.0);
00049         float concentration = 1.1 * pow(ratio,3) -3.8 * pow(ratio, 2) + 520 * ratio + 0.62;
00050         _cb(lpo, ratio, concentration);
00051 
00052         _busy = false;
00053     }
00054 
00055     InterruptIn _input;
00056     Callback<void(int, float, float)> _cb;
00057 
00058     bool _busy;
00059 
00060     Timer t;                    // measure amount of time signal is low
00061     Timeout m;                   // every 30 seconds calculate measurement
00062 };
00063 
00064 #endif // _DUST_SENSOR_H