Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagexml
titlepom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.example</groupId>
	<artifactId>opendevice-tutorial</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<opendevice-version>0.1.2-SNAPSHOT</opendevice-version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>br.com.criativasoft.opendevice</groupId>
			<artifactId>opendevice-connection-stream</artifactId>
			<version>${opendevice-version}</version>
		</dependency>
		<dependency>
			<groupId>br.com.criativasoft.opendevice</groupId>
			<artifactId>opendevice-core</artifactId>
			<version>${opendevice-version}</version>
		</dependency>
	</dependencies>
	
    <repositories>
        <repository>
            <id>oss.sonatype.org</id>
            <url>https://oss.sonatype.org/content/repositories/releases</url>
        </repository>
        <repository>
            <id>oss.sonatype.org-snapshot</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots</url>
        </repository>
    </repositories>
</project>

 

3.3 Running Demo.java

 

Code Block
languagejava
titleDemo.java source
import br.com.criativasoft.opendevice.core.SimpleDeviceManager;
import br.com.criativasoft.opendevice.core.connection.Connections;
import br.com.criativasoft.opendevice.core.model.Device;
import br.com.criativasoft.opendevice.core.model.DeviceType;

public class Demo extends SimpleDeviceManager {
    public static void main(String[] args) throws Exception {
        new Demo();
    }
    public Demo() throws Exception {
        Device led = new Device(1, DeviceType.DIGITAL);
        // setup connection with arduino/hardware
        addOutput(Connections.out.usb()); // Connect to first USB port available

        connect();
        addDevice(led);
        while(true){
            led.on();
            delay(200);
            led.off();
            delay(200);
        }
    }
}

 

 

Now you must see the LED blinking in the Arduino

 

Tip

Of course you can use the "SimpleDeviceManager" without using inheritance only calling instance.

 

Other usage style, it's working directly with the connection.
It is more decoupled, but less powerful 

Code Block
languagejava
import br.com.criativasoft.opendevice.connection.ConnectionListener;
import br.com.criativasoft.opendevice.connection.ConnectionStatus;
import br.com.criativasoft.opendevice.connection.DeviceConnection;
import br.com.criativasoft.opendevice.connection.message.Message;
import br.com.criativasoft.opendevice.core.command.DeviceCommand;
import br.com.criativasoft.opendevice.core.connection.Connections;
public class Demo implements ConnectionListener {
    public Demo() throws Exception {
        DeviceConnection conn = Connections.out.usb();
        conn.addListener(this);
        conn.connect();
        long delay = 500;
        while(conn.isConnected()) {
            conn.send(DeviceCommand.ON(1)); // '1' is Device ID not pin !
            Thread.sleep(delay);
            conn.send(DeviceCommand.OFF(1));
            Thread.sleep(delay);
        }
        System.out.println("TERMINATED !");
    }
    
    public static void main(String[] args) throws Exception {
        new Demo();
    }

    // ------------------------------------------------------------
    // ------------- ConnectionListener Impl --------------------------
    public void onMessageReceived(Message message, DeviceConnection connection) {
        String type = message.getClass().getSimpleName();
        System.out.println("onMessageReceived("+type+"): "+ message);
    }
    public void connectionStateChanged(DeviceConnection connection, ConnectionStatus status) {
        System.out.println("connectionStateChanged :  " + status);
    }
}

 

 

Troubleshooting