Search in sources :

Example 1 with RobotUsbDeviceClosedException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException in project robotcode by OutoftheBoxFTC.

the class FtDevice method setBaudRate.

public void setBaudRate(int baudRate) throws RobotUsbException {
    if (!this.isOpen()) {
        throw new RobotUsbDeviceClosedException("setBaudRate");
    } else {
        byte rc = 1;
        int[] divisors = new int[2];
        switch(baudRate) {
            case 300:
                divisors[0] = 0x2710;
                break;
            case 600:
                divisors[0] = 0x1388;
                break;
            case 1200:
                divisors[0] = 0x09c4;
                break;
            case 2400:
                divisors[0] = 0x04e2;
                break;
            case 4800:
                divisors[0] = 0x0271;
                break;
            case 9600:
                divisors[0] = 0x4138;
                break;
            case 19200:
                divisors[0] = 0x809c;
                break;
            case 38400:
                divisors[0] = 0xc04e;
                break;
            case 57600:
                divisors[0] = 0x0034;
                break;
            case 115200:
                divisors[0] = 0x001a;
                break;
            case 230400:
                divisors[0] = 0x000d;
                break;
            case 460800:
                divisors[0] = 0x4006;
                break;
            case 921600:
                divisors[0] = 0x8003;
                break;
            default:
                if (this.isHiSpeed() && baudRate >= 1200) {
                    rc = BaudRate.getDivisorHi(baudRate, divisors);
                } else {
                    rc = BaudRate.getDivisor(baudRate, divisors, this.isBitModeDevice());
                }
                break;
        }
        if (this.isMultiInterfaceDevice() || this.isFt232h() || this.isFt232ex()) {
            divisors[1] <<= 8;
            divisors[1] &= '\uff00';
            divisors[1] |= this.mInterfaceID;
        }
        if (rc == 1) {
            int status = this.getConnection().controlTransfer(UsbConstants.USB_TYPE_VENDOR | UsbConstants.USB_DIR_OUT, FTDI_SIO_SET_BAUDRATE, divisors[0], divisors[1], (byte[]) null, 0, 0);
            if (status != 0) {
                throw new RobotUsbUnspecifiedException("setBaudRate: status=%d", status);
            }
        } else {
            throw new RobotUsbUnspecifiedException("setBaudRate: rc=%d", rc);
        }
    }
}
Also used : RobotUsbUnspecifiedException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbUnspecifiedException) UsbEndpoint(android.hardware.usb.UsbEndpoint) RobotUsbDeviceClosedException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException)

Example 2 with RobotUsbDeviceClosedException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException in project robotcode by OutoftheBoxFTC.

the class FtDevice method setDataCharacteristics.

public void setDataCharacteristics(byte dataBits, byte stopBits, byte parity) throws RobotUsbException {
    boolean rc = false;
    if (!this.isOpen()) {
        throw new RobotUsbDeviceClosedException("setDataCharacteristics");
    } else {
        short wValue = (short) (dataBits | parity << 8);
        wValue = (short) (wValue | stopBits << 11);
        this.mDeviceInfo.breakOnParam = wValue;
        int status = vendorCmdSet(FTDI_SIO_SET_DATA, wValue);
        throwIfStatus(status, "setDataCharacteristics");
    }
}
Also used : UsbEndpoint(android.hardware.usb.UsbEndpoint) RobotUsbDeviceClosedException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException)

Example 3 with RobotUsbDeviceClosedException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException in project robotcode by OutoftheBoxFTC.

the class ReadWriteRunnableStandard method run.

/**
 * Main read / write loop
 */
