1 /*
2 * Author: Zion Orent <sorent@ics.com>
3 * Copyright (c) 2014 Intel Corporation.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 #include <iostream>
26 #include <string>
27 #include <stdexcept>
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <time.h>
31 #include <math.h>
32 #include <sys/sysinfo.h>
33 #include "ppd42ns.h"
34
35 using namespace upm;
36
PPD42NS(int pin)37 PPD42NS::PPD42NS(int pin)
38 {
39 if ( !(m_gpio = mraa_gpio_init(pin)) )
40 {
41 throw std::invalid_argument(std::string(__FUNCTION__) +
42 ": mraa_gpio_init() failed, invalid pin?");
43 return;
44 }
45
46 mraa_gpio_dir(m_gpio, MRAA_GPIO_IN);
47 }
48
~PPD42NS()49 PPD42NS::~PPD42NS()
50 {
51 mraa_gpio_close(m_gpio);
52 }
53
getData()54 dustData PPD42NS::getData()
55 {
56 dustData data;
57 struct timespec printData_start={0,0};
58 struct timespec printData_now={0,0};
59 clock_gettime(CLOCK_MONOTONIC, &printData_start);
60 clock_gettime(CLOCK_MONOTONIC, &printData_now);
61 double low_pulse_occupancy = 0;
62 double pulse_check_time = 30;
63
64 // Keep reading dust data until 30 seconds have passed
65 double start_time, end_time;
66 while (m_timediff(printData_start, printData_now) < pulse_check_time)
67 {
68 start_time = m_timediff(printData_start, printData_now);
69 end_time = pulse_check_time - start_time;
70 low_pulse_occupancy += pulseIn_polyfill(0, end_time);
71 clock_gettime(CLOCK_MONOTONIC, &printData_now);
72 }
73
74 // Store dust data
75 double ratio = low_pulse_occupancy / (pulse_check_time * 1000 * 10.0); // Integer percentage 0=>100
76 double concentration = (1.1 * pow(ratio,3)) - (3.8 * pow(ratio, 2)) + (520 * ratio) + 0.62; // using spec sheet curve
77 data.lowPulseOccupancy = (int)low_pulse_occupancy;
78 data.ratio = ratio;
79 data.concentration = concentration;
80
81 return data;
82 }
83
84
85 // Mimicking Arduino's pulseIn function
86 // return how long it takes a pin to go from HIGH to LOW or LOW to HIGH
pulseIn_polyfill(bool high_low_value,double end_time)87 double PPD42NS::pulseIn_polyfill(bool high_low_value, double end_time)
88 {
89 struct timespec pulseIn_start={0,0};
90 struct timespec pulseIn_now={0,0};
91 struct timespec pulsetime_start={0,0};
92 struct timespec pulsetime_end={0,0};
93
94 int pin_val = 5; // some non-zero, non-1 number
95 bool started_timing = false;
96 bool ended_timing = false;
97 clock_gettime(CLOCK_MONOTONIC, &pulseIn_start);
98
99 // run through this loop until either:
100 // a) we detect a change in pulse
101 // b) we've hit 30 seconds
102 while (!ended_timing && (m_timediff(pulseIn_start, pulseIn_now) < end_time))
103 {
104 pin_val = (bool)mraa_gpio_read(m_gpio);
105 if (pin_val == high_low_value && !started_timing)
106 {
107 clock_gettime(CLOCK_MONOTONIC, &pulsetime_start);
108 started_timing = true;
109 //std::cout << "Started counting pulse change" << std::endl;
110 usleep(50);
111 }
112 else if (started_timing && pin_val != high_low_value)
113 {
114 clock_gettime(CLOCK_MONOTONIC, &pulsetime_end);
115 ended_timing = true;
116 //std::cout << "Ended counting pulse change" << (m_timediff(pulseIn_start, pulseIn_now)) << std::endl;
117 }
118 else
119 usleep(50);
120 clock_gettime(CLOCK_MONOTONIC, &pulseIn_now);
121 }
122
123 double low_pulse_occupancy = 0;
124
125 // if we ended due to detecting a pulse change and not due to hitting 30 seconds
126 if (started_timing && ended_timing)
127 low_pulse_occupancy = m_timediff(pulsetime_start, pulsetime_end);
128 //std::cout << "Low pulse occupancy is " << low_pulse_occupancy << " seconds" << std::endl;
129
130 return (low_pulse_occupancy * 1000000); // convert to microseconds
131 }
132
m_timediff(timespec time1,timespec time2)133 double PPD42NS::m_timediff(timespec time1, timespec time2)
134 {
135 return ((double)time2.tv_sec + 1.0e-9*time2.tv_nsec) -
136 ((double)time1.tv_sec + 1.0e-9*time1.tv_nsec);
137 }
138