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