From 1f954a8cbf918142cd37530ff0ef7561457d4949 Mon Sep 17 00:00:00 2001 From: Jon Trulson Date: Wed, 2 Nov 2016 17:38:44 -0600 Subject: [PATCH] ppd42ns: C implementation; C++ wraps C Signed-off-by: Jon Trulson --- examples/c++/ppd42ns.cxx | 37 +++---- examples/c/CMakeLists.txt | 1 + examples/c/ppd42ns.c | 66 ++++++++++++ examples/java/PPD42NSSample.java | 30 +++--- src/ppd42ns/CMakeLists.txt | 14 ++- src/ppd42ns/javaupm_ppd42ns.i | 5 +- src/ppd42ns/jsupm_ppd42ns.i | 3 +- src/ppd42ns/ppd42ns.c | 167 +++++++++++++++++++++++++++++++ src/ppd42ns/ppd42ns.cxx | 108 ++------------------ src/ppd42ns/ppd42ns.h | 82 +++++++++++++++ src/ppd42ns/ppd42ns.hpp | 103 +++++++++---------- src/ppd42ns/ppd42ns_data.h | 44 ++++++++ src/ppd42ns/pyupm_ppd42ns.i | 1 + 13 files changed, 467 insertions(+), 194 deletions(-) create mode 100644 examples/c/ppd42ns.c create mode 100644 src/ppd42ns/ppd42ns.c create mode 100644 src/ppd42ns/ppd42ns.h create mode 100644 src/ppd42ns/ppd42ns_data.h diff --git a/examples/c++/ppd42ns.cxx b/examples/c++/ppd42ns.cxx index 8a2a302c..29446399 100644 --- a/examples/c++/ppd42ns.cxx +++ b/examples/c++/ppd42ns.cxx @@ -33,31 +33,34 @@ int shouldRun = true; void sig_handler(int signo) { - if (signo == SIGINT) - shouldRun = false; + if (signo == SIGINT) + shouldRun = false; } int main () { - signal(SIGINT, sig_handler); + signal(SIGINT, sig_handler); //! [Interesting] - // Instantiate a dust sensor on GPIO pin D8 - upm::PPD42NS* dust = new upm::PPD42NS(8); - upm::dustData data; - cout << "This program will give readings every 30 seconds until you stop it" << endl; - while (shouldRun) - { - data = dust->getData(); - cout << "Low pulse occupancy: " << data.lowPulseOccupancy << endl; - cout << "Ratio: " << data.ratio << endl; - cout << "Concentration: " << data.concentration << endl; - } + // Instantiate a dust sensor on GPIO pin D8 + upm::PPD42NS* dust = new upm::PPD42NS(8); + ppd42ns_dust_data data; + cout << "This program will give readings every 30 seconds until " + << "you stop it" + << endl; + while (shouldRun) + { + data = dust->getData(); + cout << "Low pulse occupancy: " << data.lowPulseOccupancy << endl; + cout << "Ratio: " << data.ratio << endl; + cout << "Concentration: " << data.concentration << endl; + cout << endl; + } //! [Interesting] - cout << "Exiting" << endl; + cout << "Exiting" << endl; - delete dust; - return 0; + delete dust; + return 0; } diff --git a/examples/c/CMakeLists.txt b/examples/c/CMakeLists.txt index 9318fe54..51d34c9c 100644 --- a/examples/c/CMakeLists.txt +++ b/examples/c/CMakeLists.txt @@ -132,6 +132,7 @@ add_example (linefinder) add_example (uln200xa) add_example (mma7660) add_example (buzzer) +add_example (ppd42ns) # Custom examples add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps) diff --git a/examples/c/ppd42ns.c b/examples/c/ppd42ns.c new file mode 100644 index 00000000..013029fe --- /dev/null +++ b/examples/c/ppd42ns.c @@ -0,0 +1,66 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation 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 furnished 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 FOR 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 +#include +#include + +#include + +int shouldRun = true; + +void sig_handler(int signo) +{ + if (signo == SIGINT) + shouldRun = false; +} + + +int main() +{ + signal(SIGINT, sig_handler); + +//! [Interesting] + // Instantiate a dust sensor on GPIO pin D8 + ppd42ns_context dust = ppd42ns_init(8); + + ppd42ns_dust_data data; + printf("This program will give readings every 30 seconds until " + "you stop it\n"); + + while (shouldRun) + { + data = ppd42ns_get_data(dust); + printf("Low pulse occupancy: %d\n", data.lowPulseOccupancy); + printf("Ratio: %f\n", data.ratio); + printf("Concentration: %f\n\n", data.concentration); + } + + printf("Exiting...\n"); + + ppd42ns_close(dust); + +//! [Interesting] + return 0; +} diff --git a/examples/java/PPD42NSSample.java b/examples/java/PPD42NSSample.java index 78d06280..4d5178b9 100644 --- a/examples/java/PPD42NSSample.java +++ b/examples/java/PPD42NSSample.java @@ -24,22 +24,22 @@ public class PPD42NSSample { - public static void main(String[] args) throws InterruptedException { - // ! [Interesting] - // Instantiate a dust sensor on GPIO pin D8 - upm_ppd42ns.PPD42NS dust = new upm_ppd42ns.PPD42NS(8); - upm_ppd42ns.dustData data; + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a dust sensor on GPIO pin D8 + upm_ppd42ns.PPD42NS dust = new upm_ppd42ns.PPD42NS(8); + upm_ppd42ns.ppd42ns_dust_data data; - System.out.println("This program will give readings every 30 seconds until you stop it"); + System.out.println("This program will give readings every 30 seconds until you stop it"); - while (true) { - data = dust.getData(); + while (true) { + data = dust.getData(); - System.out.println("Low pulse occupancy: " + data.getLowPulseOccupancy()); - System.out.println("Ratio: " + data.getRatio()); - System.out.println("Concentration: " + data.getConcentration()); - } - // ! [Interesting] - } + System.out.println("Low pulse occupancy: " + data.getLowPulseOccupancy()); + System.out.println("Ratio: " + data.getRatio()); + System.out.println("Concentration: " + data.getConcentration()); + } + // ! [Interesting] + } -} \ No newline at end of file +} diff --git a/src/ppd42ns/CMakeLists.txt b/src/ppd42ns/CMakeLists.txt index 9c97ba1e..f16272d6 100644 --- a/src/ppd42ns/CMakeLists.txt +++ b/src/ppd42ns/CMakeLists.txt @@ -1,5 +1,9 @@ -set (libname "ppd42ns") -set (libdescription "Ppd42ns dust sensor module") -set (module_src ${libname}.cxx) -set (module_hpp ${libname}.hpp) -upm_module_init("-lrt") +upm_mixed_module_init (NAME ppd42ns + DESCRIPTION "PPD42NS Dust Sensor" + C_HDR ppd42ns.h ppd42ns_data.h + C_SRC ppd42ns.c + CPP_HDR ppd42ns.hpp + CPP_SRC ppd42ns.cxx + CPP_WRAPS_C + REQUIRES mraa) +target_link_libraries(${libnamec} m) diff --git a/src/ppd42ns/javaupm_ppd42ns.i b/src/ppd42ns/javaupm_ppd42ns.i index 2be86f7e..907ca299 100644 --- a/src/ppd42ns/javaupm_ppd42ns.i +++ b/src/ppd42ns/javaupm_ppd42ns.i @@ -1,11 +1,12 @@ %module javaupm_ppd42ns %include "../upm.i" +%include "ppd42ns_data.h" +%include "ppd42ns.hpp" %{ #include "ppd42ns.hpp" %} -%include "ppd42ns.hpp" %pragma(java) jniclasscode=%{ static { @@ -16,4 +17,4 @@ System.exit(1); } } -%} \ No newline at end of file +%} diff --git a/src/ppd42ns/jsupm_ppd42ns.i b/src/ppd42ns/jsupm_ppd42ns.i index 415c4d5a..e0ebe9b0 100644 --- a/src/ppd42ns/jsupm_ppd42ns.i +++ b/src/ppd42ns/jsupm_ppd42ns.i @@ -1,8 +1,9 @@ %module jsupm_ppd42ns %include "../upm.i" +%include "ppd42ns_data.h" +%include "ppd42ns.hpp" %{ #include "ppd42ns.hpp" %} -%include "ppd42ns.hpp" diff --git a/src/ppd42ns/ppd42ns.c b/src/ppd42ns/ppd42ns.c new file mode 100644 index 00000000..f75a688b --- /dev/null +++ b/src/ppd42ns/ppd42ns.c @@ -0,0 +1,167 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Rewritten Based on original C++ driver written by: + * Author: Zion Orent + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation 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 furnished 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 FOR 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 +#include +#include + +#include +#include + +#include "ppd42ns.h" + +// Returns the amount of time it takes a pin to go from HIGH to LOW or +// from LOW to HIGH +static uint32_t ppd42ns_pulse_in(const ppd42ns_context dev, + bool high_low_value); + +ppd42ns_context ppd42ns_init(int pin) +{ + ppd42ns_context dev = + (ppd42ns_context)malloc(sizeof(struct _ppd42ns_context)); + + if (!dev) + return NULL; + + dev->gpio = NULL; + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) + { + printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv); + ppd42ns_close(dev); + return NULL; + } + + // MRAA contexts... + if ( !(dev->gpio = mraa_gpio_init(pin)) ) + { + printf("%s: mraa_gpio_init() failed\n", + __FUNCTION__); + ppd42ns_close(dev); + return NULL; + } + + mraa_gpio_dir(dev->gpio, MRAA_GPIO_IN); + + return dev; +} + +void ppd42ns_close(ppd42ns_context dev) +{ + assert(dev != NULL); + + if (dev->gpio) + mraa_gpio_close(dev->gpio); +} + +ppd42ns_dust_data ppd42ns_get_data(const ppd42ns_context dev) +{ + assert(dev != NULL); + + ppd42ns_dust_data data; + + // in ms, 30 seconds + const int pulse_check_time = 30000; + // loop timer + upm_clock_t max_loop_time; + + unsigned int low_pulse_occupancy = 0; + + upm_clock_init(&max_loop_time); + + do { + low_pulse_occupancy += ppd42ns_pulse_in(dev, 0); + } while (upm_elapsed_ms(&max_loop_time) < pulse_check_time); + + // Store dust data + // Integer percentage 0=>100 + double ratio = (float)low_pulse_occupancy + / ((float)pulse_check_time * 10.0); + + // using spec sheet curve + double concentration = (1.1 * pow(ratio,3)) - (3.8 * pow(ratio, 2)) + + (520 * ratio) + 0.62; + + data.lowPulseOccupancy = low_pulse_occupancy; + data.ratio = ratio; + data.concentration = concentration; + + return data; +} + + +// Mimicking Arduino's pulseIn function +// return how long it takes a pin to go from HIGH to LOW or LOW to HIGH +static uint32_t ppd42ns_pulse_in(const ppd42ns_context dev, + bool high_low_value) +{ + assert(dev != NULL); + + // we run for no more than 1 second at a time + upm_clock_t max_time; + upm_clock_t pulse_time; + uint32_t total_pulse_time = 0; + + upm_clock_init(&max_time); + bool pin_level; + bool is_timing = false; + + do { + pin_level = (bool)mraa_gpio_read(dev->gpio); + + if (!is_timing && pin_level == high_low_value) + { + // level is desired level, but not currently timing + upm_clock_init(&pulse_time); + is_timing = true; + } + else if (is_timing && pin_level != high_low_value) + { + // we started timing, but level changed + total_pulse_time += upm_elapsed_us(&pulse_time); + is_timing = false; + } + else + { + // not timing and/or level is not equal to desired level + // so we "wait". + upm_delay_us(10); + } + } while (upm_elapsed_ms(&max_time) < 1000); // 1 second + + if (is_timing) + { + // if we were still timing when the loop expired, add the + // accumulated time. + total_pulse_time += upm_elapsed_us(&pulse_time); + } + + return total_pulse_time; +} diff --git a/src/ppd42ns/ppd42ns.cxx b/src/ppd42ns/ppd42ns.cxx index 651049c2..72e83714 100644 --- a/src/ppd42ns/ppd42ns.cxx +++ b/src/ppd42ns/ppd42ns.cxx @@ -1,6 +1,7 @@ /* * Author: Zion Orent - * Copyright (c) 2014 Intel Corporation. + * Jon Trulson + * Copyright (c) 2014-2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -25,113 +26,26 @@ #include #include #include -#include -#include -#include -#include -#include + #include "ppd42ns.hpp" using namespace upm; -PPD42NS::PPD42NS(int pin) +PPD42NS::PPD42NS(int pin) : + m_ppd42ns(ppd42ns_init(pin)) { - if ( !(m_gpio = mraa_gpio_init(pin)) ) - { - throw std::invalid_argument(std::string(__FUNCTION__) + - ": mraa_gpio_init() failed, invalid pin?"); - return; - } - - mraa_gpio_dir(m_gpio, MRAA_GPIO_IN); + if (!m_ppd42ns) + throw std::runtime_error(std::string(__FUNCTION__) + + ": ppd42ns_init() failed"); } PPD42NS::~PPD42NS() { - mraa_gpio_close(m_gpio); + ppd42ns_close(m_ppd42ns); } -dustData PPD42NS::getData() +ppd42ns_dust_data PPD42NS::getData() { - dustData data; - struct timespec printData_start={0,0}; - struct timespec printData_now={0,0}; - clock_gettime(CLOCK_MONOTONIC, &printData_start); - clock_gettime(CLOCK_MONOTONIC, &printData_now); - double low_pulse_occupancy = 0; - double pulse_check_time = 30; - - // Keep reading dust data until 30 seconds have passed - double start_time, end_time; - while (m_timediff(printData_start, printData_now) < pulse_check_time) - { - start_time = m_timediff(printData_start, printData_now); - end_time = pulse_check_time - start_time; - low_pulse_occupancy += pulseIn_polyfill(0, end_time); - clock_gettime(CLOCK_MONOTONIC, &printData_now); - } - - // Store dust data - double ratio = low_pulse_occupancy / (pulse_check_time * 1000 * 10.0); // Integer percentage 0=>100 - double concentration = (1.1 * pow(ratio,3)) - (3.8 * pow(ratio, 2)) + (520 * ratio) + 0.62; // using spec sheet curve - data.lowPulseOccupancy = (int)low_pulse_occupancy; - data.ratio = ratio; - data.concentration = concentration; - - return data; + return ppd42ns_get_data(m_ppd42ns); } - -// Mimicking Arduino's pulseIn function -// return how long it takes a pin to go from HIGH to LOW or LOW to HIGH -double PPD42NS::pulseIn_polyfill(bool high_low_value, double end_time) -{ - struct timespec pulseIn_start={0,0}; - struct timespec pulseIn_now={0,0}; - struct timespec pulsetime_start={0,0}; - struct timespec pulsetime_end={0,0}; - - int pin_val = 5; // some non-zero, non-1 number - bool started_timing = false; - bool ended_timing = false; - clock_gettime(CLOCK_MONOTONIC, &pulseIn_start); - - // run through this loop until either: - // a) we detect a change in pulse - // b) we've hit 30 seconds - while (!ended_timing && (m_timediff(pulseIn_start, pulseIn_now) < end_time)) - { - pin_val = (bool)mraa_gpio_read(m_gpio); - if (pin_val == high_low_value && !started_timing) - { - clock_gettime(CLOCK_MONOTONIC, &pulsetime_start); - started_timing = true; - //std::cout << "Started counting pulse change" << std::endl; - usleep(50); - } - else if (started_timing && pin_val != high_low_value) - { - clock_gettime(CLOCK_MONOTONIC, &pulsetime_end); - ended_timing = true; - //std::cout << "Ended counting pulse change" << (m_timediff(pulseIn_start, pulseIn_now)) << std::endl; - } - else - usleep(50); - clock_gettime(CLOCK_MONOTONIC, &pulseIn_now); - } - - double low_pulse_occupancy = 0; - - // if we ended due to detecting a pulse change and not due to hitting 30 seconds - if (started_timing && ended_timing) - low_pulse_occupancy = m_timediff(pulsetime_start, pulsetime_end); - //std::cout << "Low pulse occupancy is " << low_pulse_occupancy << " seconds" << std::endl; - - return (low_pulse_occupancy * 1000000); // convert to microseconds -} - -double PPD42NS::m_timediff(timespec time1, timespec time2) -{ - return ((double)time2.tv_sec + 1.0e-9*time2.tv_nsec) - - ((double)time1.tv_sec + 1.0e-9*time1.tv_nsec); -} diff --git a/src/ppd42ns/ppd42ns.h b/src/ppd42ns/ppd42ns.h new file mode 100644 index 00000000..96d8d5eb --- /dev/null +++ b/src/ppd42ns/ppd42ns.h @@ -0,0 +1,82 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Based on original C++ driver written by: + * Author: Zion Orent + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation 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 furnished 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 FOR 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. + */ +#pragma once + +#include +#include +#include + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * @file ppd42ns.h + * @library ppd42ns + * @brief C API for the ppd42ns driver + * + * @include ppd42ns.c + */ + + /** + * Device context + */ + typedef struct _ppd42ns_context { + mraa_gpio_context gpio; + + } *ppd42ns_context; + + /** + * PPD42NS initialization + * + * @param pin Digital pin to use + * @return ppd42ns device context + */ + ppd42ns_context ppd42ns_init(int pin); + + /** + * PPD42NS close + * + * @param dev Device context. + */ + void ppd42ns_close(ppd42ns_context dev); + + /** + * Prints dust concentration + * + * @param dev Device context. + * @return ppd42ns_dust_data Contains data from the dust sensor + */ + ppd42ns_dust_data ppd42ns_get_data(const ppd42ns_context dev); + +#ifdef __cplusplus +} +#endif diff --git a/src/ppd42ns/ppd42ns.hpp b/src/ppd42ns/ppd42ns.hpp index 0c654674..2db26ba9 100644 --- a/src/ppd42ns/ppd42ns.hpp +++ b/src/ppd42ns/ppd42ns.hpp @@ -1,6 +1,7 @@ /* * Author: Zion Orent - * Copyright (c) 2014 Intel Corporation. + * Jon Trulson + * Copyright (c) 2014-2016 Intel Corporation. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -24,70 +25,58 @@ #pragma once #include -#include -#include +#include namespace upm { -typedef struct -{ - int lowPulseOccupancy; - double ratio; - double concentration; -} dustData; - - /** - * @brief PPD42NS Dust Sensor library - * @defgroup ppd42ns libupm-ppd42ns - * @ingroup seeed gpio other eak - */ - /** - * @library ppd42ns - * @sensor ppd42ns - * @comname PPD42NS Dust Sensor - * @altname Grove Dust Sensor - * @type other - * @man seeed - * @web http://www.seeedstudio.com/wiki/Grove_-_Dust_Sensor - * @con gpio - * @kit eak - * - * @brief API for the PPD42NS Dust Sensor - * - * UPM module for the PPD42NS dust sensor - * - * @image html ppd42ns.jpg - * @snippet ppd42ns.cxx Interesting - */ - class PPD42NS { - public: /** - * PPD42NS constructor + * @brief PPD42NS Dust Sensor library + * @defgroup ppd42ns libupm-ppd42ns + * @ingroup seeed gpio other eak + */ + /** + * @library ppd42ns + * @sensor ppd42ns + * @comname PPD42NS Dust Sensor + * @altname Grove Dust Sensor + * @type other + * @man seeed + * @web http://www.seeedstudio.com/wiki/Grove_-_Dust_Sensor + * @con gpio + * @kit eak * - * @param pin Digital pin to use - */ - PPD42NS(int pin); - /** - * PPD42NS destructor - */ - ~PPD42NS(); - /** - * Prints dust concentration + * @brief API for the PPD42NS Dust Sensor * - * @return struct dustData Contains data from the dust sensor + * UPM module for the PPD42NS dust sensor + * + * @image html ppd42ns.jpg + * @snippet ppd42ns.cxx Interesting */ - dustData getData(); + class PPD42NS { + public: - private: - mraa_gpio_context m_gpio; - /** - * Returns the amount of time it takes a pin to go from HIGH to LOW or from LOW to HIGH - * - * @param highLowValue int Do we measure movements from HIGH to LOW or from LOW to HIGH? highLowValue is the "from" value - */ - double pulseIn_polyfill(bool highLowValue, double endTime); - double m_timediff(timespec time1, timespec time2); - }; + /** + * PPD42NS constructor + * + * @param pin Digital pin to use + */ + PPD42NS(int pin); + + /** + * PPD42NS destructor + */ + ~PPD42NS(); + + /** + * Prints dust concentration + * + * @return struct ppd42ns_dust_data Contains data from the dust sensor + */ + ppd42ns_dust_data getData(); + + private: + ppd42ns_context m_ppd42ns; + }; } diff --git a/src/ppd42ns/ppd42ns_data.h b/src/ppd42ns/ppd42ns_data.h new file mode 100644 index 00000000..1b65e7f9 --- /dev/null +++ b/src/ppd42ns/ppd42ns_data.h @@ -0,0 +1,44 @@ +/* + * Author: Jon Trulson + * Copyright (c) 2016 Intel Corporation. + * + * Based on original C++ driver written by: + * Author: Zion Orent + * Copyright (c) 2014 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation 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 furnished 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 FOR 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. + */ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + + // This is a data struct used by the ppd42ns (dust sensor) driver. + typedef struct + { + unsigned int lowPulseOccupancy; + double ratio; + double concentration; + } ppd42ns_dust_data; + +#ifdef __cplusplus +} +#endif diff --git a/src/ppd42ns/pyupm_ppd42ns.i b/src/ppd42ns/pyupm_ppd42ns.i index 1f5852c5..97a001c4 100644 --- a/src/ppd42ns/pyupm_ppd42ns.i +++ b/src/ppd42ns/pyupm_ppd42ns.i @@ -5,6 +5,7 @@ %feature("autodoc", "3"); +%include "ppd42ns_data.h" %include "ppd42ns.hpp" %{ #include "ppd42ns.hpp"