No Description

tp401.cxx 2.4KB

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