first commit
This commit is contained in:
commit
a5a0434432
1126 changed files with 439481 additions and 0 deletions
73
Software/Java8/examples/build.xml
Normal file
73
Software/Java8/examples/build.xml
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- You may freely edit this file. See commented blocks below for -->
|
||||
<!-- some examples of how to customize the build. -->
|
||||
<!-- (If you delete it and reopen the project it will be recreated.) -->
|
||||
<!-- By default, only the Clean and Build commands use this build script. -->
|
||||
<!-- Commands such as Run, Debug, and Test only use this build script if -->
|
||||
<!-- the Compile on Save feature is turned off for the project. -->
|
||||
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
|
||||
<!-- in the project's Project Properties dialog box.-->
|
||||
<project name="examples" default="default" basedir=".">
|
||||
<description>Builds, tests, and runs the project examples.</description>
|
||||
<import file="nbproject/build-impl.xml"/>
|
||||
<!--
|
||||
|
||||
There exist several targets which are by default empty and which can be
|
||||
used for execution of your tasks. These targets are usually executed
|
||||
before and after some main targets. They are:
|
||||
|
||||
-pre-init: called before initialization of project properties
|
||||
-post-init: called after initialization of project properties
|
||||
-pre-compile: called before javac compilation
|
||||
-post-compile: called after javac compilation
|
||||
-pre-compile-single: called before javac compilation of single file
|
||||
-post-compile-single: called after javac compilation of single file
|
||||
-pre-compile-test: called before javac compilation of JUnit tests
|
||||
-post-compile-test: called after javac compilation of JUnit tests
|
||||
-pre-compile-test-single: called before javac compilation of single JUnit test
|
||||
-post-compile-test-single: called after javac compilation of single JUunit test
|
||||
-pre-jar: called before JAR building
|
||||
-post-jar: called after JAR building
|
||||
-post-clean: called after cleaning build products
|
||||
|
||||
(Targets beginning with '-' are not intended to be called on their own.)
|
||||
|
||||
Example of inserting an obfuscator after compilation could look like this:
|
||||
|
||||
<target name="-post-compile">
|
||||
<obfuscate>
|
||||
<fileset dir="${build.classes.dir}"/>
|
||||
</obfuscate>
|
||||
</target>
|
||||
|
||||
For list of available properties check the imported
|
||||
nbproject/build-impl.xml file.
|
||||
|
||||
|
||||
Another way to customize the build is by overriding existing main targets.
|
||||
The targets of interest are:
|
||||
|
||||
-init-macrodef-javac: defines macro for javac compilation
|
||||
-init-macrodef-junit: defines macro for junit execution
|
||||
-init-macrodef-debug: defines macro for class debugging
|
||||
-init-macrodef-java: defines macro for class execution
|
||||
-do-jar: JAR building
|
||||
run: execution of project
|
||||
-javadoc-build: Javadoc generation
|
||||
test-report: JUnit report generation
|
||||
|
||||
An example of overriding the target for project execution could look like this:
|
||||
|
||||
<target name="run" depends="examples-impl.jar">
|
||||
<exec dir="bin" executable="launcher.exe">
|
||||
<arg file="${dist.jar}"/>
|
||||
</exec>
|
||||
</target>
|
||||
|
||||
Notice that the overridden target depends on the jar target and not only on
|
||||
the compile target as the regular run target does. Again, for a list of available
|
||||
properties which you can use, check the target you are overriding in the
|
||||
nbproject/build-impl.xml file.
|
||||
|
||||
-->
|
||||
</project>
|
||||
3
Software/Java8/examples/manifest.mf
Normal file
3
Software/Java8/examples/manifest.mf
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
X-COMMENT: Main-Class will be added automatically by build
|
||||
|
||||
1420
Software/Java8/examples/nbproject/build-impl.xml
Normal file
1420
Software/Java8/examples/nbproject/build-impl.xml
Normal file
File diff suppressed because it is too large
Load diff
8
Software/Java8/examples/nbproject/genfiles.properties
Normal file
8
Software/Java8/examples/nbproject/genfiles.properties
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
build.xml.data.CRC32=dc38a7f0
|
||||
build.xml.script.CRC32=f1113447
|
||||
build.xml.stylesheet.CRC32=8064a381@1.80.1.48
|
||||
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
|
||||
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
|
||||
nbproject/build-impl.xml.data.CRC32=dc38a7f0
|
||||
nbproject/build-impl.xml.script.CRC32=e2a7c1a9
|
||||
nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48
|
||||
80
Software/Java8/examples/nbproject/project.properties
Normal file
80
Software/Java8/examples/nbproject/project.properties
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
annotation.processing.enabled=true
|
||||
annotation.processing.enabled.in.editor=false
|
||||
annotation.processing.processors.list=
|
||||
annotation.processing.run.all.processors=true
|
||||
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
|
||||
application.title=examples
|
||||
application.vendor=ed_ze
|
||||
build.classes.dir=${build.dir}/classes
|
||||
build.classes.excludes=**/*.java,**/*.form
|
||||
# This directory is removed when the project is cleaned:
|
||||
build.dir=build
|
||||
build.generated.dir=${build.dir}/generated
|
||||
build.generated.sources.dir=${build.dir}/generated-sources
|
||||
# Only compile against the classpath explicitly listed here:
|
||||
build.sysclasspath=ignore
|
||||
build.test.classes.dir=${build.dir}/test/classes
|
||||
build.test.results.dir=${build.dir}/test/results
|
||||
# Uncomment to specify the preferred debugger connection transport:
|
||||
#debug.transport=dt_socket
|
||||
debug.classpath=\
|
||||
${run.classpath}
|
||||
debug.test.classpath=\
|
||||
${run.test.classpath}
|
||||
# Files in build.classes.dir which should be excluded from distribution jar
|
||||
dist.archive.excludes=
|
||||
# This directory is removed when the project is cleaned:
|
||||
dist.dir=dist
|
||||
dist.jar=${dist.dir}/examples.jar
|
||||
dist.javadoc.dir=${dist.dir}/javadoc
|
||||
endorsed.classpath=
|
||||
excludes=
|
||||
file.reference.GrovePi-pi4j-0.1.0-SNAPSHOT.jar=..\\GrovePi-pi4j\\target\\GrovePi-pi4j-0.1.0-SNAPSHOT.jar
|
||||
file.reference.GrovePi-spec-0.1.0-SNAPSHOT.jar=..\\GrovePi-spec\\target\\GrovePi-spec-0.1.0-SNAPSHOT.jar
|
||||
includes=**
|
||||
jar.compress=false
|
||||
javac.classpath=\
|
||||
${file.reference.GrovePi-spec-0.1.0-SNAPSHOT.jar}:\
|
||||
${file.reference.GrovePi-pi4j-0.1.0-SNAPSHOT.jar}
|
||||
# Space-separated list of extra javac options
|
||||
javac.compilerargs=
|
||||
javac.deprecation=false
|
||||
javac.external.vm=true
|
||||
javac.processorpath=\
|
||||
${javac.classpath}
|
||||
javac.source=1.8
|
||||
javac.target=1.8
|
||||
javac.test.classpath=\
|
||||
${javac.classpath}:\
|
||||
${build.classes.dir}
|
||||
javac.test.processorpath=\
|
||||
${javac.test.classpath}
|
||||
javadoc.additionalparam=
|
||||
javadoc.author=false
|
||||
javadoc.encoding=${source.encoding}
|
||||
javadoc.noindex=false
|
||||
javadoc.nonavbar=false
|
||||
javadoc.notree=false
|
||||
javadoc.private=false
|
||||
javadoc.splitindex=true
|
||||
javadoc.use=true
|
||||
javadoc.version=false
|
||||
javadoc.windowtitle=
|
||||
main.class=org.iot.raspberry.examples.Runner
|
||||
manifest.file=manifest.mf
|
||||
meta.inf.dir=${src.dir}/META-INF
|
||||
mkdist.disabled=false
|
||||
platform.active=default_platform
|
||||
run.classpath=\
|
||||
${javac.classpath}:\
|
||||
${build.classes.dir}
|
||||
# Space-separated list of JVM arguments used when running the project.
|
||||
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
|
||||
# To set system properties for unit tests define test-sys-prop.name=value:
|
||||
run.jvmargs=
|
||||
run.test.classpath=\
|
||||
${javac.test.classpath}:\
|
||||
${build.test.classes.dir}
|
||||
source.encoding=UTF-8
|
||||
src.dir=src
|
||||
test.src.dir=test
|
||||
15
Software/Java8/examples/nbproject/project.xml
Normal file
15
Software/Java8/examples/nbproject/project.xml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||
<type>org.netbeans.modules.java.j2seproject</type>
|
||||
<configuration>
|
||||
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
|
||||
<name>examples</name>
|
||||
<source-roots>
|
||||
<root id="src.dir"/>
|
||||
</source-roots>
|
||||
<test-roots>
|
||||
<root id="test.src.dir"/>
|
||||
</test-roots>
|
||||
</data>
|
||||
</configuration>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GroveDigitalOut;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRelay;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRgbLcd;
|
||||
import org.iot.raspberry.grovepi.devices.GroveTemperatureAndHumiditySensor;
|
||||
import org.iot.raspberry.grovepi.devices.GroveTemperatureAndHumidityValue;
|
||||
import org.iot.raspberry.grovepi.devices.GroveUltrasonicRanger;
|
||||
|
||||
/**
|
||||
* Connect:
|
||||
* Digital 3 -> Red led
|
||||
* Digital 4 -> Green led
|
||||
* Digital 5 -> blue led
|
||||
* Digital 6 -> Ultrasonic ranger
|
||||
* Digital 7 -> TemperatureSensor DHT11
|
||||
* Digital 8 -> Relay
|
||||
* Any I2C -> LCD
|
||||
* @author Eduardo Moranchel <emoranchel@asmatron.org>
|
||||
*/
|
||||
public class AutomaticAC implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveDigitalOut redLed = grovePi.getDigitalOut(3);
|
||||
GroveDigitalOut greenLed = grovePi.getDigitalOut(4);
|
||||
GroveDigitalOut blueLed = grovePi.getDigitalOut(5);
|
||||
GroveUltrasonicRanger ranger = new GroveUltrasonicRanger(grovePi, 6);
|
||||
GroveTemperatureAndHumiditySensor tempSensor = new GroveTemperatureAndHumiditySensor(grovePi, 7, GroveTemperatureAndHumiditySensor.Type.DHT11);
|
||||
GroveRelay relay = new GroveRelay(grovePi, 8);
|
||||
GroveRgbLcd lcd = grovePi.getLCD();
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
GroveTemperatureAndHumidityValue dht = tempSensor.get();
|
||||
double temperature = dht.getTemperature();
|
||||
double distance = ranger.get();
|
||||
String message = String.format("TEMP:%.2f\nDistance%.2f", temperature, distance);
|
||||
System.out.println(message);
|
||||
lcd.setText(message);
|
||||
if (temperature > 28) {
|
||||
if (distance < 50) {
|
||||
relay.set(true);
|
||||
} else {
|
||||
relay.set(false);
|
||||
}
|
||||
lcd.setRGB(255, 50, 50);
|
||||
redLed.set(true);
|
||||
blueLed.set(false);
|
||||
greenLed.set(false);
|
||||
} else if (temperature < 17) {
|
||||
relay.set(false);
|
||||
blueLed.set(true);
|
||||
redLed.set(false);
|
||||
greenLed.set(false);
|
||||
lcd.setRGB(50, 50, 255);
|
||||
} else {
|
||||
relay.set(false);
|
||||
blueLed.set(false);
|
||||
greenLed.set(true);
|
||||
lcd.setRGB(50, 255, 50);
|
||||
}
|
||||
} catch (IOException ioex) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
}
|
||||
redLed.set(false);
|
||||
greenLed.set(false);
|
||||
blueLed.set(false);
|
||||
relay.set(false);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import org.iot.raspberry.grovepi.GroveDigitalOut;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
|
||||
/*
|
||||
Connect: Led to D4
|
||||
*/
|
||||
public class BlinkingLed implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveDigitalOut led = grovePi.getDigitalOut(2);
|
||||
boolean state = false;
|
||||
while (monitor.isRunning()) {
|
||||
state = !state;
|
||||
led.set(state);
|
||||
Thread.sleep(500);
|
||||
}
|
||||
led.set(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import org.iot.raspberry.grovepi.GroveDigitalIn;
|
||||
import org.iot.raspberry.grovepi.GroveDigitalOut;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
/*
|
||||
Connect: Led to D4 and Button to D6
|
||||
*/
|
||||
|
||||
public class ButtonLed implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveDigitalIn button = grovePi.getDigitalIn(6);
|
||||
GroveDigitalOut led = grovePi.getDigitalOut(4);
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
led.set(button.get());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
led.set(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.iot.raspberry.grovepi.GroveDigitalIn;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
/*
|
||||
Connect: Button to D6
|
||||
*/
|
||||
|
||||
public class ButtonListener implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
|
||||
|
||||
GroveDigitalIn button = grovePi.getDigitalIn(6);
|
||||
button.setListener((boolean oldValue, boolean newValue) -> {
|
||||
System.out.println("Button " + oldValue + "->" + newValue);
|
||||
});
|
||||
|
||||
exec.scheduleAtFixedRate(button, 0, 200, TimeUnit.MILLISECONDS);
|
||||
monitor.waitForStop();
|
||||
exec.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
|
||||
public interface Example {
|
||||
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GroveDigitalOut;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveLightSensor;
|
||||
/*
|
||||
Connect Light sensor to A2
|
||||
Connect Red Led to D3 (emergency Light)
|
||||
*/
|
||||
|
||||
public class LightSensor implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveLightSensor lightSensor = new GroveLightSensor(grovePi, 2);
|
||||
GroveDigitalOut emergencyLight = new GroveDigitalOut(grovePi, 3);
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
Double value = lightSensor.get();
|
||||
emergencyLight.set(value < 250);
|
||||
System.out.println(value);
|
||||
} catch (IOException ex) {
|
||||
System.out.println("error");
|
||||
}
|
||||
}
|
||||
emergencyLight.set(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
public class Monitor {
|
||||
|
||||
private Semaphore semaphore = new Semaphore(0);
|
||||
private boolean running = true;
|
||||
|
||||
public void waitForStop() throws InterruptedException {
|
||||
semaphore.acquire();
|
||||
semaphore.release();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
running = false;
|
||||
semaphore.release();
|
||||
}
|
||||
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRelay;
|
||||
|
||||
/*
|
||||
* Connect Relay to D8
|
||||
*/
|
||||
public class Relay implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveRelay relay = new GroveRelay(grovePi, 8);
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
relay.set(true);
|
||||
Thread.sleep(10000);
|
||||
relay.set(false);
|
||||
Thread.sleep(5000);
|
||||
} catch (IOException io) {
|
||||
System.err.println("error");
|
||||
}
|
||||
}
|
||||
relay.set(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRgbLcd;
|
||||
|
||||
public class RgbLcd implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveRgbLcd lcd = grovePi.getLCD();
|
||||
|
||||
String[] phrases = new String[]{
|
||||
"Hi! ALL!",
|
||||
"This should work just fine",
|
||||
"A message\nA response",
|
||||
"More data that you can handle for sure!",
|
||||
"Welcome to the internet of things with java",
|
||||
"Short\nMessage"
|
||||
};
|
||||
int[][] colors = new int[][]{
|
||||
{50, 255, 30},
|
||||
{15, 88, 245},
|
||||
{248, 52, 100},
|
||||
{48, 56, 190},
|
||||
{178, 25, 180},
|
||||
{210, 210, 210}
|
||||
};
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
String text = phrases[new Random().nextInt(phrases.length)];
|
||||
int[] color = colors[new Random().nextInt(colors.length)];
|
||||
lcd.setRGB(color[0], color[1], color[2]);
|
||||
Thread.sleep(100);
|
||||
lcd.setText(text);
|
||||
Thread.sleep(2000);
|
||||
} catch (IOException io) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GroveDigitalOut;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRotarySensor;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRotaryValue;
|
||||
|
||||
/*
|
||||
Connect:
|
||||
RotarySensor to A1
|
||||
Leds to D3 (red),D4 (green) and D5 (blue)
|
||||
*/
|
||||
public class RotarySensor3Led implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveRotarySensor rotarySensor = new GroveRotarySensor(grovePi, 1);
|
||||
GroveDigitalOut redLed = grovePi.getDigitalOut(3);
|
||||
GroveDigitalOut greenLed = grovePi.getDigitalOut(4);
|
||||
GroveDigitalOut blueLed = grovePi.getDigitalOut(5);
|
||||
GroveDigitalOut onLed = null;
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
GroveRotaryValue value = rotarySensor.get();
|
||||
System.out.println(value);
|
||||
GroveDigitalOut ledToTurn;
|
||||
if (value.getDegrees() > 250) {
|
||||
ledToTurn = redLed;
|
||||
} else if (value.getDegrees() < 100) {
|
||||
ledToTurn = blueLed;
|
||||
} else {
|
||||
ledToTurn = greenLed;
|
||||
}
|
||||
if (ledToTurn != onLed) {
|
||||
redLed.set(ledToTurn == redLed);
|
||||
blueLed.set(ledToTurn == blueLed);
|
||||
greenLed.set(ledToTurn == greenLed);
|
||||
onLed = ledToTurn;
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
}
|
||||
blueLed.set(false);
|
||||
greenLed.set(false);
|
||||
redLed.set(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveLed;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRotarySensor;
|
||||
import org.iot.raspberry.grovepi.devices.GroveRotaryValue;
|
||||
/*
|
||||
Connect:
|
||||
RotarySensor to A1
|
||||
Led D5
|
||||
*/
|
||||
|
||||
public class RotarySensorDimLed implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveRotarySensor rotarySensor = new GroveRotarySensor(grovePi, 1);
|
||||
GroveLed blueLed = new GroveLed(grovePi, 5);
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
GroveRotaryValue value = rotarySensor.get();
|
||||
int brightness = (int) (value.getFactor() * GroveLed.MAX_BRIGTHNESS);
|
||||
blueLed.set(brightness);
|
||||
} catch (IOException ex) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
}
|
||||
blueLed.set(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,111 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Scanner;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.pi4j.GrovePi4J;
|
||||
|
||||
public class Runner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Logger.getLogger("GrovePi").setLevel(Level.WARNING);
|
||||
Logger.getLogger("RaspberryPi").setLevel(Level.WARNING);
|
||||
|
||||
File control = new File("RUNNINGSAMPLES");
|
||||
control.deleteOnExit();
|
||||
if (control.exists()) {
|
||||
control.delete();
|
||||
System.out.println("STOPING CURRENT SAMPLE");
|
||||
System.exit(0);
|
||||
}
|
||||
/*
|
||||
if (args.length != 2) {
|
||||
System.err.println("You need to provide 2 arguments DIO|PI4J EXAMPLECLASS");
|
||||
System.exit(-1);
|
||||
}
|
||||
*/
|
||||
if (args.length != 1) {
|
||||
System.err.println("You need to provide 1 argument - name of the class");
|
||||
System.exit(-1);
|
||||
}
|
||||
|
||||
control.createNewFile();
|
||||
|
||||
/*
|
||||
String mode = args[0];
|
||||
GrovePi grovePi;
|
||||
switch (mode.toLowerCase()) {
|
||||
case "pi4j":
|
||||
grovePi = new GrovePi4J();
|
||||
break;
|
||||
case "test":
|
||||
grovePi = createProxy(GrovePi.class);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("You must provide either DIO or PI4J implementation");
|
||||
}
|
||||
*/
|
||||
|
||||
GrovePi grovePi = new GrovePi4J();
|
||||
Example example = (Example) Class.forName("org.iot.raspberry.examples." + args[0]).newInstance();
|
||||
System.out.println("RUNNING EXAMPLE: " + args[0] + " USING: PI4J");
|
||||
final ExecutorService runner = Executors.newSingleThreadExecutor();
|
||||
final ExecutorService consoleMonitor = Executors.newSingleThreadExecutor();
|
||||
final ExecutorService fileMonitor = Executors.newSingleThreadExecutor();
|
||||
final Semaphore lock = new Semaphore(0);
|
||||
final Monitor monitor = new Monitor();
|
||||
|
||||
runner.execute(() -> {
|
||||
try {
|
||||
example.run(grovePi, monitor);
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(Runner.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
lock.release();
|
||||
});
|
||||
|
||||
consoleMonitor.execute(() -> {
|
||||
try (Scanner scanner = new Scanner(System.in)) {
|
||||
String command;
|
||||
while (!(command = scanner.next()).equalsIgnoreCase("quit")) {
|
||||
System.out.println("Command " + command + " not recognized, try quit");
|
||||
}
|
||||
}
|
||||
monitor.stop();
|
||||
lock.release();
|
||||
});
|
||||
|
||||
fileMonitor.execute(() -> {
|
||||
while (control.exists()) {
|
||||
try {
|
||||
Thread.sleep(100);
|
||||
} catch (InterruptedException ex) {
|
||||
}
|
||||
}
|
||||
monitor.stop();
|
||||
lock.release();
|
||||
});
|
||||
|
||||
lock.acquire();
|
||||
runner.shutdown();
|
||||
consoleMonitor.shutdownNow();
|
||||
fileMonitor.shutdownNow();
|
||||
runner.awaitTermination(10, TimeUnit.SECONDS);
|
||||
control.delete();
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private static <T> T createProxy(Class<T> aClass) {
|
||||
return (T) Proxy.newProxyInstance(aClass.getClassLoader(), new Class<?>[]{aClass}, (Object proxy, Method method, Object[] args1) -> {
|
||||
throw new RuntimeException("Test class methods not allowed");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
|
||||
public class RunnerTest implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
while (monitor.isRunning()) {
|
||||
Thread.sleep(1000);
|
||||
System.out.println("Running...");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GroveDigitalOut;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveSoundSensor;
|
||||
|
||||
/*
|
||||
Connect:
|
||||
SoundSensor to A0
|
||||
Leds to D3 (red),D4 (green) and D5 (blue)
|
||||
*/
|
||||
public class SoundSensor implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveSoundSensor soundSensor = new GroveSoundSensor(grovePi, 0);
|
||||
GroveDigitalOut redLed = grovePi.getDigitalOut(3);
|
||||
GroveDigitalOut greenLed = grovePi.getDigitalOut(4);
|
||||
GroveDigitalOut blueLed = grovePi.getDigitalOut(5);
|
||||
GroveDigitalOut onLed = null;
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
double soundLevel = soundSensor.get();
|
||||
System.out.println(soundLevel);
|
||||
GroveDigitalOut ledToTurn;
|
||||
if (soundLevel > 1000) {
|
||||
ledToTurn = redLed;
|
||||
} else if (soundLevel < 300) {
|
||||
ledToTurn = blueLed;
|
||||
} else {
|
||||
ledToTurn = greenLed;
|
||||
}
|
||||
if (ledToTurn != onLed) {
|
||||
redLed.set(ledToTurn == redLed);
|
||||
blueLed.set(ledToTurn == blueLed);
|
||||
greenLed.set(ledToTurn == greenLed);
|
||||
onLed = ledToTurn;
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.println("Error");
|
||||
}
|
||||
}
|
||||
blueLed.set(false);
|
||||
greenLed.set(false);
|
||||
redLed.set(false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveTemperatureAndHumiditySensor;
|
||||
/*
|
||||
Connect Temp & Humidity sensor to D4
|
||||
*/
|
||||
|
||||
public class TemperatureAndHumidity implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveTemperatureAndHumiditySensor dht = new GroveTemperatureAndHumiditySensor(grovePi, 4, GroveTemperatureAndHumiditySensor.Type.DHT11);
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
System.out.println(dht.get());
|
||||
} catch (IOException ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package org.iot.raspberry.examples;
|
||||
|
||||
import java.io.IOException;
|
||||
import org.iot.raspberry.grovepi.GroveDigitalOut;
|
||||
import org.iot.raspberry.grovepi.GrovePi;
|
||||
import org.iot.raspberry.grovepi.devices.GroveUltrasonicRanger;
|
||||
|
||||
/*
|
||||
Connect ultrasonic ranger to port D6
|
||||
buzzer to D7
|
||||
*/
|
||||
|
||||
public class UltrasonicRanger implements Example {
|
||||
|
||||
@Override
|
||||
public void run(GrovePi grovePi, Monitor monitor) throws Exception {
|
||||
GroveUltrasonicRanger ranger = new GroveUltrasonicRanger(grovePi, 6);
|
||||
GroveDigitalOut buzzer = grovePi.getDigitalOut(7);
|
||||
while (monitor.isRunning()) {
|
||||
try {
|
||||
double distance = ranger.get();
|
||||
System.out.println(distance);
|
||||
buzzer.set(distance < 20);
|
||||
} catch (IOException ex) {
|
||||
System.out.println("error!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue