All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lejos.remote.ev3.RemoteRequestPort Maven / Gradle / Ivy

Go to download

leJOS (pronounced like the Spanish word "lejos" for "far") is a tiny Java Virtual Machine. In 2013 it was ported to the LEGO EV3 brick.

The newest version!
package lejos.remote.ev3;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import lejos.hardware.DeviceException;
import lejos.hardware.port.AnalogPort;
import lejos.hardware.port.BasicMotorPort;
import lejos.hardware.port.I2CPort;
import lejos.hardware.port.IOPort;
import lejos.hardware.port.Port;
import lejos.hardware.port.TachoMotorPort;
import lejos.hardware.port.UARTPort;
import lejos.hardware.sensor.EV3SensorConstants;

public class RemoteRequestPort implements Port {
    public static final int SENSOR_PORT = 0;
    public static final int MOTOR_PORT = 1;
    protected String name;
    protected int typ;
    protected int portNum;
    protected ObjectInputStream is;
    protected ObjectOutputStream os;
    
    public RemoteRequestPort(String name, int typ, int portNum, ObjectInputStream is, ObjectOutputStream os) {
    	this.name = name;
    	this.typ = typ;
    	this.portNum = portNum;
    	this.is = is;
    	this.os = os;
    }

	@Override
	public String getName() {
		return name;
	}

	@Override
	public  T open(Class portclass) {
        RemoteRequestIOPort p = null;
        switch(typ)
        {
        case SENSOR_PORT:
            if (portclass == RemoteRequestUARTPort.class || portclass == UARTPort.class)
                p = new RemoteRequestUARTPort(is,os);
            if (portclass == RemoteRequestAnalogPort.class || portclass == AnalogPort.class)
                p = new RemoteRequestAnalogPort(is, os);
            else if (portclass == RemoteRequestI2CPort.class|| portclass == I2CPort.class)
                p = new RemoteRequestI2CPort(is, os);
            break;
        case MOTOR_PORT:
            if (portclass == BasicMotorPort.class)
                p = new RemoteRequestMotorPort(is, os);
            else if (portclass == TachoMotorPort.class)
                p = new RemoteRequestMotorPort(is, os);
            // TODO: Should we also allow Encoder?
            break;
        }
        if (p == null)
            throw new IllegalArgumentException("Invalid port interface");
        if (!p.open(typ,  portNum, this))
            throw new DeviceException("unable to open port");
        return portclass.cast(p);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy