Private
Public Access
2
0

examples/java: Update, indent and add [Interesting] tags to examples

Signed-off-by: Petre Eftime <petre.p.eftime@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Petre Eftime
2015-09-30 13:21:23 +03:00
committed by Brendan Le Foll
parent 013c04c7b9
commit 0e44dfac44
5 changed files with 133 additions and 108 deletions

View File

@@ -1,3 +1,8 @@
import mraa.Dir;
import mraa.Gpio;
import mraa.Platform;
import mraa.mraa;
/* /*
* Author: Brendan Le Foll <brendan.le.foll@intel.com> * Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
@@ -25,52 +30,52 @@
*/ */
public class BlinkOnboard { public class BlinkOnboard {
static { static {
try { try {
System.loadLibrary("mraajava"); System.loadLibrary("mraajava");
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
System.err.println( System.err.println(
"Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
e); e);
System.exit(1); System.exit(1);
}
} }
} public static void main(String argv[]) throws InterruptedException {
public static void main(String argv[]) throws InterruptedException { Platform platform = mraa.getPlatformType();
mraa.mraa_platform_t platform = mraa.mraa.getPlatformType(); Gpio gpio, gpio_in;
mraa.Gpio gpio, gpio_in; if (platform == Platform.INTEL_GALILEO_GEN1) {
if (platform == mraa.mraa_platform_t.INTEL_GALILEO_GEN1) { gpio = new Gpio(3);
gpio = new mraa.Gpio(3); } else if (platform == Platform.INTEL_MINNOWBOARD_MAX) {
} else if (platform == mraa.mraa_platform_t.INTEL_MINNOWBOARD_MAX) { gpio = new Gpio(21);
gpio = new mraa.Gpio(21); } else {
} else { gpio = new Gpio(13);
gpio = new mraa.Gpio(13); }
} System.out.format("Welcome to libmraa\n Version: %s\n Running on %s\n",
System.out.format("Welcome to libmraa\n Version: %s\n Running on %s\n", mraa.getVersion(), platform.toString());
mraa.mraa.getVersion(), platform.toString());
gpio.dir(mraa.Dir.DIR_OUT); gpio.dir(Dir.DIR_OUT);
// on platforms with physical button use gpio_in // on platforms with physical button use gpio_in
if (platform == mraa.mraa_platform_t.INTEL_MINNOWBOARD_MAX) { if (platform == Platform.INTEL_MINNOWBOARD_MAX) {
gpio_in = new mraa.Gpio(14); gpio_in = new Gpio(14);
gpio_in.dir(mraa.Dir.DIR_IN); gpio_in.dir(Dir.DIR_IN);
System.out.println("Press and hold S1 to stop, Press SW1 to shutdown!"); System.out.println("Press and hold S1 to stop, Press SW1 to shutdown!");
} else { } else {
gpio_in = null; gpio_in = null;
} }
boolean state = false; boolean state = false;
while (true) { while (true) {
if (gpio_in != null && gpio_in.read() == 0) { if (gpio_in != null && gpio_in.read() == 0) {
return; return;
} }
if (state) { if (state) {
state = false; state = false;
gpio.write(1); gpio.write(1);
} else { } else {
state = true; state = true;
gpio.write(0); gpio.write(0);
} }
Thread.sleep(1000); Thread.sleep(1000);
}
} }
}
} }

View File

@@ -24,31 +24,34 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
public class CyclePwm3 { import mraa.Pwm;
static {
try {
System.loadLibrary("mraajava");
} catch (UnsatisfiedLinkError e) {
System.err.println(
"Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
e);
System.exit(1);
}
}
public static void main(String argv[]) throws InterruptedException {
mraa.mraa.init();
mraa.Pwm pwm = new mraa.Pwm(3);
pwm.period_us(200);
pwm.enable(true);
float value = 0; public class CyclePwm3 {
while (true) { static {
value += 0.01; try {
pwm.write(value); System.loadLibrary("mraajava");
Thread.sleep(50); } catch (UnsatisfiedLinkError e) {
if (value >= 1) { System.err.println(
value = 0; "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
} e);
System.exit(1);
}
}
public static void main(String argv[]) throws InterruptedException {
//! [Interesting]
Pwm pwm = new mraa.Pwm(3);
pwm.period_us(200);
pwm.enable(true);
float value = 0;
while (true) {
value += 0.01;
pwm.write(value);
Thread.sleep(50);
if (value >= 1) {
value = 0;
}
}
//! [Interesting]
} }
}
} }

View File

@@ -1,6 +1,8 @@
/* /*
* Author: Alexander Komarov <alexander.komarov@intel.com> * Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 Intel Corporation. * Copyright (c) 2014 Intel Corporation.
* Author: Petre Eftime <petre.p.eftime@intel.com>
* Copyright (c) 2015 Intel Corporation.
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@@ -22,6 +24,9 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
//! [Interesting]
import mraa.mraa;
public class Example { public class Example {
static { static {
try { try {
@@ -34,8 +39,11 @@ public class Example {
} }
} }
public static void main(String argv[]) { public static void main(String argv[]) {
mraa.mraa.init(); String board = mraa.getPlatformName();
System.out.println(mraa.mraa.getVersion()); String version = mraa.getVersion();
System.out.println("hello mraa");
System.out.println(String.format("Version: %s", version));
System.out.println(String.format("Running on %s", board));
}; };
} }
; //! [Interesting]

View File

@@ -24,21 +24,32 @@
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
import mraa.Dir;
import mraa.Gpio;
import mraa.mraa;
public class GpioRead6 { public class GpioRead6 {
static { static {
try { try {
System.loadLibrary("mraajava"); System.loadLibrary("mraajava");
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
System.err.println( System.err.println(
"Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
e); e);
System.exit(1); System.exit(1);
}
}
public static void main(String argv[]) throws InterruptedException {
System.out.println(String.format("MRAA Version: %s\nStarting Read on IO6\n", mraa.getVersion()));
//! [Interesting]
Gpio gpio = new Gpio(6);
gpio.dir(Dir.DIR_IN);
while (true) {
System.out.format("Gpio is %d\n", gpio.read());
Thread.sleep(1000);
}
//! [Interesting]
} }
}
public static void main(String argv[]) {
mraa.mraa.init();
mraa.Gpio gpio_in = new mraa.Gpio(6);
gpio_in.dir(mraa.Dir.DIR_IN);
System.out.format("Gpio is %d\n", gpio_in.read());
}
} }

View File

@@ -23,32 +23,30 @@
*/ */
public class Isr { public class Isr {
static { static {
try { try {
System.loadLibrary("mraajava"); System.loadLibrary("mraajava");
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
System.err.println( System.err.println(
"Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
e); e);
System.exit(1); System.exit(1);
}
} }
} public static void main(String argv[]) throws InterruptedException {
public static void main(String argv[]) { mraa.mraa.init();
mraa.mraa.init();
mraa.Gpio gpio = new mraa.Gpio(7); mraa.Gpio gpio = new mraa.Gpio(7);
mraa.IsrCallback callback = new JavaCallback(); mraa.IsrCallback callback = new JavaCallback();
gpio.isr(mraa.Edge.EDGE_RISING, callback, null); gpio.isr(mraa.Edge.EDGE_RISING, callback);
while (true) Thread.sleep(0);
; };
};
} }
;
class JavaCallback extends mraa.IsrCallback { class JavaCallback extends mraa.IsrCallback {
public JavaCallback() { super(); } public JavaCallback() { super(); }
public void run() { System.out.println("JavaCallback.run()"); } public void run() { System.out.println("JavaCallback.run()"); }
} }