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

lejos.internal.ev3.EV3Port 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.internal.ev3;

import lejos.hardware.DeviceException;
import lejos.hardware.port.AnalogPort;
import lejos.hardware.port.BasicMotorPort;
import lejos.hardware.port.ConfigurationPort;
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 EV3Port 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;

    public EV3Port(String name, int typ, int portNum)
    {
        if (typ < SENSOR_PORT || typ > MOTOR_PORT)
            throw new IllegalArgumentException("Invalid port type");
        if (portNum < 0 || 
            (typ == SENSOR_PORT && portNum >= EV3SensorConstants.PORTS) ||
            (typ == MOTOR_PORT && portNum >= EV3SensorConstants.MOTORS))
            throw new IllegalArgumentException("Invalid port number");
        this.name = name;
        this.typ = typ;
        this.portNum = portNum;
        
    }
    
    /** {@inheritDoc}
     */    
    @Override
    public String getName()
    {
        return name;
    }

    /** {@inheritDoc}
     */    
    @Override
    public  T open(Class portclass)
    {
        EV3IOPort p = null;
        switch(typ)
        {
        case SENSOR_PORT:
            if (portclass == UARTPort.class)
                p = new EV3UARTPort();
            else if (portclass == AnalogPort.class)
                p = new EV3AnalogPort();
            else if (portclass == I2CPort.class)
                p = new EV3I2CPort();
            else if (portclass == ConfigurationPort.class)
                p = new EV3ConfigurationPort();
            break;
        case MOTOR_PORT:
            if (portclass == BasicMotorPort.class)
                p = new EV3MotorPort();
            else if (portclass == TachoMotorPort.class)
                p = new EV3MotorPort();
            else if (portclass == ConfigurationPort.class)
                p = new EV3ConfigurationPort();
            // 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