@Override
public void run() {
    // Paranoia: avoid re-execution, as we weren't designed for that
    if (shutdownComplete) {
        return;
    }
    ThreadPool.logThreadLifeCycle(String.format("r/w loop: %s", HardwareFactory.getDeviceDisplayName(context, serialNumber)), new Runnable() {

        @Override
        public void run() {
            // read the entire buffer at least one time.
            fullWriteNeeded = false;
            pruneBufferAfterRead = true;
            setFullActive();
            ElapsedTime timer = new ElapsedTime();
            String timerString = "Device " + serialNumber.toString();
            // Tell those awaiting our startup that we are up and running
            running = true;
            try {
                callback.startupComplete();
            } catch (InterruptedException e) {
                // shutdown, in the normal way, soon!
                running = false;
            }
            runningInterlock.countDown();
            try {
                while (running) {
                    if (debugLogging) {
                        timer.log(timerString);
                        timer.reset();
                    }
                    doReadCycle();
                    doWriteCycle();
                    usbHandler.throwIfTooManySequentialCommErrors();
                    if (!robotUsbDevice.isOpen()) {
                        throw new RobotUsbDeviceClosedException("%s: closed", robotUsbDevice.getSerialNumber());
                    }
                }
            // end loop
            } catch (InterruptedException e) {
                RobotLog.logExceptionHeader(TAG, e, "thread interrupt while communicating with %s", HardwareFactory.getDeviceDisplayName(context, serialNumber));
            // Shutting down due to an interrupt is NOT abnormal: this may well happen when an invocation
            // of our close() method requests an interrupt from the FTDI layer.
            } catch (RobotUsbException | RuntimeException e) {
                // than something we should pollute the logs with
                if (e.getClass() != RobotUsbDeviceClosedException.class) {
                    RobotLog.ee(TAG, e, "exception while communicating with %s", HardwareFactory.getDeviceDisplayName(context, serialNumber));
                }
                String format = context.getString(robotUsbDevice.isAttached() ? R.string.warningProblemCommunicatingWithUSBDevice : R.string.warningUSBDeviceDetached);
                setOwnerWarningMessage(format, HardwareFactory.getDeviceDisplayName(context, serialNumber));
                // cases.
                if (e.getClass() == RobotUsbTimeoutException.class || e.getClass() == RobotUsbStuckUsbWriteException.class) {
                    shutdownReason = ShutdownReason.ABNORMAL_ATTEMPT_REOPEN;
                } else {
                    shutdownReason = ShutdownReason.ABNORMAL;
                }
            } finally {
                usbHandler.close();
                running = false;
                try {
                    callback.shutdownComplete();
                } catch (InterruptedException e) {
                // ignore: we're about to terminate this thread anyway
                }
                shutdownComplete = true;
            }
        }
    });
}
Also used : RobotUsbTimeoutException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbTimeoutException) ElapsedTime(com.qualcomm.robotcore.util.ElapsedTime) RobotUsbStuckUsbWriteException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbStuckUsbWriteException) RobotUsbDeviceClosedException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException)

Example 4 with RobotUsbDeviceClosedException

use of org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException in project robotcode by OutoftheBoxFTC.

the class FtDevice method setBreak.

private void setBreak(int OnOrOff) throws RobotUsbException {
    int wValue = this.mDeviceInfo.breakOnParam;
    wValue |= OnOrOff;
    if (!this.isOpen()) {
        throw new RobotUsbDeviceClosedException("setBreak");
    } else {
        int status = this.getConnection().controlTransfer(UsbConstants.USB_TYPE_VENDOR | UsbConstants.USB_DIR_OUT, FTDI_SIO_SET_DATA, wValue, this.mInterfaceID, (byte[]) null, 0, 0);
        throwIfStatus(status, "setBreak");
    }
}
Also used : UsbEndpoint(android.hardware.usb.UsbEndpoint) RobotUsbDeviceClosedException(org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException)

Aggregations

RobotUsbDeviceClosedException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbDeviceClosedException)4 UsbEndpoint (android.hardware.usb.UsbEndpoint)3 ElapsedTime (com.qualcomm.robotcore.util.ElapsedTime)1 RobotUsbStuckUsbWriteException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbStuckUsbWriteException)1 RobotUsbTimeoutException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbTimeoutException)1 RobotUsbUnspecifiedException (org.firstinspires.ftc.robotcore.internal.usb.exception.RobotUsbUnspecifiedException)1