Search in sources :

Example 6 with I2cDeviceSynch

use of com.qualcomm.robotcore.hardware.I2cDeviceSynch in project robotcode by OutoftheBoxFTC.

the class AMSColorSensorImpl method internalInitialize.

// ----------------------------------------------------------------------------------------------
// Initialization
// ----------------------------------------------------------------------------------------------
@Override
protected synchronized boolean internalInitialize(@NonNull Parameters parameters) {
    RobotLog.vv(TAG, "internalInitialize()...");
    try {
        if (this.parameters.deviceId != parameters.deviceId) {
            throw new IllegalArgumentException(String.format("can't change device types (modify existing params instead): old=%d new=%d", this.parameters.deviceId, parameters.deviceId));
        }
        // Remember the parameters for future use
        this.parameters = parameters.clone();
        // Make sure we're talking to the correct I2c device
        this.setI2cAddress(parameters.i2cAddr);
        // Can't do anything if we're not really talking to the hardware
        if (!this.deviceClient.isArmed()) {
            return false;
        }
        // Verify that that's a color sensor!
        byte id = this.getDeviceID();
        if (id != parameters.deviceId) {
            RobotLog.ee(TAG, "unexpected AMS color sensor chipid: found=%d expected=%d", id, parameters.deviceId);
            return false;
        }
        // sanity check: before
        dumpState();
        // Turn off the integrator so that we can change parameters
        disable();
        // Set the gain and integration time. Note that we don't seem to be
        // able to successfully set these values AFTER we enable, so we need to
        // do so before.
        setIntegrationTime(parameters.atime);
        setGain(parameters.gain);
        setPDrive(parameters.ledDrive);
        if (is3782() && parameters.useProximityIfAvailable) {
            setProximityPulseCount(parameters.proximityPulseCount);
        }
        // Enable the device
        enable();
        // sanity check: after
        dumpState();
        // Set up a read-ahead, if supported and requested
        if (this.deviceClient instanceof I2cDeviceSynch && parameters.readWindow != null) {
            I2cDeviceSynch synch = ((I2cDeviceSynch) this.deviceClient);
            synch.setReadWindow(parameters.readWindow);
        }
        return true;
    } finally {
        RobotLog.vv(TAG, "...internalInitialize()");
    }
}
Also used : I2cDeviceSynch(com.qualcomm.robotcore.hardware.I2cDeviceSynch)

Example 7 with I2cDeviceSynch

use of com.qualcomm.robotcore.hardware.I2cDeviceSynch in project robotcode by OutoftheBoxFTC.

the class UserI2cSensorType method createInstance.

@NonNull
public HardwareDevice createInstance(I2cController controller, int port) throws InvocationTargetException {
    try {
        Constructor ctor;
        ctor = findMatch(ctorI2cDeviceSynch);
        if (null == ctor) {
            ctor = findMatch(ctorI2cDeviceSynchSimple);
        }
        if (null != ctor) {
            I2cDevice i2cDevice = new I2cDeviceImpl(controller, port);
            I2cDeviceSynch i2cDeviceSynch = new I2cDeviceSynchImpl(i2cDevice, true);
            return (HardwareDevice) ctor.newInstance(i2cDeviceSynch);
        }
        ctor = findMatch(ctorI2cDevice);
        if (null != ctor) {
            I2cDevice i2cDevice = new I2cDeviceImpl(controller, port);
            return (HardwareDevice) ctor.newInstance(i2cDevice);
        }
        ctor = findMatch(ctorI2cControllerPort);
        if (null != ctor) {
            return (HardwareDevice) ctor.newInstance(controller, port);
        }
    } catch (IllegalAccessException | InstantiationException e) {
        throw new RuntimeException("internal error: exception");
    }
    throw new RuntimeException("internal error: unable to locate constructor for user sensor type");
}
Also used : I2cDeviceSynchImpl(com.qualcomm.robotcore.hardware.I2cDeviceSynchImpl) Constructor(java.lang.reflect.Constructor) I2cDevice(com.qualcomm.robotcore.hardware.I2cDevice) I2cDeviceSynch(com.qualcomm.robotcore.hardware.I2cDeviceSynch) HardwareDevice(com.qualcomm.robotcore.hardware.HardwareDevice) I2cDeviceImpl(com.qualcomm.robotcore.hardware.I2cDeviceImpl) NonNull(android.support.annotation.NonNull)

Aggregations

I2cDeviceSynch (com.qualcomm.robotcore.hardware.I2cDeviceSynch)7 I2cDevice (com.qualcomm.robotcore.hardware.I2cDevice)3 I2cDeviceSynchImpl (com.qualcomm.robotcore.hardware.I2cDeviceSynchImpl)3 NonNull (android.support.annotation.NonNull)2 LynxI2cDeviceSynch (com.qualcomm.hardware.lynx.LynxI2cDeviceSynch)2 HardwareDevice (com.qualcomm.robotcore.hardware.HardwareDevice)2 I2cDeviceImpl (com.qualcomm.robotcore.hardware.I2cDeviceImpl)2 I2cDeviceSynchSimple (com.qualcomm.robotcore.hardware.I2cDeviceSynchSimple)2 Constructor (java.lang.reflect.Constructor)2 I2cDeviceSynchImplOnSimple (com.qualcomm.robotcore.hardware.I2cDeviceSynchImplOnSimple)1