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

lejos.hardware.sensor.UARTSensor 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.hardware.sensor;

import lejos.hardware.DeviceException;
import lejos.hardware.port.Port;
import lejos.hardware.port.UARTPort;

/**
 * Base class for EV3 UART based sensors. UART sensor drivers should extend this class
 * @author andy
 *
 */
public class UARTSensor extends BaseSensor
{
    protected UARTPort port;
    protected int currentMode;
    

     /**
     * Standard constructor for a UARTSensor initialises things and places the 
     * device into mode 0.
     * @param port The port the sensor is attached to.
     */
    public UARTSensor(UARTPort port)
    {
        this(port, 0);
    }
    
    /**
    * Standard constructor for a UARTSensor initialises things and places the 
    * device into mode 0.
    * @param port The port the sensor is attached to.
    */
   public UARTSensor(Port port)
   {
       this(port, 0);
   }

    /**
     * Create the sensor object and switch to the selected mode
     * @param port The port the sensor is attached to.
     * @param mode Operating mode for the sensor.
     */
    public UARTSensor(UARTPort port, int mode)
    {
        this.port = port;
        if (!port.setMode(mode))
            throw new DeviceException("Unable to initialize device");
        currentMode = mode;
    }
    
    /**
     * Create the sensor object and switch to the selected mode
     * @param port The port the sensor is attached to.
     * @param mode Operating mode for the sensor.
     */
    public UARTSensor(Port port, int mode)
    {
        this.port = port.open(UARTPort.class);
        if (!this.port.setMode(mode))
        {
            this.port.close();
            throw new IllegalArgumentException("Invalid sensor mode");
        }
        currentMode = mode;
        releaseOnClose(this.port);
    }
    

    /**
     * Switch to the selected mode (if not already in that mode) and delay for the
     * specified period to allow the sensor to settle in the new mode. 
* A mode of -1 resets the sensor. * TODO: There really should be a better way to work out when the switch is * complete, if you don't wait though you end up reading data from the previous * mode. * @param newMode The mode to switch to. * @param switchDelay Time in mS to delay after the switch. */ protected void switchMode(int newMode, long switchDelay) { if (currentMode != newMode) { if (newMode == -1) port.resetSensor(); else if (!port.setMode(newMode)) throw new IllegalArgumentException("Invalid sensor mode"); currentMode = newMode; //Delay.msDelay(switchDelay); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy