Level measurement using range finder and lora technology
Dependencies: Cayenne-LPP SDBlockDevice
inc/dust_sensor.h@0:f930f0440fd5, 2019-06-26 (annotated)
- Committer:
- wamae
- Date:
- Wed Jun 26 10:35:50 2019 +0000
- Revision:
- 0:f930f0440fd5
better copy
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wamae | 0:f930f0440fd5 | 1 | #ifndef _DUST_SENSOR_H |
wamae | 0:f930f0440fd5 | 2 | #define _DUST_SENSOR_H |
wamae | 0:f930f0440fd5 | 3 | |
wamae | 0:f930f0440fd5 | 4 | #include "mbed.h" |
wamae | 0:f930f0440fd5 | 5 | |
wamae | 0:f930f0440fd5 | 6 | class DustSensor : public NonCopyable<DustSensor> { |
wamae | 0:f930f0440fd5 | 7 | public: |
wamae | 0:f930f0440fd5 | 8 | DustSensor(PinName pin) : _input(pin), _cb(NULL), _busy(false) { |
wamae | 0:f930f0440fd5 | 9 | |
wamae | 0:f930f0440fd5 | 10 | } |
wamae | 0:f930f0440fd5 | 11 | |
wamae | 0:f930f0440fd5 | 12 | bool measure(Callback<void(int, float, float)> cb) { |
wamae | 0:f930f0440fd5 | 13 | if (_busy) { |
wamae | 0:f930f0440fd5 | 14 | return false; |
wamae | 0:f930f0440fd5 | 15 | } |
wamae | 0:f930f0440fd5 | 16 | |
wamae | 0:f930f0440fd5 | 17 | _cb = cb; |
wamae | 0:f930f0440fd5 | 18 | |
wamae | 0:f930f0440fd5 | 19 | _busy = true; |
wamae | 0:f930f0440fd5 | 20 | |
wamae | 0:f930f0440fd5 | 21 | m.attach(callback(this, &DustSensor::calculate), 30.0f); |
wamae | 0:f930f0440fd5 | 22 | t.reset(); // reset mesurement every 30 seconds |
wamae | 0:f930f0440fd5 | 23 | _input.fall(callback(this, &DustSensor::start)); // start measuring when signal is low |
wamae | 0:f930f0440fd5 | 24 | _input.rise(callback(this, &DustSensor::stop)); // stop measuring when signal is high |
wamae | 0:f930f0440fd5 | 25 | |
wamae | 0:f930f0440fd5 | 26 | return true; |
wamae | 0:f930f0440fd5 | 27 | } |
wamae | 0:f930f0440fd5 | 28 | |
wamae | 0:f930f0440fd5 | 29 | bool is_busy() { |
wamae | 0:f930f0440fd5 | 30 | return _busy; |
wamae | 0:f930f0440fd5 | 31 | } |
wamae | 0:f930f0440fd5 | 32 | |
wamae | 0:f930f0440fd5 | 33 | private: |
wamae | 0:f930f0440fd5 | 34 | // Start Timer |
wamae | 0:f930f0440fd5 | 35 | void start(void) { |
wamae | 0:f930f0440fd5 | 36 | t.start(); |
wamae | 0:f930f0440fd5 | 37 | } |
wamae | 0:f930f0440fd5 | 38 | |
wamae | 0:f930f0440fd5 | 39 | // Stop Timer |
wamae | 0:f930f0440fd5 | 40 | void stop(void) { |
wamae | 0:f930f0440fd5 | 41 | t.stop(); |
wamae | 0:f930f0440fd5 | 42 | } |
wamae | 0:f930f0440fd5 | 43 | |
wamae | 0:f930f0440fd5 | 44 | // calculate sensor value |
wamae | 0:f930f0440fd5 | 45 | void calculate() { |
wamae | 0:f930f0440fd5 | 46 | // run measurements |
wamae | 0:f930f0440fd5 | 47 | int lpo = t.read_us(); |
wamae | 0:f930f0440fd5 | 48 | float ratio = t.read_us() / (30000 * 10.0); |
wamae | 0:f930f0440fd5 | 49 | float concentration = 1.1 * pow(ratio,3) -3.8 * pow(ratio, 2) + 520 * ratio + 0.62; |
wamae | 0:f930f0440fd5 | 50 | _cb(lpo, ratio, concentration); |
wamae | 0:f930f0440fd5 | 51 | |
wamae | 0:f930f0440fd5 | 52 | _busy = false; |
wamae | 0:f930f0440fd5 | 53 | } |
wamae | 0:f930f0440fd5 | 54 | |
wamae | 0:f930f0440fd5 | 55 | InterruptIn _input; |
wamae | 0:f930f0440fd5 | 56 | Callback<void(int, float, float)> _cb; |
wamae | 0:f930f0440fd5 | 57 | |
wamae | 0:f930f0440fd5 | 58 | bool _busy; |
wamae | 0:f930f0440fd5 | 59 | |
wamae | 0:f930f0440fd5 | 60 | Timer t; // measure amount of time signal is low |
wamae | 0:f930f0440fd5 | 61 | Timeout m; // every 30 seconds calculate measurement |
wamae | 0:f930f0440fd5 | 62 | }; |
wamae | 0:f930f0440fd5 | 63 | |
wamae | 0:f930f0440fd5 | 64 | #endif // _DUST_SENSOR_H |