1 /*
2 * Author: Brendan Le Foll <brendan.le.foll@intel.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 <unistd.h>
27 #include <stdlib.h>
28 #include <functional>
29 #include <string.h>
30 #include <stdexcept>
31 #include "gas.h"
32
33 using namespace upm;
34
Gas(int gasPin)35 Gas::Gas(int gasPin) : m_aio(gasPin) {
36 }
37
~Gas()38 Gas::~Gas() {
39 }
40
41 int
getSampledWindow(unsigned int freqMS,int numberOfSamples,uint16_t * buffer)42 Gas::getSampledWindow (unsigned int freqMS, int numberOfSamples,
43 uint16_t * buffer) {
44 int sampleIdx = 0;
45
46 // must have freq
47 if (!freqMS) {
48 return 0;
49 }
50
51 // too many samples
52 if (numberOfSamples > 0xFFFFFF) {
53 return 0;
54 }
55
56 while (sampleIdx < numberOfSamples) {
57 buffer[sampleIdx++] = getSample();
58 usleep(freqMS * 1000);
59 }
60
61 return sampleIdx;
62 }
63
64 int
findThreshold(thresholdContext * ctx,unsigned int threshold,uint16_t * buffer,int len)65 Gas::findThreshold (thresholdContext* ctx, unsigned int threshold,
66 uint16_t * buffer, int len) {
67 long sum = 0;
68 for (unsigned int i = 0; i < len; i++) {
69 sum += buffer[i];
70 }
71
72 ctx->averageReading = sum / len;
73 ctx->runningAverage = (((ctx->averagedOver-1) * ctx->runningAverage) + ctx->averageReading) / ctx->averagedOver;
74
75 if (ctx->runningAverage > threshold) {
76 return ctx->runningAverage;
77 } else {
78 return 0;
79 }
80 }
81
82 int
getSampledData(thresholdContext * ctx)83 Gas::getSampledData (thresholdContext* ctx) {
84 return ctx->runningAverage;
85 }
86
87 int
getSample()88 Gas::getSample () {
89 return m_aio.read();
90 }
91
92 void
printGraph(thresholdContext * ctx,uint8_t resolution)93 Gas::printGraph (thresholdContext* ctx, uint8_t resolution) {
94 std::cout << "(" << ctx->runningAverage << ") | ";
95 for (int i = 0; i < ctx->runningAverage / resolution; i++)
96 std::cout << "*";
97 std::cout << std::endl;
98 }
99