upm/examples/c++/gas-mq8.cxx
Mihai Tudor Panu 89d5de43e0 license: update to SPDX style license text throughout
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
2020-03-05 15:13:36 -08:00

66 lines
1.4 KiB
C++

/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2015 Intel Corporation.
*
* This program and the accompanying materials are made available under the
* terms of the The MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
*/
#include <iostream>
#include <signal.h>
#include <stdint.h>
#include "gas.hpp"
#include "mq8.hpp"
bool shouldRun = true;
using namespace std;
void
sig_handler(int signo)
{
if (signo == SIGINT) {
shouldRun = false;
}
}
//! [Interesting]
int
main(int argc, char** argv)
{
// Attach gas sensor to Analog A0
upm::MQ8 sensor(0);
signal(SIGINT, sig_handler);
uint16_t buffer[128];
thresholdContext ctx;
ctx.averageReading = 0;
ctx.runningAverage = 0;
ctx.averagedOver = 2;
// Infinite loop, ends when script is cancelled
// Repeatedly, take a sample every 2 microseconds;
// find the average of 128 samples; and
// print a running graph of asteriskss as averages
while (shouldRun) {
int len = sensor.getSampledWindow(2, 128, buffer);
if (len) {
int thresh = sensor.findThreshold(&ctx, 30, buffer, len);
sensor.printGraph(&ctx, 5);
if (thresh) {
// do something ....
}
}
}
cout << "Exiting" << endl;
return 0;
}
//! [Interesting]