Aucune description

TP401Sample.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Author: Stefan Andritoiu <stefan.andritoiu@intel.com>
  3. * Copyright (c) 2015 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. public class TP401Sample{
  25. static {
  26. try {
  27. System.loadLibrary("javaupm_gas");
  28. }catch (UnsatisfiedLinkError e) {
  29. System.err.println("error in loading native library");
  30. System.exit(-1);
  31. }
  32. }
  33. private static String airQuality(int value){
  34. if(value < 50) return "Fresh Air";
  35. if(value < 200) return "Normal Indoor Air";
  36. if(value < 400) return "Low Pollution";
  37. if(value < 600) return "High Pollution - Action Recommended";
  38. return "Very High Pollution - Take Action Immediately";
  39. }
  40. public static void main(String[] args) throws InterruptedException {
  41. //! [Interesting]
  42. // Instantiate new grove air quality sensor on analog pin A0
  43. upm_gas.TP401 airSensor = new upm_gas.TP401(0);
  44. System.out.println(airSensor.name());
  45. System.out.println("Heating sensor for 3 minutes...");
  46. // wait 3 minutes for sensor to warm up
  47. for(int i = 0; i < 3; i++) {
  48. if(i != 0)
  49. System.out.println("Please wait, " + i + " minute(s) passed..");
  50. Thread.sleep(60000);
  51. }
  52. System.out.println( "Sensor ready!");
  53. while(true){
  54. int value = airSensor.getSample(); // Read raw value
  55. float ppm = airSensor.getPPM(); // Read CO ppm (can vary slightly from previous read)
  56. System.out.println("raw: " + value + " ppm: " + ppm + " " + airQuality(value) );
  57. Thread.sleep(100);
  58. }
  59. //! [Interesting]
  60. }
  61. }