mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
ppd42ns: C implementation; C++ wraps C
Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
parent
8f6442e9c7
commit
1f954a8cbf
@ -33,31 +33,34 @@ int shouldRun = true;
|
|||||||
|
|
||||||
void sig_handler(int signo)
|
void sig_handler(int signo)
|
||||||
{
|
{
|
||||||
if (signo == SIGINT)
|
if (signo == SIGINT)
|
||||||
shouldRun = false;
|
shouldRun = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main ()
|
int main ()
|
||||||
{
|
{
|
||||||
signal(SIGINT, sig_handler);
|
signal(SIGINT, sig_handler);
|
||||||
|
|
||||||
//! [Interesting]
|
//! [Interesting]
|
||||||
// Instantiate a dust sensor on GPIO pin D8
|
// Instantiate a dust sensor on GPIO pin D8
|
||||||
upm::PPD42NS* dust = new upm::PPD42NS(8);
|
upm::PPD42NS* dust = new upm::PPD42NS(8);
|
||||||
upm::dustData data;
|
ppd42ns_dust_data data;
|
||||||
cout << "This program will give readings every 30 seconds until you stop it" << endl;
|
cout << "This program will give readings every 30 seconds until "
|
||||||
while (shouldRun)
|
<< "you stop it"
|
||||||
{
|
<< endl;
|
||||||
data = dust->getData();
|
while (shouldRun)
|
||||||
cout << "Low pulse occupancy: " << data.lowPulseOccupancy << endl;
|
{
|
||||||
cout << "Ratio: " << data.ratio << endl;
|
data = dust->getData();
|
||||||
cout << "Concentration: " << data.concentration << endl;
|
cout << "Low pulse occupancy: " << data.lowPulseOccupancy << endl;
|
||||||
}
|
cout << "Ratio: " << data.ratio << endl;
|
||||||
|
cout << "Concentration: " << data.concentration << endl;
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
//! [Interesting]
|
//! [Interesting]
|
||||||
|
|
||||||
cout << "Exiting" << endl;
|
cout << "Exiting" << endl;
|
||||||
|
|
||||||
delete dust;
|
delete dust;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -132,6 +132,7 @@ add_example (linefinder)
|
|||||||
add_example (uln200xa)
|
add_example (uln200xa)
|
||||||
add_example (mma7660)
|
add_example (mma7660)
|
||||||
add_example (buzzer)
|
add_example (buzzer)
|
||||||
|
add_example (ppd42ns)
|
||||||
|
|
||||||
# Custom examples
|
# Custom examples
|
||||||
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
add_custom_example (nmea_gps_i2c-example-c nmea_gps_i2c.c nmea_gps)
|
||||||
|
66
examples/c/ppd42ns.c
Normal file
66
examples/c/ppd42ns.c
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* 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 <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <ppd42ns.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
@ -24,22 +24,22 @@
|
|||||||
|
|
||||||
public class PPD42NSSample {
|
public class PPD42NSSample {
|
||||||
|
|
||||||
public static void main(String[] args) throws InterruptedException {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
// ! [Interesting]
|
// ! [Interesting]
|
||||||
// Instantiate a dust sensor on GPIO pin D8
|
// Instantiate a dust sensor on GPIO pin D8
|
||||||
upm_ppd42ns.PPD42NS dust = new upm_ppd42ns.PPD42NS(8);
|
upm_ppd42ns.PPD42NS dust = new upm_ppd42ns.PPD42NS(8);
|
||||||
upm_ppd42ns.dustData data;
|
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) {
|
while (true) {
|
||||||
data = dust.getData();
|
data = dust.getData();
|
||||||
|
|
||||||
System.out.println("Low pulse occupancy: " + data.getLowPulseOccupancy());
|
System.out.println("Low pulse occupancy: " + data.getLowPulseOccupancy());
|
||||||
System.out.println("Ratio: " + data.getRatio());
|
System.out.println("Ratio: " + data.getRatio());
|
||||||
System.out.println("Concentration: " + data.getConcentration());
|
System.out.println("Concentration: " + data.getConcentration());
|
||||||
}
|
}
|
||||||
// ! [Interesting]
|
// ! [Interesting]
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
set (libname "ppd42ns")
|
upm_mixed_module_init (NAME ppd42ns
|
||||||
set (libdescription "Ppd42ns dust sensor module")
|
DESCRIPTION "PPD42NS Dust Sensor"
|
||||||
set (module_src ${libname}.cxx)
|
C_HDR ppd42ns.h ppd42ns_data.h
|
||||||
set (module_hpp ${libname}.hpp)
|
C_SRC ppd42ns.c
|
||||||
upm_module_init("-lrt")
|
CPP_HDR ppd42ns.hpp
|
||||||
|
CPP_SRC ppd42ns.cxx
|
||||||
|
CPP_WRAPS_C
|
||||||
|
REQUIRES mraa)
|
||||||
|
target_link_libraries(${libnamec} m)
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
%module javaupm_ppd42ns
|
%module javaupm_ppd42ns
|
||||||
%include "../upm.i"
|
%include "../upm.i"
|
||||||
|
|
||||||
|
%include "ppd42ns_data.h"
|
||||||
|
%include "ppd42ns.hpp"
|
||||||
%{
|
%{
|
||||||
#include "ppd42ns.hpp"
|
#include "ppd42ns.hpp"
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%include "ppd42ns.hpp"
|
|
||||||
|
|
||||||
%pragma(java) jniclasscode=%{
|
%pragma(java) jniclasscode=%{
|
||||||
static {
|
static {
|
||||||
@ -16,4 +17,4 @@
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
%}
|
%}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
%module jsupm_ppd42ns
|
%module jsupm_ppd42ns
|
||||||
%include "../upm.i"
|
%include "../upm.i"
|
||||||
|
|
||||||
|
%include "ppd42ns_data.h"
|
||||||
|
%include "ppd42ns.hpp"
|
||||||
%{
|
%{
|
||||||
#include "ppd42ns.hpp"
|
#include "ppd42ns.hpp"
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%include "ppd42ns.hpp"
|
|
||||||
|
167
src/ppd42ns/ppd42ns.c
Normal file
167
src/ppd42ns/ppd42ns.c
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2016 Intel Corporation.
|
||||||
|
*
|
||||||
|
* Rewritten Based on original C++ driver written by:
|
||||||
|
* Author: Zion Orent <sorent@ics.com>
|
||||||
|
* 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 <assert.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <upm_math.h>
|
||||||
|
#include <upm_utilities.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Zion Orent <sorent@ics.com>
|
* Author: Zion Orent <sorent@ics.com>
|
||||||
* Copyright (c) 2014 Intel Corporation.
|
* Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2014-2016 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -25,113 +26,26 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <time.h>
|
|
||||||
#include <math.h>
|
|
||||||
#include <sys/sysinfo.h>
|
|
||||||
#include "ppd42ns.hpp"
|
#include "ppd42ns.hpp"
|
||||||
|
|
||||||
using namespace upm;
|
using namespace upm;
|
||||||
|
|
||||||
PPD42NS::PPD42NS(int pin)
|
PPD42NS::PPD42NS(int pin) :
|
||||||
|
m_ppd42ns(ppd42ns_init(pin))
|
||||||
{
|
{
|
||||||
if ( !(m_gpio = mraa_gpio_init(pin)) )
|
if (!m_ppd42ns)
|
||||||
{
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
": ppd42ns_init() failed");
|
||||||
": mraa_gpio_init() failed, invalid pin?");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PPD42NS::~PPD42NS()
|
PPD42NS::~PPD42NS()
|
||||||
{
|
{
|
||||||
mraa_gpio_close(m_gpio);
|
ppd42ns_close(m_ppd42ns);
|
||||||
}
|
}
|
||||||
|
|
||||||
dustData PPD42NS::getData()
|
ppd42ns_dust_data PPD42NS::getData()
|
||||||
{
|
{
|
||||||
dustData data;
|
return ppd42ns_get_data(m_ppd42ns);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
|
82
src/ppd42ns/ppd42ns.h
Normal file
82
src/ppd42ns/ppd42ns.h
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2016 Intel Corporation.
|
||||||
|
*
|
||||||
|
* Based on original C++ driver written by:
|
||||||
|
* Author: Zion Orent <sorent@ics.com>
|
||||||
|
* 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 <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <upm.h>
|
||||||
|
|
||||||
|
#include <mraa/gpio.h>
|
||||||
|
#include <ppd42ns_data.h>
|
||||||
|
|
||||||
|
#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
|
@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* Author: Zion Orent <sorent@ics.com>
|
* Author: Zion Orent <sorent@ics.com>
|
||||||
* Copyright (c) 2014 Intel Corporation.
|
* Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2014-2016 Intel Corporation.
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
* a copy of this software and associated documentation files (the
|
* a copy of this software and associated documentation files (the
|
||||||
@ -24,70 +25,58 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <time.h>
|
#include <ppd42ns.h>
|
||||||
#include <mraa/aio.h>
|
|
||||||
|
|
||||||
namespace upm {
|
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
|
* @brief API for the PPD42NS Dust Sensor
|
||||||
*/
|
|
||||||
PPD42NS(int pin);
|
|
||||||
/**
|
|
||||||
* PPD42NS destructor
|
|
||||||
*/
|
|
||||||
~PPD42NS();
|
|
||||||
/**
|
|
||||||
* Prints dust concentration
|
|
||||||
*
|
*
|
||||||
* @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;
|
* PPD42NS constructor
|
||||||
/**
|
*
|
||||||
* Returns the amount of time it takes a pin to go from HIGH to LOW or from LOW to HIGH
|
* @param pin Digital pin to use
|
||||||
*
|
*/
|
||||||
* @param highLowValue int Do we measure movements from HIGH to LOW or from LOW to HIGH? highLowValue is the "from" value
|
PPD42NS(int pin);
|
||||||
*/
|
|
||||||
double pulseIn_polyfill(bool highLowValue, double endTime);
|
/**
|
||||||
double m_timediff(timespec time1, timespec time2);
|
* 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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
44
src/ppd42ns/ppd42ns_data.h
Normal file
44
src/ppd42ns/ppd42ns_data.h
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* Author: Jon Trulson <jtrulson@ics.com>
|
||||||
|
* Copyright (c) 2016 Intel Corporation.
|
||||||
|
*
|
||||||
|
* Based on original C++ driver written by:
|
||||||
|
* Author: Zion Orent <sorent@ics.com>
|
||||||
|
* 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
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
%feature("autodoc", "3");
|
%feature("autodoc", "3");
|
||||||
|
|
||||||
|
%include "ppd42ns_data.h"
|
||||||
%include "ppd42ns.hpp"
|
%include "ppd42ns.hpp"
|
||||||
%{
|
%{
|
||||||
#include "ppd42ns.hpp"
|
#include "ppd42ns.hpp"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user