1 /* 2 * Author: Mihai Tudor Panu <mihai.tudor.panu (at) 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 26 #include <unistd.h> 27 #include <iostream> 28 #include "tp401.h" 29 30 using namespace std; 31 32 //! [Interesting] 33 // Give a qualitative meaning to the value from the sensor 34 std::string 35 airQuality(uint16_t value) 36 { 37 if(value < 50) return "Fresh Air"; 38 if(value < 200) return "Normal Indoor Air"; 39 if(value < 400) return "Low Pollution"; 40 if(value < 600) return "High Pollution - Action Recommended"; 41 return "Very High Pollution - Take Action Immediately"; 42 } 43 44 int main () 45 { 46 upm::TP401* airSensor = new upm::TP401(0); // Instantiate new grove air quality sensor on analog pin A0 47 48 cout << airSensor->name() << endl; 49 50 fprintf(stdout, "Heating sensor for 3 minutes...\n"); 51 // wait 3 minutes for sensor to warm up 52 for(int i = 0; i < 3; i++) { 53 if(i) { 54 fprintf(stdout, "Please wait, %d minute(s) passed..\n", i); 55 } 56 sleep(60); 57 } 58 fprintf(stdout, "Sensor ready!\n"); 59 60 while(true) { 61 uint16_t value = airSensor->getSample(); // Read raw value 62 float ppm = airSensor->getPPM(); // Read CO ppm (can vary slightly from previous read) 63 fprintf(stdout, "raw: %4d ppm: %5.2f %s\n", value, ppm, airQuality(value).c_str()); 64 usleep(2500000); // Sleep for 2.5s 65 } 66 67 delete airSensor; 68 return 0; 69 } 70 //! [Interesting] 71