From ae77966204528efd73ec3acabbd2c94c8989002f Mon Sep 17 00:00:00 2001 From: Stefan Andritoiu Date: Mon, 26 Jun 2017 16:49:13 +0300 Subject: [PATCH] java: added some of the missing samples Signed-off-by: Stefan Andritoiu Signed-off-by: Abhishek Malik --- examples/java/BMA220Sample.java | 46 +++++ examples/java/DFRPHSample.java | 49 +++++ examples/java/GroveUltraSonicSample.java | 41 +++++ examples/java/I2CLCD_EBOLEDSample.java | 47 +++++ examples/java/I2CLCD_SSD1306Sample.java | 146 +++++++++++++++ examples/java/LM35Sample.java | 43 +++++ examples/java/LSM9DS0Sample.java | 59 +++++++ examples/java/LoudnessSample.java | 42 +++++ examples/java/MCP9808Sample.java | 216 +++++++++++++++++++++++ examples/java/MG811Sample.java | 44 +++++ examples/java/MQ4Sample.java | 56 ++++++ examples/java/MQ6Sample.java | 56 ++++++ examples/java/MQ7Sample.java | 57 ++++++ examples/java/MQ8Sample.java | 56 ++++++ examples/java/NLGPIO16Sample.java | 44 +++++ examples/java/PN532Sample.java | 76 ++++++++ examples/java/PN532_writeurlSample.java | 97 ++++++++++ examples/java/RHUSBSample.java | 60 +++++++ examples/java/WheelEncoderSample.java | 45 +++++ 19 files changed, 1280 insertions(+) create mode 100644 examples/java/BMA220Sample.java create mode 100644 examples/java/DFRPHSample.java create mode 100644 examples/java/GroveUltraSonicSample.java create mode 100644 examples/java/I2CLCD_EBOLEDSample.java create mode 100644 examples/java/I2CLCD_SSD1306Sample.java create mode 100644 examples/java/LM35Sample.java create mode 100644 examples/java/LSM9DS0Sample.java create mode 100644 examples/java/LoudnessSample.java create mode 100644 examples/java/MCP9808Sample.java create mode 100644 examples/java/MG811Sample.java create mode 100644 examples/java/MQ4Sample.java create mode 100644 examples/java/MQ6Sample.java create mode 100644 examples/java/MQ7Sample.java create mode 100644 examples/java/MQ8Sample.java create mode 100644 examples/java/NLGPIO16Sample.java create mode 100644 examples/java/PN532Sample.java create mode 100644 examples/java/PN532_writeurlSample.java create mode 100644 examples/java/RHUSBSample.java create mode 100644 examples/java/WheelEncoderSample.java diff --git a/examples/java/BMA220Sample.java b/examples/java/BMA220Sample.java new file mode 100644 index 00000000..24cb12f6 --- /dev/null +++ b/examples/java/BMA220Sample.java @@ -0,0 +1,46 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class BMA220Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + // Instantiate an BMA220 using default parameters (bus 0, addr 0x0a) + upm_bma220.BMA220 sensor = new upm_bma220.BMA220(); + + while (true) { + float acc[]; + + sensor.update(); + acc = sensor.getAccelerometer(); + System.out.print("Accelerometer: "); + System.out.println("AX: " + acc[0] + "AY: " + acc[1] + "AZ: " + acc[2]); + + Thread.sleep(1000); + } + + // ! [Interesting] + } +} diff --git a/examples/java/DFRPHSample.java b/examples/java/DFRPHSample.java new file mode 100644 index 00000000..edc6484d --- /dev/null +++ b/examples/java/DFRPHSample.java @@ -0,0 +1,49 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class DFRPHSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + // Instantiate a DFRPH sensor on analog pin A0, with an analog + // reference voltage of 5.0 V + upm_dfrph.DFRPH sensor = new upm_dfrph.DFRPH(0, 5.0f); + + // After calibration, set the offset (based on calibration with a pH + // 7.0 buffer solution). See the UPM sensor documentation for + // calibrations instructions. + sensor.setOffset(0.065f); + + // Every second, sample the pH and output it's corresponding + // analog voltage. + while (true) { + System.out.println("Detected volts: = " + sensor.volts()); + System.out.println("pH value: " + sensor.pH()); + Thread.sleep(1000); + } + + // ! [Interesting] + } +} diff --git a/examples/java/GroveUltraSonicSample.java b/examples/java/GroveUltraSonicSample.java new file mode 100644 index 00000000..e776b46a --- /dev/null +++ b/examples/java/GroveUltraSonicSample.java @@ -0,0 +1,41 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class GroveUltraSonicSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + upm_groveultrasonic.GroveUltraSonic sonar = new upm_groveultrasonic.GroveUltraSonic(2); + + while (true) { + int width = sonar.getDistance(); + System.out.println("Echo width = " + width); + System.out.println("Distance inches = " + width / 148.0); + Thread.sleep(1000); + } + + // ! [Interesting] + } +} diff --git a/examples/java/I2CLCD_EBOLEDSample.java b/examples/java/I2CLCD_EBOLEDSample.java new file mode 100644 index 00000000..81f1df7c --- /dev/null +++ b/examples/java/I2CLCD_EBOLEDSample.java @@ -0,0 +1,47 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class I2CLCD_EBOLEDSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + // Instantiate an Edison Block OLED using default values + upm_i2clcd.EBOLED lcd = new upm_i2clcd.EBOLED(); + + lcd.clear(); + lcd.setCursor(10, 15); + lcd.write("Hello"); + lcd.setCursor(30, 15); + lcd.write("World!"); + lcd.refresh(); + + System.out.println("Sleeping for 5 secconds..."); + Thread.sleep(5000); + + lcd.delete(); + + // ! [Interesting] + } +} diff --git a/examples/java/I2CLCD_SSD1306Sample.java b/examples/java/I2CLCD_SSD1306Sample.java new file mode 100644 index 00000000..fffde879 --- /dev/null +++ b/examples/java/I2CLCD_SSD1306Sample.java @@ -0,0 +1,146 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class I2CLCD_SSD1306Sample { + + static private final int[] intel_logo = new int[]{ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -128, -128, -128, -128, 192, 192, 192, 224, + 224, 224, 224, 240, 240, 248, 248, 120, 120, 120, 120, 60, 60, 60, 60, 60, + 62, 30, 30, 30, 30, 30, 30, 30, 31, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, + 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 31, 31, 31, 31, 31, + 30, 62, 62, 62, 62, 126, 126, 124, 124, 252, 252, 248, 248, 240, 240, 240, + 224, 224, 224, 192, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, + 128, 0, 56, 56, 28, 30, 14, 15, 15, 7, 7, 7, 7, 3, 3, 1, 1, 1, 1, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 0, 0, 192, 193, 195, 195, + 195, 7, 15, 15, 63, 127, 255, 255, 255, 254, 252, 252, 240, 192, 0, 0, 0, + 0, 0, 0, 0, 0, 128, 192, 192, 240, 248, 124, 124, 60, 0, 0, 0, 0, 159, 159, + 159, 159, 159, 159, 159, 159, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 0, 0, + 0, 0, 0, 0, 254, 254, 254, 254, 254, 254, 254, 254, 128, 128, 128, 128, + 128, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 192, 192, 192, 192, 192, 192, 128, + 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, + 0, 0, 0, 0, 3, 7, 3, 3, 3, 0, 0, 0, 0, 0, 1, 1, 255, 255, 255, 255, 255, + 255, 255, 0, 0, 224, 248, 252, 252, 255, 127, 15, 15, 3, 1, 0, 0, 0, 0, 0, + 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, + 255, 255, 255, 255, 255, 15, 15, 15, 15, 15, 15, 255, 255, 255, 255, 255, + 255, 255, 252, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 15, 15, + 15, 15, 15, 224, 224, 252, 254, 255, 255, 255, 255, 159, 159, 143, 143, + 135, 135, 143, 159, 255, 255, 255, 255, 255, 255, 252, 248, 0, 0, 0, 255, + 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, + 224, 248, 248, 255, 255, 255, 255, 255, 127, 15, 255, 255, 255, 255, 255, + 255, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, + 255, 255, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, + 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 255, 255, 255, + 255, 255, 255, 255, 255, 192, 192, 192, 192, 192, 31, 31, 255, 255, 255, + 255, 255, 255, 231, 231, 199, 199, 199, 199, 199, 199, 199, 199, 231, 231, + 231, 231, 199, 135, 0, 0, 0, 63, 255, 255, 255, 255, 255, 255, 255, 0, 0, + 0, 0, 224, 240, 248, 248, 252, 254, 255, 255, 255, 127, 63, 63, 31, 15, 7, + 7, 1, 0, 0, 63, 63, 255, 255, 255, 255, 255, 240, 192, 192, 128, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 3, 3, 7, 7, 7, 7, 7, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, + 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 3, 3, 7, 7, 7, + 7, 7, 7, 7, 7, 7, 0, 0, 0, 1, 3, 3, 3, 7, 7, 7, 7, 15, 15, 15, 15, 7, 7, 7, + 7, 7, 3, 3, 3, 1, 0, 0, 0, 0, 1, 3, 3, 7, 135, 135, 135, 192, 192, 0, 0, 7, + 7, 3, 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 7, 15, 15, + 31, 127, 127, 127, 255, 255, 252, 252, 252, 248, 240, 240, 240, 224, 224, + 224, 192, 192, 192, 192, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 128, 128, 128, 128, 128, 128, 128, 192, 192, 192, 192, 192, + 224, 224, 224, 224, 240, 240, 240, 240, 248, 248, 248, 248, 252, 252, 252, + 254, 254, 255, 255, 255, 255, 255, 255, 127, 127, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, + 3, 3, 3, 7, 7, 7, 15, 15, 31, 31, 31, 63, 63, 63, 63, 63, 127, 127, 127, + 127, 127, 255, 255, 255, 255, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, + 255, 255, 255, 255, 255, 255, 255, 127, 127, 127, 127, 127, 127, 127, 127, + 63, 63, 63, 63, 63, 31, 31, 31, 31, 31, 15, 15, 15, 15, 7, 7, 7, 7, 3, 3, + 3, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0 + }; + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + upm_i2clcd.SSD1306 lcd = new upm_i2clcd.SSD1306(0x0, 0x3D); + + byte[] image = new byte[intel_logo.length]; + for (int i = 0; i < intel_logo.length; i++) + image[i] = (byte) intel_logo[i]; + + lcd.clear(); + lcd.stopscroll(); + lcd.draw(image); + + Thread.sleep(5000); + + lcd.clear(); + lcd.setCursor(2, 0); + lcd.write("Hello"); + lcd.setCursor(3,0); + lcd.write("World"); + + Thread.sleep(3000); + + lcd.invert(true); + + Thread.sleep(3000); + + lcd.dim(true); + + Thread.sleep(3000); + + lcd.dim(false); + + Thread.sleep(3000); + + lcd.invert(false); + lcd.startscrollright((short) 0x00, (short) 0x0F); + + Thread.sleep(5000); + + lcd.stopscroll(); + lcd.startscrollleft((short) 0x00, (short) 0x0F); + + Thread.sleep(5000); + + lcd.stopscroll(); + lcd.startscrolldiagleft((short) 0x00, (short) 0x0F); + + Thread.sleep(5000);; + + lcd.stopscroll(); + lcd.startscrolldiagright((short) 0x00,(short) 0x0F); + + Thread.sleep(5000); + + lcd.stopscroll(); + + lcd.delete(); + + // ! [Interesting] + } +} diff --git a/examples/java/LM35Sample.java b/examples/java/LM35Sample.java new file mode 100644 index 00000000..ab268f66 --- /dev/null +++ b/examples/java/LM35Sample.java @@ -0,0 +1,43 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class LM35Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + // Instantiate a LM35 on analog pin A0, with a default analog + // reference voltage of 5.0 + upm_lm35.LM35 sensor = new upm_lm35.LM35(0); + + // Every half second, sample the sensor and output the temperature + + while (true) { + System.out.println("Temperature: " + sensor.getTemperature() + " C"); + Thread.sleep(500); + } + + // ! [Interesting] + } +} diff --git a/examples/java/LSM9DS0Sample.java b/examples/java/LSM9DS0Sample.java new file mode 100644 index 00000000..bd75cc18 --- /dev/null +++ b/examples/java/LSM9DS0Sample.java @@ -0,0 +1,59 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class LSM9DS0Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting + + // Instantiate an LSM9DS0 using default parameters (bus 1, gyro addr 6b, + // xm addr 1d) + + upm_lsm9ds0.LSM9DS0 sensor = new upm_lsm9ds0.LSM9DS0(); + + sensor.init(); + + while (true) { + sensor.update(); + + float[] accel = sensor.getAccelerometer(); + System.out.println("Accelerometer: "); + System.out.println("AX: " + accel[0] + "; AY: " + accel[1] + "; AZ: " + accel[2]); + + float[] gyro = sensor.getGyroscope(); + System.out.println("Gyroscope: "); + System.out.println("GX: " + gyro[0] + "; GY: " + gyro[1] + "; GZ: " + gyro[2]); + + float[] magnet = sensor.getMagnetometer(); + System.out.println("Magnetometer: "); + System.out.println("MX: " + magnet[0] + "; MY: " + magnet[1] + "; MZ: " + magnet[2]); + + System.out.println("Temperature: " + sensor.getTemperature()); + + Thread.sleep(500); + } + + // ! [Interesting] + } +} diff --git a/examples/java/LoudnessSample.java b/examples/java/LoudnessSample.java new file mode 100644 index 00000000..1a183b52 --- /dev/null +++ b/examples/java/LoudnessSample.java @@ -0,0 +1,42 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class LoudnessSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a Loudness sensor on analog pin A0, with an analog + // reference voltage of 5.0V + upm_loudness.Loudness loud = new upm_loudness.Loudness(0, (float) 5.0); + + // Every tenth of a second, sample the loudness and output it's + // corresponding analog voltage. + + while (true) { + System.out.println("Detected loudness (volts): " + loud.loudness()); + Thread.sleep(100); + } + // ! [Interesting] + } +} diff --git a/examples/java/MCP9808Sample.java b/examples/java/MCP9808Sample.java new file mode 100644 index 00000000..68bbe58a --- /dev/null +++ b/examples/java/MCP9808Sample.java @@ -0,0 +1,216 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +import java.io.*; +import upm_mcp9808.MCP9808; + +public class MCP9808Sample { + + public static void main(String[] args) throws InterruptedException, IOException { + // ! [Interesting] + + int command; + upm_mcp9808.MCP9808 temp = new upm_mcp9808.MCP9808(6); + BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); + + do { + System.out.println("1 - read temp"); + System.out.println("2 - sleep mode"); + System.out.println("3 - wake up"); + System.out.println("4 - set mode to " + (temp.isCelsius() == true ? "Fahrenheit" : "Celsius")); + System.out.println("5 - show status bits"); + System.out.println("6 - Set Tcrit"); + System.out.println("7 - Set Tupper"); + System.out.println("8 - Set Tlower"); + System.out.println("9 - Display monitor temps"); + System.out.println("10 - Enable alert default"); + System.out.println("11 - Enable alert interrupt"); + System.out.println("12 - Clear interrupt"); + System.out.println("13 - Clear alert mode"); + System.out.println("14 - Get Hysteresis"); + System.out.println("15 - Set Hysteresis"); + System.out.println("16 - Get Resolution"); + System.out.println("17 - Set Resolution"); + System.out.println("18 - Get Manufacturer ID"); + System.out.println("19 - Get Device ID"); + System.out.println("-1 - exit"); + System.out.print("Enter a command:"); + + try { + command = Integer.parseInt(read.readLine()); + } catch (Exception e) { + System.err.println("Not valid command"); + command = -1; + } + + float t; + switch (command) { + case 1: + System.out.println("Temp: " + temp.getTemp() + " degrees " + (temp.isCelsius() ? "Celsius" : "Fahrenheit")); + break; + case 2: + System.out.println("shutdown sensor (sleep mode)"); + temp.shutDown(); + break; + case 3: + System.out.println("wake up sensor"); + temp.shutDown(false); + break; + case 4: + System.out.println("set mode to " + (temp.isCelsius() ? "Fahrenheit" : "Celsius")); + temp.setMode(!temp.isCelsius()); + break; + case 5: + System.out.println("Tcrit = " + temp.isTcrit()); + System.out.println("Tupper = " + temp.isTupper()); + System.out.println("Tlower = " + temp.isTlower()); + break; + case 6: + System.out.print("Enter a value: "); + try { + t = Float.valueOf(read.readLine()).floatValue(); + } catch (Exception e) { + System.err.println("\nNot a valid value\n"); + break; + } + temp.setMonitorReg(MCP9808.MCP9808_REG.CRIT_TEMP, t); + break; + case 7: + System.out.print("Enter a value: "); + try { + t = Float.valueOf(read.readLine()).floatValue(); + } catch (Exception e) { + System.err.println("\nNot a valid value\n"); + break; + } + temp.setMonitorReg(MCP9808.MCP9808_REG.UPPER_TEMP, t); + break; + case 8: + System.out.print("Enter a value: "); + try { + t = Float.valueOf(read.readLine()).floatValue(); + } catch (Exception e) { + System.err.println("\nNot a valid value\n"); + break; + } + temp.setMonitorReg(MCP9808.MCP9808_REG.LOWER_TEMP, t); + break; + case 9: + System.out.println("Tcrit = " + temp.getMonitorReg(MCP9808.MCP9808_REG.CRIT_TEMP)); + System.out.println("Tupper = " + temp.getMonitorReg(MCP9808.MCP9808_REG.UPPER_TEMP)); + System.out.println("Tlower = " + temp.getMonitorReg(MCP9808.MCP9808_REG.LOWER_TEMP)); + break; + case 10: + System.out.println("Set alert mode default"); + temp.setAlertMode(MCP9808.MCP9808_CONFIG.ALERTCTRL.swigValue()); + break; + case 11: + System.out.println("Set alert mode interrupt"); + temp.setAlertMode( + MCP9808.MCP9808_CONFIG.ALERTMODE.swigValue() | MCP9808.MCP9808_CONFIG.ALERTCTRL.swigValue()); + break; + case 12: + temp.clearInterrupt(); + break; + case 13: + System.out.println("Clear alerts"); + temp.clearAlertMode(); + break; + case 14: + System.out.println("Hysteresis: " + temp.getHysteresis()); + break; + case 15: + int u; + System.out.print("Enter 1 to 4: "); + try { + u = Integer.parseInt(read.readLine()); + } catch (Exception e) { + System.err.println("\nNot a valid command\n"); + break; + } + switch (u) { + case 1: + temp.setHysteresis(MCP9808.MCP9808_CONFIG.HYST_0); + break; + case 2: + temp.setHysteresis(MCP9808.MCP9808_CONFIG.HYST_1_5); + break; + case 3: + temp.setHysteresis(MCP9808.MCP9808_CONFIG.HYST_3_0); + break; + case 4: + temp.setHysteresis(MCP9808.MCP9808_CONFIG.HYST_6_0); + break; + default: + System.out.println("\nNot a valid option\n"); + break; + } + break; + case 16: + System.out.println("Resolution: " + temp.getResolution()); + break; + case 17: + int v; + System.out.print("Enter 1 to 4: "); + try { + v = Integer.parseInt(read.readLine()); + } catch (Exception e) { + System.err.println("\nNot a valid command\n"); + break; + } + switch (v) { + case 1: + temp.setResolution(MCP9808.MCP9808_RESOLUTION.RES_LOW); + break; + case 2: + temp.setResolution(MCP9808.MCP9808_RESOLUTION.RES_MEDIUM); + break; + case 3: + temp.setResolution(MCP9808.MCP9808_RESOLUTION.RES_HIGH); + break; + case 4: + temp.setResolution(MCP9808.MCP9808_RESOLUTION.RES_PRECISION); + break; + default: + System.out.println("\nNot a valid option\n"); + break; + } + break; + case 18: + System.out.println("Manufacturer ID: " + temp.getManufacturer()); + break; + case 19: + System.out.println("Get device ID: " + temp.getDevicedId()); + break; + case -1: + break; + default: + System.out.println("That option is not available. Try again"); + break; + } + } while (command != -1); + + // ! [Interesting] + } +} + diff --git a/examples/java/MG811Sample.java b/examples/java/MG811Sample.java new file mode 100644 index 00000000..01ddf87f --- /dev/null +++ b/examples/java/MG811Sample.java @@ -0,0 +1,44 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class MG811Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + // Instantiate an MG811 on analog pin A0, and digital pin D2 with an + // analog reference voltage of 5.0V + upm_mg811.MG811 sensor = new upm_mg811.MG811(0, 2, (float) 5.0); + + // Every tenth of a second, sample the sensor and output it's + // detected CO2 concentration in parts per million (ppm) + + while (true) { + System.out.println("CO2 concentration in PPM: " + sensor.ppm()); + Thread.sleep(100); + } + + // ! [Interesting] + } +} diff --git a/examples/java/MQ4Sample.java b/examples/java/MQ4Sample.java new file mode 100644 index 00000000..75f1ee4a --- /dev/null +++ b/examples/java/MQ4Sample.java @@ -0,0 +1,56 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class MQ4Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + short[] buffer = new short[128]; + + // Attach gas sensor to A0 + upm_gas.MQ4 sensor = new upm_gas.MQ4(0); + + upm_gas.thresholdContext ctx = new upm_gas.thresholdContext(); + ctx.setAverageReading(0); + ctx.setRunningAverage(0); + ctx.setAveragedOver(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 asterisks as averages + while (true) { + int len = sensor.getSampledWindow(2, buffer); + + if (len != 0) { + int thresh = sensor.findThreshold(ctx, 30, buffer); + sensor.printGraph(ctx, (short) 5); + if (thresh != 0) { + // do something... + } + } + } + // ! [Interesting] + } +} diff --git a/examples/java/MQ6Sample.java b/examples/java/MQ6Sample.java new file mode 100644 index 00000000..6d9f609d --- /dev/null +++ b/examples/java/MQ6Sample.java @@ -0,0 +1,56 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class MQ6Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + short[] buffer = new short[128]; + + // Attach gas sensor to A0 + upm_gas.MQ6 sensor = new upm_gas.MQ6(0); + + upm_gas.thresholdContext ctx = new upm_gas.thresholdContext(); + ctx.setAverageReading(0); + ctx.setRunningAverage(0); + ctx.setAveragedOver(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 asterisks as averages + while (true) { + int len = sensor.getSampledWindow(2, buffer); + + if (len != 0) { + int thresh = sensor.findThreshold(ctx, 30, buffer); + sensor.printGraph(ctx, (short) 5); + if (thresh != 0) { + // do something .... + } + } + } + // ! [Interesting] + } +} diff --git a/examples/java/MQ7Sample.java b/examples/java/MQ7Sample.java new file mode 100644 index 00000000..2042950f --- /dev/null +++ b/examples/java/MQ7Sample.java @@ -0,0 +1,57 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class MQ7Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + short[] buffer = new short[128]; + + // Attach gas sensor to A0 + upm_gas.MQ7 sensor = new upm_gas.MQ7(0); + + upm_gas.thresholdContext ctx = new upm_gas.thresholdContext(); + ctx.setAverageReading(0); + ctx.setRunningAverage(0); + ctx.setAveragedOver(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 asterisks as averages + + while (true) { + int len = sensor.getSampledWindow(2, buffer); + + if (len != 0) { + int thresh = sensor.findThreshold(ctx, 30, buffer); + sensor.printGraph(ctx, (short) 5); + if (thresh != 0) { + // do something... + } + } + } + // ! [Interesting] + } +} diff --git a/examples/java/MQ8Sample.java b/examples/java/MQ8Sample.java new file mode 100644 index 00000000..4774abb2 --- /dev/null +++ b/examples/java/MQ8Sample.java @@ -0,0 +1,56 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class MQ8Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + short[] buffer = new short[128]; + + // Attach gas sensor to A0 + upm_gas.MQ8 sensor = new upm_gas.MQ8(0); + + upm_gas.thresholdContext ctx = new upm_gas.thresholdContext(); + ctx.setAverageReading(0); + ctx.setRunningAverage(0); + ctx.setAveragedOver(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 asterisks as averages + while (true) { + int len = sensor.getSampledWindow(2, buffer); + + if (len != 0) { + int thresh = sensor.findThreshold(ctx, 30, buffer); + sensor.printGraph(ctx, (short) 5); + if (thresh != 0) { + // do something... + } + } + } + // ! [Interesting] + } +} diff --git a/examples/java/NLGPIO16Sample.java b/examples/java/NLGPIO16Sample.java new file mode 100644 index 00000000..99d12aa9 --- /dev/null +++ b/examples/java/NLGPIO16Sample.java @@ -0,0 +1,44 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class NLGPIO16Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + // Instantiate a NLGPIO16 Module on the default UART (/dev/ttyACM0) + upm_nlgpio16.NLGPIO16 sensor = new upm_nlgpio16.NLGPIO16(); + + // get the Version + System.out.println("Device Version: " + sensor.getVersion()); + // read the gpio at pin 3 + System.out.println("GPIO 3 Value: " + sensor.gpioRead(3)); + // read the analog voltage at pin 5 + System.out.println("Analog 5 Voltage: " + sensor.analogReadVolts(5)); + // set the gpio at pin 14 to HIGH + sensor.gpioSet(14); + + // ! [Interesting] + } +} \ No newline at end of file diff --git a/examples/java/PN532Sample.java b/examples/java/PN532Sample.java new file mode 100644 index 00000000..1e53767c --- /dev/null +++ b/examples/java/PN532Sample.java @@ -0,0 +1,76 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class PN532Sample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the + // IRQ, and gpio 2 for the reset pin. + + upm_pn532.PN532 nfc = new upm_pn532.PN532(3, 2); + + if (!nfc.init()) + System.out.println("init() failed"); + + long vers = nfc.getFirmwareVersion(); + + if (vers != 0) + System.out.println("Got firmware version: " + String.format("0x%08x", vers)); + else { + System.out.println("Could not identify PN532\n"); + return; + } + + // Now scan and identify any cards that come in range (1 for now) + + // Retry forever + nfc.setPassiveActivationRetries((short) 0xff); + + nfc.SAMConfig(); + + short[] uidSize = new short[1]; + byte[] uid = new byte[7]; + + while (true) { + if (nfc.readPassiveTargetID(PN532.BAUD_T.BAUD_MIFARE_ISO14443A, uid, uidSize, 2000)) { + // found a card + System.out.println("Found a card: UID len " + uidSize[0]); + System.out.print("UID: "); + + for (int i = 0; i < uidSize[0]; i++) + System.out.print(String.format("%02x", uid[i]) + " "); + + System.out.println("\nSAK: " + String.format("0x%02x", nfc.getSAK())); + System.out.println("ATQA: " + String.format("0x%04x\n\n", nfc.getATQA())); + + Thread.sleep(1000); + } else { + System.out.println("Waiting for a card..."); + } + } + + // ! [Interesting] + } +} diff --git a/examples/java/PN532_writeurlSample.java b/examples/java/PN532_writeurlSample.java new file mode 100644 index 00000000..c88267aa --- /dev/null +++ b/examples/java/PN532_writeurlSample.java @@ -0,0 +1,97 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +// NOT TESTED!!! +public class PN532_writeurlSample { + + static private final String url = "iotdk.intel.com"; + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate an PN532 on I2C bus 0 (default) using gpio 3 for the + // IRQ, and gpio 2 for the reset pin. + + upm_pn532.PN532 nfc = new upm_pn532.PN532(3, 2); + + if (!nfc.init()) + System.out.println("init() failed"); + + long vers = nfc.getFirmwareVersion(); + + if (vers != 0) + System.out.println("Got firmware version: " + String.format("0x%08x", vers)); + else { + System.out.println("Could not identify PN532\n"); + return; + } + + // Now scan and identify any cards that come in range (1 for now) + + // Retry forever + nfc.setPassiveActivationRetries((short) 0xff); + + nfc.SAMConfig(); + + short[] uidSize = new short[1]; + byte[] uid = new byte[7]; + + boolean foundCard = false; + while (!foundCard) { + if (nfc.readPassiveTargetID(PN532.BAUD_T.BAUD_MIFARE_ISO14443A, uid, uidSize, 2000)) { + // found a card + System.out.println("Found a card: UID len " + uidSize[0]); + System.out.print("UID: "); + + for (int i = 0; i < uidSize[0]; i++) + System.out.print(String.format("%02x", uid[i]) + " "); + + System.out.println("\nSAK: " + String.format("0x%02x", nfc.getSAK())); + System.out.println("ATQA: " + String.format("0x%04x\n\n", nfc.getATQA())); + + foundCard = true; + } else { + System.out.println("Waiting for a card..."); + } + } + + if (uidSize[0] != 7) { + System.out.println("This example will only write an NDEF URI to preformatted"); + System.out.println("Mifare Ultralight or NTAG2XX tags\n"); + return; + } + + // 48 bytes is maximum data area on ultralight cards, so we use that + // as the maximum datasize here. Obviously if you have a bigger + // card, you can write more data. + if (!nfc.ntag2xx_WriteNDEFURI(PN532.NDEF_URI_T.NDEF_URIPREFIX_HTTP, url, (short) 48)) { + // failure + System.out.println("Failed to write NDEF record tag.\n"); + return; + } + + System.out.println("Success, URL record written to tag.\n"); + + // ! [Interesting] + } +} diff --git a/examples/java/RHUSBSample.java b/examples/java/RHUSBSample.java new file mode 100644 index 00000000..466590fe --- /dev/null +++ b/examples/java/RHUSBSample.java @@ -0,0 +1,60 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class RHUSBSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + + String defaultDev = "/dev/ttyUSB0"; + + // if an argument was specified, use it as the device instead + if (args.length > 0) + defaultDev = args[0]; + + System.out.println("Using device " + defaultDev); + System.out.println("Initializing..."); + + // Instantiate an RHUSB instance on defaultDev + upm_rhusb.RHUSB sensor = new upm_rhusb.RHUSB(defaultDev); + + // output the firmware ID + System.out.println("Firmware ID: " + sensor.getFirmwareID()); + + // update and print available values every second + while (true) { + // update our values from the sensor + sensor.update(); + + // we show both C and F for temperature + System.out.println("Temperature: " + sensor.getTemperature() + " C / " + sensor.getTemperature(true) + " F"); + + System.out.println("Humidity: " + sensor.getHumidity() + " %"); + + Thread.sleep(1000); + } + + // ! [Interesting] + } +} diff --git a/examples/java/WheelEncoderSample.java b/examples/java/WheelEncoderSample.java new file mode 100644 index 00000000..1f7a64e4 --- /dev/null +++ b/examples/java/WheelEncoderSample.java @@ -0,0 +1,45 @@ +/* + * Author: Stefan Andritoiu + * Copyright (c) 2017 Intel Corporation. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +public class WheelEncoderSample { + + public static void main(String[] args) throws InterruptedException { + // ! [Interesting] + // Instantiate a DFRobot Wheel Encoder on digital pin D2 + upm_wheelencoder.WheelEncoder sensor = new upm_wheelencoder.WheelEncoder(2); + + // set the counter to 0 and start counting + sensor.clearCounter(); + sensor.startCounter(); + + while (true) { + // output milliseconds passed and current sensor count + System.out.println("Millis: " + sensor.getMillis() + "; Count: " + sensor.counter()); + Thread.sleep(1000); + } + + // ! [Interesting] + } +} +